[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-content/add-ons/ -> MailPress_bounce_handling_II.php (source)

   1  <?php
   2  if ( class_exists( 'MailPress' ) && !class_exists( 'MailPress_bounce_handling_II' ) )
   3  {
   4  /*
   5  Plugin Name: MailPress_bounce_handling_II
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/bounce_handling_ii/
   7  Description: Users : bounce management ( based on mail headers & string detection in mail body )
   8  Version: 7.2
   9  */
  10  
  11  class MailPress_bounce_handling_II
  12  {
  13      const meta_key         = '_MailPress_bounce_handling';
  14      const option_name     = 'MailPress_bounce_handling_II';
  15      const log_name = 'bounce_handling_II';
  16  
  17      const process_name        = 'mp_process_bounce_handling_II';
  18  
  19      const bt = 132;
  20  
  21  	function __construct()
  22      {
  23  // prepare mail
  24          add_filter( 'MailPress_swift_message_headers',            array( __CLASS__, 'swift_message_headers' ), 8, 2 );
  25  
  26  // for batch mode
  27          add_action( self::process_name,            array( __CLASS__, 'process' ) );
  28  
  29          $config = get_option( self::option_name );
  30          if ( 'wpcron' == $config['batch_mode'] )
  31          {    
  32              add_action( 'MailPress_schedule_bounce_handling_II',    array( __CLASS__, 'schedule' ) );
  33          }
  34  
  35          if ( is_admin() )
  36          {
  37          // for install
  38              register_activation_hook( plugin_basename( __FILE__ ),    array( __CLASS__, 'install' ) );
  39              register_deactivation_hook( plugin_basename( __FILE__ ),array( __CLASS__, 'uninstall' ) );
  40          // for link on plugin page
  41              add_filter( 'plugin_action_links',                array( __CLASS__, 'plugin_action_links' ), 10, 2 );
  42  
  43          // for settings
  44              add_filter( 'MailPress_settings_tab',                array( __CLASS__, 'settings_tab' ), 20, 1 );
  45          // for settings logs
  46              add_action( 'MailPress_settings_logs_form',                array( __CLASS__, 'settings_logs_form' ), 30, 1 );
  47  
  48  
  49              if ( 'wpcron' == $config['batch_mode'] )
  50              {    
  51              // for autorefresh
  52                  add_filter( 'MailPress_autorefresh_every',        array( __CLASS__, 'autorefresh_every' ), 8, 1 );
  53                  add_filter( 'MailPress_autorefresh_js',        array( __CLASS__, 'autorefresh_js' ), 8, 1 );
  54              }
  55              if ( !class_exists( 'MailPress_bounce_handling' ) )
  56              {
  57              // for users list
  58                  add_filter( 'MailPress_get_icon_users',        array( __CLASS__, 'get_icon_users' ), 8, 2 );
  59              // for meta box in user page
  60                  add_action( 'MailPress_add_meta_boxes_user',    array( __CLASS__, 'meta_boxes_user' ), 8, 2 );
  61              }
  62          }
  63  
  64          if ( !class_exists( 'MailPress_bounce_handling' ) )
  65          {
  66          // for mails list
  67              add_filter( 'MailPress_mails_columns',            array( __CLASS__, 'mails_columns' ), 10, 1 );
  68              add_filter( 'MailPress_mails_get_row',            array( __CLASS__, 'mails_get_row' ), 10, 4 );
  69          // view bounce
  70              add_action( 'mp_action_view_bounce',                array( __CLASS__, 'mp_action_view_bounce' ) ); 
  71          }
  72      }
  73  
  74  // prepare mail
  75  	public static function swift_message_headers( $message, $row )
  76      {
  77          if ( class_exists( 'MailPress_bounce_handling' ) ) return $message;
  78  
  79          if ( !empty( $message->getReturnPath() ) ) return $message;
  80  
  81          $config = get_option( self::option_name );
  82  
  83          if ( !$config ) return $message;
  84  
  85          if ( !MailPress::is_email( $config['Return-Path'] ?? '' ) ) return $message;
  86  
  87          $headers = array( 
  88                      array(     'type' => Swift_Mime_Header::TYPE_TEXT , 
  89                              'name' => 'X-MailPress-blog-id',
  90                              'value' => get_current_blog_id()
  91                       ),
  92                      array(     'type' => Swift_Mime_Header::TYPE_TEXT , 
  93                              'name' => 'X-MailPress-mail-id', 
  94                              'value' => $row->id
  95                       ),
  96                      array(     'type' => Swift_Mime_Header::TYPE_TEXT , 
  97                              'name' => 'X-MailPress-user-id',
  98                              'value' => $row->mp_user_id ?? '{{_user_id}}'
  99                       ),
 100                   );
 101  
 102          $_headers = $message->getHeaders();
 103          foreach ( $headers as $header )
 104          {
 105              switch ( $header['type'] )
 106              {
 107                  case Swift_Mime_Header::TYPE_TEXT :
 108                      $_headers->addTextHeader( $header['name'], $header['value'] );
 109                    break;
 110  /*
 111                  case Swift_Mime_Header::TYPE_PARAMETERIZED :
 112                      $_headers->addParameterizedHeader( $header['name'], $header['value'], $header['parms'] );
 113                    break;
 114                  case Swift_Mime_Header::TYPE_MAILBOX :
 115                      $_headers->addMailboxHeader( $header['name'], $header['value'] );
 116                    break;
 117                  case Swift_Mime_Header::TYPE_DATE :
 118                      $_headers->addDateHeader( $header['name'], $header['value'] );
 119                    break;
 120                  case Swift_Mime_Header::TYPE_ID :
 121                      $_headers->addIdHeader( $header['name'], $header['value'] );
 122                    break;
 123                  case Swift_Mime_Header::TYPE_PATH :
 124                      $_headers->addPathHeader( $header['name'], $header['value'] );
 125                    break;
 126  */
 127              }
 128          }
 129  
 130          $message->setReturnPath( $config['Return-Path'] );
 131  
 132          return $message;
 133      }
 134  
 135  // process
 136  	public static function process()
 137      {
 138          MP_::no_abort_limit();
 139  
 140          new MP_Bounce_II();
 141      }
 142  
 143  // schedule
 144  	public static function schedule()
 145      {
 146          $config = get_option( self::option_name );
 147          $now4cron = current_time( 'timestamp', 'gmt' );
 148  
 149          if ( !wp_next_scheduled( self::process_name ) ) 
 150              wp_schedule_single_event( $now4cron + $config['every'], self::process_name );
 151      }
 152  
 153  ////  ADMIN  ////
 154  ////  ADMIN  ////
 155  ////  ADMIN  ////
 156  ////  ADMIN  ////
 157  
 158  // install
 159  	public static function install() 
 160      {
 161          self::uninstall();
 162  
 163          $options = get_option( self::option_name );
 164          if ( !$options )
 165          {
 166              $pop3 = get_option( 'MailPress_connection_pop3' );
 167              if ( $pop3 )
 168              {
 169                  $options['pop3'] = $pop3;
 170                  update_option( self::option_name, $options );
 171              }
 172          }
 173  
 174          MP_Log::set_option( self::log_name );
 175  
 176          do_action( 'MailPress_schedule_bounce_handling_II' );
 177      }
 178  
 179  	public static function uninstall() 
 180      {
 181          wp_clear_scheduled_hook( self::process_name );
 182      }
 183  
 184  // for link on plugin page
 185  	public static function plugin_action_links( $links, $file )
 186      {
 187          return MailPress::plugin_links( $links, $file, plugin_basename( __FILE__ ), 'batches' );
 188      }
 189  
 190  // for settings
 191  	public static function settings_tab( $tabs )
 192      {
 193          if ( !class_exists( 'MailPress_bounce_handling' ) )
 194              $tabs['bounce_handling_II'] = __( 'Bounce II', 'MailPress' );
 195          return $tabs;
 196      }
 197  
 198  // for settings logs    
 199  	public static function settings_logs_form( $logs )
 200      {
 201          MP_AdminPage::log_form( self::log_name, $logs, __( 'Bounce', 'MailPress' ) . ' II' );
 202      }
 203  
 204  	public static function autorefresh_every( $every = 30 )
 205      {
 206          $config = get_option( self::option_name );
 207          if ( !$config ) return $every;
 208          if ( $every < $config['every'] ) return $every;
 209          return $config['every'];
 210      }
 211  
 212  	public static function autorefresh_js( $scripts )
 213      {
 214          return MP_WP_AutoRefresh_js::getInstance( $scripts );
 215      }
 216  
 217  // for users list
 218  	public static function get_icon_users( $out, $mp_user )
 219      {
 220          if ( 'bounced' == $mp_user->status ) $out .= '<span class="mp_icon mp_icon_bounced" title="' . esc_attr( __( 'Bounced', 'MailPress' ) ) . '"></span>';
 221          return $out;
 222      }
 223  
 224  // for user page
 225  	public static function meta_boxes_user( $mp_user_id, $screen )
 226      {
 227          $usermeta = MP_User_meta::get( $mp_user_id, self::meta_key );
 228          if ( !$usermeta ) return;
 229  
 230          add_meta_box( 'bouncehandlingdiv', __( 'Bounces', 'MailPress' ), array( __CLASS__, 'meta_box_user' ), $screen, 'side', 'core' );
 231      }
 232  
 233  	public static function meta_box_user( $mp_user )
 234      {
 235          $usermeta = MP_User_meta::get( $mp_user->id, self::meta_key );
 236          if ( !$usermeta ) return;
 237  
 238          global $wpdb;
 239          echo '<b>' . __( 'Bounces', 'MailPress' ) . '</b> : &#160;' . $usermeta['bounce'] . '<br />';
 240          foreach( $usermeta['bounces'] as $mail_id => $messages )
 241          {
 242              foreach( $messages as $k => $message )
 243              {
 244                  echo '<br />';
 245                  $subject = $wpdb->get_var( "SELECT subject FROM $wpdb->mp_mails WHERE id = " . $mail_id . ';' );
 246                  // $subject = $subject ?: __( '(deleted)', 'MailPress' ); //
 247                  $subject = ( $subject ) ? $subject : __( '(deleted)', 'MailPress' );
 248  
 249                  $view_url        = esc_url( add_query_arg( array( 'action' => 'mp_ajax', 'mp_action' => 'view_bounce', 'user_id' => $mp_user->id, 'mail_id' => $mail_id, 'id' => $k, 'TB_iframe' => 'true' ), admin_url( 'admin-ajax.php' ) ) );
 250                  $actions['view'] = '<a href="' . $view_url . '" class="thickbox thickbox-preview" title="' . esc_attr( __( 'View', 'MailPress' ) ) . '">' . $subject . '</a>';
 251  
 252                  echo '( ' . $mail_id . ' ) ' . $actions['view'];
 253              }
 254          }
 255      }
 256  
 257  // for mails list
 258  	public static function mails_columns( $x )
 259      {
 260          $date = array_pop( $x );
 261          $x['bounce_handling']    =  __( 'Bounce rate', 'MailPress' );
 262          $x['date']            = $date;
 263          return $x;
 264      }
 265  
 266  	public static function mails_get_row( $out, $column_name, $mail, $url_parms )
 267      {
 268          switch ( $column_name )
 269          {
 270              case 'bounce_handling' :
 271                  if ( MailPress::is_email( $mail->toemail ) ) $total = 1;
 272                  elseif( is_serialized( $mail->toemail ) ) $total = count( unserialize( $mail->toemail ) );
 273                  else break;
 274  
 275                  $result = MP_Mail_meta::get( $mail->id, self::meta_key );
 276                  if ( $result ) if ( $total > 0 ) $out .= sprintf( "%01.2f %%", 100 * $result/$total );
 277              break;
 278          }
 279          return $out;
 280      }
 281  
 282  // view bounce
 283  	public static function mp_action_view_bounce()
 284      {
 285          $mp_user_id = MP_WP_Ajax::$get_['user_id'];
 286          $mail_id    = MP_WP_Ajax::$get_['mail_id'];
 287          $bounce_id  = MP_WP_Ajax::$get_['id'];
 288  
 289          $usermeta = MP_User_meta::get( $mp_user_id, self::meta_key );
 290          if ( !$usermeta ) return;
 291  
 292          $plaintext = $usermeta['bounces'][$mail_id][$bounce_id]['message'];
 293  
 294          include ( MP_ABSPATH . 'mp-includes/html/plaintext.php' );
 295      }
 296  }
 297  new MailPress_bounce_handling_II();
 298  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1