[ Index ]

MailPress 7.2

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


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