[ Index ]

MailPress 7.2

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


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