[ Index ]

MailPress 7.1

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


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1