[ Index ]

MailPress 544

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

title

Body

[close]

/mp-content/add-ons/ -> MailPress_sync_wordpress_user.php (source)

   1  <?php
   2  if (class_exists('MailPress') && !class_exists('MailPress_sync_wordpress_user'))
   3  {
   4  /*
   5  Plugin Name: MailPress_sync_wordpress_user
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/sync_wordpress_user/
   7  Description: Users : synchronise with WordPress users
   8  Version: 5.4.4
   9  */
  10  
  11  // 3.
  12  
  13  /** for admin plugin pages */
  14  define ('MailPress_page_subscriptions',     'mailpress_subscriptions');
  15  
  16  class MailPress_sync_wordpress_user
  17  {
  18      const option_name = 'MailPress_sync_wordpress_user';
  19      const meta_key    = '_MailPress_sync_wordpress_user';
  20  
  21  	function __construct()
  22      {
  23  // for wordpress hooks
  24  // for plugin
  25          add_action('MailPress_addons_loaded',     array(__CLASS__, 'addons_loaded'));
  26  
  27  // WP user management
  28          $settings = get_option(self::option_name);
  29          if (isset($settings['register_form']))            add_action('register_form', array(__CLASS__, 'register_form'));
  30  
  31          add_action('user_register',                     array(__CLASS__, 'user_register'), 1, 1);
  32          add_action('profile_update',                     array(__CLASS__, 'update'), 1, 1);
  33          add_action('delete_user',                     array(__CLASS__, 'delete'), 1, 1);
  34  
  35          add_action('personal_options_update',             array(__CLASS__, 'profile_update'), 8, 1);
  36          add_action('edit_user_profile_update',             array(__CLASS__, 'profile_update'), 8, 1);
  37  
  38  // MP user management
  39          add_action('MailPress_insert_user',             array(__CLASS__, 'mp_insert_user'), 1, 1);
  40          add_action('MailPress_delete_user',             array(__CLASS__, 'mp_delete_user'), 1, 1);
  41  
  42  // for wp admin
  43          if (is_admin())
  44          {
  45          // for install
  46              register_activation_hook(plugin_basename(__FILE__),     array(__CLASS__, 'install'));
  47          // for link on plugin page
  48              add_filter('plugin_action_links',             array(__CLASS__, 'plugin_action_links'), 10, 2 );
  49          // for role & capabilities
  50              $_tabs = apply_filters('MailPress_settings_tab', array());
  51              if (isset($_tabs['subscriptions']))
  52              {
  53                  add_filter('MailPress_capabilities',         array(__CLASS__, 'capabilities'), 1, 1);
  54                  if (!class_exists('MailPress_roles_and_capabilities')) add_action('MailPress_roles_and_capabilities',     array(__CLASS__, 'roles_and_capabilities'));
  55              }
  56          // for load admin page
  57              add_filter('MailPress_load_admin_page',         array(__CLASS__, 'load_admin_page'), 10, 1);
  58          // for settings
  59              add_action('MailPress_settings_general_forms',     array(__CLASS__, 'settings_general_forms'));
  60              add_action('MailPress_settings_general_update',    array(__CLASS__, 'settings_general_update'));
  61  
  62          // for meta box in user page
  63              add_action('MailPress_add_meta_boxes_user',     array(__CLASS__, 'meta_boxes_user'), 1, 2); 
  64          }
  65      }
  66  
  67  //// Plugin ////
  68  
  69  	public static function addons_loaded()
  70      {
  71      // for role & capabilities
  72          $_tabs = apply_filters('MailPress_settings_tab', array());
  73          if (isset($_tabs['subscriptions']))
  74          {
  75              add_filter('MailPress_capabilities',         array(__CLASS__, 'capabilities'), 1, 1);
  76              if (!class_exists('MailPress_roles_and_capabilities')) add_action('MailPress_roles_and_capabilities',     array(__CLASS__, 'roles_and_capabilities'));
  77          }
  78      }
  79  
  80  //// WP user management ////
  81  
  82  	public static function register_form()
  83      {
  84          do_action('MailPress_register_form');
  85  ?>
  86      <br /><br />
  87  <?php
  88      }
  89  
  90  	public static function user_register($wp_user_id)
  91      {
  92          $wp_user = self::get_wp_user($wp_user_id);
  93          if ($wp_user) self::sync($wp_user);
  94      }
  95  
  96  	public static function update($wp_user_id)
  97      {
  98          $wp_user = self::get_wp_user($wp_user_id);
  99          if ($wp_user)
 100          {
 101              $oldid = get_user_meta( $wp_user->ID, self::meta_key, true);
 102              if ($oldid)
 103              {
 104                  $oldemail = MP_User::get_email($oldid);
 105                  if ($oldemail == $wp_user->user_email) return true;
 106                  else
 107                  {
 108                      self::sync($wp_user);
 109                      $newid =  MP_User::get_id_by_email($wp_user->user_email);
 110  
 111                      if (apply_filters('MailPress_has_subscriptions', false, $oldid)) do_action('MailPress_sync_subscriptions', $oldid, $newid);
 112                      $count = self::count_emails($oldemail);
 113                      if (0 == $count)                            MP_User::delete($oldid);
 114                  }
 115              }
 116              else
 117              {
 118                  self::user_register($wp_user_id);
 119              }
 120          }
 121      }
 122  
 123  	public static function delete($wp_user_id)
 124      {
 125          $wp_user = self::get_wp_user($wp_user_id);
 126          if ($wp_user) 
 127          {
 128              $id = get_user_meta( $wp_user->ID, self::meta_key, true);
 129              if ($id)
 130              {
 131                  $email = MP_User::get_email($id);
 132                  if ($email)
 133                  {
 134                      $count = self::count_emails($email);
 135                      if ((1 == $count) && !apply_filters('MailPress_has_subscriptions', false, $id)) MP_User::delete($id);
 136                  }
 137              }
 138          }
 139          return true;
 140      }
 141  
 142  	public static function profile_update($wid)
 143      {
 144          $id = get_user_meta( $wid, self::meta_key, true );
 145          if ($id)
 146          {
 147              $mp_user = MP_User::get($id);
 148              if (stripslashes($_POST['display_name']) != $mp_user->name)
 149              {
 150                  MP_User::update_name($id, $_POST['display_name']);
 151              }
 152              if ($_POST['email'] != $mp_user->email)
 153              {
 154                  $wp_user = self::get_wp_user($wid);
 155                  if (is_email($_POST['email'])) self::sync($wp_user);
 156              }
 157          }
 158          else
 159          {
 160              $wp_user = self::get_wp_user($wid);
 161              self::sync($wp_user);
 162          }
 163      }
 164  
 165  //// MP user management ////
 166  
 167  	public static function mp_insert_user($mp_user_id)
 168      {
 169          $mp_email    = MP_User::get_email($mp_user_id);
 170          $wp_users      = self::get_wp_users_by_email($mp_email);
 171          if (is_array($wp_users)) foreach ($wp_users as $wp_user) update_user_meta( $wp_user->ID, self::meta_key, $mp_user_id);
 172      }
 173  
 174  	public static function mp_delete_user($mp_user_id)
 175      {
 176          global $wpdb;
 177          $results = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = %s ;", self::meta_key, $mp_user_id ) );
 178      }
 179  
 180  // generic functions
 181  
 182  	public static function sync($wp_user)
 183      {
 184  
 185   // Already a MailPress user ?
 186  
 187          $id = get_user_meta( $wp_user->ID, self::meta_key, true);
 188          if ($id)
 189          {
 190              if (MP_User::get_email($id) == $wp_user->user_email) return true;
 191          }
 192  
 193  // Mail already in MailPress table ?
 194  
 195          $id =  MP_User::get_id_by_email($wp_user->user_email);
 196          if ($id) 
 197          {
 198              update_user_meta( $wp_user->ID, self::meta_key, $id );
 199              MP_User::set_status($id, 'active');
 200              return true;                                          
 201          }
 202  
 203  // so insert !
 204  
 205          return self::insert($wp_user);
 206      }
 207  
 208  	public static function insert($wp_user, $type = 'activate')
 209      {
 210          if ( !is_email($wp_user->user_email) )    return false; // not an email
 211  
 212          if ('activate' == $type) 
 213          {
 214              if (!MP_User::insert($wp_user->user_email, $wp_user->display_name, array('status' => 'active')))    return false; // user not inserted
 215          }
 216          else
 217          {
 218              $return = MP_User::add($wp_user->user_email, $wp_user->display_name);
 219              if (!$return['result'])                 return false; // user not inserted
 220          }
 221          $id = MP_User::get_id_by_email($wp_user->user_email);
 222          update_user_meta( $wp_user->ID, self::meta_key, $id );
 223          return true;
 224      }
 225  
 226  	public static function get_wp_user($wp_user_id)
 227      {
 228          global $wpdb;
 229          return $wpdb->get_row($wpdb->prepare("SELECT ID, user_email, display_name FROM $wpdb->users WHERE ID = %d ", $wp_user_id));
 230      }
 231  
 232  	public static function get_wp_users_by_mp_user_id($mp_user_id)
 233      {
 234          global $wpdb;
 235          return $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT ID, user_email, display_name FROM $wpdb->users a, $wpdb->usermeta b WHERE a.ID = b.user_id AND b.meta_key = %s AND b.meta_value = %s ", self::meta_key, $mp_user_id ) );
 236      }
 237  
 238  	public static function get_wp_users_by_email($email)
 239      {
 240          global $wpdb;
 241          $email = trim($email);
 242          if (!is_email($email)) return false;
 243  
 244          return $wpdb->get_results( $wpdb->prepare( "SELECT ID, user_email, display_name FROM $wpdb->users WHERE user_email = %s ;", $email ) );
 245      }
 246  
 247  	public static function count_emails($email)
 248      {
 249          global $wpdb;
 250          return $wpdb->get_var($wpdb->prepare( "SELECT count(*) FROM $wpdb->users WHERE user_email = %s ;", $email ) );
 251      }
 252  
 253  ////  ADMIN  ////
 254  ////  ADMIN  ////
 255  ////  ADMIN  ////
 256  ////  ADMIN  ////
 257  
 258  // for install
 259  	public static function install()
 260      {
 261          $users = get_users();
 262          if ($users) foreach($users as $user) self::sync($user);
 263      }
 264  
 265  // for link on plugin page
 266  	public static function plugin_action_links($links, $file)
 267      {
 268          return MailPress::plugin_links($links, $file, plugin_basename(__FILE__), 'general');
 269      }
 270  
 271  // for role & capabilities
 272  	public static function capabilities($capabilities) 
 273      {
 274          $pu = ( current_user_can('edit_users') ) ? 'users.php' : 'profile.php';
 275  
 276          $capabilities['MailPress_manage_subscriptions'] = array(    'name'    => __('Your Subscriptions', MP_TXTDOM), 
 277                                                  'group'    => 'admin', 
 278                                                  'menu'    => 33, 
 279      
 280                                                  'parent'    => $pu, 
 281                                                  'page_title'=> __('MailPress - Subscriptions', MP_TXTDOM), 
 282                                                  'menu_title'=> __('Your Subscriptions', MP_TXTDOM), 
 283                                                  'page'    => MailPress_page_subscriptions, 
 284                                                  'func'    => array('MP_AdminPage', 'body')
 285                                              );
 286          return $capabilities;
 287      }
 288  
 289  	public static function roles_and_capabilities()
 290      {
 291          global $wp_roles;
 292          foreach($wp_roles->role_names as $role => $name)
 293          {
 294              if ('administrator' == $role) continue;
 295              $r = get_role($role);
 296              $r->add_cap('MailPress_manage_subscriptions');
 297          }
 298      }
 299  
 300  // for load admin page
 301  	public static function load_admin_page($hub)
 302      {
 303          $hub[MailPress_page_subscriptions] = 'subscriptions';
 304          return $hub;
 305      }
 306  
 307  // for settings subscriptions
 308  	public static function settings_general_forms()
 309      {
 310          $sync_wordpress_user = get_option(self::option_name);
 311  ?>
 312  <tr>
 313      <th><label for='sync_wordpress_user_register_form'><?php _e('Registration Form subscriptions', MP_TXTDOM); ?></label></th>
 314      <td>
 315          <input type='checkbox' name='sync_wordpress_user[register_form]' id='sync_wordpress_user_register_form'<?php if (isset($sync_wordpress_user['register_form'])) checked($sync_wordpress_user['register_form'], 'on'); ?> />
 316      </td>
 317  </tr>
 318  <?php
 319      }
 320  
 321  	public static function settings_general_update()
 322      {
 323          update_option(self::option_name, $_POST['sync_wordpress_user']);
 324      }
 325  
 326  // for meta box in user page
 327  	public static function meta_boxes_user($mp_user_id, $mp_screen)
 328      {
 329          add_meta_box('mp_user_syncwordpress', __('WP User sync', MP_TXTDOM) , array(__CLASS__, 'meta_box'), MP_AdminPage::screen, 'side', 'core');
 330      }
 331  
 332  	public static function meta_box($mp_user)
 333      {
 334          $wp_users = self::get_wp_users_by_mp_user_id( $mp_user->id );
 335          if ($wp_users)
 336          {
 337              $count = 0;
 338              $total = count($wp_users);
 339              $separator = " style='border-bottom:1px solid #DFDFDF;'";
 340  ?>
 341  <div id="user-syncwordpress">
 342      <table class='form-table'>
 343  <?php
 344  
 345              foreach ($wp_users as $wp_user)
 346              {
 347                  $count++;
 348                  if ($total == $count) $separator = '';
 349  
 350                  $wp_user = get_userdata($wp_user->ID);
 351                  if (empty($wp_user->first_name) && empty($wp_user->last_name) && empty($wp_user->nickname)) continue;
 352  ?>
 353          <tr><td style='padding:0px;' colspan='2'><strong><?php printf(__('WP User # %1$s', MP_TXTDOM), $wp_user->ID); ?></strong></td></tr>
 354  <?php if (isset($wp_user->first_name)) : ?>
 355          <tr><td style='padding:0px;'><?php _e('First name'); ?></td><td style='padding:0px;'><?php echo $wp_user->first_name; ?></td></tr>
 356  <?php endif; ?>
 357  <?php if (isset($wp_user->last_name)) : ?>
 358          <tr><td style='padding:0px;'><?php _e('Last name');  ?></td><td style='padding:0px;'><?php echo $wp_user->last_name; ?></td></tr>
 359  <?php endif; ?>
 360  <?php if (isset($wp_user->nickname)) : ?>
 361          <tr<?php echo $separator; ?>><td style='padding:0px;'><?php _e('Nickname');   ?></td><td style='padding:0px;'><?php echo $wp_user->nickname; ?></td></tr>
 362  <?php endif; ?>
 363  <?php
 364              }
 365  ?>
 366      </table>
 367  </div>
 368  <?php
 369          }
 370          else 
 371              printf(__('%1$s is not a WordPress user', MP_TXTDOM), $mp_user->email);
 372      }
 373  }
 374  new MailPress_sync_wordpress_user();
 375  }


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