[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/class/ -> MP_Batch_spool.class.php (source)

   1  <?php
   2  class MP_Batch_spool extends MP_db_connect_
   3  {
   4      public static $count = 0;
   5  
   6  	function __construct()
   7      {
   8          $this->config = get_option(MailPress_batch_spool_send::option_name);
   9          $this->report = array();
  10  
  11          $this->trace = new MP_Log('mp_process_batch_spool_send', array('option_name' => 'batch_spool_send'));
  12  
  13          $this->process();
  14          if ($this->have_batch()) do_action('MailPress_schedule_batch_spool_send');
  15  
  16          $this->write_report();
  17          $this->trace->end(true);
  18      }
  19  
  20  // have_batch
  21  	function have_batch()
  22      {
  23          global $wpdb;
  24          return $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->mp_mails WHERE status IN ( %s , %s ) ;", MailPress_batch_spool_send::status_mail(), 'paused' ) );
  25      }
  26  
  27  // process
  28  	function process()
  29      {
  30          if (self::$count) return;
  31          self::$count++;
  32  
  33      // select path
  34          $this->path = (empty($this->config['path'])) ? MP_ABSPATH . 'tmp/spool/' : $this->config['path'] . '/' ;
  35  
  36      // select mail
  37          global $wpdb;
  38          $mails = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->mp_mails WHERE status = %s ;", MailPress_batch_spool_send::status_mail() ) );
  39  
  40          if (!$mails) { $this->alldone(); return; }
  41  
  42          $this->mail = $this->mailmeta = false;
  43  
  44          $mail = $mailmeta = $this->mail = $this->mailmeta = false;
  45          $try = 10000;
  46  
  47          foreach ($mails as $this->mail)
  48          {
  49              $this->spool_path = $this->path . $this->mail->id;
  50              if (!is_dir($this->spool_path))     // no spool path for this mail => everything was sent
  51              {
  52                  $this->update_mail_status();
  53                  continue;
  54              }
  55  
  56              $this->mailmeta = $this->get_mailmeta();
  57              if (!$this->mailmeta) continue;
  58  
  59              if ($this->mailmeta['try'] < $try)
  60              {
  61                  $try = $this->mailmeta['try'];
  62                  $mail = $this->mail;
  63                  $mailmeta = $this->mailmeta;                
  64              }
  65          }
  66          if (!$mail) { $this->alldone(); return; }
  67  
  68          $this->mail     = $mail;
  69          $this->mailmeta    = $mailmeta;
  70          $this->spool_path = $this->path . $this->mail->id;
  71          unset($mails, $mail, $mailmeta);
  72                  
  73          $this->mailmeta['pass']++;
  74          $this->report['header']  = 'Batch Spool Report mail #' . $this->mail->id . '  / count : ' . $this->mailmeta['count'] . ' / per_pass : ' . $this->config['per_pass'] . ' / max_try : ' . $this->config['max_retry'];
  75          $this->report['start']   = $this->mailmeta;
  76                  
  77      // recipients
  78          $max = ($this->mailmeta['try']) ? count($this->mailmeta['failed']) : $this->mailmeta['count'];
  79          $count_recipients = (($this->mailmeta['processed'] + $this->config['per_pass']) > $max) ? fmod($max, $this->config['per_pass']) : $this->config['per_pass'];
  80                  
  81          // processing
  82          if (!$count_recipients)
  83          {
  84              $this->report['processing']  = array_merge($this->mailmeta, array( ">> WARNING >>" => 'No more recipient' ) );
  85              $this->mailmeta['processed'] = $this->mailmeta['pass'] = 0;
  86              $this->mailmeta['try']++;
  87          }
  88          else
  89          {
  90              $this->mailmeta['processed'] += $count_recipients;
  91              $this->report['processing'] = $this->mailmeta;
  92              $this->write_report();
  93                          
  94          // saving context, if abort, current recipients may or may be not sent anyway empty spool will tell !
  95              if ($this->mailmeta['try'])
  96              {
  97                  if (count($this->mailmeta['failed']) <= $count_recipients) $this->mailmeta['failed'] = array();
  98                  else for ($i = 1; $i <= $count_recipients; $i++) array_shift($this->mailmeta['failed']);
  99              }
 100  
 101              if (!MP_Mail_meta::add($this->mail->id, MailPress_batch_spool_send::meta_key, $this->mailmeta, true))
 102                  MP_Mail_meta::update($this->mail->id, MailPress_batch_spool_send::meta_key, $this->mailmeta);
 103              $this->trace->restart();
 104  
 105      // sending
 106              $swiftfailed = $this->send();
 107  
 108      // results
 109              $this->trace->restart();
 110              switch (true)
 111              {
 112                  case (is_array($swiftfailed)) : 
 113                      $ko = array_flip($swiftfailed);
 114                  break;
 115                  default : 
 116                      $ko = array();
 117                      break;
 118              }
 119  
 120              if ($this->mailmeta['try']) $this->mailmeta['failed'] = array_merge($ko, $this->mailmeta['failed']);
 121              else $this->mailmeta['failed'] = array_merge($this->mailmeta['failed'], $ko);
 122          }
 123      // saving context
 124          $this->report['end']  = $this->mailmeta;
 125          if (!MP_Mail_meta::add($this->mail->id, MailPress_batch_spool_send::meta_key, $this->mailmeta, true))
 126              MP_Mail_meta::update($this->mail->id, MailPress_batch_spool_send::meta_key, $this->mailmeta);
 127  
 128      // the end for this mail ?
 129          if ($this->mailmeta['sent'] == $this->mailmeta['count'])         $this->update_mail_status();
 130          if ($this->mailmeta['try'] >= $this->config['max_retry'] + 1)   $this->update_mail_status();
 131      }
 132  
 133  // get mailmeta
 134  	function get_mailmeta()
 135      {
 136          $mailmeta = MP_Mail_meta::get( $this->mail->id , MailPress_batch_spool_send::meta_key);
 137  
 138          if (!$mailmeta)
 139          {
 140              $mailmeta = array();
 141  
 142              if (is_serialized ($this->mail->toemail))    $mailmeta['count'] = count(unserialize($this->mail->toemail));
 143              else                                $mailmeta['count'] = 1;
 144  
 145              $mailmeta['sent'] = $mailmeta['try'] = $mailmeta['processed'] = $mailmeta['pass'] = 0;
 146              $mailmeta['failed'] = array();
 147              return $mailmeta;
 148          }
 149  
 150          $failed = (isset($mailmeta['failed'])) ? count($mailmeta['failed']) : 0;
 151  
 152          if ($mailmeta['sent'] == $mailmeta['count']) { $this->update_mail_status(); return false; }
 153  
 154          $processed = $mailmeta['processed'];
 155          $count     = ($mailmeta['try']) ? $failed : $mailmeta['count'];
 156  
 157          if ($processed >= $count) 
 158          {
 159              $mailmeta['processed'] = $mailmeta['pass'] = 0;
 160              $mailmeta['try']++;            
 161          }
 162  
 163          if ($mailmeta['try'] >= $this->config ['max_retry'] + 1) {    $this->update_mail_status(); return false; }
 164          if ($mailmeta['try'] && !$failed) { $this->update_mail_status(); return false; }
 165  
 166          return $mailmeta;
 167      }
 168  
 169          // finish
 170  	function update_mail_status($failed = 0)
 171      {
 172          if (is_dir($this->spool_path))
 173          {
 174              $directoryIterator = new DirectoryIterator($this->spool_path);
 175              foreach ($directoryIterator as $file)
 176              {
 177                  if (in_array($file, array('.', '..'))) continue;
 178                  if (unlink("{$this->spool_path}/{$file}")) $failed++;
 179              }
 180              rmdir($this->spool_path);
 181          }
 182  
 183          global $wpdb;
 184                  
 185          $x = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->mp_mails SET status = 'sent' WHERE id = %d ", $this->mail->id) );
 186          if (!$failed) MP_Mail_meta::delete( $this->mail->id , MailPress_batch_spool_send::meta_key);
 187      }
 188  
 189  // batch sending
 190  	function send()
 191      {
 192             return $this->swift_processing();
 193      }
 194  
 195  // send
 196  	function swift_processing()
 197      {
 198          require_once  (MP_ABSPATH . 'mp-includes/Swiftmailer/swift_required.php');
 199                  
 200      // Swift spool connection //
 201  
 202          try 
 203          {
 204                  $spool_conn = Swift_SpoolTransport::newInstance( new Swift_FileSpool($this->spool_path) );
 205          }
 206          catch (Swift_SwiftException $e) 
 207          {
 208              $this->trace->log('SWIFTMAILER [ERROR] - ' . "There was an unexpected problem accessing spool :\n\n" . $e->getMessage() . "\n\n");    
 209              return false;
 210          }
 211                  
 212      // Swift connection //
 213  
 214          if (!has_filter('MailPress_Swift_Connection_type')) new MP_Connection_smtp();
 215  
 216          try 
 217          {
 218              $Swift_Connection_type = apply_filters('MailPress_Swift_Connection_type', null);
 219  
 220              $conn = apply_filters('MailPress_Swift_Connection_' . $Swift_Connection_type , $this->mail->id, $this->trace );
 221                          
 222              $swift = Swift_Mailer::newInstance($conn);
 223          }
 224          catch (Swift_SwiftException $e) 
 225          {
 226              $this->trace->log('SWIFTMAILER [ERROR] - ' . "There was a problem connecting with $Swift_Connection_type :\n\n" . $e->getMessage() . "\n\n");    
 227              $this->mysql_connect(__CLASS__ . ' connect error :  ' . $Swift_Connection_type);
 228              return false;
 229          } 
 230  
 231          Swift_Preferences::getInstance()->setTempDir(MP_ABSPATH . "tmp")->setCacheType('disk');
 232  
 233      // Swift sending ... //
 234          try 
 235          {
 236               $swift = apply_filters('MailPress_swift_registerPlugin', $swift);
 237                          
 238              $this->mysql_disconnect(__CLASS__);
 239  
 240          // swift flushing queues //
 241              $spool = $spool_conn->getSpool();
 242              $spool->setMessageLimit($this->config['per_pass']);
 243              $spool->setTimeLimit($this->config['time_limit']);
 244              $this->mailmeta['sent'] += $spool->flushQueue($conn, $failures);
 245          }
 246          catch (Swift_SwiftException $e) 
 247          {
 248              $this->trace->log('SWIFTMAILER [ERROR] - ' . "There was a problem sending with $Swift_Connection_type :\n\n" . $e->getMessage() . "\n\n");    
 249              $this->mysql_connect(__CLASS__ . ' sending error : ' . $Swift_Connection_type);
 250              return false;
 251          }
 252  
 253          $this->mysql_connect(__CLASS__);
 254                          
 255          return $failures;
 256      }
 257  
 258  //reports
 259  	function alldone()
 260      {
 261          $this->report['header2'] = 'Batch Spool Report';
 262          $this->report['alldone']  = true;
 263      }
 264  
 265  	function write_report($zz = 12)
 266      {
 267          $order = array('sent', 'processed', 'try', 'pass', 'failed');
 268          $unsets = array(  'count', 'per_pass', 'max_try' );
 269          $t = (count($order) + 1) * ($zz + 1) -1;
 270  
 271          foreach($this->report as $k => $v)
 272          {
 273              switch ($k)
 274              {
 275                  case 'header' :
 276                      $this->trace->log('!' . str_repeat( '-', $t) . '!');
 277                      $l = strlen($v);
 278                      $this->trace->log('!' . str_repeat( ' ', 5) . $v . str_repeat( ' ', $t - 5 - $l) . '!');
 279                      $this->trace->log('!' . str_repeat( ' ', 5) . $this->spool_path);
 280                      $this->trace->log('!' . str_repeat( '-', $t) . '!');
 281                      $s = '!            !';
 282                      foreach($order as $o)
 283                      {
 284                          $l = strlen($o);
 285                          $s .= " $o" . str_repeat( ' ', $zz - $l -1) . '!';
 286                      }
 287                      $this->trace->log($s);
 288                      $this->trace->log('!' . str_repeat( '-', $t) . '!');
 289                  break;
 290                  case 'header2' :
 291                      $t = count($order) * 15;
 292                      $this->trace->log('!' . str_repeat( '-', $t) . '!');
 293                      $l = strlen($v);
 294                      $this->trace->log('!' . str_repeat( ' ', 5) . $v . str_repeat( ' ', $t - 5 - $l) . '!');
 295                      $this->trace->log('!' . str_repeat( '-', $t) . '!');
 296                  break;
 297                  case 'alldone' :
 298                      $t = count($order) * 15;
 299                      $v = ' *** ALL DONE ***       *** ALL DONE ***       *** ALL DONE *** '; 
 300                      $l = strlen($v);
 301                      $this->trace->log('!' . str_repeat( ' ', 15) . $v . str_repeat( ' ', $t -15 - $l) . '!');
 302                  break;
 303                  case 'locked' :
 304                      $t = count($order) * 15;
 305                      $v = " locked : $v "; 
 306                      $l = strlen($v);
 307                      $this->trace->log('!' . str_repeat( ' ', 10) . $v . str_repeat( ' ', $t -10 - $l) . '!');
 308                      $this->trace->log('!' . str_repeat( '-', $t) . '!');
 309                      $this->trace->log('!' . str_repeat( '-', $t) . '!');
 310                  break;
 311                  case 'end' :
 312                      $this->trace->log('!' . str_repeat( '-', $t) . '!');
 313                  default :
 314                      foreach ($unsets as $unset) unset($v[$unset]);
 315                      $c = 0;
 316                      $l = strlen($k);
 317                      $s = "! $k" . str_repeat( ' ', $zz - $l -1) . '!';
 318                      foreach($order as $o)
 319                      {
 320                          if (isset($v[$o])) { if (is_array($v[$o])) $v[$o] = count($v[$o]); $l = strlen($v[$o]); $s .= str_repeat( ' ', $zz - $l -1) . $v[$o] .  ' !'; unset($v[$o]); $c++;}
 321                      }
 322                      if ($c < count($order)) do { $s.= str_repeat( ' ', $zz) . '!'; $c++;} while($c <  count($order));
 323                      $this->trace->log($s);
 324                      if (!empty($v)) foreach($v as $a => $b) $this->trace->log("$a $b");
 325                  break;
 326              }
 327          }
 328          $this->trace->log('!' . str_repeat( '-', $t) . '!');
 329          $this->report = array();
 330      }
 331  }


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