[ Index ]

MailPress 7.1

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


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1