[ Index ]

MailPress 544

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

title

Body

[close]

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

   1  <?php
   2  class MP_AdminPage extends MP_adminpage_list_
   3  {
   4      const screen         = MailPress_page_users;
   5      const capability     = 'MailPress_edit_users';
   6      const help_url        = 'http://blog.mailpress.org/tutorials/';
   7      const file            = __FILE__;
   8  
   9  ////  Redirect  ////
  10  
  11  	public static function redirect() 
  12      {
  13          add_action('MailPress_users_restrict',     array(__CLASS__, 'users_restrict'), 1, 1);
  14  
  15          if     ( !empty($_REQUEST['action'])  && ($_REQUEST['action']  != -1))    $action = $_REQUEST['action'];
  16          elseif ( !empty($_REQUEST['action2']) && ($_REQUEST['action2'] != -1) )    $action = $_REQUEST['action2'];
  17          if (!isset($action)) return;
  18  
  19          $url_parms     = self::get_url_parms();
  20          $checked    = (isset($_GET['checked'])) ? $_GET['checked'] : array();
  21  
  22          $count    = str_replace('bulk-', '', $action);
  23          $count     .= 'd';
  24          $$count    = 0;
  25  
  26          switch($action)
  27          {
  28              case 'bulk-activate' :
  29                  foreach($checked as $id) if (MP_User::set_status($id, 'active'))  $$count++;
  30              break;
  31              case 'bulk-deactivate' :
  32                  foreach($checked as $id) if (MP_User::set_status($id, 'waiting')) $$count++;
  33              break;
  34              case 'bulk-unbounce' :
  35                  foreach($checked as $id) if (MP_User::set_status($id, 'waiting'))
  36                  {
  37                      MP_User_meta::delete($id, '_MailPress_bounce_handling');
  38                      $$count++;
  39                  }
  40              break;
  41              case 'bulk-delete' :
  42                  foreach($checked as $id) if (MP_User::set_status($id, 'delete')) $$count++;
  43              break;
  44              default :
  45                  $$count = do_action('MailPress_do_bulk_action_' . self::screen, $action, $checked);
  46              break;
  47          }
  48          if ($$count) $url_parms[$count] = $$count;
  49          self::mp_redirect( self::url(MailPress_users, $url_parms) );
  50      }
  51  
  52  ////  Styles  ////
  53  
  54  	public static function print_styles($styles = array()) 
  55      {
  56          wp_register_style ( self::screen,         '/' . MP_PATH . 'mp-admin/css/users.css' );
  57  
  58          $styles[] =self::screen;
  59          parent::print_styles($styles);
  60      }
  61  
  62  //// Scripts ////
  63  
  64  	public static function print_scripts($scripts = array()) 
  65      {
  66          wp_register_script( 'mp-ajax-response',     '/' . MP_PATH . 'mp-includes/js/mp_ajax_response.js', array('jquery'), false, 1);
  67          wp_localize_script( 'mp-ajax-response',     'wpAjax', array(
  68              'noPerm' => __('Update database failed', MP_TXTDOM), 
  69              'broken' => __('An unidentified error has occurred.'), 
  70              'l10n_print_after' => 'try{convertEntities(wpAjax);}catch(e){};' 
  71          ));
  72  
  73          wp_register_script( 'mp-lists',         '/' . MP_PATH . 'mp-includes/js/mp_lists.js', array('mp-ajax-response'), false, 1);
  74          wp_localize_script( 'mp-lists',         'wpListL10n', array( 
  75              'url' => MP_Action_url
  76          ));
  77  
  78          wp_register_script( self::screen,         '/' . MP_PATH . 'mp-admin/js/users.js', array('mp-lists'), false, 1);
  79          wp_localize_script( self::screen,     'MP_AdminPageL10n', array(
  80              'pending' => __('%i% pending'), 
  81              'screen' => self::screen, 
  82              'l10n_print_after' => 'try{convertEntities(MP_AdminPageL10n);}catch(e){};' 
  83          ));
  84  
  85          $scripts[] = self::screen;
  86          parent::print_scripts($scripts);
  87      }
  88  
  89  //// Columns ////
  90  
  91  	public static function get_columns() 
  92      {
  93          $disabled = (!current_user_can('MailPress_delete_users')) ? " disabled='disabled'" : '';
  94          $columns = array(    'cb'         => "<input type='checkbox'$disabled />", 
  95                      'title'     => __('E-mail', MP_TXTDOM), 
  96                      'user_name'    => __('Name', MP_TXTDOM), 
  97                      'author'     => __('Author'), 
  98                      'date'    => __('Date'));
  99          $columns = apply_filters('MailPress_users_columns', $columns);
 100          return $columns;
 101      }
 102  
 103  //// List ////
 104  
 105  	public static function get_list($args) 
 106      {
 107          extract($args);
 108  
 109          global $wpdb;
 110  
 111          $where = $tables = '';
 112          $order = "a.created";
 113  
 114          if (isset($url_parms['s']))
 115          {
 116              $sc = array('a.email', 'a.name', 'a.laststatus_IP', 'a.created_IP');
 117  
 118              $where .= self::get_search_clause($url_parms['s'], $sc);
 119          }
 120  
 121          if (isset($url_parms['status']) && !empty($url_parms['status']))
 122              $where .= " AND a.status = '" . $url_parms['status'] . "'";
 123          if (isset($url_parms['author']) && !empty($url_parms['author']))
 124              $where .= " AND ( a.created_user_id = " . $url_parms['author'] . "  OR a.laststatus_user_id = " . $url_parms['author'] . " ) ";
 125  
 126          list($where, $tables, $no_cls) = apply_filters('MailPress_users_get_list', array($where, $tables, false), $url_parms);
 127  
 128          if (isset($url_parms['startwith']))
 129          {
 130              $where .= " AND (a.email >= '" . $url_parms['startwith'] . "') ";
 131              $order = "a.email";
 132              $no_cls = true;
 133          }
 134  
 135          $args['query'] = "SELECT DISTINCT SQL_CALC_FOUND_ROWS a.id, a.email, a.name, a.status, a.confkey, a.created, a.created_IP, a.created_agent, a.created_user_id, a.created_country, a.created_US_state, a.laststatus, a.laststatus_IP, a.laststatus_agent, a.laststatus_user_id FROM $wpdb->mp_users a $tables WHERE 1=1 $where ORDER BY $order";
 136          $args['cache_name'] = 'mp_user';
 137  
 138          list($_users, $total) = parent::get_list($args);
 139  
 140          $subsubsub_urls = false;
 141  
 142          $libs = array( 'all' => __('All'), 'waiting' => __('Waiting', MP_TXTDOM), 'active' => __('Active', MP_TXTDOM), 'bounced' => __('Bounced', MP_TXTDOM), 'unsubscribed' => __('Unsubscribed', MP_TXTDOM), 'search' => __('Search Results') );
 143  
 144          $counts = array();
 145          $query = "SELECT status, count(*) as count FROM $wpdb->mp_users GROUP BY status;";
 146          $statuses = $wpdb->get_results($query);
 147  
 148          if ($statuses)
 149          {
 150              $status_links_url  = MailPress_users ;
 151  
 152              foreach($statuses as $status) if ($status->count) $counts[$status->status] = $status->count;
 153              $counts['all'] = $wpdb->get_var("SELECT count(*) FROM $wpdb->mp_users;");
 154              if (isset($url_parms['s'])) $counts['search'] = count($_users);
 155              $out = array();
 156  
 157              foreach($libs as $k => $lib)
 158              {
 159                  if (!isset($counts[$k]) || !$counts[$k]) continue;
 160  
 161                  $url = $status_links_url . ( ('search' == $k) ? '&amp;s=' . $url_parms['s'] : ( ('all' == $k) ? '' : "&amp;status=$k" ) );
 162  
 163                  $sum = $counts[$k];
 164  
 165                  $cls = '';
 166                  if (!$no_cls)
 167                  {
 168                      if (isset($url_parms['s']))            { if ('search' == $k)             $cls = " class='current'"; }
 169                      elseif (isset($url_parms['status'])) { if ($url_parms['status'] == $k )    $cls = " class='current'"; }
 170                      elseif ('all' == $k)                                    $cls = " class='current'"; 
 171                  }
 172  
 173                  $out[] = "<a$cls href='$url'>$lib <span class='count'>(<span class='user-count-$k'>$sum</span>)</span></a>";
 174              }
 175  
 176              if (!empty($out)) $subsubsub_urls = '<li>' . join( ' | </li><li>', $out ) . '</li>';
 177          }
 178          return array($_users, $total, $subsubsub_urls);
 179      }
 180  
 181  ////  Row  ////
 182  
 183  	public static function get_row( $id, $url_parms, $checkbox = true ) {
 184  
 185          global $mp_user;
 186          static $row_class = '';
 187  
 188          $mp_user = $user = MP_User::get( $id );
 189          $the_user_status = $user->status;
 190  
 191          static $to_do_add_action = true;
 192          if ($to_do_add_action) add_action('MailPress_get_icon_users',     array(__CLASS__, 'get_icon_users'), 8, 1);
 193          $to_do_add_action = false;
 194  
 195  // url's
 196          $args = array();
 197          $args['id']     = $id;
 198  
 199          $edit_url        = esc_url(self::url( MailPress_user, array_merge($args, $url_parms) ));
 200  
 201          $args['action']     = 'activate';
 202          $activate_url     = esc_url(self::url( MailPress_user, array_merge($args, $url_parms), "activate-user_$id"));
 203  
 204          $args['action']     = 'deactivate';
 205          $deactivate_url     = esc_url(self::url( MailPress_user, array_merge($args, $url_parms), "deactivate-user_$id"));
 206  
 207          $args['action']     = 'delete';
 208          $delete_url      = esc_url(self::url( MailPress_user, array_merge($args, $url_parms), "delete-user_$id"));
 209  
 210          unset($args['action']);
 211  
 212  // actions
 213          $actions = array();
 214          $actions['edit']      = "<a href='$edit_url'  title='" . sprintf( __('Edit "%1$s"', MP_TXTDOM), $user->email ) . "'>" . __('Edit') . '</a>';
 215  
 216          $actions = apply_filters('MailPress_users_actions', $actions, $mp_user, $url_parms);
 217  
 218          $actions['approve']   = "<a href='$activate_url'     class='dim:the-user-list:user-$id:unapproved:e7e7d3:e7e7d3:' title='" . sprintf( __('Activate "%1$s"', MP_TXTDOM), $user->email ) . "'>" . __( 'Activate', MP_TXTDOM )      . '</a>';
 219          $actions['unapprove'] = "<a href='$deactivate_url'     class='dim:the-user-list:user-$id:unapproved:e7e7d3:e7e7d3:' title='" . sprintf( __('Deactivate "%1$s"', MP_TXTDOM), $user->email ) . "'>" . __( 'Deactivate', MP_TXTDOM ) . '</a>';
 220  
 221          if ('bounced' == $user->status)
 222          {
 223              unset($actions['approve'], $actions['unapprove']);
 224              $args['action'] = 'unbounce';
 225              $unbounce_url   =    esc_url(self::url( MailPress_user, array_merge($args, $url_parms) ));
 226              $unbounce_click = "onclick=\"return (confirm('" . esc_js(sprintf( __("You are about to unbounce this MailPress user '%s'\n  'Cancel' to stop, 'OK' to unbounce.", MP_TXTDOM), $id )) . "'));\"";
 227              $actions['unbounce'] = "<a href='$unbounce_url' $unbounce_click title='" . sprintf( __('Unbounce "%1$s"', MP_TXTDOM), $user->email ) . "'>" . __('Unbounce', MP_TXTDOM) . '</a>';
 228          }
 229  
 230          if ('unsubscribed' == $user->status)
 231          {
 232              unset($actions['approve']);
 233          }
 234  
 235          $actions['delete']    = "<a href='$delete_url'         class='submitdelete' title='" . __('Delete this user', MP_TXTDOM ) . "'>" . __('Delete', MP_TXTDOM) . '</a>';
 236  
 237          if (!current_user_can('MailPress_delete_users'))     unset($actions['delete']);
 238  
 239  // table row 
 240  //    class
 241          $row_class = ('alternate' == substr($row_class, 0, 9)) ? '' : 'alternate ';
 242          if ('waiting' == $the_user_status) $row_class .= 'unapproved';
 243          if ('bounced' == $the_user_status) $row_class .= 'bounced';
 244          if ('unsubscribed' == $the_user_status) $row_class .= 'unsubscribed';
 245  //     checkbox
 246          $disabled = (!current_user_can('MailPress_delete_users')) ? " disabled='disabled'" : '';
 247  //     email
 248          $email_display = $user->email;
 249          if ( strlen($email_display) > 40 )    $email_display = substr($email_display, 0, 39) . '...';
 250  //    author
 251          $x             = (isset($url_parms['s'])) ? $url_parms['s'] : '';
 252          $url_parms['s']     = self::get_user_author_IP();
 253          $ip_url         = esc_url(self::url( MailPress_users, $url_parms ));
 254          $url_parms['s']     = $x;
 255  
 256          $author = ( 0 == $user->laststatus_user_id) ? $user->created_user_id : $user->laststatus_user_id;
 257          if ($author != 0 && is_numeric($author)) {
 258              unset($url_parms['author']);
 259              $wp_user = get_userdata($author);
 260              $author_url = esc_url(self::url( MailPress_users, array_merge( array('author'=>$author), $url_parms) ));
 261          }
 262  //    date
 263  
 264  ?>
 265      <tr id="user-<?php echo $id; ?>" class='<?php echo $row_class; ?>'>
 266  <?php
 267          $columns = self::get_columns();
 268          $hidden  = self::get_hidden_columns();
 269  
 270          foreach ( $columns as $column_name => $column_display_name ) 
 271          {
 272              $class = "class='$column_name column-$column_name'";
 273  
 274              $style = '';
 275              if ('bounced' == $user->status)         $style .= 'font-style:italic;';
 276              if ('unsubscribed' == $user->status)     $style .= 'font-style:italic;';
 277              if ( in_array($column_name, $hidden) )    $style .= 'display:none;';
 278              $style = ' style="' . $style . '"';
 279  
 280              $attributes = "$class$style";
 281  
 282              switch ($column_name) 
 283              {
 284  
 285                  case 'cb':
 286                      if ( $checkbox ) : 
 287  ?>
 288          <th class='check-column'>
 289              <input type='checkbox' name='checked[]' value='<?php echo $id; ?>'<?php echo $disabled; ?> />
 290          </th>
 291  <?php
 292                       endif;
 293                  break;
 294                  case 'title' :
 295                      $attributes = 'class="username column-username"' . $style;
 296  ?>
 297          <td  <?php echo $attributes ?>>
 298  <?php self::flag_IP(); ?>
 299  <?php    do_action('MailPress_get_icon_users', $mp_user); ?>
 300  <?php if (get_option('show_avatars')) echo get_avatar( $user->email, 32 ); ?>
 301                      <strong>
 302                          <a class='row-title' href='<?php echo $edit_url; ?>' title='<?php printf( __('Edit "%1$s"', MP_TXTDOM) ,$user->email ); ?>'>
 303                              <?php echo $email_display; ?>
 304                          </a>
 305                      </strong>
 306  <?php echo self::get_actions($actions); ?>
 307          </td>
 308  <?php
 309                  break;
 310                  case 'user_name' :
 311  ?>
 312          <td  <?php echo $attributes ?>>
 313              <abbr title="<?php echo esc_attr($user->name); ?>"><?php echo esc_attr($user->name); ?></abbr>
 314          </td>
 315  <?php
 316                  break;
 317                  case 'date' :
 318  
 319                      $t_time = self::get_user_date(__('Y/m/d H:i:s'));
 320                      $h_time = self::human_time_diff(self::get_user_date_raw());
 321  ?>
 322          <td  <?php echo $attributes ?>>
 323              <abbr title="<?php echo $t_time; ?>"><?php echo $h_time; ?></abbr>
 324          </td>
 325  <?php
 326                  break;
 327                  case 'author' :
 328  ?>
 329          <td  <?php echo $attributes ?>>    
 330  <?php                 if ($author != 0 && is_numeric($author)) { ?>
 331                  <a href='<?php echo $author_url; ?>' title='<?php printf( __('Users by "%1$s"', MP_TXTDOM), $wp_user->display_name); ?>'><?php echo $wp_user->display_name; ?></a>
 332  <?php                 } else      _e("(unknown)", MP_TXTDOM); ?>
 333          </td>
 334  <?php
 335                  break;
 336                  default:
 337  ?>
 338          <td  <?php echo $attributes ?>>
 339              <?php    do_action('MailPress_users_get_row', $column_name, $user, $url_parms); ?>
 340          </td>
 341  <?php
 342                  break;
 343              }
 344          }
 345  ?>
 346      </tr>
 347  <?php
 348      }
 349  
 350  	public static function user_date( $d = '' ) {
 351          echo self::get_user_date( $d );
 352      }
 353  
 354  	public static function get_user_date( $d = '' ) {
 355          $x = self::get_user_date_raw();
 356          return ( '' == $d ) ? mysql2date( get_option('date_format'), $x) : mysql2date($d, $x);
 357      }
 358  
 359  	public static function get_user_date_raw() {
 360          global $mp_user;
 361          return ( $mp_user->created >= $mp_user->laststatus) ? $mp_user->created : $mp_user->laststatus;
 362      }
 363  
 364  	public static function user_author_IP() {
 365          echo self::get_user_author_IP();
 366      }
 367  
 368  	public static function get_user_author_IP() {
 369          global $mp_user;
 370          $ip = ( '' == $mp_user->laststatus_IP) ? $mp_user->created_IP : $mp_user->laststatus_IP;
 371          return $ip;
 372      }
 373  
 374  	public static function flag_IP() {
 375          echo self::get_flag_IP();
 376      }
 377  
 378  	public static function get_flag_IP() {
 379          global $mp_user;
 380          return (('ZZ' == $mp_user->created_country) || empty($mp_user->created_country)) ? '' : "<img class='flag' alt='" . strtolower($mp_user->created_country) . "' title='" . strtolower($mp_user->created_country) . "' src='" . site_url() . '/' . MP_PATH . 'mp-admin/images/flag/' . strtolower($mp_user->created_country) . ".gif' />\n";
 381      }
 382  
 383  	public static function get_icon_users($mp_user)
 384      {
 385          if ('unsubscribed' != $mp_user->status) return;
 386  ?>
 387              <span class='icon unsubscribed' title="<?php _e('Unsubscribed', MP_TXTDOM); ?>"></span>
 388  <?php
 389      }
 390  
 391  //// Body ////
 392  
 393  	public static function users_restrict($url_parms)
 394      {
 395          global $wpdb;
 396          $list = array();
 397  
 398          $query = "SELECT DISTINCT UPPER(SUBSTRING(email, 1, 1)) as letter FROM $wpdb->mp_users ORDER BY 1;";
 399          $letters = $wpdb->get_results( $query );
 400  
 401          if ($letters) $list[-1] = __('Starting with...', MP_TXTDOM);
 402          foreach ($letters as $letter) $list[$letter->letter] = $letter->letter;
 403  
 404          $selected = (isset($url_parms['startwith'])) ? $url_parms['startwith'] : -1;
 405  
 406          echo "<select id='letters_dropdown' class='postform' name='startwith'>\n";
 407          self::select_option($list, $selected);
 408          echo "</select>\n";
 409      }
 410  }


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