[ Index ]

MailPress 544

[ 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: 5.4.4
   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 bt = 132;
  19  
  20  	function __construct()
  21      {
  22  // prepare mail
  23          add_filter('MailPress_swift_message_headers',      array(__CLASS__, 'swift_message_headers'), 8, 2);
  24  
  25  // for batch mode
  26          add_action('mp_process_bounce_handling',         array(__CLASS__, 'process'));
  27  
  28          $config = get_option(self::option_name);
  29          if ('wpcron' == $config['batch_mode'])
  30          {    
  31              add_action('MailPress_schedule_bounce_handling',     array(__CLASS__, 'schedule'));
  32          }
  33  
  34          if (is_admin())
  35          {
  36          // for install
  37              register_activation_hook(plugin_basename(__FILE__),     array(__CLASS__, 'install'));
  38              register_deactivation_hook(plugin_basename(__FILE__), array(__CLASS__, 'uninstall'));
  39          // for link on plugin page
  40              add_filter('plugin_action_links',         array(__CLASS__, 'plugin_action_links'), 10, 2 );
  41  
  42          // for settings
  43              add_filter('MailPress_scripts',         array(__CLASS__, 'scripts'), 8, 2);
  44              add_filter('MailPress_settings_tab',     array(__CLASS__, 'settings_tab'), 20, 1);
  45              add_filter('MailPress_settings_tab_connection',     array(__CLASS__, 'settings_tab_connection'), 95, 1);
  46          // for settings batches
  47              add_action('MailPress_settings_batches',     array(__CLASS__, 'settings_batches'), 20);
  48          // for settings logs
  49              add_action('MailPress_settings_logs',     array(__CLASS__, 'settings_logs'), 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_action('MailPress_get_icon_users',     array(__CLASS__, 'get_icon_users'), 8, 1);
  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_action('MailPress_mails_get_row',          array(__CLASS__, 'mails_get_row'), 10, 3);
  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          $config = get_option(self::option_name);
  76  
  77          if (!is_email($config['Return-Path'])) return $message;
  78  
  79          $prefix = substr($config['Return-Path'], 0, strpos($config['Return-Path'], '@'));
  80          $domain = substr($config['Return-Path'], strpos($config['Return-Path'], '@') + 1 );
  81  
  82          $ReturnPath = $prefix . '+' . $row->id . '+' . '{{_user_id}}' . '@' . $domain;
  83          if (isset($row->mp_user_id)) $ReturnPath = str_replace('{{_user_id}}', $row->mp_user_id, $ReturnPath);
  84  
  85          $message->setReturnPath($ReturnPath);
  86  
  87          return $message;
  88      }
  89  
  90  // process
  91  	public static function process()
  92      {
  93          MP_::no_abort_limit();
  94  
  95          new MP_Bounce();
  96      }
  97  
  98  // schedule
  99  	public static function schedule()
 100      {
 101          $config = get_option(self::option_name);
 102          $now4cron = current_time('timestamp', 'gmt');
 103  
 104          if (!wp_next_scheduled( 'mp_process_bounce_handling' )) 
 105              wp_schedule_single_event($now4cron + $config['every'], 'mp_process_bounce_handling');
 106      }
 107  
 108  ////  ADMIN  ////
 109  ////  ADMIN  ////
 110  ////  ADMIN  ////
 111  ////  ADMIN  ////
 112  
 113  // install
 114  	public static function install() 
 115      {
 116          self::uninstall();
 117  
 118          $pop3 = get_option(self::option_name_pop3);
 119          if (!$pop3)
 120          {
 121              $pop3 = get_option(self::option_name);
 122              if ($pop3) update_option(self::option_name_pop3, $pop3);
 123          }
 124  
 125          $logs = get_option(MailPress::option_name_logs);
 126          if (!isset($logs[self::log_name]))
 127          {
 128              $logs[self::log_name] = MailPress::$default_option_logs;
 129              update_option(MailPress::option_name_logs, $logs );
 130          }
 131  
 132          do_action('MailPress_schedule_bounce_handling');
 133      }
 134  
 135  	public static function uninstall() 
 136      {
 137          wp_clear_scheduled_hook('mp_process_bounce_handling');
 138      }
 139  
 140  // for link on plugin page
 141  	public static function plugin_action_links($links, $file)
 142      {
 143          return MailPress::plugin_links($links, $file, plugin_basename(__FILE__), 'batches');
 144      }
 145  
 146  // for settings
 147  	public static function scripts($scripts, $screen) 
 148      {
 149          if ($screen != MailPress_page_settings) return $scripts;
 150  
 151          wp_register_script( 'mp-bounce-handling',     '/' . MP_PATH . 'mp-admin/js/settings_bounce_handling.js', array(), false, 1);
 152          $scripts[] = 'mp-bounce-handling';
 153          return $scripts;
 154      }
 155  
 156  	public static function settings_tab($tabs)
 157      {
 158          $tabs['batches'] = __('Batches', MP_TXTDOM);
 159          return $tabs;
 160      }
 161  
 162  	public static function settings_tab_connection($tabs)
 163      {
 164          $tabs['connection_pop3'] = __('POP3', MP_TXTDOM);
 165          return $tabs;
 166      }
 167  
 168  	public static function settings_batches()
 169      {
 170          include  (MP_ABSPATH . 'mp-admin/includes/settings/batches_bounce_handling.form.php');
 171      }
 172  
 173  	public static function settings_logs($logs)
 174      {
 175          MP_AdminPage::logs_sub_form(self::log_name, $logs, __('Bounce', MP_TXTDOM));
 176      }
 177  
 178  	public static function autorefresh_every($every = 30)
 179      {
 180          $config = get_option(self::option_name);
 181          if (!$config) return $every;
 182          if ($every < $config['every']) return $every;
 183          return $config['every'];
 184      }
 185  
 186  	public static function autorefresh_js($scripts)
 187      {
 188          return MP_AutoRefresh_js::register_scripts($scripts);
 189      }
 190  
 191  // for users list
 192  	public static function get_icon_users($mp_user)
 193      {
 194          if ('bounced' != $mp_user->status) return;
 195  ?>
 196              <span class='icon bounce_handling' title="<?php _e('Bounced', MP_TXTDOM); ?>"></span>
 197  <?php
 198      }
 199  
 200  // for user page
 201  	public static function meta_boxes_user($mp_user_id, $screen)
 202      {
 203          $usermeta = MP_User_meta::get($mp_user_id, self::meta_key);
 204          if (!$usermeta) return;
 205  
 206          add_meta_box('bouncehandlingdiv', __('Bounces', MP_TXTDOM), array(__CLASS__, 'meta_box_user'), $screen, 'side', 'core');
 207      }
 208  
 209  	public static function meta_box_user($mp_user)
 210      {
 211          $usermeta = MP_User_meta::get($mp_user->id, self::meta_key);
 212          if (!$usermeta) return;
 213  
 214          global $wpdb;
 215          echo '<b>' . __('Bounces', MP_TXTDOM) . '</b> : &#160;' . $usermeta['bounce'] . '<br />';
 216          foreach($usermeta['bounces'] as $mail_id => $messages)
 217          {
 218              foreach($messages as $k => $message)
 219              {
 220                  echo '<br />';
 221                  $subject = $wpdb->get_var("SELECT subject FROM $wpdb->mp_mails WHERE id = " . $mail_id . ';');
 222                  $subject = ($subject) ? $subject : __('(deleted)', MP_TXTDOM);
 223  
 224                  $view_url        = esc_url(add_query_arg( array('action' => 'view_bounce', 'user_id' => $mp_user->id, 'mail_id' => $mail_id, 'id' => $k, 'preview_iframe' => 1, 'TB_iframe' => 'true'), MP_Action_url ));
 225                  $actions['view'] = "<a href='$view_url' class='thickbox thickbox-preview'  title='" . __('View', MP_TXTDOM) . "'>" . $subject . '</a>';
 226  
 227                  echo '(' . $mail_id . ') ' . $actions['view'];
 228              }
 229          }
 230      }
 231  
 232  // for mails list
 233  	public static function mails_columns($x)
 234      {
 235          $date = array_pop($x);
 236          $x['bounce_handling']    =  __('Bounce rate', MP_TXTDOM);
 237          $x['date']            = $date;
 238          return $x;
 239      }
 240  
 241  	public static function mails_get_row($column_name, $mail, $url_parms)
 242      {
 243          global $wpdb;
 244          switch ($column_name)
 245          {
 246              case 'bounce_handling' :
 247                  if (is_email($mail->toemail)) $total = 1;
 248                  elseif(is_serialized($mail->toemail)) $total = count(unserialize($mail->toemail));
 249                  else return;
 250  
 251                  $result = MP_Mail_meta::get($mail->id, self::meta_key);
 252                  if ($result) if ($total > 0) printf("%01.2f %%", 100 * $result/$total );
 253              break;
 254          }
 255      }
 256  
 257  // view bounce
 258  	public static function mp_action_view_bounce()
 259      {
 260          $mp_user_id = $_GET['user_id'];
 261          $mail_id    = $_GET['mail_id'];
 262          $bounce_id  = $_GET['id'];
 263  
 264          $usermeta = MP_User_meta::get($mp_user_id, self::meta_key);
 265          if (!$usermeta) return;
 266  
 267          $plaintext = $usermeta['bounces'][$mail_id][$bounce_id]['message'];
 268  
 269          include (MP_ABSPATH . 'mp-includes/html/plaintext.php');
 270      }
 271  }
 272  new MailPress_bounce_handling();
 273  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1