[ Index ]

MailPress 544

[ 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: 5.4.4
   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 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_II',     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_II',     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              if (!class_exists('MailPress_bounce_handling'))
  59              {
  60              // for users list
  61                  add_action('MailPress_get_icon_users',     array(__CLASS__, 'get_icon_users'), 8, 1);
  62              // for meta box in user page
  63                  add_action('MailPress_add_meta_boxes_user',array(__CLASS__, 'meta_boxes_user'), 8, 2);
  64              }
  65          }
  66  
  67          if (!class_exists('MailPress_bounce_handling'))
  68          {
  69          // for mails list
  70              add_filter('MailPress_mails_columns',         array(__CLASS__, 'mails_columns'), 10, 1);
  71              add_action('MailPress_mails_get_row',          array(__CLASS__, 'mails_get_row'), 10, 3);
  72          // view bounce
  73              add_action('mp_action_view_bounce',         array(__CLASS__, 'mp_action_view_bounce')); 
  74          }
  75      }
  76  
  77  // prepare mail
  78  	public static function swift_message_headers($message, $row)
  79      {
  80          if (!class_exists('MailPress_bounce_handling'))
  81          {
  82              $config = get_option(self::option_name);
  83              if (isset($config['Return-Path']) && is_email($config['Return-Path'])) $message->setReturnPath($config['Return-Path']);
  84          }
  85  
  86          global $wpdb;
  87          $headers = array(
  88                      array(    'type' => Swift_Mime_Header::TYPE_TEXT , 
  89                              'name' => 'X-MailPress-blog-id',
  90                              'value' => $wpdb->blogid
  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' => (isset($row->mp_user_id)) ? $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          return $message;
 131      }
 132  
 133  // process
 134  	public static function process()
 135      {
 136          MP_::no_abort_limit();
 137  
 138          new MP_Bounce_II();
 139      }
 140  
 141  // schedule
 142  	public static function schedule()
 143      {
 144          $config = get_option(self::option_name);
 145          $now4cron = current_time('timestamp', 'gmt');
 146  
 147          if (!wp_next_scheduled( 'mp_process_bounce_handling_II' )) 
 148              wp_schedule_single_event($now4cron + $config['every'], 'mp_process_bounce_handling_II');
 149      }
 150  
 151  ////  ADMIN  ////
 152  ////  ADMIN  ////
 153  ////  ADMIN  ////
 154  ////  ADMIN  ////
 155  
 156  // install
 157  	public static function install() 
 158      {
 159          self::uninstall();
 160  
 161          $pop3 = get_option(self::option_name_pop3);
 162          if (!$pop3)
 163          {
 164              $pop3 = get_option(self::option_name);
 165              if ($pop3) update_option(self::option_name_pop3, $pop3);
 166          }
 167  
 168          $logs = get_option(MailPress::option_name_logs);
 169          if (!isset($logs[self::log_name]))
 170          {
 171              $logs[self::log_name] = MailPress::$default_option_logs;
 172              update_option(MailPress::option_name_logs, $logs );
 173          }
 174  
 175          do_action('MailPress_schedule_bounce_handling_II');
 176      }
 177  
 178  	public static function uninstall() 
 179      {
 180          wp_clear_scheduled_hook('mp_process_bounce_handling_II');
 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', MP_TXTDOM);
 202          return $tabs;
 203      }
 204  
 205  	public static function settings_tab_connection($tabs)
 206      {
 207          $tabs['connection_pop3'] = __('POP3', MP_TXTDOM);
 208          return $tabs;
 209      }
 210  
 211  	public static function settings_batches()
 212      {
 213          include  (MP_ABSPATH . 'mp-admin/includes/settings/batches_bounce_handling_II.form.php');
 214      }
 215  
 216  	public static function settings_logs($logs)
 217      {
 218          MP_AdminPage::logs_sub_form(self::log_name, $logs, __('Bounce', MP_TXTDOM) . ' II');
 219      }
 220  
 221  	public static function autorefresh_every($every = 30)
 222      {
 223          $config = get_option(self::option_name);
 224          if (!$config) return $every;
 225          if ($every < $config['every']) return $every;
 226          return $config['every'];
 227      }
 228  
 229  	public static function autorefresh_js($scripts)
 230      {
 231          return MP_AutoRefresh_js::register_scripts($scripts);
 232      }
 233  
 234  // for users list
 235  	public static function get_icon_users($mp_user)
 236      {
 237          if ('bounced' != $mp_user->status) return;
 238  ?>
 239              <span class='icon bounce_handling' title="<?php _e('Bounced', MP_TXTDOM); ?>"></span><?php
 240      }
 241  
 242  // for user page
 243  	public static function meta_boxes_user($mp_user_id, $screen)
 244      {
 245          $usermeta = MP_User_meta::get($mp_user_id, self::meta_key);
 246          if (!$usermeta) return;
 247  
 248          add_meta_box('bouncehandlingdiv', __('Bounces', MP_TXTDOM), array(__CLASS__, 'meta_box_user'), $screen, 'side', 'core');
 249      }
 250  
 251  	public static function meta_box_user($mp_user)
 252      {
 253          $usermeta = MP_User_meta::get($mp_user->id, self::meta_key);
 254          if (!$usermeta) return;
 255  
 256          global $wpdb;
 257          echo '<b>' . __('Bounces', MP_TXTDOM) . '</b> : &#160;' . $usermeta['bounce'] . '<br />';
 258          foreach($usermeta['bounces'] as $mail_id => $messages)
 259          {
 260              foreach($messages as $k => $message)
 261              {
 262                  echo '<br />';
 263                  $subject = $wpdb->get_var("SELECT subject FROM $wpdb->mp_mails WHERE id = " . $mail_id . ';');
 264                  $subject = ($subject) ? $subject : __('(deleted)', MP_TXTDOM);
 265  
 266                  $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 ));
 267                  $actions['view'] = "<a href='$view_url' class='thickbox thickbox-preview'  title='" . __('View', MP_TXTDOM) . "'>" . $subject . '</a>';
 268  
 269                  echo '(' . $mail_id . ') ' . $actions['view'];
 270              }
 271          }
 272      }
 273  
 274  // for mails list
 275  	public static function mails_columns($x)
 276      {
 277          $date = array_pop($x);
 278          $x['bounce_handling']    =  __('Bounce rate', MP_TXTDOM);
 279          $x['date']            = $date;
 280          return $x;
 281      }
 282  
 283  	public static function mails_get_row($column_name, $mail, $url_parms)
 284      {
 285          global $wpdb;
 286          switch ($column_name)
 287          {
 288              case 'bounce_handling' :
 289                  if (is_email($mail->toemail)) $total = 1;
 290                  elseif(is_serialized($mail->toemail)) $total = count(unserialize($mail->toemail));
 291                  else return;
 292  
 293                  $result = MP_Mail_meta::get($mail->id, self::meta_key);
 294                  if ($result) if ($total > 0) printf("%01.2f %%", 100 * $result/$total );
 295              break;
 296          }
 297      }
 298  
 299  // view bounce
 300  	public static function mp_action_view_bounce()
 301      {
 302          $mp_user_id = $_GET['user_id'];
 303          $mail_id    = $_GET['mail_id'];
 304          $bounce_id  = $_GET['id'];
 305  
 306          $usermeta = MP_User_meta::get($mp_user_id, self::meta_key);
 307          if (!$usermeta) return;
 308  
 309          $plaintext = $usermeta['bounces'][$mail_id][$bounce_id]['message'];
 310  
 311          include (MP_ABSPATH . 'mp-includes/html/plaintext.php');
 312      }
 313  }
 314  new MailPress_bounce_handling_II();
 315  }


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