[ Index ]

MailPress 544

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

title

Body

[close]

/mp-admin/ -> mails.php (source)

   1  <?php
   2  class MP_AdminPage extends MP_adminpage_list_
   3  {
   4      const screen         = MailPress_page_mails;
   5      const capability     = 'MailPress_edit_mails';
   6      const help_url        = 'http://blog.mailpress.org/tutorials/';
   7      const file            = __FILE__;
   8  
   9  ////  Redirect  ////
  10  
  11  	public static function redirect() 
  12      {
  13          if     ( !empty($_REQUEST['action'])  && ($_REQUEST['action']  != -1))    $action = $_REQUEST['action'];
  14          elseif ( !empty($_REQUEST['action2']) && ($_REQUEST['action2'] != -1) )    $action = $_REQUEST['action2'];
  15          if (!isset($action)) return;
  16  
  17          $url_parms     = self::get_url_parms();
  18          $checked    = (isset($_GET['checked'])) ? $_GET['checked'] : array();
  19  
  20          $count    = str_replace('bulk-', '', $action);
  21          $count     .= 'd';
  22          $$count    = 0;
  23  
  24          switch($action)
  25          {
  26              case 'bulk-pause' :
  27                  foreach($checked as $id) if (MP_Mail::set_status($id, 'paused'))  $$count++;
  28              break;
  29              case 'bulk-restart' :
  30                  foreach($checked as $id) if (MP_Mail::set_status($id, 'unsent'))  $$count++;
  31              break;
  32              case 'bulk-archive' :
  33                  foreach($checked as $id) if (MP_Mail::set_status($id, 'archived'))  $$count++;
  34              break;
  35              case 'bulk-unarchive' :
  36                  foreach($checked as $id) if (MP_Mail::set_status($id, 'sent'))     $$count++;
  37              break;
  38              case 'bulk-send' :
  39                  $sent = $notsent = 0;
  40                  foreach($checked as $id)
  41                  {
  42                      if ('draft' != MP_Mail::get_status($id)) continue;
  43                      $x = MP_Mail_draft::send($id);
  44                      $url = (is_numeric($x))    ? $sent += $x : $notsent++ ;
  45                  }
  46                  if ($sent)    $url_parms['sent']    = $sent;
  47                  if ($notsent) $url_parms['notsent'] = $notsent;
  48              break;
  49              case 'bulk-delete' :
  50                  foreach($checked as $id) if (MP_Mail::set_status($id, 'delete')) $$count++;
  51              break;
  52          }
  53  
  54          if ($$count) $url_parms[$count] = $$count;
  55          self::mp_redirect( self::url(MailPress_mails, $url_parms) );
  56      }
  57  
  58  ////  Styles  ////
  59  
  60  	public static function print_styles($styles = array()) 
  61      {
  62          wp_register_style ( self::screen,         '/' . MP_PATH . 'mp-admin/css/mails.css',       array('thickbox') );
  63  
  64          $styles[] = self::screen;
  65          parent::print_styles($styles);
  66      }
  67  
  68  //// Scripts ////
  69  
  70  	public static function print_scripts($scripts = array()) 
  71      {
  72          $scripts = apply_filters('MailPress_autorefresh_js', $scripts);
  73  
  74          wp_register_script( 'mp-ajax-response',     '/' . MP_PATH . 'mp-includes/js/mp_ajax_response.js', array('jquery'), false, 1);
  75          wp_localize_script( 'mp-ajax-response',         'wpAjax', array( 
  76              'noPerm' => __('Email was not sent AND/OR Update database failed', MP_TXTDOM), 
  77              'broken' => __('An unidentified error has occurred.'), 
  78              'l10n_print_after' => 'try{convertEntities(wpAjax);}catch(e){};' 
  79          ));
  80  
  81          wp_register_script( 'mp-lists',         '/' . MP_PATH . 'mp-includes/js/mp_lists.js', array('mp-ajax-response'), false, 1);
  82          wp_localize_script( 'mp-lists',             'wpListL10n', array( 
  83              'url' => MP_Action_url
  84          ));
  85  
  86          wp_register_script( 'mp-thickbox',         '/' . MP_PATH . 'mp-includes/js/mp_thickbox.js', array('thickbox'), false, 1);
  87  
  88          wp_register_script( self::screen,         '/' . MP_PATH . 'mp-admin/js/mails.js', array('mp-thickbox', 'mp-lists'), false, 1);
  89          wp_localize_script( self::screen,     'MP_AdminPageL10n', array(    
  90              'pending' => __('%i% pending'), 
  91              'screen' => self::screen, 
  92              'l10n_print_after' => 'try{convertEntities(MP_AdminPageL10n);}catch(e){};' 
  93          ));
  94  
  95          $scripts[] = self::screen;
  96          parent::print_scripts($scripts);
  97      }
  98  
  99  //// Columns ////
 100  
 101  	public static function get_columns() 
 102      {
 103          $disabled = (!current_user_can('MailPress_delete_mails')) ? " disabled='disabled'" : '';
 104          $columns = array(    'cb'         => "<input type='checkbox'$disabled />", 
 105                      'title'     => __('Subject', MP_TXTDOM), 
 106                      'author'     => __('Author'), 
 107                      'theme'     => __('Theme', MP_TXTDOM), 
 108                      'to'         => __('To', MP_TXTDOM), 
 109                      'date'    => __('Date') );
 110          $columns = apply_filters('MailPress_mails_columns', $columns);
 111          return $columns;
 112      }
 113  
 114  //// List ////
 115  
 116  	public static function get_list($args) 
 117      {
 118          extract($args);
 119  
 120          global $wpdb;
 121  
 122          $where = " AND status <> '' ";
 123  
 124          if (isset($url_parms['s']))
 125          {
 126              $sc = array('theme', 'themedir', 'template', 'toemail', 'toname', 'subject', 'html', 'plaintext', 'created', 'sent' );
 127  
 128              $where .= self::get_search_clause($url_parms['s'], $sc);
 129          }
 130  
 131          if (isset($url_parms['status']) && !empty($url_parms['status']))
 132              $where .= " AND status = '" . $url_parms['status'] . "'";
 133          if (isset($url_parms['author']) && !empty($url_parms['author']))
 134              $where .= " AND ( created_user_id = " . $url_parms['author'] . "  OR sent_user_id = " . $url_parms['author'] . " ) ";
 135          if (!current_user_can('MailPress_edit_others_mails'))
 136              $where .= " AND ( created_user_id = " . MP_WP_User::get_id() . " ) ";
 137  
 138          $args['query'] = "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->mp_mails WHERE 1=1 $where ORDER BY created DESC";
 139          $args['cache_name'] = 'mp_mail';
 140  
 141          list($_mails, $total) = parent::get_list($args);
 142  
 143          $subsubsub_urls = false;
 144  
 145          $libs = array( 'all' => __('All'), 'draft' =>    __('Draft', MP_TXTDOM), 'unsent' => __('Unsent', MP_TXTDOM), 'paused' => __('Paused', MP_TXTDOM), 'sending' => __('Pending', MP_TXTDOM), 'sent' => __('Sent', MP_TXTDOM), 'archived' => __('Archive', MP_TXTDOM), 'search' => __('Search Results') );
 146  
 147          $counts = array();
 148          $query = "SELECT status, count(*) as count FROM $wpdb->mp_mails GROUP BY status;";
 149          $statuses = $wpdb->get_results($query);
 150  
 151          if ($statuses)
 152          {
 153              $status_links_url  = MailPress_mails ;
 154  
 155              $status_links_url .= (isset($url_parms['mode'])) ? "&amp;mode=" . $url_parms['mode']  : '';
 156  
 157              foreach($statuses as $status) if ($status->count) $counts[$status->status] = $status->count;
 158              $counts['all'] = $wpdb->get_var("SELECT count(*) FROM $wpdb->mp_mails WHERE status <> '';");
 159              if (isset($url_parms['s'])) $counts['search'] = count($_mails);
 160              $out = array();
 161  
 162              foreach($libs as $k => $lib)
 163              {
 164                  if (!isset($counts[$k]) || !$counts[$k]) continue;
 165  
 166                      $url = $status_links_url . ( ('search' == $k) ? '&s=' . $url_parms['s'] : ( ('all' == $k) ? '' : "&amp;status=$k" ) );
 167  
 168                  $sum = $counts[$k];
 169  
 170                  $cls = '';
 171                  if (isset($url_parms['s']))            { if ('search' == $k)             $cls = " class='current'"; }
 172                  elseif (isset($url_parms['status'])) { if ($url_parms['status'] == $k ) $cls = " class='current'"; }
 173                  elseif ('all' == $k)                                    $cls = " class='current'"; 
 174  
 175                  $out[] = "<a$cls href='$url'>$lib <span class='count'>(<span class='mail-count-$k'>$sum</span>)</span></a>";
 176              }
 177  
 178              if (!empty($out)) $subsubsub_urls = '<li>' . join( ' | </li><li>', $out ) . '</li>';
 179          }
 180          return array($_mails, $total, $subsubsub_urls);
 181      }
 182  
 183  ////  Row  ////
 184  
 185  	public static function get_row( $id, $url_parms, $xtra = false) 
 186      {
 187          global $mp_mail;
 188          static $row_class = '';
 189  
 190          $mp_mail = $mail = MP_Mail::get( $id );
 191          $the_mail_status = $mail->status;
 192  
 193  // url's
 194          $args = array();
 195          $args['id']     = $id;
 196  
 197          $edit_url        = esc_url(self::url( MailPress_edit, array_merge($args, $url_parms) ));
 198  
 199          $args['action']        = 'pause';
 200          $pause_url        = esc_url(self::url( MailPress_write, array_merge($args, $url_parms) ));
 201  
 202          $args['action']        = 'restart';
 203          $restart_url       = esc_url(self::url( MailPress_write, array_merge($args, $url_parms) ));
 204  
 205          $args['action']     = 'archive';
 206          $archive_url     = esc_url(self::url( MailPress_write, array_merge($args, $url_parms), "archive-mail_{$mail->id}" ));
 207  
 208          $args['action']     = 'unarchive';
 209          $unarchive_url     = esc_url(self::url( MailPress_write, array_merge($args, $url_parms), "unarchive-mail_{$mail->id}" ));
 210  
 211          $args['action']     = 'send';
 212          $send_url        = esc_url(self::url( MailPress_write, array_merge($args, $url_parms) ));
 213  
 214          $args['action']     = 'delete';
 215          $delete_url     = esc_url(self::url( MailPress_write, array_merge($args, $url_parms), "delete-mail_$id" ));
 216  
 217          $args['action']     = 'iview';
 218          if ('draft' == $mail->status) if (!empty($mail->theme)) $args['theme']     = $mail->theme;
 219          $args['preview_iframe'] = 1; $args['TB_iframe']= 'true';
 220          $view_url        = esc_url(self::url(MP_Action_url, $args));
 221  
 222  
 223  // actions
 224          $actions = array();
 225          $actions['edit']    = "<a href='$edit_url'       title='" .  __('Edit') . "'>" . __('Edit') . '</a>';
 226          $actions['send']     = "<a href='$send_url'     title='" . __('Send this mail', MP_TXTDOM ) . "'>" . __( 'Send', MP_TXTDOM ) . '</a>';
 227          $actions['pause']    = "<a href='$pause_url'     title='" . __('Pause', MP_TXTDOM ) . "'>"     . __('Pause', MP_TXTDOM) . '</a>';
 228          $actions['restart']     = "<a href='$restart_url'     title='" . __('Restart', MP_TXTDOM ) . "'>"     . __('Restart', MP_TXTDOM) . '</a>';
 229          $actions = apply_filters('MailPress_mails_actions', $actions, $mp_mail, $url_parms);
 230  
 231          $actions['approve']   = "<a href='$unarchive_url'     class='dim:the-mail-list:mail-$id:unapproved:e7e7d3:e7e7d3:?mode=" . $url_parms['mode'] . "' title='" . __('Unarchive this mail', MP_TXTDOM ) . "'>" . __( 'Unarchive', MP_TXTDOM ) . '</a>';
 232          $actions['unapprove'] = "<a href='$archive_url'     class='dim:the-mail-list:mail-$id:unapproved:e7e7d3:e7e7d3:?mode=" . $url_parms['mode'] . "' title='" . __('Archive this mail', MP_TXTDOM )   . "'>" . __( 'Archive', MP_TXTDOM )     . '</a>';
 233  
 234          $actions['delete']= "<a href='$delete_url'     class='submitdelete' title='" . __('Delete this mail', MP_TXTDOM ) . "'>"     . __('Delete', MP_TXTDOM) . '</a>';
 235          $actions['view']     = "<a href='$view_url'         class='thickbox thickbox-preview'  title='" . __('View', MP_TXTDOM ) . "'>"                                . __('View', MP_TXTDOM) . '</a>';
 236  
 237          //$include = array('edit', 'send', 'pause', 'restart', 'tracking', 'approve', 'unapprove', 'delete', 'view');
 238          switch($mail->status)
 239          {
 240              case 'draft' :
 241                  $include = array('edit', 'send', 'delete', 'view');
 242              break;
 243              case 'unsent' :
 244                  $include = array('pause', 'tracking', 'view');
 245              break;
 246              case 'sending' :
 247                  $include = array('pause', 'tracking', 'view');
 248              break;
 249              case 'paused' :
 250                  $include = array('restart', 'tracking', 'delete', 'view');
 251              break;
 252              case 'sent' :
 253                  $include = array('tracking', 'approve', 'unapprove', 'delete', 'view');
 254              break;
 255              case 'archived' :
 256                  $include = array('tracking', 'approve', 'unapprove', 'view');
 257              break;
 258              default :
 259                  $include = array('view');
 260              break;
 261          }
 262          foreach($actions as $k => $v) if (!in_array($k, $include)) unset($actions[$k]);
 263  
 264          if (!current_user_can('MailPress_send_mails'))         unset($actions['send']);
 265          if (!current_user_can('MailPress_delete_mails'))     unset($actions['delete']);
 266          if (!current_user_can('MailPress_archive_mails'))    {unset($actions['approve']); unset($actions['unapprove']);}
 267  
 268  // table row 
 269  //    class
 270          $row_class = ('alternate' == substr($row_class, 0, 9)) ? '' : 'alternate ';
 271          if ('archived' == $the_mail_status)  $row_class .= 'unapproved';
 272          if ('draft' == $the_mail_status)  $row_class .= 'draft';
 273          if ('unsent' == $the_mail_status) $row_class .= 'unsent';
 274  //     checkbox
 275          $disabled = (!current_user_can('MailPress_delete_mails') && !current_user_can('MailPress_send_mails')) ? " disabled='disabled'" : '';
 276  //    to
 277          $draft_dest = MP_User::get_mailinglists();
 278  
 279          switch (true)
 280          {
 281              case ($xtra) :
 282                  $email_display = "<blink style='color:red;font-weight:bold;'>" . $xtra . '</blink>';
 283              break;
 284              case (is_email($mail->toemail)) :
 285                  $mail_url = self::url(MailPress_mails, $url_parms);
 286                  $mail_url = remove_query_arg('s', $mail_url);
 287                  $mail_url = esc_url( $mail_url . '&s=' . $mail->toemail );
 288  
 289                  $email_display = '';
 290                  $mail_url2         = "<a class='row-title' href='$mail_url'  title='" . sprintf( __('Search "%1$s"', MP_TXTDOM), $mail->toemail) . "'>";
 291                  if ( ('detail' == $url_parms['mode']) && (get_option('show_avatars') ) )
 292                  {
 293                      $email_display .= "<div>";
 294                      $email_display .= $mail_url2;
 295                      $email_display .= get_avatar( $mail->toemail, 32 );
 296                      $email_display .= '</a>';
 297                      $email_display .= '</div>';
 298                  }
 299                  $email_display .= "<div>";
 300                  $email_display .= $mail_url2;
 301                  $email_display .= '<strong>';
 302                  $email_display .= ( strlen($mail->toemail) > 40 ) ? substr($mail->toemail, 0, 39) . '...' : $mail->toemail;
 303                  $email_display .= '</strong>';
 304                  $email_display .= '</a>';
 305                  if (!empty($mail->toname)) $email_display .= '<br />' . $mail->toname;
 306                  $email_display .= '</div>';
 307              break;
 308              case (isset($draft_dest[$mail->toemail])) :
 309                  $email_display = "<strong>" . $draft_dest[$mail->toemail] . "</strong>";
 310              break;
 311              case (is_serialized($mail->toemail)) :
 312                  $email_display = "<div class='num post-com-count-wrapper'><a class='post-com-count'><span class='comment-count'>" . count(unserialize($mail->toemail)) . "</span></a></div>"; 
 313              break;
 314              default  :
 315                  $email_display = "<span style='color:red;font-weight:bold;'>" . __('(unknown)', MP_TXTDOM) . '</span>';
 316                  unset($actions['send']);
 317              break;
 318          }
 319          $email_display = apply_filters('MailPress_to_mails_column', $email_display, $mail);
 320          if ($mailinglist_desc = MP_Mail_meta::get($mail->id, '_mailinglist_desc')) $email_display = "<div>{$email_display}</div>{$mailinglist_desc}";
 321  
 322  //    author
 323          $author = ( 0 == $mail->sent_user_id) ? $mail->created_user_id : $mail->sent_user_id;
 324          if ($author != 0 && is_numeric($author)) 
 325          {
 326              unset($url_parms['author']);
 327              $wp_user         = get_userdata($author);
 328              $author_url     = esc_url(self::url( MailPress_mails . "&author=" . $author, $url_parms ));
 329          }
 330  //    subject
 331          $metas = MP_Mail_meta::get( $id, '_MailPress_replacements' );
 332          $subject_display = $mail->subject;
 333          if ($metas) foreach($metas as $k => $v) $subject_display = str_replace($k, $v, $subject_display);
 334  //    attachements
 335          $attach = false;
 336          $metas = MP_Mail_meta::has( $id, '_MailPress_attached_file' );
 337          if ($metas)
 338          {
 339              foreach($metas as $meta)
 340              {
 341                  $meta_value = unserialize( $meta['meta_value'] );
 342                  if ($the_mail_status == 'sent')
 343                  {
 344                      $attach = true;
 345                      break;
 346                  }
 347                  elseif (is_file($meta_value['file_fullpath']))
 348                  {
 349                      $attach = true;
 350                      break;
 351                  }
 352              }
 353          }
 354  
 355  ?>
 356      <tr id="mail-<?php echo $id; ?>" class='<?php echo $row_class; ?>'>
 357  <?php
 358          $columns = self::get_columns();
 359          $hidden  = self::get_hidden_columns();
 360  
 361          foreach ( $columns as $column_name => $column_display_name ) 
 362          {
 363              $class = "class='$column_name column-$column_name'";
 364  
 365              $style = '';
 366              if ('unsent' == $mail->status)         $style .= 'font-style:italic;';
 367              if ( in_array($column_name, $hidden) )     $style .= 'display:none;';
 368              $style = ' style="' . $style . '"';
 369  
 370              $attributes = "$class$style";
 371  
 372              switch ($column_name) 
 373              {
 374  
 375                  case 'cb':
 376  ?>
 377          <th class='check-column'>
 378  <?php                    if (isset($actions['delete'])) : ?>
 379              <input type='checkbox' name='checked[]' value='<?php echo $id; ?>'<?php echo $disabled; ?> />
 380  <?php                    endif;
 381                  break;
 382                  case 'title':
 383                      $attributes = 'class="post-title column-title"' . $style;
 384  ?>
 385          <td  <?php echo $attributes ?>>
 386  <?php
 387              if ('paused' == $mail->status) :
 388  ?>
 389              <span class='icon paused' title="<?php _e('Paused', MP_TXTDOM); ?>"></span>
 390  <?php
 391              endif;
 392              if ($attach) :
 393  ?>
 394              <span class='icon attachement' title="<?php _e('Attachments', MP_TXTDOM); ?>"></span>
 395  <?php
 396              endif;
 397              if (('draft' == $mail->status) && ($mp_mail->sent >= $mp_mail->created)) :
 398  ?>
 399              <span class='icon scheduled' title="<?php _e('Scheduled', MP_TXTDOM); ?>"></span>
 400  <?php
 401              endif;
 402  
 403              do_action('MailPress_get_icon_mails', $id);
 404  ?>
 405              <strong>
 406                  <a class='row-title<?php echo ('draft' == $mail->status) ? '' : ' thickbox thickbox-preview'; ?>' href='<?php echo ('draft' == $mail->status) ? $edit_url : $view_url; ?>' title='<?php printf( ('draft' == $mail->status) ?  __('Edit "%1$s"', MP_TXTDOM) : __('View "%1$s"', MP_TXTDOM) , ( '' == $subject_display) ? __('(no subject)', MP_TXTDOM) : htmlspecialchars($subject_display, ENT_QUOTES) ); ?>'>
 407                      <?php echo ( '' == $subject_display) ? __('(no subject)', MP_TXTDOM) : (( strlen($subject_display) > 40 ) ? $subject_display = mb_substr($subject_display, 0, 39, get_option('blog_charset')) . '...' : $subject_display); ?>
 408                  </a>
 409  <?php if ('draft' == $mail->status) echo ' - ' . __('Draft'); ?>
 410  <?php if ('paused' == $mail->status) echo ' - ' . __('Paused', MP_TXTDOM); ?>
 411  <?php if ('archived' == $mail->status) echo ' - ' . __('Archive', MP_TXTDOM); ?>
 412              </strong>
 413  <?php            echo self::get_actions($actions); ?>
 414          </td>
 415  <?php
 416                  break;
 417                  case 'author':
 418  ?>
 419          <td  <?php echo $attributes ?>>
 420  <?php                    if ($author != 0 && is_numeric($author)) { ?>
 421              <a href='<?php echo $author_url; ?>' title='<?php printf( __('Mails by "%1$s"', MP_TXTDOM), $wp_user->display_name); ?>'><?php echo $wp_user->display_name; ?></a>
 422  <?php                 } else _e("(unknown)", MP_TXTDOM); ?>
 423          </td>
 424  <?php
 425                  break;
 426                  case 'theme':
 427  ?>
 428          <td  <?php echo $attributes ?>>
 429              <?php echo $mail->theme; ?>
 430              <?php if ('' != $mail->template) echo "<br />(" . $mail->template . ")"; ?>
 431  
 432          </td>
 433  <?php
 434                  break;
 435                  case 'to':
 436  ?>
 437          <td  <?php echo $attributes ?>>
 438  <?php echo $email_display; ?>
 439          </td>
 440  <?php
 441                  break;
 442                  case 'date':
 443                      $date_status = ('draft' == $mail->status) ? (($mp_mail->sent >= $mp_mail->created) ? true : __('Last Modified', MP_TXTDOM)) : __('Sent', MP_TXTDOM);
 444                      $_scheduled = false;
 445                      if (true === $date_status)
 446                      {
 447                          $_scheduled = true;
 448                          $date_status= __('Scheduled', MP_TXTDOM);
 449                      }
 450  
 451                      $t_time = self::get_mail_date(__('Y/m/d H:i:s'));
 452                      $m_time = self::get_mail_date_raw();
 453  
 454                      $time   = strtotime( get_gmt_from_date( $m_time ) );
 455                      $time_diff = current_time('timestamp', true) - $time;
 456  
 457                      if ($_scheduled)    $h_time = mysql2date(__('Y/m/d'), $m_time);
 458                      else             $h_time = self::human_time_diff(self::get_mail_date_raw());
 459  ?>
 460          <td  <?php echo $attributes ?>>
 461              <abbr title="<?php echo $t_time; ?>"><?php echo $h_time; ?></abbr>
 462              <br />
 463              <?php echo $date_status; ?>
 464          </td>
 465  <?php
 466                  break;
 467                  default:
 468  ?>
 469          <td  <?php echo $attributes ?>>
 470              <?php    do_action('MailPress_mails_get_row', $column_name, $mail, $url_parms); ?>
 471          </td>
 472  <?php
 473                  break;
 474              }
 475          }
 476  ?>
 477        </tr>
 478  <?php
 479      }
 480  
 481  	public static function mail_date($d = '') {
 482          echo  self::get_mail_date($d);
 483      }
 484  
 485  	public static function get_mail_date($d = '' ) {
 486          $x = self::get_mail_date_raw();
 487          return ( '' == $d ) ? mysql2date( get_option('date_format'), $x) : mysql2date($d, $x);
 488      }
 489  
 490  	public static function get_mail_date_raw() {
 491          global $mp_mail;
 492          $x = ($mp_mail->sent >= $mp_mail->created) ? $mp_mail->sent : $mp_mail->created;
 493          return $x;
 494      }
 495  }


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