[ Index ]

MailPress 7.1

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

title

Body

[close]

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

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


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