[ Index ]

MailPress 7.2

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

title

Body

[close]

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

   1  <?php
   2  class MP_Mail extends MP_mail_
   3  {
   4      private $_debug = null;
   5  
   6      const object_type = 'mail';
   7  
   8      const status_deleted = 'deleted';
   9  
  10      const name_required = true;
  11      const get8BitEncoding = true; // setting this to false can have an impact on perf see http://forums.devnetwork.net/viewtopic.php?f=52&t=96933
  12  
  13  	function __construct( $plug = MP_FOLDER )
  14      {
  15          if ( !isset( $this->_debug ) ) $this->_debug = ( defined( 'MP_DEBUG_LOG' ) );
  16  
  17          $this->plug = $plug;
  18  
  19          $this->theme = new MP_Themes();
  20  
  21          $this->message     = null;
  22  
  23          $this->args = new stdClass();
  24      }
  25  
  26  ////    MP_Mail send functions    ////
  27  
  28  	function send( $args )
  29      {
  30          if ( is_numeric( $args ) )
  31          {
  32              $this->args = MP_Mail::get( $args );
  33          }
  34          else
  35          {
  36              $this->args = $args ;
  37          }
  38          return $this->end( $this->start() );
  39      }
  40  
  41  	function start()
  42      {
  43          MP_::no_abort_limit();
  44  
  45          $this->row  = new stdClass();
  46          $this->mail = new stdClass();
  47  
  48          global $mp_general;
  49          $mp_general = get_option( MailPress::option_name_general );
  50  
  51  ////  Log it  ////
  52  
  53          $this->trace = new MP_Log( 'mp_mail', array( 'force' => ( isset( $this->args->forcelog ) ) ) );
  54  
  55          if ( !$this->args )    
  56          {
  57              $this->trace->log( 'MAILPRESS [ERROR] - Sorry invalid arguments in MP_Mail::send' );
  58              return false;
  59          }
  60  
  61  ////  Build it  ////
  62  
  63          $this->row->id      = $this->args->id       = $this->args->id       ?? MP_Mail::get_id( 'MP_Mail::start' );
  64          $this->row->list_id = $this->args->_list_id = $this->args->_list_id ?? 'MailPress.0';
  65  
  66      //# charset #//
  67          $this->row->charset = $this->args->charset ?? get_option( 'blog_charset' );
  68  
  69      //# fromemail & fromname #//
  70          $this->row->fromemail    = ( empty( $this->args->fromemail ) ) ? $mp_general['fromemail'] : $this->args->fromemail;
  71          $this->row->fromname     = ( empty( $this->args->fromname ) )  ? $mp_general['fromname']  : $this->args->fromname;
  72          $this->row->fromname     = ( empty( $this->row->fromname ) )   ? self::display_name( $mp_general['fromemail'] ) : $this->row->fromname;
  73  
  74      //# recipients & replacements #//
  75          self::get_mail_replacements();
  76  
  77          $this->mail->recipients_count = $this->manage_recipients();
  78          if ( !$this->mail->recipients_count )
  79          {
  80              $this->trace->log( ( 0 === $this->mail->recipients_count ) ? 'MAILPRESS [ERROR] - No recipient' : 'MAILPRESS [ERROR] - problems with recipients & replacements' );
  81              return $this->mail->recipients_count;
  82          }
  83  
  84      //# subject #//
  85          $this->row->subject     = $this->args->subject ?? false;
  86          if ( isset( $this->args->Template ) ) 
  87          {
  88              $theme = $this->args->Theme ?? $this->theme->themes[$this->theme->current_theme]['Stylesheet'];
  89              if ( !$pt = $this->theme->get_page_templates( $theme ) ) $pt = $this->theme->get_page_templates( $theme, true );
  90              if ( isset( $pt[$this->args->Template]['subject'] ) ) $this->row->subject = $pt[$this->args->Template]['subject'];
  91          }
  92          $this->row->subject     = ( $this->row->subject ) ? trim( $this->html_entity_decode( $this->do_eval( $this->row->subject ) ) ) : '';
  93  
  94      //# html #//
  95          $this->row->html        = $this->build_mail_content( 'html', 'MailPress_the_content' );
  96  
  97      //# plaintext #//
  98          $this->row->plaintext     = $this->build_mail_content( 'plaintext' );
  99  
 100      //# attachments #//
 101          $this->mail->attachments = false;
 102          if ( isset( $this->args->main_id ) )
 103          {
 104              $metas = MP_Mail_meta::has( $this->args->main_id, '_MailPress_attached_file' );
 105              if ( $metas )
 106              {
 107                  foreach( $metas as $meta )
 108                  {
 109                      $meta_value = unserialize( $meta['meta_value'] );
 110                      if ( !is_file( $meta_value['file_fullpath'] ) ) continue;
 111                      $this->mail->attachments = true;
 112                      MP_Mail_meta::add( $this->row->id, '_MailPress_attached_file', $meta_value );
 113                  }
 114              }
 115          }
 116  
 117          unset( $this->theme );
 118  
 119      //# mail empty ? #//
 120          if ( !$this->row->subject && !$this->row->plaintext && !$this->row->html && !$this->mail->attachments )
 121          {
 122              $this->trace->log( __( 'MAILPRESS [WARNING] - Mail is empty', 'MailPress' ) );
 123              return false;
 124          }
 125  
 126          $this->row->theme    = $this->mail->theme;
 127          $this->row->themedir    = $this->mail->themedir;
 128          $this->row->template     = $this->mail->template;
 129  
 130      //# no tracking on unknown recipient ! #//
 131          if ( !isset( $this->mail->external_recipient ) )
 132          {
 133              $this->row = apply_filters( 'MailPress_mail', $this->row );
 134              $this->replace_mail_urls();
 135          }
 136  
 137      //# only one recipient #//
 138          if ( 1 == $this->mail->recipients_count )
 139          {
 140              $toname = '';
 141              if ( isset( $this->row->recipients[0] ) ) $toemail = $this->row->recipients[0]; else foreach( $this->row->recipients as $toemail => $toname ) {};
 142              $this->row->toemail = $toemail;
 143              $this->row->toname  = $toname;
 144  
 145              $this->row->replacements[$toemail] = array_merge( $this->row->replacements[$toemail] ?? array(), $this->mail->replacements );
 146  
 147              foreach( $this->row->replacements[$toemail] as $k => $v ) 
 148              {
 149                  $this->row->subject     = str_replace( $k, $v, $this->row->subject, $cs );
 150                  $this->row->plaintext    = str_replace( $k, $v, $this->row->plaintext, $cp );
 151                  $this->row->html     = str_replace( $k, $v, $this->row->html, $ch );
 152              }
 153  
 154              if ( isset( $this->row->replacements[$toemail]['{{_user_id}}'] ) ) $this->row->mp_user_id = $this->row->replacements[$toemail]['{{_user_id}}'];
 155              if ( isset( $this->row->replacements[$toemail]['{{_confkey}}'] ) ) $this->row->confkey    = $this->row->replacements[$toemail]['{{_confkey}}'];
 156  
 157              MP_Mail_meta::delete_by_id( $this->mail->mmid );
 158              unset( $this->row->replacements, $this->row->recipients, $this->mail->replacements, $this->mail->mmid );
 159          }
 160  
 161  /*trace*/    $x  = " \n\n ------------------- start of mail -------------------  ";
 162  /*trace*/    $x .= " \n From : " . $this->row->fromname  . " <" . $this->row->fromemail . "> ";
 163  /*trace*/    if ( isset( $this->row->toemail ) )
 164  /*trace*/        if ( !empty( $this->row->toname ) )
 165  /*trace*/    $x .= " \n To   : " . $this->row->toname . " <" . $this->row->toemail . "> ";
 166  /*trace*/        else
 167  /*trace*/    $x .= " \n To   : " . $this->row->toemail;
 168  /*trace*/    $x .= " \n Subject : " . $this->row->subject;
 169  /*trace*/    if ( $this->row->plaintext )     $x .= " \n   ------------------- plaintext -------------------  \n " . $this->row->plaintext;
 170  /*trace*/    if ( $this->row->html )         $x .= " \n\n ------------------- text/html -------------------  \n " . $this->row->html;
 171  /*trace*/    $x .= " \n\n ------------------ end of mail ------------------  \n\n";
 172  /*trace*/    $this->trace->log( $x, $this->trace->levels[512] );
 173  
 174          $this->mail->swift_batchSend         = ( 1 < $this->mail->recipients_count );
 175          $this->mail->mailpress_batch_send    = ( $this->mail->swift_batchSend ) ? class_exists( 'MailPress_batch_send' ) : false;
 176          $this->mail->mailpress_batch_spool_send = ( $this->mail->swift_batchSend ) ? class_exists( 'MailPress_batch_spool_send' ) : false;
 177  
 178  ////  Send it  ////
 179  
 180      //# no mail ? #//
 181          if ( isset( $this->args->nomail ) )
 182          {
 183              $this->trace->log( 'MAILPRESS [NOTICE] - :::: Mail not sent as required ::::' );
 184          }
 185          elseif ( $this->mail->mailpress_batch_send )
 186          {
 187              $this->trace->log( 'MAILPRESS [NOTICE] - :::: Mail batch processing as required ::::' );
 188              do_action( 'MailPress_schedule_batch_send' );
 189          }
 190          else
 191          {
 192              if ( !$this->swift_processing() )
 193              {
 194                  $this->trace->log( '' );
 195                  $this->trace->log( 'MAILPRESS [ERROR] - *** Mail could not be sent ***' );
 196                  $this->trace->log( '' );
 197                  unset( $this->message, $this->swift ); 
 198                  return false;
 199              }
 200          }
 201  
 202          unset( $this->message, $this->swift ); 
 203  
 204  ////  Archive it  ////
 205  
 206          if ( isset( $this->args->noarchive ) )
 207          {
 208              $this->trace->log( 'MAILPRESS [NOTICE] - :::: Mail not saved as required ::::' );
 209              MP_Mail::delete( $this->row->id );
 210          }
 211          else
 212          { 
 213              global $wpdb;
 214  
 215              if ( !isset( $this->args->nostats ) ) new MP_Stat( 't', isset( $this->args->Template ) ? $this->args->Template : '', $this->mail->recipients_count );
 216  
 217              $now        = current_time( 'mysql' );
 218              $user_id     = ( empty( $this->args->wp_user_id ) ) ?  MP_WP_User::get_id() : $this->args->wp_user_id;
 219  
 220              if ( $this->mail->swift_batchSend ) 
 221              {
 222                  foreach ( $this->row->replacements as $email => $r )
 223                  {
 224                      if ( isset( $this->row->recipients[$email] ) )
 225                          $this->row->toemail[$email] = $this->row->replacements[$email];
 226                      unset( $this->row->recipients[$email], $this->row->replacements[$email] );
 227                  }
 228                  foreach ( $this->row->recipients as $k => $email )
 229                  {
 230                      $this->row->toemail[$email] = $this->row->replacements[$email];
 231                      unset( $this->row->recipients[$k], $this->row->replacements[$email] );
 232                  }
 233                  unset( $this->row->recipients, $this->row->replacements );
 234                  $this->row->toemail = serialize( $this->row->toemail );
 235              }
 236  
 237      //# status #//
 238              $this->row->status = ( $this->mail->swift_batchSend ) ? ( apply_filters( 'MailPress_status_mail', 'sent' ) ) : 'sent';
 239              if ( !isset( $this->row->toname ) ) $this->row->toname = '';
 240  
 241              wp_cache_delete( $this->row->id, 'mp_mail' );
 242  
 243              $data = $format = $where = $where_format = array();
 244  
 245              $data['status']         = $this->row->status;     $format[] = '%s';
 246              $data['theme']         = $this->row->theme;     $format[] = '%s';
 247              $data['themedir']     = $this->row->themedir; $format[] = '%s';
 248              $data['template']     = $this->row->template; $format[] = '%s';
 249              $data['fromemail']     = $this->row->fromemail;$format[] = '%s';
 250              $data['fromname']     = $this->row->fromname;    $format[] = '%s';
 251              $data['toemail']         = $this->row->toemail;     $format[] = '%s';
 252              $data['toname']         = $this->row->toname;     $format[] = '%s';
 253              $data['charset']         = $this->row->charset;     $format[] = '%s';
 254              $data['subject']         = $this->row->subject;    $format[] = '%s';
 255              $data['html']         = $this->row->html;     $format[] = '%s';
 256              $data['plaintext']     = $this->row->plaintext;$format[] = '%s';
 257              $data['created']         = $now;             $format[] = '%s';
 258              $data['created_user_id']= $user_id;         $format[] = '%d';
 259              $data['sent']         = $now;             $format[] = '%s';
 260              $data['sent_user_id']     = $user_id;         $format[] = '%d';
 261  
 262              $where['id']         = ( int ) $this->row->id;        $where_format[] = '%d';
 263  
 264              if ( !$wpdb->update( $wpdb->mp_mails, $data, $where, $format, $where_format ) )
 265              {
 266                  $this->trace->log( sprintf( 'MAILPRESS [ERROR] - *** Database error, Mail not saved : %1$s '."\n".'%2$s', $wpdb->last_error, $wpdb->last_query ) );
 267                  return false;
 268              }
 269              $this->trace->log( 'MAILPRESS [NOTICE] - :::: MAIL SAVED ::::' );
 270          }
 271  
 272          return $this->mail->recipients_count;
 273      }
 274  
 275  	function end( $rc )
 276      {
 277          $this->trace->end( $rc );
 278          return $rc;
 279      }
 280  
 281  
 282  ////  Build it  ////
 283  
 284  
 285   //# mail replacements #//
 286  
 287  	function get_mail_replacements()
 288      {
 289          $mail_replacements = $this->convert_all( $this->args );
 290          $unsets = array( '{{subject}}', '{{content}}', '{{html}}', '{{plaintext}}', '{{id}}', '{{main_id}}', '{{recipients_query}}', '{{draft}}', '{{advanced}}', );
 291          foreach ( $unsets as $unset ) unset( $mail_replacements[$unset] );
 292  
 293          $mail_main_id = $this->args->main_id ?? 0;
 294          $_mail_id = ( $mail_main_id ) ? $mail_main_id : $this->row->id;
 295          $m = MP_Mail_meta::get_replacements( $_mail_id );
 296          if ( !is_array( $m ) ) $m = array();
 297  
 298          $this->mail->replacements = array_merge( $m, $mail_replacements );
 299  
 300          $this->mail->mmid = MP_Mail_meta::add( $this->row->id, '_MailPress_replacements', $this->mail->replacements );
 301      }
 302  
 303  	function convert_all( $x = '', $sepb = '{{', $sepa = '}}', $before = '', $first = 0, $array = array() )
 304      {
 305          if ( is_object( $x ) ) $x = get_object_vars( $x );
 306          if ( empty( $x ) ) return array();
 307          foreach( $x as $key => $value )
 308          {
 309              if ( !( is_object( $value ) || is_array( $value ) ) )
 310              {
 311                  $x = ( 0 == $first ) ? $key : $before . '[' . $key . ']'; 
 312                  $array[$sepb . $x . $sepa] = $this->html_entity_decode( $value );
 313              }
 314              else 
 315              {
 316                  $abefore= ( !$first ) ? $key : $before . '[' . $key . ']'; 
 317                  $array = array_merge( $array, $this->convert_all( $value , $sepb , $sepa , $abefore , $first + 1 ) );
 318              }
 319          }
 320          return $array;
 321      }
 322  
 323   //# recipients & replacements #//
 324  
 325  	function manage_recipients()
 326      {
 327          if ( isset( $this->args->replacements ) )
 328          {
 329              if ( isset( $this->args->recipients ) )
 330              {
 331                  $this->row->replacements = $this->args->replacements;
 332                  $this->row->recipients   = $this->args->recipients;
 333                  return count( $this->row->recipients );
 334              }
 335              else
 336              {
 337                  $this->get_old_recipients();
 338                  return count( $this->row->recipients );
 339              }
 340          }
 341  
 342          $is_email = MailPress::is_email( $this->args->toemail ?? false );
 343  
 344          if ( !isset( $this->args->recipients_query ) )
 345          {
 346              if ( $is_email )
 347              {
 348                  $mp_user_id = MP_User::get_id_by_email( $this->args->toemail );
 349                  if ( $mp_user_id )
 350                  {
 351                      global $wpdb;
 352                      $this->args->recipients_query = "SELECT DISTINCT id, email, name, status, confkey FROM $wpdb->mp_users WHERE id = $mp_user_id ;";
 353                  }
 354              }
 355          }
 356  
 357          if ( isset( $this->args->recipients_query ) )
 358          {
 359              $this->get_recipients( $this->args->recipients_query );
 360  
 361              $this->args->viewhtml = MP_User::get_view_url( '{{_confkey}}', $this->row->id );
 362              if ( isset( $this->args->subscribe ) )     $this->args->subscribe   = MP_User::get_subscribe_url( '{{_confkey}}' );
 363              else                        $this->args->unsubscribe = MP_User::get_unsubscribe_url( '{{_confkey}}' );
 364  
 365              return count( $this->row->recipients );
 366          }
 367  
 368          if ( $is_email )
 369          {
 370              $this->mail->external_recipient = true;
 371              $this->get_external_recipient();
 372              return 1;
 373          }
 374  
 375          return false;
 376      }
 377  
 378  	function get_recipients( $query )
 379      {
 380          global $wpdb;
 381          $mp_users = $wpdb->get_results( $query );
 382  
 383          $this->row->recipients = array();
 384          if ( $mp_users )
 385          {
 386              foreach ( $mp_users as $mp_user )
 387              {
 388                  if ( !MailPress::is_email( $mp_user->email ) )
 389                  {
 390                      $this->trace->log( 'MAILPRESS [WARNING] - :::: this recipient ' . $mp_user->email . ' discarded due to applicable RFC ::::'  );
 391                      continue;
 392                  }
 393                  $this->row->replacements[$mp_user->email] = $this->get_user_replacements( $mp_user );
 394  
 395                  if ( isset( $this->args->toname ) ) if ( count( $mp_users ) == 1 ) $mp_user->name = $this->args->toname;
 396                  if ( empty( $mp_user->name ) )
 397                      if ( self::name_required )
 398                          $this->row->recipients[$mp_user->email]     = trim( str_replace( '.', ' ', substr( $mp_user->email, 0, strpos( $mp_user->email, '@' ) ) ) );
 399                      else
 400                          $this->row->recipients[] = $mp_user->email;
 401                  else
 402                      $this->row->recipients[$mp_user->email] = $mp_user->name;
 403              }
 404          }
 405      }
 406  
 407  	function get_external_recipient()
 408      {
 409          $this->trace->log( 'MAILPRESS [NOTICE] - :::: external recipient ::::' );
 410          $this->row->replacements[$this->args->toemail] = array();
 411  
 412          if ( isset( $this->args->toname ) )
 413              if ( !empty( $this->args->toname ) )
 414                  $this->row->recipients[$this->args->toemail] = $this->args->toname;
 415              else
 416                  if ( self::name_required )
 417                      $this->row->recipients[$this->args->toemail]     = trim( str_replace( '.', ' ', substr( $this->args->toemail, 0, strpos( $this->args->toemail, '@' ) ) ) );
 418                  else
 419                      $this->row->recipients[] = $this->args->toemail;
 420          else
 421              $this->row->recipients[] = $this->args->toemail;
 422      }
 423  
 424  	function get_old_recipients()
 425      {
 426          $this->row->replacements = $this->args->replacements;
 427  
 428          foreach( $this->row->replacements as $email => $v )
 429          {
 430              if ( isset( $v['{{toname}}'] ) && !empty( $v['{{toname}}'] ) )
 431                  $this->row->recipients[$email] = $v['{{toname}}'];
 432              else
 433              {
 434                  if ( !self::name_required )
 435                      $this->row->recipients[] = $email;
 436                  else
 437                  {
 438                      $name = trim( str_replace( '.', ' ', substr( $email, 0, strpos( $email, '@' ) ) ) );
 439                      $this->row->recipients[$email] = $this->row->replacements[$email]['{{toname}}'] =  $name;
 440                  }
 441              }
 442          }
 443      }
 444  
 445  	function get_user_replacements( $mp_user )
 446      {
 447          if ( !$mp_user ) return array();
 448  
 449          if ( is_numeric( $mp_user ) )
 450          {
 451              $mp_user = MP_User::get( $mp_user );
 452              if ( !$mp_user ) return array();
 453          }
 454  
 455          $replacements = MP_User_meta::get_replacements( $mp_user->id );
 456  
 457          $replacements ['{{toemail}}']    = $mp_user->email;
 458          $replacements ['{{toname}}']    = $mp_user->name;
 459          $replacements ['{{_user_id}}']= $mp_user->id;
 460  
 461      //# always last #//
 462          $replacements ['{{_confkey}}']= $mp_user->confkey;
 463          return $replacements;
 464      }
 465  
 466  	function replace_mail_urls()
 467      {
 468          $r = array();
 469          if ( isset( $this->args->viewhtml ) )     $r['{{viewhtml}}']     = $this->args->viewhtml;
 470          if ( isset( $this->args->subscribe ) )    $r['{{subscribe}}']     = $this->args->subscribe;
 471          if ( isset( $this->args->unsubscribe ) )$r['{{unsubscribe}}']     = $this->args->unsubscribe;
 472  
 473          foreach( $r as $k => $v )
 474          {
 475              $this->row->subject     = str_replace( $k, $v, $this->row->subject, $cs );
 476              $this->row->plaintext     = str_replace( $k, $v, $this->row->plaintext, $cp );
 477              $this->row->html         = str_replace( $k, $v, $this->row->html, $ch );
 478          }
 479      }
 480  
 481   //# plaintext, html #//
 482  
 483  	function build_mail_content( $type, $filter=false )
 484      {
 485          $this->build = new stdClass();
 486          $this->build->plaintext = ( 'plaintext' == $type );
 487          $this->build->filter = $filter;
 488  
 489          if ( !isset( $this->mail ) ) $this->mail = new stdClass();
 490          $this->mail->theme = $this->mail->themedir = $this->mail->template = null;
 491  
 492      //# find the theme and themedir #//
 493  
 494          $this->mail->theme     = $this->theme->themes[$this->theme->current_theme]['Stylesheet'];
 495          $this->mail->themedir    = $this->theme->themes[$this->theme->current_theme]['Stylesheet Dir'];
 496          if ( isset( $this->args->Theme ) && !empty( $this->args->Theme ) )
 497          {
 498              $x = $this->theme->get_theme_by_stylesheet( $this->args->Theme );
 499              if ( !empty( $x ) )
 500              {
 501                  $this->theme->current_theme     = $x['Name'];
 502                  $this->mail->theme        = $this->args->Theme;
 503                  $this->mail->themedir        = $x['Stylesheet Dir'];
 504              }
 505              elseif ( isset( $this->trace ) ) $this->trace->log( sprintf( 'MAILPRESS [WARNING] - Missing theme : >> %1$s << Type : %2$s ', $this->args->Theme, $type ), $this->trace->levels[512] );
 506          }
 507  
 508      //# find the template #//
 509  
 510          $template_name = '';
 511          $pt = $this->theme->get_page_templates( $this->mail->theme, $this->build->plaintext );
 512  
 513          $this->build->stylesheetpath  = ABSPATH . $this->theme->themes[$this->theme->current_theme] [ ( ( $this->build->plaintext ) ? 'Plaintext Stylesheet Dir' : 'Stylesheet Dir' ) ] . '/' ;
 514          $this->build->templatepath    = ABSPATH . $this->theme->themes[$this->theme->current_theme] [ ( ( $this->build->plaintext ) ? 'Plaintext Template Dir'   : 'Template Dir' ) ]   . '/' ;
 515          if ( $this->build->plaintext )
 516                      if ( $t = $this->theme->get_theme_by_stylesheet( 'plaintext' ) ) $this->build->plaintextpath = ABSPATH . $t['Stylesheet Dir'] . '/';
 517                      elseif ( isset( $this->trace ) ) $this->trace->log( 'MAILPRESS [ERROR] - Missing theme : plaintext theme has been deleted.', $this->trace->levels[512] );
 518          $this->build->dir         = $this->build->stylesheetpath;
 519  
 520          if ( isset( $this->args->Template ) && isset( $pt[$this->args->Template] ) ) $template_name = $this->locate_template( $pt[$this->args->Template]['file'] );
 521          if ( empty( $template_name ) ) $template_name = $this->locate_template(); //# call default.php #//
 522          else $this->mail->template = $this->args->Template;
 523  
 524      //# build the content #//
 525  
 526          $content = '';
 527      //# call functions.php #//
 528          foreach( $this->get_template_paths( 'functions.php', true ) as $file )
 529              if ( is_file( $file ) ) $content .= "<?php require_once ( '{$file}' ); ?>";
 530      //# call any start action after functions.php #//
 531          $content .= "<?php do_action( 'MailPress_build_mail_content_start', '$type' );?>";
 532      //# call template or default #//
 533          $content .= ( is_file( $template_name ) ) ? '<?php $this->load_template( \'' . $template_name . '\' ); ?>' : '<?php $this->get_header(); $this->the_content(); $this->get_footer(); ?>';
 534          $content .= "<?php do_action( 'MailPress_build_mail_content_end', '$type' );?>";
 535  
 536      //# build the mail #//
 537          $x = $this->do_eval( $content );
 538  
 539          unset( $this->build );
 540          return ( '<br />' == trim( $x ) ) ? '' : trim( $x );
 541      }
 542  
 543  	function do_eval( $x )
 544      {
 545          $x = 'global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID; ?>' . "\n $x";
 546          ob_start();
 547              echo( eval( $x ) );
 548              $r = ob_get_contents();
 549          ob_end_clean();
 550          return $r;
 551      }
 552  
 553  
 554  ////  Send it  ////
 555  
 556  
 557  	function swift_processing()
 558      {
 559          new MP_Swift();
 560  
 561      //# Swift message #//
 562          try 
 563          {
 564              $this->build_swift_message();
 565  
 566  if ( $this->_debug ) $eml = $this->message->toString(); file_put_contents( MP_UPL_ABSPATH . $this->row->id . '.eml', $eml );
 567          }
 568          catch ( Exception $e ) 
 569          {
 570              if ( !( $e instanceof Swift_SwiftException ) ) throw $e;
 571  
 572              $this->trace->log( 'SWIFTMAILER [ERROR] - ' . "There was an unexpected problem building the mail:\n\n" . $e->getMessage() . "\n\n" );    
 573  
 574  if ( $this->_debug ) $eml = $this->message->toString(); file_put_contents( MP_UPL_ABSPATH . $this->row->id . '.eml', $eml );
 575  
 576              return false;
 577          }
 578  
 579      //# Swift connection #//
 580  
 581          if ( isset( $this->mail->mailpress_batch_spool_send ) && $this->mail->mailpress_batch_spool_send )
 582                                                              new MP_Swift_Connection_spool();
 583          if ( !has_filter( 'MailPress_Swift_Connection_type' ) )            new MP_Swift_Connection_smtp();
 584  
 585          try 
 586          {
 587              $Swift_Connection_type = apply_filters( 'MailPress_Swift_Connection_type', null );
 588  
 589              $conn = apply_filters( "MailPress_Swift_Connection_{$Swift_Connection_type}" , $this->row->id, $this->trace );
 590  
 591              $this->swift = new Swift_Mailer( $conn );
 592          }
 593          catch ( Exception $e ) 
 594          {
 595              if ( !( $e instanceof Swift_SwiftException ) ) throw $e;
 596  
 597              $this->trace->log( 'SWIFTMAILER [ERROR] - ' . "There was a problem connecting with $Swift_Connection_type :\n\n" . $e->getMessage() . "\n\n" );    
 598              $this->mysql_connect( __CLASS__ . ' connect error :  ' . $Swift_Connection_type );
 599              return false;
 600          } 
 601  
 602      //# Swift sending ... #//
 603          try 
 604          {
 605              $this->swift = apply_filters( 'MailPress_swift_registerPlugin', $this->swift );
 606  
 607  //# Swiftmailer logging #//
 608  if ( $this->_debug )
 609  {
 610      $logger = new Swift_Plugins_Loggers_ArrayLogger();
 611      $this->swift->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));
 612  }
 613  
 614              if ( $this->mail->mailpress_batch_send )
 615                  return apply_filters( 'MailPress_swift_send', $this );
 616  
 617              $this->mysql_disconnect( __CLASS__ );
 618  
 619          //# swift batchSend #//
 620              if ( $this->mail->swift_batchSend )
 621              {
 622                  $this->swift->registerPlugin( new Swift_Plugins_DecoratorPlugin( $this->row->replacements ) );
 623                  if ( !$this->swift_batchSend() )
 624                  {
 625                      $this->mysql_connect( __CLASS__ . ' batchSend' );
 626  
 627  //# Swiftmailer logging #//
 628  if ( $this->_debug ) $this->trace->log( 'SWIFTMAILER [LOGGING] - ' . $logger->dump() . "\n\n" );
 629  
 630                      return false;
 631                  }
 632              }
 633  
 634          //# swift send #//
 635              else
 636              {
 637                  if ( !$this->swift->send( $this->message ) ) 
 638                  {
 639                      $this->mysql_connect( __CLASS__ . ' send' );
 640  
 641  //# Swiftmailer logging #//
 642  if ( $this->_debug ) $this->trace->log( 'SWIFTMAILER [LOGGING] - ' . $logger->dump() . "\n\n" );
 643  
 644                      return false;
 645                  }
 646              }
 647  
 648  
 649  
 650          }
 651          catch ( Exception $e ) 
 652          {
 653              if ( !( $e instanceof Swift_SwiftException ) ) throw $e;
 654  
 655              $this->trace->log( 'SWIFTMAILER [ERROR] - ' . "There was a problem sending with $Swift_Connection_type :\n\n" . $e->getMessage() . "\n\n" );    
 656              $this->mysql_connect( __CLASS__ . ' sending error : ' . $Swift_Connection_type );
 657              return false;
 658          }
 659  
 660          $this->mysql_connect( __CLASS__ );
 661                          
 662          if ( $this->mail->mailpress_batch_spool_send ) do_action( 'MailPress_schedule_batch_spool_send' );
 663  
 664          return true;
 665      }
 666  
 667  	function swift_batchSend( &$failedRecipients = null )
 668      {
 669          $this->message->setCc( array() );
 670          $this->message->setBcc( array() );
 671  
 672          $sent = 0;
 673  
 674          foreach ( $this->message->getTo() as $address => $name )
 675          {
 676              $this->message->setTo( array( $address => $name ) );
 677              $sent += $this->swift->send( $this->message, $failedRecipients );
 678          }
 679  
 680          return $sent;
 681      }
 682  
 683  	function build_swift_message()
 684      {
 685  
 686      //# charset #//
 687          Swift_Preferences::getInstance()->setCharset( $this->row->charset );
 688  
 689      //# message #//
 690          $this->message   = new Swift_Message();
 691  
 692          $this->message->setEncoder( Swift_DependencyContainer::getInstance()->lookup('mime.qpcontentencoder') );
 693  
 694      //# from #//
 695          $this->message->setFrom( array( $this->row->fromemail => $this->row->fromname ) );
 696  
 697      //# to & replacements #//
 698          if ( !$this->mail->swift_batchSend ) 
 699          {
 700              try
 701              {
 702                  $this->message->addTo( $this->row->toemail, $this->row->toname );
 703              }
 704              catch ( Exception $e ) 
 705              {
 706                  if ( !( $e instanceof Swift_SwiftException ) ) throw $e;
 707  
 708                  $this->trace->log( 'SWIFTMAILER [ERROR] - ' . "Recipient do not comply RFC rules ( discarded ) :\n\n" . $e->getMessage() . "\n\n" );
 709                  return false;
 710              }
 711          }
 712          else
 713          {
 714              foreach( $this->row->recipients as $toemail => $toname )
 715              {
 716                  try
 717                  {
 718                      $this->message->addTo( $toemail, $toname );
 719                  }
 720                  catch ( Exception $e ) 
 721                  {
 722                      if ( !( $e instanceof Swift_SwiftException ) ) throw $e;
 723  
 724                      $this->trace->log( 'SWIFTMAILER [WARNING] - ' . "Recipient do not comply RFC rules ( discarded ) :\n\n" . $e->getMessage() . "\n\n" );    
 725                      unset( $this->row->replacements[$toemail] );
 726                  }
 727              }
 728              foreach( $this->row->replacements as $k => $v ) $this->row->replacements[$k] = array_merge( $this->mail->replacements, $this->row->replacements[$k] );
 729          }
 730  
 731      //# subject #//
 732          $this->message->setSubject( $this->row->subject );
 733  
 734      //# html #//
 735          if ( $this->row->html )
 736          {
 737              $this->message->setBody( $this->process_img( $this->row->html, $this->row->themedir ), 'text/html' );
 738          }
 739  
 740      //# plaintext #//
 741          if ( $this->row->plaintext )
 742          {
 743              if ( $this->row->html )
 744                  $this->message->addPart( $this->row->plaintext );
 745              else
 746                  $this->message->setBody( $this->row->plaintext );
 747          }
 748  
 749      //# attachments #//
 750          $metas = MP_Mail_meta::has( $this->row->id, '_MailPress_attached_file' );
 751          if ( $metas )
 752          {
 753              foreach( $metas as $meta )
 754              {
 755                  $meta_value = unserialize( $meta['meta_value'] );
 756                  if ( !is_file( $meta_value['file_fullpath'] ) ) continue;
 757                  $this->message->attach( Swift_Attachment::fromPath( $meta_value['file_fullpath'], $meta_value['mime_type'] )->setFilename( $meta_value['name'] ) );
 758              }
 759          }
 760  
 761      //# headers from wp_mail ? #//
 762          if ( isset( $this->mail->recipients_count ) && ( 1 == $this->mail->recipients_count ) )
 763          {
 764              $meta = MP_Mail_meta::get( $this->row->id, '_MailPress_accepted_headers' );
 765              if ( $meta )
 766              {
 767                  $hfunc = array( 'cc' => 'addCc', 'bcc' => 'addBcc', 'reply_to' => 'addReplyTo' );
 768                  foreach( $meta as $k => $v )
 769                  {
 770                      if ( is_array( $v ) )
 771                      {
 772                          switch( $k )
 773                          {
 774                              case 'X' :
 775                                  $_headers = $this->message->getHeaders();
 776                                  foreach( $v as $vv ) { foreach( $vv as $name => $content ) { $_headers->addTextHeader( $name, $content ); } }
 777                              break;
 778                              case 'return_path' :
 779                                  $this->message->setReturnPath( $v[0] );
 780                              break;
 781                              case 'cc' :
 782                              case 'bcc' :
 783                              case 'reply_to' :
 784                                  if ( !isset( $hfunc[$k] ) ) continue;
 785                                  $func = $hfunc[$k];
 786                                  foreach( $v as $toemail => $toname )
 787                                  {
 788                                      try
 789                                      {
 790                                          $this->message->$func( $toemail, $toname );
 791                                      }
 792                                      catch ( Exception $e ) 
 793                                      {
 794                                          if ( !( $e instanceof Swift_SwiftException ) ) throw $e;
 795                                          $this->trace->log( 'SWIFTMAILER [WARNING] - ' . "{$hfunc[$k]} - Recipient do not comply RFC rules ( discarded ) :\n\n" . $e->getMessage() . "\n\n" );    
 796                                      }
 797                                  }
 798                              break;
 799                          }
 800                      }
 801                  }
 802              }
 803          }
 804  
 805      //# filter headers #//
 806          $this->message    = apply_filters( 'MailPress_swift_message_headers', $this->message, $this->row );
 807  
 808      //# signer #//
 809          if ( has_filter( 'MailPress_Swift_Signer_type' ) )
 810          {
 811              $Swift_Signer_type = apply_filters( 'MailPress_Swift_Signer_type', false );
 812  
 813              if ( $Swift_Signer_type )
 814              {
 815                  $signer = apply_filters( "MailPress_Swift_Signer_{$Swift_Signer_type}", false );
 816                  if ( $signer )
 817                  {
 818                      $this->message->attachSigner( $signer );
 819                      $this->trace->log( '!            ! [notice] MailPress - Signer found : ' . $Swift_Signer_type, $this->trace->levels[512] );
 820                  }
 821              }
 822          }
 823  
 824      //# MailPress_swift_message_built #//
 825          do_action( 'MailPress_swift_message_built', $this->message, $this->row );
 826      }
 827  
 828  	function process_img( $html, $path, $dest = 'mail' )
 829      {
 830          $x        = $matches = $imgtags = array();
 831          $masks     = array ( '', $path . '/images/', $path . '/' );
 832  
 833          $keepurl    = apply_filters( 'MailPress_img_mail_keepurl', false );
 834  
 835          $siteurl     = site_url() . '/';
 836          $fprefix     = ( 'mail' == $dest ) ? ABSPATH : $siteurl;
 837  
 838          $out = preg_match_all( '/<img[^>]*>/Ui', $html, $imgtags, PREG_SET_ORDER ); // all img tag
 839  
 840          if ( empty( $imgtags ) ) return $html;
 841  
 842          foreach ( $imgtags as $imgtag )
 843          {
 844              $out = preg_match_all( '/src=[\'"]([^\'"]+)[\'"]/Ui', $imgtag[0], $src, PREG_SET_ORDER ); // for src attribute
 845              $matches[] = array( 0 => $imgtag[0], 1 => $src[0][1] );
 846          }
 847  
 848          $imgs = array();
 849          foreach ( $matches as $match )
 850          {
 851              $f = $u = false;
 852  
 853              if ( !MP_::is_image( $match[1] ) ) continue;
 854  
 855              if ( $keepurl && stristr( $match[1], $siteurl ) )
 856              {
 857                  $imgs[$match[1]] = $match[1];
 858                  continue;
 859              }
 860              elseif ( stristr( $match[1], $siteurl ) ) $u = true;
 861              elseif ( ( stristr( $match[1], 'http://' ) ) || ( stristr( $match[1], 'https://' ) ) )
 862              {
 863                  $imgs[$match[1]] = $match[1];
 864                  continue;
 865              }
 866  
 867              foreach ( $masks as $mask )
 868              {
 869                  if ( $u )     $file = str_ireplace( $siteurl, '', $match[1] );
 870                  else        $file = $mask . $match[1];
 871  
 872                  if ( is_file( ABSPATH . $file ) ) 
 873                  {
 874                      $f = true;
 875                      $x[$match[1]] = $fprefix . $file;        // we can have the src/url image in different img tags ... so we embed it one time only
 876                      if ( isset( $this->trace ) ) if ( 'mail' == $dest ) $this->trace->log( '!            ! [notice] MailPress - Image found : ' . $file, $this->trace->levels[512] );
 877                  }
 878                  if ( $f ) break;
 879                  
 880              }
 881          }
 882  
 883          if ( 'mail' == $dest )
 884          {
 885              foreach ( $x as $key => $file )
 886              {
 887                  try 
 888                  {
 889                      $imgs[$key] = $this->message->embed( Swift_Image::fromPath( $file ) );
 890                  }
 891                  catch ( Exception $e ) 
 892                  {
 893                      if ( !( $e instanceof Swift_SwiftException ) ) throw $e;
 894  
 895                      if ( isset( $this->trace ) ) $this->trace->log( 'SWIFTMAILER [ERROR] - ' . "There was a problem with this image: $file \n\n" . $e->getMessage() . "\n\n" );
 896                  } 
 897              }
 898          }
 899          else
 900          {
 901              foreach ( $x as $key => $file )
 902              {
 903                  $imgs[$key] = $file;
 904              }
 905          }
 906          foreach ( $matches as $match )
 907          {
 908              $match[3]    = ( isset( $imgs[$match[1]] ) ) ? str_replace( $match[1], $imgs[$match[1]], $match[0] ) : $match[0]; // and we retrieve it now with the proper <img ... />
 909              if ( 'html' != $dest ) $match[3] = apply_filters( 'MailPress_img_mail', $match[3] ); // apply_filters for 'mail', 'draft'
 910              $html        = str_replace( $match[0], $match[3], $html );
 911          }
 912          return $html;
 913      }
 914  
 915  /// DISPLAYING MAILS ///
 916  
 917  
 918  	function get_replacements( $id, $main_id = false, $mp_user_id = false )
 919      {
 920          $mail_r = MP_Mail_meta::get( $id, '_MailPress_replacements' );
 921          if ( !$mail_r && $main_id ) $mail_r = MP_Mail_meta::get( $main_id, '_MailPress_replacements' );
 922          if ( !$mail_r ) $mail_r = array();
 923  
 924          if ( !$mp_user_id ) return $mail_r;
 925  
 926          $mail = MP_Mail::get( $id );
 927  
 928          if ( !$mail ) return $mail_r;
 929          if ( MailPress::is_email( $mail->toemail ) ) return $mail_r;
 930  
 931          $mail->toemail = unserialize( $mail->toemail );
 932  
 933          $mp_user = MP_User::get( $mp_user_id );
 934  
 935          if ( !$mp_user ) return $mail_r;
 936          if ( !isset( $mail->toemail[$mp_user->email] ) ) return $mail_r;
 937  
 938          return array_merge( $mail_r, $mail->toemail[$mp_user->email] );
 939      }
 940  
 941  	function viewsubject( $subject, $id, $main_id, $mp_user_id = false )
 942      {
 943          $replacements = $this->get_replacements( $id, $main_id, $mp_user_id );
 944          foreach( $replacements as $k => $v ) 
 945              $subject = str_replace( $k, $v, $subject, $ch );
 946          return $subject;
 947      }
 948  
 949  	function view( $args )
 950      {
 951          $defaults = array( 'type' => 'html', 'id' => 0, 'main_id' => 0, 'theme' => 0, 'template' => 0, 'mp_user_id' => 0 );
 952          $r = wp_parse_args( $args, $defaults );
 953          extract( $r );
 954  
 955          $x = MP_Mail::get( $id );
 956          $y = array( 'sent', 'unsent', 'archived' );
 957          if ( !in_array( $x->status, $y ) )
 958          {
 959              $this->args         = new stdClass();
 960              $this->args->id        = $id;
 961              $this->args->main_id    = $main_id;
 962  
 963              if ( $theme )         $this->args->Theme = $theme;
 964              if ( $template )         $this->args->Template = $template;
 965  
 966              if ( !isset( $this->row ) ) $this->row = new stdClass();
 967              $this->row->subject     = $x->subject;
 968  
 969              $this->args->{$type}     = $x->{$type};
 970              $x->{$type}         = $this->build_mail_content( $type, ( 'html' == $type ) ? 'MailPress_the_content' : false );
 971              $x->themedir         = $this->mail->themedir;
 972          }
 973          $$type = ( 'html' == $type )? $this->process_img( $x->html, $x->themedir, 'draft' ) : $x->plaintext;
 974  
 975          $replacements = $this->get_replacements( $id, $main_id, $mp_user_id );
 976          foreach( $replacements as $k => $v ) $$type = str_replace( $k, $v, $$type, $ch );
 977  
 978          include MP_ABSPATH . "mp-includes/html/{$type}.php";
 979      }
 980  
 981  /// Get & Status ///
 982  
 983  	public static function get( $mail, $output = OBJECT ) 
 984      {
 985          switch ( true )
 986          {
 987              case ( empty( $mail ) ) :
 988                  if ( isset( $GLOBALS['mp_mail'] ) )     $_mail = & $GLOBALS['mp_mail'];
 989                  else                        return null;
 990              break;
 991              case ( is_object( $mail ) ) :
 992                  wp_cache_add( $mail->id, $mail, 'mp_mail' );
 993                  $_mail = $mail;
 994              break;
 995              default :
 996                  if ( isset( $GLOBALS['mp_mail'] ) && ( $GLOBALS['mp_mail']->id == $mail ) ) 
 997                  {
 998                      $_mail = & $GLOBALS['mp_mail'];
 999                  } 
1000                  elseif ( ! $_mail = wp_cache_get( $mail, 'mp_mail' ) ) 
1001                  {
1002                      global $wpdb;
1003                      $_mail = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->mp_mails WHERE id = %d LIMIT 1", $mail ) );
1004                      if ( $_mail ) wp_cache_add( $_mail->id, $_mail, 'mp_mail' );
1005                  }
1006              break;
1007          }
1008  
1009          if ( $output == OBJECT ) {
1010              return $_mail;
1011          } elseif ( $output == ARRAY_A ) {
1012              return get_object_vars( $_mail );
1013          } elseif ( $output == ARRAY_N ) {
1014              return array_values( get_object_vars( $_mail ) );
1015          } else {
1016              return $_mail;
1017          }
1018      }
1019  
1020  	public static function get_id( $from = 'inconnu' )
1021      {
1022          $data = $format = array();
1023  
1024          $data['status']         = '';                     $format[] = '%s';
1025          $data['created']         = current_time( 'mysql' );        $format[] = '%s';
1026          $data['created_user_id']= MP_WP_User::get_id();            $format[] = '%d';
1027          // longtext
1028          $data['toemail']         = '';                     $format[] = '%s';
1029          $data['html']         = '';                     $format[] = '%s';
1030          $data['plaintext']     = '';                     $format[] = '%s';
1031  
1032          global $wpdb;
1033          $wpdb->insert( $wpdb->mp_mails, $data, $format );
1034  
1035          return $wpdb->insert_id;
1036      }
1037  
1038  	public static function get_var( $var, $key_col, $key, $format = '%s' ) 
1039      {
1040          global $wpdb;
1041          return $wpdb->get_var( $wpdb->prepare( "SELECT $var FROM $wpdb->mp_mails WHERE $key_col = $format LIMIT 1;", $key ) );
1042      }
1043  
1044  	public static function get_status( $id ) 
1045      {
1046          $result = self::get_var( 'status', 'id', $id );
1047          return ( $result == NULL ) ? self::status_deleted : $result;
1048      }
1049  
1050  	public static function update_status( $id, $status )
1051      {
1052          wp_cache_delete( $id, 'mp_mail' );
1053  
1054          $data = $format = $where = $where_format = array();
1055  
1056          $data['status']             = $status;                             $format[] = '%s';
1057  
1058          $where['id']             = ( int ) $id;                        $where_format[] = '%d';
1059  
1060          global $wpdb;
1061          return $wpdb->update( $wpdb->mp_mails, $data, $where, $format, $where_format );
1062      }
1063  
1064  	public static function set_status( $id, $status ) 
1065      {
1066          switch( $status ) 
1067          {
1068              case 'sent':
1069                  if ( 'archived' == self::get_status( $id ) ) return self::update_status( $id, 'sent' );
1070                  return false;
1071              break;
1072              case 'archived':
1073                  if ( 'sent' == self::get_status( $id ) ) return self::update_status( $id, 'archived' );
1074                  return false;
1075              break;
1076              case 'paused':
1077                  if ( 'unsent' == self::get_status( $id ) ) return self::update_status( $id, 'paused' );
1078                  return false;
1079              break;
1080              case 'unsent':
1081                  if ( 'paused' == self::get_status( $id ) ) return self::update_status( $id, 'unsent' );
1082                  return false;
1083              break;
1084              case 'delete':
1085                  return self::delete( $id );
1086              break;
1087          }
1088          wp_cache_delete( $id, 'mp_mail' );
1089          return true;
1090      }
1091  
1092  	public static function delete( $id )
1093      {
1094          global $wpdb;
1095          do_action( 'MailPress_delete_mail', $id );
1096          wp_clear_scheduled_hook( 'mp_process_send_draft', array( $id ) );
1097  
1098          MP_Mail_revision::delete( $id );
1099  
1100          MP_Mail_meta::delete( $id );
1101  
1102          wp_cache_delete( $id, 'mp_mail' );
1103  
1104          return $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_mails WHERE id = %d ; ", $id ) );
1105      }
1106  
1107  
1108  /// DISPLAYING E-MAILS & NAMES ///
1109  
1110  	public static function display_toemail( $toemail, $toname, $tolist = '', $selected=false )
1111      {
1112          $return = '';
1113          $draft_dest = MP_User::get_mailinglists();
1114  
1115          if         ( !empty( $tolist )  && isset( $draft_dest[$tolist] ) )    return "<b>" . $draft_dest[$tolist] . "</b>"; 
1116          elseif     ( !empty( $toemail ) && isset( $draft_dest[$toemail] ) )    return "<b>" . $draft_dest[$toemail] . "</b>"; 
1117          elseif     ( MailPress::is_email( $toemail ) )
1118          {
1119                  return self::display_name_email( $toname, $toemail );
1120          }
1121          else
1122          {
1123              $y = unserialize( $toemail );
1124              unset( $y['MP_Mail'] );
1125              if ( is_array( $y ) )
1126              {
1127                  $return = $s = '';
1128                  foreach ( $y as $k => $v )
1129                  {
1130                      if ( ( int ) $selected == ( int ) $v['{{_user_id}}'] ) $s = ' selected="selected"';
1131                      $opt = ( is_numeric( $k ) ) ? __( 'anonymized', 'MailPress' ) : $k;
1132                      $return .= "<option$s>$opt</option>";
1133                      if ( !empty( $s ) ) $selected = -1;
1134                      $s = '';
1135                  }
1136                  $selected = ( $selected == -1 ) ? ' disabled="disabled"' : '';
1137                  $return = "<select{$selected}>{$return}</select>";
1138                  return $return;
1139              }
1140          }
1141          return false;
1142      }
1143  
1144  	public static function display_name_email( $name, $email )
1145      {
1146          if ( empty( $name ) ) return $email;
1147          return self::display_name( esc_attr( $name ), false ) . " &lt;$email&gt;";
1148      }
1149  
1150  	public static function display_name( $name, $for_mail = true )
1151      {
1152          $default = '_';
1153          if ( MailPress::is_email( $name ) )    $name = trim( str_replace( '.', ' ', substr( $name, 0, strpos( $name, '@' ) ) ) );
1154          if ( $for_mail ) 
1155          { if ( empty( $name ) )     $name = $default; }
1156          else
1157          { if ( $default == $name )$name = '';}
1158          return $name;                                    
1159      }
1160  
1161  //// attachments
1162  
1163  	public static function get_attachment_link( $meta, $mail_status )
1164      {
1165          $meta_value = unserialize( $meta['meta_value'] );
1166          $href = esc_url( add_query_arg( array( 'action' => 'mp_ajax', 'mp_action' => 'attach_download', 'id' => $meta['meta_id'] ), admin_url( 'admin-ajax.php' ) ) );
1167  
1168          if ( in_array( $mail_status, array( 'sent', 'archived' ) ) )
1169          {
1170              if ( is_file( $meta_value['file_fullpath'] ) )
1171              {
1172                  return '<a href="' . $href . '" style="text-decoration:none;">' . $meta_value['name'] . '</a>';
1173              }
1174              else
1175              {
1176                  return '<span>' . $meta_value['name'] . '</span>';
1177              }
1178          }
1179          else
1180          {
1181              if ( is_file( $meta_value['file_fullpath'] ) )
1182              {
1183                  return '<a href="' . $href . '" style="text-decoration:none;">' . $meta_value['name'] . '</a>';
1184              }
1185          }
1186      }
1187  
1188  /* ajax */
1189  	public static function add_object()
1190      {
1191          MP_WP_List::add_object( self::object_type );
1192      }
1193  
1194  	public static function dim_object() 
1195      {
1196          MP_WP_List::dim_object( self::object_type );
1197      }
1198  
1199  	public static function delete_object() 
1200      {
1201          MP_WP_List::delete_object( self::object_type );
1202      }
1203  }


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