[ Index ]

MailPress 7.0.1

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

title

Body

[close]

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

   1  <?php
   2  class MP_Batch
   3  {
   4      public static $count = 0;
   5  
   6  	function __construct()
   7      {
   8          $this->config = get_option( MailPress_batch_send::option_name );
   9          $this->report = array();
  10  
  11          $this->trace = new MP_Log( 'mp_process_batch_send', array( 'option_name' => 'batch_send' ) );
  12  
  13          $this->process();
  14          if ( $this->have_batch() ) do_action( 'MailPress_schedule_batch_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_send::status_mail(), 'paused' ) );
  25      }
  26  
  27  // process
  28  	function process()
  29      {
  30          if ( self::$count ) return;
  31          self::$count++;
  32  
  33      // select mail
  34          global $wpdb;
  35          $mails = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->mp_mails WHERE status = %s ;", MailPress_batch_send::status_mail() ) );
  36  
  37          if ( !$mails ) { $this->alldone(); return; }
  38  
  39          $mail = $mailmeta = $this->mail = $this->mailmeta = false;
  40          $try = 10000;
  41  
  42          foreach ( $mails as $this->mail )
  43          {
  44              $this->mailmeta = $this->get_mailmeta();
  45              if ( !$this->mailmeta ) continue;
  46  
  47              if ( $this->mailmeta['try'] < $try )
  48              {
  49                  $try = $this->mailmeta['try'];
  50                  $mail = $this->mail;
  51                  $mailmeta = $this->mailmeta;                
  52              }
  53          }
  54          if ( !$mail ) { $this->alldone(); return; }
  55  
  56          $this->mail     = $mail;
  57          $this->mailmeta    = $mailmeta;
  58          unset( $mails, $mail, $mailmeta );
  59  
  60          $this->mailmeta['pass']++;
  61          $this->report['header'] = 'Batch Report mail #' . $this->mail->id . '  / count : ' . $this->mailmeta['count'] . ' / per_pass : ' . $this->config ['per_pass'] . ' / max_try : ' . $this->config ['max_retry'];
  62          $this->report['start']  = $this->mailmeta;
  63  
  64      // recipients
  65          $recipients = unserialize( $this->mail->toemail );
  66          $this->toemail= array();
  67  
  68          if ( $this->mailmeta['try'] )
  69          {
  70              $this->toemail     = array_slice( $this->mailmeta['failed'],     $this->mailmeta['offset'], $this->config ['per_pass'], true );
  71              foreach( $this->toemail as $k => $v ) $this->toemail[$k] = $recipients [$k];
  72          }
  73          else $this->toemail     = array_slice( $recipients,             $this->mailmeta['offset'], $this->config ['per_pass'], true );
  74  
  75          $count_recipients = count( $this->toemail );
  76  
  77      // processing
  78          if ( !$count_recipients )
  79          {
  80              $this->report['processing']  = array_merge( $this->mailmeta, array( ">> WARNING >>" => 'No more recipient' ) );
  81              $this->mailmeta['processed'] = $this->mailmeta['offset'] = $this->mailmeta['pass'] = 0;
  82              $this->mailmeta['try']++;
  83          }
  84          else
  85          {
  86              $this->mailmeta['processed'] += $count_recipients;
  87              $this->report['processing'] = $this->mailmeta;
  88              $this->write_report();
  89  
  90      // saving context, if abort, current recipients will be on error & next recipients will be processed.
  91              $this->mailmeta['offset'] += $count_recipients;
  92  
  93              $maybe_failures = array_flip( array_keys( $this->toemail ) );
  94              if ( $this->mailmeta['try'] )     $this->mailmeta['failed'] = array_merge( $maybe_failures, $this->mailmeta['failed'] );
  95              else                  $this->mailmeta['failed'] = array_merge( $this->mailmeta['failed'], $maybe_failures );
  96  
  97              if ( !MP_Mail_meta::add( $this->mail->id, MailPress_batch_send::meta_key, $this->mailmeta, true ) )
  98                  MP_Mail_meta::update( $this->mail->id, MailPress_batch_send::meta_key, $this->mailmeta );
  99              $this->trace->restart();
 100  
 101      // sending
 102              $swiftfailed = $this->send();
 103  
 104      // results
 105              $this->trace->restart();
 106              switch ( true )
 107              {
 108                  case ( is_array( $swiftfailed ) ) : 
 109                      $ko = array_flip( $swiftfailed );
 110                      $ok = array_diff_key( $this->toemail, $ko );
 111                  break;
 112                  case ( !$swiftfailed ) : 
 113                      $ko = $maybe_failures ;
 114                      $ok = array();
 115                  break;
 116                  default : 
 117                      $ko = array();
 118                      $ok = $maybe_failures ;
 119                  break;
 120              }
 121  
 122              $count_sent  = count( $ok );
 123              $this->mailmeta['sent'] += $count_sent;
 124  
 125              foreach ( $ok as $k => $v )
 126              {
 127                  unset( $this->mailmeta['failed'][$k] );
 128                  if ( $this->mailmeta['try'] ) $this->mailmeta['offset']-- ;
 129              }
 130              if ( $this->mailmeta['try'] ) $this->mailmeta['failed'] = array_merge( $ko, $this->mailmeta['failed'] );
 131              else $this->mailmeta['failed'] = array_merge( $this->mailmeta['failed'], $ko );
 132          }
 133      // saving context
 134          $this->report['end']  = $this->mailmeta;
 135          if ( !MP_Mail_meta::add( $this->mail->id, MailPress_batch_send::meta_key, $this->mailmeta, true ) )
 136              MP_Mail_meta::update( $this->mail->id, MailPress_batch_send::meta_key, $this->mailmeta );
 137  
 138      // the end for this mail ?
 139          if ( $this->mailmeta['sent'] == $this->mailmeta['count'] )         $this->update_mail_status();
 140          if ( $this->mailmeta['try']  >= $this->config ['max_retry'] + 1 )     $this->update_mail_status( count( $this->mailmeta['failed'] ) );
 141      }
 142  
 143  // get mailmeta
 144  	function get_mailmeta()
 145      {
 146          $mailmeta = MP_Mail_meta::get( $this->mail->id , MailPress_batch_send::meta_key );
 147  
 148          if ( !$mailmeta )
 149          {
 150              $mailmeta = array();
 151  
 152              if ( is_serialized ( $this->mail->toemail ) )    $mailmeta['count'] = count( unserialize( $this->mail->toemail ) );
 153              else                        $mailmeta['count'] = 1;
 154  
 155              $mailmeta['sent'] = $mailmeta['try'] = $mailmeta['processed'] = $mailmeta['pass'] = $mailmeta['offset'] = 0;
 156              $mailmeta['failed'] = array();
 157              return $mailmeta;
 158          }
 159  
 160          if ( isset( $mailmeta['per_pass'] ) ) // convert old format prior to mailpress 4.0
 161          {
 162              if ( !$mailmeta['try'] ) $mailmeta['offset'] = ( $mailmeta['pass'] ) ? ( $mailmeta['pass'] - 1 ) * $mailmeta['per_pass'] : 0;
 163              unset( $mailmeta['per_pass'], $mailmeta['max_try'] );
 164          }
 165  
 166          $failed = ( isset( $mailmeta['failed'] ) ) ? count( $mailmeta['failed'] ) : 0;
 167  
 168          if ( $mailmeta['sent'] == $mailmeta['count'] ) { $this->update_mail_status( $failed ); return false; }
 169  
 170          $processed = ( $mailmeta['try'] ) ? $mailmeta['offset'] : $mailmeta['processed'];
 171          $count     = ( $mailmeta['try'] ) ? $failed : $mailmeta['count'];
 172  
 173          if ( $processed >= $count ) 
 174          {
 175              $mailmeta['processed'] = $mailmeta['offset'] = $mailmeta['pass'] = 0;
 176              $mailmeta['try']++;            
 177          }
 178  
 179          if ( $mailmeta['try'] >= $this->config ['max_retry'] + 1 ) { $this->update_mail_status( $failed ); return false; }
 180          if ( $mailmeta['try'] && !$failed )              { $this->update_mail_status( $failed ); return false; }
 181  
 182          return $mailmeta;
 183      }
 184  
 185  // finish
 186  	function update_mail_status( $failed = 0 )
 187      {
 188          global $wpdb;
 189                  
 190          $x = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->mp_mails SET status = 'sent' WHERE id = %d ", $this->mail->id ) );
 191          if ( !$failed ) MP_Mail_meta::delete( $this->mail->id , MailPress_batch_send::meta_key );
 192      }
 193  
 194  // batch sending
 195  	function send()
 196      {
 197  // instaure the context
 198          $_this = new MP_Mail( __CLASS__ );
 199  
 200          $_this->trace                 = $this->trace;
 201  
 202          $_this->mail                 = new stdClass();
 203          $_this->mail->swift_batchSend     = true;
 204          $_this->mail->mailpress_batch_send     = true;
 205  
 206          $_this->row                 =  new stdClass();
 207          $_this->row                 = $this->mail;
 208          $_this->args                 = new stdClass();
 209  
 210          $_this->args->replacements         = $this->toemail;
 211          $_this->get_old_recipients();
 212  
 213          $m = MP_Mail_meta::get( $_this->row->id, '_MailPress_replacements' );
 214          if ( !is_array( $m ) ) $m = array();
 215          $_this->mail->replacements = $m;
 216  
 217          add_filter( 'MailPress_swift_send', array( $this, 'swift_send' ), 8, 1 );
 218          return $_this->swift_processing(); // will activate swift_send function
 219      }
 220  
 221  // send
 222  	function swift_send( $_this )
 223      {
 224          if ( $_this->mail->mailpress_batch_send )
 225          {
 226              $_this->mysql_disconnect( __CLASS__ );
 227  
 228              $_this->swift->registerPlugin( new Swift_Plugins_DecoratorPlugin( $_this->row->replacements ) );
 229              if ( !$_this->swift_batchSend( $failures ) )
 230              {
 231                  $_this->mysql_connect( __CLASS__ . ' 2' );
 232                  return false;
 233              }
 234              $_this->mysql_connect( __CLASS__ );
 235              return $failures;
 236          }
 237          return true;
 238      }
 239  
 240  //reports
 241  	function alldone()
 242      {
 243          $this->report['header2'] = 'Batch Report';
 244          $this->report['alldone']  = true;
 245      }
 246  
 247  	function write_report( $zz = 12 )
 248      {
 249          $order = array( 'sent', 'processed', 'try', 'pass', 'offset', 'failed' );
 250          $unsets = array(  'count', 'per_pass', 'max_try' );
 251          $t = ( count( $order ) + 1 ) * ( $zz + 1 ) -1;
 252  
 253          foreach( $this->report as $k => $v )
 254          {
 255              switch ( $k )
 256              {
 257                  case 'header' :
 258                      $this->trace->log( '!' . str_repeat( '-', $t ) . '!' );
 259                      $l = strlen( $v );
 260                      $this->trace->log( '!' . str_repeat( ' ', 5 ) . $v . str_repeat( ' ', $t - 5 - $l ) . '!' );
 261                      $this->trace->log( '!' . str_repeat( '-', $t ) . '!' );
 262                      $s = '!            !';
 263                      foreach( $order as $o )
 264                      {
 265                          $l = strlen( $o );
 266                          $s .= " $o" . str_repeat( ' ', $zz - $l -1 ) . '!';
 267                      }
 268                      $this->trace->log( $s );
 269                      $this->trace->log( '!' . str_repeat( '-', $t ) . '!' );
 270                  break;
 271                  case 'header2' :
 272                      $t = count( $order ) * 15;
 273                      $this->trace->log( '!' . str_repeat( '-', $t ) . '!' );
 274                      $l = strlen( $v );
 275                      $this->trace->log( '!' . str_repeat( ' ', 5 ) . $v . str_repeat( ' ', $t - 5 - $l ) . '!' );
 276                      $this->trace->log( '!' . str_repeat( '-', $t ) . '!' );
 277                  break;
 278                  case 'alldone' :
 279                      $t = count( $order ) * 15;
 280                      $v = ' *** ALL DONE ***       *** ALL DONE ***       *** ALL DONE *** '; 
 281                      $l = strlen( $v );
 282                      $this->trace->log( '!' . str_repeat( ' ', 5 ) . $v . str_repeat( ' ', $t -15 - $l ) . '!' );
 283                  break;
 284                  case 'locked' :
 285                      $t = count( $order ) * 15;
 286                      $v = " locked : $v "; 
 287                      $l = strlen( $v );
 288                      $this->trace->log( '!' . str_repeat( ' ', 5  ) . $v . str_repeat( ' ', $t -10 - $l ) . '!' );
 289                      $this->trace->log( '!' . str_repeat( '-', $t ) . '!' );
 290                      $this->trace->log( '!' . str_repeat( '-', $t ) . '!' );
 291                  break;
 292                  case 'end' :
 293                      $this->trace->log( '!' . str_repeat( '-', $t ) . '!' );
 294                  default :
 295                      foreach ( $unsets as $unset ) unset( $v[$unset] );
 296                      $c = 0;
 297                      $l = strlen( $k );
 298                      $s = "! $k" . str_repeat( ' ', $zz - $l -1 ) . '!';
 299                      foreach( $order as $o )
 300                      {
 301                          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++;}
 302                      }
 303                      if ( $c < count( $order ) ) do { $s.= str_repeat( ' ', $zz ) . '!'; $c++;} while( $c <  count( $order ) );
 304                      $this->trace->log( $s );
 305                      if ( !empty( $v ) ) foreach( $v as $a => $b ) $this->trace->log( "$a $b" );
 306                  break;
 307              }
 308          }
 309          $this->trace->log( '!' . str_repeat( '-', $t ) . '!' );
 310          $this->report = array();
 311      }
 312  }


Generated: Mon Jan 28 00:07:10 2019 Cross-referenced by PHPXref 0.7.1