[ Index ]

MailPress 544

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

title

Body

[close]

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

   1  <?php
   2  class MP_AdminPage extends MP_adminpage_list_
   3  {
   4      const screen         = MailPress_page_mailinglists;
   5      const capability     = 'MailPress_manage_mailinglists';
   6      const help_url        = 'http://blog.mailpress.org/tutorials/add-ons/mailinglist/';
   7      const file            = __FILE__;
   8  
   9      const taxonomy         = MailPress_mailinglist::taxonomy;
  10  
  11      const add_form_id     = 'add';
  12      const list_id         = 'the-list';
  13      const tr_prefix_id     = 'mlnglst';
  14  
  15  ////  Redirect  ////
  16  
  17  	public static function redirect() 
  18      {
  19          if     ( !empty($_REQUEST['action'])  && ($_REQUEST['action']  != -1))    $action = $_REQUEST['action'];
  20          elseif ( !empty($_REQUEST['action2']) && ($_REQUEST['action2'] != -1) )    $action = $_REQUEST['action2'];
  21          if (!isset($action)) return;
  22  
  23          $url_parms = self::get_url_parms(array('s', 'paged', 'id'));
  24          $checked    = (isset($_GET['checked'])) ? $_GET['checked'] : array();
  25  
  26          $count    = str_replace('bulk-', '', $action);
  27          $count     .= 'd';
  28          $$count    = 0;
  29  
  30          switch($action) 
  31          {
  32              case 'bulk-delete' :
  33                  foreach($checked as $id)
  34                  {
  35                      if ( $id == get_option(MailPress_mailinglist::option_name_default) )
  36                          wp_die(sprintf(__("Can&#8217;t delete the <strong>%s</strong> mailing list: this is the default one", MP_TXTDOM), MP_Mailinglist::get_name($id)));
  37  
  38                      if (MP_Mailinglist::delete($id)) $$count++;
  39                  }
  40  
  41                  if ($$count) $url_parms[$count] = $$count;
  42                  $url_parms['message'] = ($$count <= 1) ? 3 : 4;
  43                  self::mp_redirect( self::url(MailPress_mailinglists, $url_parms) );
  44              break;
  45  
  46              case 'add':
  47                  $e = MP_Mailinglist::insert($_POST);
  48                  $url_parms['message'] = ( $e && !is_wp_error( $e ) ) ? 1 : 91;
  49                  unset($url_parms['s']);
  50                  self::mp_redirect( self::url(MailPress_mailinglists, $url_parms) );
  51              break;
  52              case 'edited':
  53                  unset($_GET['action']);
  54                  if (!isset($_POST['cancel'])) 
  55                  {
  56                      $e = MP_Mailinglist::insert($_POST);
  57                      $url_parms['message'] = ( $e && !is_wp_error( $e ) ) ? 2 : 92 ;
  58                  }
  59                  unset($url_parms['id']);
  60                  self::mp_redirect( self::url(MailPress_mailinglists, $url_parms) );
  61              break;
  62              case 'delete':
  63                  if ( $url_parms['id'] == get_option(MailPress_mailinglist::option_name_default) )
  64                      wp_die(sprintf(__("Can&#8217;t delete the <strong>%s</strong> mailing list: this is the default one", MP_TXTDOM), MP_Mailinglist::get_name($id)));
  65  
  66                  MP_Mailinglist::delete($url_parms['id']);
  67                  unset($url_parms['id']);
  68  
  69                  $url_parms['message'] = 3;
  70                  self::mp_redirect( self::url(MailPress_mailinglists, $url_parms) );
  71              break;
  72          }
  73      }
  74  
  75  ////  Title  ////
  76  
  77  	public static function title() { if (isset($_GET['id'])) { global $title; $title = __('Edit Mailinglist', MP_TXTDOM); } }
  78  
  79  //// Scripts ////
  80  
  81  	public static function print_scripts($scripts = array()) 
  82      {
  83          wp_register_script( 'mp-ajax-response',    '/' . MP_PATH . 'mp-includes/js/mp_ajax_response.js', array('jquery'), false, 1);
  84          wp_localize_script( 'mp-ajax-response',     'wpAjax', array( 
  85              'noPerm' => __('An unidentified error has occurred.'), 
  86              'broken' => __('An unidentified error has occurred.'), 
  87              'l10n_print_after' => 'try{convertEntities(wpAjax);}catch(e){};' 
  88          ));
  89  
  90          wp_register_script( 'mp-lists',         '/' . MP_PATH . 'mp-includes/js/mp_lists.js', array('mp-ajax-response'), false, 1);
  91          wp_localize_script( 'mp-lists',         'wpListL10n', array( 
  92              'url' => MP_Action_url
  93          ));
  94  
  95          wp_register_script( self::screen,         '/' . MP_PATH . 'mp-includes/js/mp_taxonomy.js', array('mp-lists'), false, 1);
  96          wp_localize_script( self::screen,         'MP_AdminPageL10n', array(
  97              'pending' => __('%i% pending'), 
  98              'screen'  => self::screen,
  99              'list_id' => self::list_id,
 100              'add_form_id' => self::add_form_id,
 101              'tr_prefix_id' => self::tr_prefix_id,
 102              'l10n_print_after' => 'try{convertEntities(MP_AdminPageL10n);}catch(e){};' 
 103          ));
 104  
 105          $scripts[] = self::screen;
 106          parent::print_scripts($scripts);
 107      }
 108  
 109  //// Columns ////
 110  
 111  	public static function get_columns() 
 112      {
 113          $columns = array(    'cb'        => '<input type="checkbox" />',
 114                      'name'     => __('Name', MP_TXTDOM),
 115                      'desc'    => __('Description', MP_TXTDOM),
 116                      'allowed'    => __('Allowed', MP_TXTDOM),
 117                      'num'     => __('MP users', MP_TXTDOM));
 118          return $columns;
 119      }
 120  
 121  //// List ////
 122  
 123  	public static function get_list($args)
 124      {
 125          extract($args);
 126  
 127          $url_parms = self::get_url_parms(array('s', 'paged'));
 128  
 129          $_args = array('offset' => ($start - 1) * $_per_page, 'number' => $_per_page, 'hide_empty' => 0);
 130          if (isset($url_parms['s'])) $_args['search'] = $url_parms['s'];
 131  
 132          $_terms = MP_Mailinglist::get_all($_args);
 133          if (empty($_terms)) return false;
 134  
 135          $children = _get_term_hierarchy(self::taxonomy);
 136  
 137          foreach($_terms as $_term) { $_term->_found = true; $terms[$_term->term_id] = $_term; }
 138          unset($_terms, $_term);
 139  
 140          foreach($terms as $term)
 141          {
 142              if (!$my_parent = $term->parent) continue; 
 143  
 144              do {  
 145                  if (!isset($terms[$my_parent])) $terms[$my_parent] = get_term( $my_parent, self::taxonomy );  
 146                  $my_parent = $terms[$my_parent]->parent;  
 147              } while ( $my_parent );
 148          }
 149          echo self::_get_list($url_parms, $terms, $children);
 150      }
 151  
 152  	public static function _get_list($url_parms, $mailinglists, &$children, $level = 0, $parent = 0 )
 153      {
 154          $out = ''; 
 155          foreach ( $mailinglists as $key => $mailinglist )  
 156          { 
 157              if ( $parent == $mailinglist->parent )  
 158              { 
 159                  $out .= self::get_row( $mailinglist, $url_parms, $level ); 
 160                  unset( $mailinglists[ $key ] ); 
 161                  if ( isset($children[$mailinglist->term_id]) ) 
 162                  $out .= self::_get_list( $url_parms, $mailinglists, $children, $level + 1, $mailinglist->term_id );
 163              } 
 164          } 
 165          return $out;
 166      }
 167  
 168  ////  Row  ////
 169  
 170  	public static function get_row( $mailinglist, $url_parms, $level, $name_override = false ) 
 171      {
 172          global $mp_subscriptions;
 173  
 174          static $row_class = '';
 175  
 176          $mailinglist = MP_Mailinglist::get( $mailinglist );
 177  
 178          $default_mailinglist_id = get_option( MailPress_mailinglist::option_name_default );
 179          $pad = str_repeat( '&#8212; ', $level );
 180          $name = ( $name_override ) ? $name_override : $pad . ' ' . $mailinglist->name ;
 181  
 182  // url's
 183          $url_parms['action'] = 'edit';
 184          $url_parms['id'] = $mailinglist->term_id;
 185  
 186          $edit_url = esc_url(self::url( MailPress_mailinglists, $url_parms ));
 187          $url_parms['action']    = 'delete';
 188          $delete_url = esc_url(self::url( MailPress_mailinglists, $url_parms,  'delete-mailinglist_' . $mailinglist->term_id ));
 189  // actions
 190          $actions = array();
 191          $actions['edit'] = '<a href="' . $edit_url . '">' . __('Edit') . '</a>';
 192          $actions['delete'] = "<a class='submitdelete' href='$delete_url'>" . __('Delete') . "</a>";
 193  
 194          if ( $default_mailinglist_id == $mailinglist->term_id ) 
 195              unset($actions['delete']);
 196  
 197          $mailinglist->allowed = (isset($mp_subscriptions['display_mailinglists'][$mailinglist->term_id]));
 198  
 199          $row_class = ('alternate' == substr($row_class, 0, 9)) ? '' : 'alternate '; $row_class .= 'iedit';
 200          $tr_style = (isset($mailinglist->_found)) ? '' : " style='background-color:#EBEEEF;'"; 
 201  
 202          $out = '';
 203          $out .= "<tr id='" . self::tr_prefix_id . "-$mailinglist->term_id' class='$row_class'$tr_style>";
 204  
 205          $columns = self::get_columns();
 206          $hidden  = self::get_hidden_columns();
 207  
 208          foreach ( $columns as $column_name => $column_display_name ) 
 209          {
 210              $class = "class='$column_name column-$column_name'";
 211  
 212              $style = '';
 213              if ( in_array($column_name, $hidden) )
 214                  $style = ' style="display:none;"';
 215  
 216              $attributes = "$class$style";
 217  
 218              switch ($column_name) 
 219              {
 220                  case 'cb':
 221                      $out .= "<th class='check-column'>";
 222                      if ( $default_mailinglist_id != $mailinglist->term_id ) {
 223                          $out .= "<input type='checkbox' name='checked[]' value='$mailinglist->term_id' />";
 224                      } else {
 225                          $out .= "&#160;";
 226                      }
 227                      $out .= '</th>';
 228                  break;
 229                  case 'name':
 230                      $out .= '<td ' . $attributes . '><strong><a class="row-title" href="' . $edit_url . '" title="' . esc_attr(sprintf(__('Edit "%s"'), $name)) . '">' . $name . '</a></strong><br />';
 231                      $out .= self::get_actions($actions);
 232                      $out .= '</td>';
 233                   break;
 234                   case 'desc':
 235                       $out .= "<td $attributes>" . stripslashes($mailinglist->description) . "</td>";
 236                   break;
 237                   case 'allowed':
 238                       $out .= "<td $attributes>" . (($mailinglist->allowed) ? __('yes', MP_TXTDOM) : __('no', MP_TXTDOM)) . "</td>";
 239                   break;
 240                  case 'num':
 241                      $mailinglist->count = number_format_i18n( $mailinglist->count );
 242  
 243                      if (current_user_can('MailPress_edit_users')) 
 244                          $mp_users_count = ( $mailinglist->count > 0 ) ? "<a href='" . MailPress_users . "&amp;mailinglist=$mailinglist->term_id'>$mailinglist->count</a>" : $mailinglist->count;
 245                      else
 246                          $mp_users_count =  $mailinglist->count;
 247  
 248                       $attributes = 'class="num column-num"' . $style;
 249                      $out .= "<td $attributes>$mp_users_count</td>\n";
 250                   break;
 251              }
 252          }
 253          $out .= "</tr>\n";
 254  
 255          return $out;
 256      }
 257  }


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