[ Index ]

MailPress 544

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

title

Body

[close]

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

   1  <?php
   2  class MP_AdminPage extends MP_adminpage_
   3  {
   4      const screen         = 'mailpress_user';
   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          if (!empty($_REQUEST['action'])) $action = $_REQUEST['action'];
  14          if (!isset($action)) return;
  15  
  16          if (isset($_GET['id'])) $id = $_GET['id'];
  17  
  18          $list_url = self::url(MailPress_users, self::get_url_parms());
  19  
  20          switch($action) 
  21          {
  22              case 'activate' :
  23                  if (MP_User::set_status($id, 'active'))  $list_url .= '&activated=1';
  24                  self::mp_redirect($list_url);
  25              break;
  26              case 'deactivate' :
  27                  if (MP_User::set_status($id, 'waiting'))    $list_url .= '&deactivated=1';
  28                  self::mp_redirect($list_url);
  29              break;
  30              case 'unbounce' :
  31                  if (MP_User::set_status($id, 'waiting'))
  32                  {
  33                      MP_User_meta::delete($id, '_MailPress_bounce_handling');
  34                      $list_url .= '&unbounced=1';
  35                  }
  36                  self::mp_redirect($list_url);
  37              break;
  38              case 'delete' :
  39                  if (MP_User::set_status($id, 'delete'))    $list_url .= '&deleted=1';
  40                  self::mp_redirect($list_url);
  41              break;
  42  
  43              case 'save' :
  44                  $id = (int) $_POST['id'];
  45  
  46                  if ($_POST['mp_user_name'] != $_POST['mp_user_old_name'])
  47                  {
  48                      MP_User::update_name($id, $_POST['mp_user_name']);
  49                  }
  50  
  51                  switch (true)
  52                  {
  53                      case isset($_POST['addmeta']) :
  54                          MP_User_meta::add_meta($id);
  55                      break;
  56                      case isset($_POST['usermeta']) :
  57                          foreach ($_POST['usermeta'] as $meta_id => $meta)
  58                          {
  59                              $meta_key = $meta['key'];
  60                              $meta_value = $meta['value'];
  61                              MP_User_meta::update_by_id($meta_id , $meta_key, $meta_value);
  62                          }
  63                      break;
  64                      case isset($_POST['deletemeta']) :
  65                          foreach ($_POST['deletemeta'] as $meta_id => $x)
  66                              MP_User_meta::delete_by_id( $meta_id );
  67                      break;
  68                  }
  69  
  70                  // what else ?
  71                  do_action('MailPress_update_meta_boxes_user');
  72  
  73                  $parm = "&saved=1";
  74  
  75                  $url = MailPress_user;
  76                  $url .= "$parm&id=$id";
  77                  self::mp_redirect($url);
  78              break;
  79          } 
  80      }
  81  
  82  ////  Title  ////
  83  
  84  	public static function title() { global $title; $title = __('MailPress User', MP_TXTDOM); }
  85  
  86  ////  Styles  ////
  87  
  88  	public static function print_styles($styles = array()) 
  89      {
  90          wp_register_style (self::screen,     '/' . MP_PATH . 'mp-admin/css/user.css',     array('thickbox') );
  91  
  92          $styles[] = self::screen;
  93          parent::print_styles($styles);
  94      }
  95  
  96  ////  Scripts  ////
  97  
  98  	public static function print_scripts($scripts = array()) 
  99      {
 100          // google map
 101              wp_register_script( 'google-map',    'http://maps.googleapis.com/maps/api/js?sensor=false', false, false, 1);
 102  
 103          // mp-gmap3
 104              wp_register_script( 'mp-gmap3',    '/' . MP_PATH . 'mp-includes/js/mp_gmap3.js', array('google-map', 'schedule'), false, 1);
 105              wp_localize_script( 'mp-gmap3',     'mp_gmapL10n', array(
 106                  'id'        => $_GET['id'],
 107                  'type'    => 'mp_user',
 108                  'url'        => site_url() . '/' . MP_PATH . 'mp-admin/images/',
 109                  'ajaxurl'    => MP_Action_url,
 110                  'center'    => esc_js(__('Center', MP_TXTDOM)),
 111                  'changemap'    => esc_js(__('Change map', MP_TXTDOM))
 112              ));
 113  
 114              $deps[] = 'mp-gmap3';
 115  
 116          wp_register_script( 'mp-ajax-response',     '/' . MP_PATH . 'mp-includes/js/mp_ajax_response.js', array('jquery'), false, 1);
 117          wp_localize_script( 'mp-ajax-response',     'wpAjax', array(     
 118              'noPerm' => __('Update database failed', MP_TXTDOM), 
 119              'broken' => __('An unidentified error has occurred.'), 
 120              'l10n_print_after' => 'try{convertEntities(wpAjax);}catch(e){};' 
 121          ));
 122          $deps[] = 'jquery-ui-tabs';
 123  
 124          wp_register_script( 'mp-lists',         '/' . MP_PATH . 'mp-includes/js/mp_lists.js', array('mp-ajax-response'), false, 1);
 125          wp_localize_script( 'mp-lists',         'wpListL10n', array(
 126              'url' => MP_Action_url
 127          ));
 128          $deps[] = 'mp-lists';
 129          $deps[] = 'postbox';
 130  
 131          wp_register_script( 'mp-thickbox',         '/' . MP_PATH . 'mp-includes/js/mp_thickbox.js', array('thickbox'), false, 1);
 132          $deps[] = 'mp-thickbox';
 133  
 134          wp_register_script( self::screen,         '/' . MP_PATH . 'mp-admin/js/user.js', $deps, false, 1);
 135          wp_localize_script( self::screen,         'MP_AdminPageL10n',  array(
 136              'screen' => self::screen
 137          ));
 138  
 139          $scripts[] = self::screen;
 140          parent::print_scripts($scripts);
 141      }
 142  
 143  ////  Metaboxes  ////
 144  
 145  	public static function screen_meta() 
 146      {
 147          $id = (isset($_GET['id'])) ? $_GET['id'] : 0;
 148          add_meta_box('submitdiv',         __('Save', MP_TXTDOM), array(__CLASS__, 'meta_box_submit'), self::screen, 'side', 'core');
 149  
 150          do_action('MailPress_add_meta_boxes_user', $id, self::screen);
 151  
 152          if ( current_user_can('MailPress_user_custom_fields') )
 153              add_meta_box('customfieldsdiv',     __('Custom Fields'),     array(__CLASS__, 'meta_box_customfields'), self::screen, 'normal', 'core');
 154          else
 155          {
 156              if ($id)
 157              {
 158                  $metas = MP_User_meta::get($id);
 159                  if ($metas) 
 160                  {
 161                      if (!is_array($metas)) $metas = array($metas);
 162                      foreach ($metas as $meta)
 163                      {
 164                          if ($meta->meta_key[0] == '_') continue;
 165                          add_meta_box('customfieldsdiv',     __('Custom Fields'),     array(__CLASS__, 'meta_box_browse_customfields'), self::screen, 'normal', 'core');
 166                          break;
 167                      }
 168                  }
 169              }
 170          }
 171  
 172          add_meta_box('IP_info', __('IP info', MP_TXTDOM), array(__CLASS__, 'meta_box_IP_info'), self::screen, 'side', 'core');
 173  
 174          parent::screen_meta();
 175      }
 176  /**/
 177  	public static function meta_box_submit($mp_user) 
 178      {
 179          $url_parms     = self::get_url_parms();
 180          if (current_user_can('MailPress_delete_users'))     $delete_url =   esc_url(self::url(MailPress_user . "&amp;action=delete&amp;id=$mp_user->id",   $url_parms));
 181  
 182          $unbounce_url = esc_url(self::url(MailPress_user . "&amp;action=unbounce&amp;id=$mp_user->id", $url_parms));
 183          $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), $mp_user->id )) . "'));\"";
 184          if ('bounced' == $mp_user->status) $unbounce = "<a class='button button-highlighted' style='float:left;min-width:80px;text-align:center;' href='$unbounce_url' $unbounce_click>" . __('Unbounce', MP_TXTDOM) . "</a>";
 185  
 186          if (class_exists('MailPress_tracking'))
 187          {
 188              $tracking_url = esc_url(self::url(MailPress_tracking_u . "&amp;id=$mp_user->id"));
 189              $tracking = "<a class='button preview' href='$tracking_url'>" . __('Tracking', MP_TXTDOM) . "</a>";
 190          }
 191  ?>
 192  <div class="submitbox" id="submitpost">
 193      <div id="minor-publishing">
 194          <div id="minor-publishing-actions">
 195                 <span id='unbounce'><?php if (isset($unbounce)) echo $unbounce; ?></span>
 196              <span id='tracking'><?php if (isset($tracking)) echo $tracking; ?></span>
 197          </div>
 198          <div class="clear"><br /><br /><br /><br /><br /></div>
 199      </div>
 200      <div id="major-publishing-actions">
 201          <div id="delete-action">
 202  <?php     if ($delete_url) : ?>
 203              <a class='submitdelete' href='<?php echo $delete_url ?>' onclick="return (confirm('<?php echo(esc_js(sprintf( __("You are about to delete this MailPress user '%s'\n  'Cancel' to stop, 'OK' to delete.", MP_TXTDOM), $mp_user->id ))); ?>'));">
 204                  <?php _e('Delete', MP_TXTDOM); ?>
 205              </a>
 206  <?php        endif; ?>
 207          </div>
 208          <div id="publishing-action">
 209              <input id='publish' type="submit" name='save' class='button-primary' value="<?php _e('Save', MP_TXTDOM); ?>" />
 210          </div>
 211      <div class="clear"></div>
 212      </div>
 213  </div>
 214  <?php
 215      }
 216  /**/
 217  	public static function meta_box_browse_customfields($mp_user)
 218      {
 219  ?>
 220  <div id="user-import">
 221  <?php
 222          $header = true;
 223          $metas = MP_User_meta::get($mp_user->id);
 224  
 225          if ($metas)
 226          {
 227              if (!is_array($metas)) $metas = array($metas);
 228  
 229              foreach ($metas as $meta)
 230              {
 231                  if ($meta->meta_key[0] == '_') continue;
 232      
 233                  if ($header)
 234                  {
 235                      $header = false;
 236  ?>
 237      <table class='form-table'>
 238          <thead>
 239              <tr>
 240                  <td style='border-bottom:none;padding:0px;width:20px;'>
 241                  </td>
 242                  <td style='border-bottom:none;padding:0px;'>
 243                      <b><?php _e('Key') ?></b>
 244                  </td>
 245                  <td style='border-bottom:none;padding:0px;'>
 246                      <b><?php _e('Value') ?></b>
 247                  </td>
 248              </tr>
 249          </thead>
 250          <tbody>
 251  <?php
 252                  }
 253  ?>
 254              <tr>
 255                  <td style='border-bottom:none;padding:0px;width:20px;'></td>
 256                  <td style='border-bottom:none;line-height:0.8em;padding:0px;'>
 257                      <input style='padding:3px;margin:0 10px 0 0;width:250px;' type='text' disabled='disabled' value="<?php echo esc_attr($meta->meta_key); ?>" />
 258                  </td>
 259                  <td style='border-bottom:none;line-height:0.8em;padding:0px;'>
 260                      <input style='padding:3px;margin:0 10px 0 0;width:250px;' type='text' disabled='disabled' value="<?php echo esc_attr($meta->meta_value); ?>" />
 261                  </td>
 262              </tr>
 263  <?php
 264              }
 265          }
 266      
 267          if ($header)     _e('No meta data', MP_TXTDOM);
 268          else
 269          {
 270  ?>
 271              <tr>
 272                  <td style='border-bottom:none;padding:0px;width:20px;'>&#160;</td>
 273                  <td style='border-bottom:none;padding:0px;width:20px;'></td>
 274                  <td style='border-bottom:none;padding:0px;width:20px;'></td>
 275              </tr>
 276          </tbody>
 277      </table>
 278  <?php
 279          }
 280  ?>
 281  </div>
 282  <?php
 283      }
 284  /**/
 285  	public static function meta_box_customfields($mp_user)
 286      {
 287  ?>
 288  <div id='postcustomstuff'>
 289      <div id='ajax-response'></div>
 290  <?php
 291          $metadata = MP_User_meta::has($mp_user->id);
 292          $count = 0;
 293          if ( !$metadata ) : $metadata = array(); 
 294  ?>
 295      <table id='list-table' style='display: none;'>
 296          <thead>
 297              <tr>
 298                  <th class='left'><?php _e( 'Name' ); ?></th>
 299                  <th><?php _e( 'Value' ); ?></th>
 300              </tr>
 301          </thead>
 302          <tbody id='the-list' class='list:usermeta'>
 303              <tr><td></td></tr>
 304          </tbody>
 305      </table>
 306  <?php else : ?>
 307      <table id='list-table'>
 308          <thead>
 309              <tr>
 310                  <th class='left'><?php _e( 'Name' ) ?></th>
 311                  <th><?php _e( 'Value' ) ?></th>
 312              </tr>
 313          </thead>
 314          <tbody id='the-list' class='list:usermeta'>
 315  <?php foreach ( $metadata as $entry ) echo self::meta_box_customfield_row( $entry, $count ); ?>
 316          </tbody>
 317      </table>
 318  <?php endif; ?>
 319  <?php
 320          global $wpdb;
 321          $keys = $wpdb->get_col( "SELECT meta_key FROM $wpdb->mp_usermeta GROUP BY meta_key ORDER BY meta_key ASC LIMIT 30" );
 322          foreach ($keys as $k => $v)
 323          {
 324              if ($keys[$k][0] == '_') unset($keys[$k]);
 325          }
 326  ?>
 327      <p>
 328          <strong>
 329              <?php _e( 'Add New Custom Field:' ) ?>
 330          </strong>
 331      </p>
 332      <table id='newmeta'>
 333          <thead>
 334              <tr>
 335                  <th class='left'>
 336                      <label for='metakeyselect'>
 337                          <?php _e( 'Name' ) ?>
 338                      </label>
 339                  </th>
 340                  <th>
 341                      <label for='metavalue'>
 342                          <?php _e( 'Value' ) ?>
 343                      </label>
 344                  </th>
 345              </tr>
 346          </thead>
 347          <tbody>
 348              <tr>
 349                  <td id='newmetaleft' class='left'>
 350  <?php 
 351          if ( $keys ) 
 352          { 
 353  ?>
 354                      <select id='metakeyselect' name='metakeyselect' tabindex='7'>
 355                          <option value="#NONE#"><?php _e( '- Select -' ); ?></option>
 356  <?php
 357              foreach ( $keys as $key ) 
 358              {
 359                  $key = esc_attr($key);
 360                  echo "\n<option value=\"$key\">$key</option>";
 361              }
 362  ?>
 363                      </select>
 364                      <input class='hide-if-js' type='text' id='metakeyinput' name='metakeyinput' tabindex='7' value='' />
 365                      <a href='#postcustomstuff' class='hide-if-no-js' onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
 366                      <span id='enternew'><?php _e('Enter new'); ?></span>
 367                      <span id='cancelnew' class='hidden'><?php _e('Cancel'); ?></span></a>
 368  <?php 
 369          } 
 370          else 
 371          { 
 372  ?>
 373                      <input type='text' id='metakeyinput' name='metakeyinput' tabindex='7' value='' />
 374  <?php 
 375          } 
 376  ?>
 377                  </td>
 378                  <td>
 379                      <textarea id='metavalue' name='metavalue' rows='2' cols='25' tabindex='8'></textarea>
 380                  </td>
 381              </tr>
 382              <tr>
 383                  <td colspan='2'>
 384                      <div  class='submit'>
 385                          <input type='submit' id='addmetasub' name='addmeta' class='add:the-list:newmeta button' tabindex='9' value="<?php _e( 'Add Custom Field' ) ?>" />
 386                          <?php wp_nonce_field( 'add-usermeta', '_ajax_nonce', false ); ?>
 387                      </div>
 388                  </td>
 389              </tr>
 390          </tbody>
 391      </table>
 392  </div>
 393  <p><?php _e('Custom fields can be used to add extra metadata to a user that you can <a href="http://www.mailpress.org" target="_blank">use in your mail</a>.', MP_TXTDOM); ?></p>
 394  <?php
 395      }
 396  
 397  	public static function meta_box_customfield_row( $entry, &$count )
 398      {
 399          if ('_' == $entry['meta_key'] { 0 } ) return;
 400  
 401          static $update_nonce = false;
 402          if ( !$update_nonce ) $update_nonce = wp_create_nonce( 'add-usermeta' );
 403  
 404          $r = '';
 405          ++ $count;
 406  
 407          if ( $count % 2 )    $style = 'alternate';
 408          else            $style = '';
 409      
 410          $entry['meta_key']     = esc_attr($entry['meta_key']);
 411          $entry['meta_value']     = esc_attr($entry['meta_value']); // using a <textarea />
 412          $entry['meta_id']     = (int) $entry['meta_id'];
 413  
 414          $delete_nonce         = wp_create_nonce( 'delete-usermeta_' . $entry['meta_id'] );
 415  
 416          $r .= "
 417              <tr id='usermeta-{$entry['meta_id']}' class='$style'>
 418                  <td class='left'>
 419                      <label class='hidden' for='usermeta[{$entry['meta_id']}][key]'>
 420  " . __( 'Key' ) . "
 421                      </label>
 422                      <input name='usermeta[{$entry['meta_id']}][key]' id='usermeta[{$entry['meta_id']}][key]' tabindex='6' type='text' size='20' value=\"" . esc_attr($entry['meta_key']) . "\" />
 423                      <div class='submit'>
 424                          <input name='deleteusermeta[{$entry['meta_id']}]' type='submit' class='delete:the-list:usermeta-{$entry['meta_id']}::_ajax_nonce=$delete_nonce deleteusermeta button' tabindex='6' value='" . esc_attr(__( 'Delete' )) . "' />
 425                          <input name='updateusermeta' type='submit' tabindex='6' value='" . esc_attr(__( 'Update' )) . "' class='add:the-list:usermeta-{$entry['meta_id']}::_ajax_nonce=$update_nonce updateusermeta button' />
 426                      </div>
 427  " . wp_nonce_field( 'change-usermeta', '_ajax_nonce', false, false ) . "
 428                  </td>
 429                  <td>
 430                      <label class='hidden' for='usermeta[{$entry['meta_id']}][value]'>
 431  " . __( 'Value' ) . "
 432                      </label>
 433                      <textarea name='usermeta[{$entry['meta_id']}][value]' id='usermeta[{$entry['meta_id']}][value]' tabindex='6' rows='2' cols='30'>" . esc_attr($entry['meta_value']) . "</textarea>
 434                  </td>
 435              </tr>
 436              ";
 437          return $r;
 438      }
 439  /**/
 440  	public static function meta_box_IP_info($mp_user)
 441      {
 442      // meta_box_IP_info
 443          $x = false;
 444          $ip = ( '' == $mp_user->laststatus_IP) ? $mp_user->created_IP : $mp_user->laststatus_IP;
 445          $x  = MP_Ip::get_all($ip);
 446  
 447      // meta_box_IP_info_user_settings
 448          $u['meta_box_IP_info_user_settings'] = MP_User_meta::get($mp_user->id, '_MailPress_meta_box_IP_info');
 449          if (!$u['meta_box_IP_info_user_settings']) $u['meta_box_IP_info_user_settings'] = get_user_option('_MailPress_meta_box_IP_info');
 450          $def_lat = (isset($x['geo']['lat']))? $x['geo']['lat'] : 48.8352;
 451          $def_lng = (isset($x['geo']['lng']))? $x['geo']['lng'] : 2.4718;
 452          if (!$u['meta_box_IP_info_user_settings']) $u['meta_box_IP_info_user_settings'] = array('center_lat' => $def_lat, 'center_lng' => $def_lng, 'zoomlevel' => 3, 'maptype' => 'NORMAL');
 453          $u['meta_box_IP_info_user_settings']['prefix'] = 'meta_box_IP_info';
 454  ?>
 455  <script type='text/javascript'>
 456  /* <![CDATA[ */
 457  <?php
 458          $eol = "";
 459          foreach ( $u as $var => $val ) {
 460              echo "var $var = " . self::print_scripts_l10n_val($val);
 461              $eol = ",\n\t\t";
 462          }
 463          echo ";\n";
 464  ?>
 465  /* ]]> */
 466  </script>
 467  <?php
 468          if (isset($x['geo']))
 469          {
 470  ?>
 471  <script type='text/javascript'>
 472  /* <![CDATA[ */
 473  <?php
 474              $m['meta_box_IP_info'] = $x['geo'];
 475              $eol = "";
 476              foreach ( $m as $var => $val ) {
 477                  echo "var $var = " . self::print_scripts_l10n_val($val);
 478                  $eol = ", \n\t\t";
 479              }
 480              echo ";\n";    
 481  ?>
 482  /* ]]> */
 483  </script>
 484          <div id='meta_box_IP_info_map'></div>
 485  <?php         foreach($u['meta_box_IP_info_user_settings'] as $k => $v)
 486              {
 487                      if ('prefix' == $k) continue;
 488  ?>
 489          <input type='hidden' id='meta_box_IP_info_<?php echo $k; ?>' value="<?php echo $v; ?>" />
 490  <?php
 491              }
 492          }
 493  
 494          if (isset($x['html']))
 495          {
 496              echo $x['html'];
 497          }
 498          if (isset($x['provider']))
 499          {
 500              printf("<div><p style='margin:3px;'><i><small>" . '%1$s' . "</small></i></p></div>\n", sprintf(__('ip data provided by %1$s', MP_TXTDOM), sprintf('<a href="%1$s">%2$s</a>', $x['provider']['credit'], $x['provider']['credit'])));
 501          }
 502      }
 503  }


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