[ Index ]

MailPress 544

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

title

Body

[close]

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

   1  <?php
   2  class MP_Mail_draft
   3  {
   4  	public static function update($id, $status='draft')
   5      {
   6          global $wpdb, $mp_general;
   7          $id = (int) $id;
   8  
   9          wp_cache_delete($id, 'mp_mail');
  10          $draft = MP_Mail::get($id);
  11  
  12  // scheduled ?
  13          $scheduled = false;
  14          $draft->sent = '0000-00-00 00:00:00';
  15  
  16          wp_clear_scheduled_hook( 'mp_process_send_draft', array( $id ) );
  17  
  18          if (isset($_POST['aa'])) 
  19          {
  20              foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) 
  21              {
  22                  $$timeunit = $_POST[$timeunit];
  23                  if ( $_POST['cur_' . $timeunit] == $_POST[$timeunit] ) continue;
  24      
  25                  $scheduled = true;
  26              }
  27          // update schedule ?
  28              if ($scheduled)
  29              {
  30                  $aa = ( $aa < 1 )  ? date('Y') : $aa;
  31                  $maxd = array(31,(!($aa%4)&&($aa%100||!($aa%400)))?29:28,31,30,31,30,31,31,30,31,30,31); 
  32                  $mm = ( $mm < 1 || $mm > 12 ) ? date('n') : $mm;
  33                  $jj = ( $jj < 1 ) ? 1 : $jj;
  34                  $jj = ( $jj > $maxd[$mm-1] ) ? $maxd[$mm-1] : $jj;
  35                  $hh = ( $hh < 0 || $hh > 23 ) ? 00 : $hh;
  36                  $mn = ( $mn < 0 || $mn > 59 ) ? 00 : $mn;
  37      
  38                  $draft->sent = date('Y-m-d H:i:s', mktime($hh, $mn, 0, $mm, $jj, $aa));
  39                  $sched_time  = strtotime( get_gmt_from_date( $draft->sent ) . ' GMT');
  40  
  41                  wp_schedule_single_event( $sched_time, 'mp_process_send_draft', array( $id ) );
  42  
  43                  $old_sched = strtotime( get_gmt_from_date( date('Y-m-d H:i:s', mktime($_POST['hidden_hh'], $_POST['hidden_mn'], 0, $_POST['hidden_mm'], $_POST['hidden_jj'], $_POST['hidden_aa']))) . ' GMT');
  44              }
  45          }
  46  
  47  // process attachements
  48          if (isset($_POST['type_of_upload']))
  49          {
  50              $files = array();
  51              if (isset($_POST['Files'])) foreach ($_POST['Files'] as $k => $v) if (is_numeric($k)) $files[] = $k;
  52  
  53              $attach = (empty($files)) ? '' : join(', ', $files);
  54  
  55              $file_exits = $wpdb->get_results( $wpdb->prepare( "SELECT meta_id FROM $wpdb->mp_mailmeta WHERE mp_mail_id = %d AND meta_key = %s", $id, '_MailPress_attached_file') . ( (empty($attach)) ? ';' : " AND meta_id NOT IN ($attach);" ) );
  56              if ($file_exits) foreach($file_exits as $entry) MP_Mail_meta::delete_by_id( $entry->meta_id );
  57          }
  58  
  59  // mail_format
  60  
  61          if (isset($_POST['mail_format']))
  62          {
  63              MP_Mail_meta::delete($id, '_MailPress_format');
  64              if (!empty($_POST['mail_format'])) MP_Mail_meta::add($id, '_MailPress_format', $_POST['mail_format'], true);
  65          }
  66  
  67  // recipients
  68          if (isset($_POST['to_list']) && !empty($_POST['to_list']))
  69          {
  70              $_POST['toemail'] = $_POST['to_list'];
  71              $_POST['toname']  = '';
  72          }
  73  
  74  // content
  75          if (isset($_POST['content'])) $_POST['html'] = $_POST['content'];
  76          unset($_POST['content']);
  77  
  78  
  79          $_POST = stripslashes_deep($_POST);
  80  
  81  
  82  // from
  83          $fromemail = trim($_POST['fromemail']);
  84          $fromname  = trim($_POST['fromname']) ;
  85          if ($fromemail == $mp_general['fromemail'] && $fromname == $mp_general['fromname']) $fromemail = $fromname = '';
  86  
  87          $data = $format = $where = $where_format = array();
  88  
  89          $data['status']     = $status;                                 $format[] = '%s';
  90          $data['theme']         = (isset($_POST['Theme'])) ? $_POST['Theme'] : '';    $format[] = '%s';
  91          $data['fromemail']    = $fromemail;                                 $format[] = '%s';
  92          $data['fromname']     = $fromname ;                                 $format[] = '%s';
  93          $data['toemail']     = trim($_POST['toemail']);                     $format[] = '%s';
  94          $data['toname']     = trim($_POST['toname']) ;                     $format[] = '%s';
  95          $data['subject']     = trim($_POST['subject']);                        $format[] = '%s';
  96          $data['html']         = trim($_POST['html']);                         $format[] = '%s';
  97          $data['plaintext']     = trim($_POST['plaintext'], " \r\n");             $format[] = '%s';
  98          $data['created']     = isset($_POST['created']) ? $_POST['created'] : current_time( 'mysql' ); $format[] = '%s';
  99          $data['created_user_id']= MP_WP_User::get_id();                     $format[] = '%d';
 100          $data['sent']         = $draft->sent;                             $format[] = '%s';
 101  
 102          if ($scheduled)
 103              $data['sent_user_id']   = $data['created_user_id'];                $format[] = '%d';
 104  
 105          $where['id']         = $id;                                $where_format[] = '%d';
 106  
 107          $wpdb->update( $wpdb->mp_mails, $data, $where, $format, $where_format );
 108  
 109          return ( $scheduled && $sched_time != $old_sched );
 110      }
 111  
 112  	public static function reset_scheduled($id = NULL)
 113      {
 114          if (NULL == $id) return false;
 115          $id = (int) $id;
 116  
 117          wp_clear_scheduled_hook( 'mp_process_send_draft', array( $id ) );
 118  
 119          $data = $format = $where = $where_format = array();
 120  
 121          $data['sent']    = '0000-00-00 00:00:00';    $format[] = '%s';
 122  
 123          $where['id']     = $id;                $where_format[] = '%d';
 124  
 125          global $wpdb;
 126          $wpdb->update( $wpdb->mp_mails, $data, $where, $format, $where_format );
 127      }
 128  
 129  	public static function send($id, $args = array()) 
 130      {
 131          $defaults = array(    'ajax'        => 0,
 132          );
 133          $r = wp_parse_args( $args, $defaults );
 134          extract( $r );
 135  
 136          $id = (int) $id;
 137  
 138          self::reset_scheduled($id);
 139  
 140          $template = apply_filters('MailPress_draft_template', false, $id);
 141  
 142          $draft = MP_Mail::get($id);
 143  
 144          if ('draft' != $draft->status) return false;
 145          $mail         = new stdClass();    /* so we duplicate the draft into a new mail */
 146          $mail->id         = MP_Mail::get_id(__CLASS__ . ' ' . __METHOD__);
 147          $mail->main_id     = $id;
 148  
 149          if (!empty($draft->theme)) $mail->Theme = $draft->theme;
 150          if (!empty($template))     $mail->Template = $template;
 151  
 152          if (!empty($draft->fromemail))
 153          {
 154              $mail->fromemail= $draft->fromemail;
 155              $mail->fromname    = $draft->fromname;
 156          }
 157  
 158          if (isset($toemail) && !empty($toemail))
 159          {
 160              $mail->toemail    = $toemail;
 161              $mail->toname    = (isset($toname)) ? $toname : '';
 162          }
 163          else
 164          {
 165              $query = self::get_query_mailinglist($draft->toemail);
 166              if ($query)
 167              {
 168                  MP_Mail_meta::add($mail->id, '_mailinglist_id', $draft->toemail, true);
 169                  $draft_dest = MP_User::get_mailinglists();
 170                  MP_Mail_meta::add($mail->id, '_mailinglist_desc', $draft_dest[$draft->toemail], true);
 171  
 172                  $mail->recipients_query = $query;
 173              }
 174              else
 175              {
 176                  if     (!is_email($draft->toemail)) return 'y';
 177                  $mail->toemail    = $draft->toemail;
 178                  $mail->toname    = $draft->toname;
 179              }
 180          }
 181  
 182          $mail->subject    = $draft->subject;
 183          $mail->html        = $draft->html;
 184          $mail->plaintext    = $draft->plaintext;
 185  
 186          $mail->wp_user_id    = $draft->created_user_id;
 187  
 188          $mail->draft     = true;
 189  
 190          $count = MailPress::mail($mail);
 191  
 192          if (0 === $count)        return 'x'; // no recipient
 193          if (!$count) return 0;            // something wrong !
 194  
 195          if ($ajax)     return array($mail->id);
 196          return $count;
 197      }
 198  
 199  //// Recipients queries ////
 200  
 201  	public static function get_query_mailinglist($draft_toemail)
 202      {
 203          switch ($draft_toemail)
 204          {
 205              case '1' :
 206                         global $wpdb;
 207                  return "SELECT id, email, name, status, confkey FROM $wpdb->mp_users WHERE status = 'active';";
 208              break;
 209  /* 2 & 3 used by comments */
 210              case '4' :
 211                         global $wpdb;
 212                  return "SELECT id, email, name, status, confkey FROM $wpdb->mp_users WHERE status IN ('active', 'waiting');";
 213              break;
 214              case '5' :
 215                         global $wpdb;
 216                  return "SELECT id, email, name, status, confkey FROM $wpdb->mp_users WHERE status IN ('waiting');";
 217              break;
 218              default :
 219                  return apply_filters('MailPress_query_mailinglist', false, $draft_toemail);
 220              break;
 221          }
 222          return false;
 223      }
 224  }


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