[ Index ]

MailPress 544

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

title

Body

[close]

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

   1  <?php
   2  abstract class MP_autoresponder_event_
   3  {
   4      const bt = 100;
   5  
   6  	function __construct($desc)
   7      {
   8          $this->desc = $desc;
   9          if (!isset($this->callback)) $this->callback = array($this, 'callback');
  10  
  11          add_filter('MailPress_autoresponder_events_register',    array($this, 'register'), 8, 1);
  12          add_action($this->event, $this->callback, 8, 1);
  13          add_action('mp_process_autoresponder_' . $this->id, array($this, 'process'), 8, 1);
  14  
  15          add_action('MailPress_autoresponder_' . $this->id . '_settings_form',    array($this, 'settings_form'),    8, 1);
  16      }
  17  
  18  	function register($events)
  19      {
  20          $events[$this->id] = $this->desc;
  21          return $events;
  22      }
  23  
  24  //// Tracking events to autorespond to  ////
  25  
  26  	function callback($args)
  27      {
  28          $autoresponders = MP_Autoresponder::get_from_event($this->id);
  29          if (empty($autoresponders)) return;
  30  
  31          foreach( $autoresponders as $autoresponder )
  32          {
  33              if (!$this->to_do($autoresponder, $args)) continue;
  34  
  35              $_mails = MP_Autoresponder::get_term_objects($autoresponder->term_id);
  36  
  37              if (!isset($_mails[0])) continue;
  38  
  39              $term_id = $autoresponder->term_id;
  40  
  41              $time = time();
  42              $schedule = $this->schedule($time, $_mails[0]['schedule']);
  43              $meta_id = MP_User_meta::add($this->mp_user_id, '_MailPress_autoresponder_' . $term_id, $time);
  44  
  45              $this->trace = new MP_Log('mp_process_autoresponder_'. $term_id, array('option_name' => 'autoresponder'));
  46  
  47              $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
  48              $bm = "Batch Report autoresponder #$term_id            meta_id : $meta_id  mail_order : 0";
  49              $this->trace->log('!' . str_repeat( ' ', 5) . $bm . str_repeat( ' ', self::bt - 5 - strlen($bm)) . '!');
  50              $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
  51              $bm = " mp_user    ! $this->mp_user_id";
  52              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
  53              $bm = " event      ! " . $this->event;
  54              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
  55              $bm = " 1st sched. ! ";
  56              $bm .= ('000000' == $_mails[0]['schedule']) ? __('now',  MP_TXTDOM) : date('Y-m-d H:i:s', $schedule);
  57              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
  58              $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
  59  
  60              $this->trace->end(true);
  61  
  62              $_args = array('meta_id' => $meta_id, 'mail_order'=> 0 );
  63              if ('000000' == $_mails[0]['schedule'])
  64                  do_action ('mp_process_autoresponder_' . $this->id, $_args);
  65              else
  66                  wp_schedule_single_event($schedule, 'mp_process_autoresponder_' . $this->id,     array('args' => $_args));
  67          }
  68      }
  69  
  70  	function to_do($autoresponder, $args)
  71      {
  72          $this->mp_user_id = $args;
  73          return true;
  74      }
  75  
  76  	function schedule($time, $value)
  77      {
  78          $Y = date('Y', $time) + $value['Y'];
  79          $M = date('n', $time) + $value['M'];
  80          $D = date('j', $time) + $value['D'] + ($value['W'] * 7);
  81          $H = date('G', $time) + $value['H'];
  82          $Mn= date('i', $time);
  83          $S = date('s', $time);
  84  
  85          return mktime($H, $Mn, $S, $M, $D, $Y);
  86      }
  87  
  88  	function process($args)
  89      {
  90          MP_::no_abort_limit();
  91  
  92          extract($args);        // $meta_id, $mail_order
  93          $meta_id = (isset($umeta_id)) ? $umeta_id : $meta_id;
  94  
  95          $meta = MP_User_meta::get_by_id($meta_id);
  96          $term_id     = (!$meta) ? 'unknown' : str_replace('_MailPress_autoresponder_', '', $meta->meta_key);
  97  
  98          $this->trace = new MP_Log('mp_process_autoresponder_'. $term_id, array('option_name' => 'autoresponder'));
  99  
 100          $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
 101          $bm = "Batch Report autoresponder #$term_id            meta_id : $meta_id  mail_order : $mail_order";
 102          $this->trace->log('!' . str_repeat( ' ', 5) . $bm . str_repeat( ' ', self::bt - 5 - strlen($bm)) . '!');
 103          $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
 104  
 105  
 106          $bm = " start      !";
 107          $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 108  
 109          $this->trace->end($this->send($args));
 110      }
 111  
 112  	function send($args)
 113      {
 114          extract($args);        // $meta_id, $mail_order
 115          $meta_id = (isset($umeta_id)) ? $umeta_id : $meta_id;
 116  
 117          $meta = MP_User_meta::get_by_id($meta_id);
 118          if (!$meta)
 119          {
 120              $bm = "** WARNING *! ** Unable to read table usermeta for id : $meta_id **";
 121              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 122              $bm = " end        ! Abort";
 123              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 124              $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
 125              return false;
 126          }
 127  
 128          $mp_user_id = $meta->mp_user_id;
 129          $term_id     = str_replace('_MailPress_autoresponder_', '', $meta->meta_key);
 130          $time        = $meta->meta_value;
 131  
 132          $autoresponder = MP_Autoresponder::get($term_id);
 133          if (!isset($autoresponder->description['active']))
 134          {
 135              $bm = "** WARNING *! ** Autoresponder :  $term_id is inactive **";
 136              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 137              $bm = " end        ! Abort";
 138              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 139              $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
 140              return false;
 141          }
 142  
 143          $mp_user = MP_User::get($mp_user_id);
 144          if (!$mp_user)
 145          {
 146              $bm = "** WARNING *! ** mp_user_id : $mp_user_id is not found **";
 147              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 148              $bm = " end        ! Abort";
 149              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 150              $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
 151              return false;
 152          }
 153  
 154          $_mails = MP_Autoresponder::get_term_objects($term_id);
 155          if (!$_mails)
 156          {
 157              $bm = "** WARNING *! ** Autoresponder :  $term_id has no mails **";
 158              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 159              $bm = " end        ! Abort";
 160              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 161              $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
 162              return false;
 163          }
 164          if (!isset($_mails[$mail_order]))
 165          {
 166              $bm = "** WARNING *! ** mail_order : $mail_order NOT in mails to be processed **";
 167              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 168              $bm = " end        ! Abort";
 169              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 170              $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
 171              return false;
 172          }
 173  
 174          $_mail = $_mails[$mail_order];
 175  
 176          $draft = MP_Mail::get($_mail['mail_id']);
 177          if (!$draft)
 178          {
 179              $bm = " processing ! mail_id : " . $_mail['mail_id'] . " NOT in mail table, skip to next mail/schedule if any";
 180              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 181          }
 182  
 183          if (!MP_Mail_draft::send($_mail['mail_id'], array('toemail' => $mp_user->email, 'toname' => $mp_user->name)))
 184          {
 185              $bm = " processing ! Sending mail_id : " . $_mail['mail_id'] . " failed, skip to next mail/schedule if any";
 186              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 187          }
 188          else
 189          {
 190              $bm = " processing ! Sending mail_id : " . $_mail['mail_id'] . " successful ";
 191              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 192          }
 193  
 194          $mail_order++;
 195          if (!isset($_mails[$mail_order]))
 196          {
 197              $bm = " end        ! last mail processed";
 198              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 199              $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
 200              return true;
 201          }
 202  
 203          $schedule = $this->schedule($time, $_mails[$mail_order]['schedule']);
 204          wp_schedule_single_event($schedule, 'mp_process_autoresponder_' . $this->id, array('args' => array('meta_id' => $meta_id, 'mail_order'=> $mail_order)));
 205  
 206          $bm = " end        !  next mail to be processed : $mail_order scheduled on : " . date('Y-m-d H:i:s', $schedule);
 207          $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 208          $this->trace->log('!' . str_repeat( '-', self::bt) . '!');
 209  
 210          return true;
 211      }
 212  
 213  	function settings_form($settings)
 214      {
 215          return;
 216      }
 217  }


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