[ Index ]

MailPress 7.1

[ 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: 7.1
   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 general
  59              add_filter( 'MailPress_settings_general_help',    array( __CLASS__, 'settings_general_help' ), 20, 1 );
  60              add_action( 'MailPress_settings_general_update',array( __CLASS__, 'settings_general_update' ) );
  61              add_action( 'MailPress_settings_general_form',    array( __CLASS__, 'settings_general_form' ) );
  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          echo ( "\r\n<br /><br />\r\n" );
  87      }
  88  
  89  	public static function user_register( $wp_user_id )
  90      {
  91          $wp_user = self::get_wp_user( $wp_user_id );
  92          if ( $wp_user ) self::sync( $wp_user );
  93      }
  94  
  95  	public static function update( $wp_user_id )
  96      {
  97          $wp_user = self::get_wp_user( $wp_user_id );
  98          if ( $wp_user )
  99          {
 100              $oldid = get_user_meta( $wp_user->ID, self::meta_key, true );
 101              if ( $oldid )
 102              {
 103                  $oldemail = MP_User::get_email( $oldid );
 104                  if ( $oldemail == $wp_user->user_email ) return true;
 105                  else
 106                  {
 107                      self::sync( $wp_user );
 108                      $newid =  MP_User::get_id_by_email( $wp_user->user_email );
 109  
 110                      if ( apply_filters( 'MailPress_has_subscriptions', false, $oldid ) ) do_action( 'MailPress_sync_subscriptions', $oldid, $newid );
 111                      $count = self::count_emails( $oldemail );
 112                      if ( 0 == $count )                            MP_User::delete( $oldid );
 113                  }
 114              }
 115              else
 116              {
 117                  self::user_register( $wp_user_id );
 118              }
 119          }
 120      }
 121  
 122  	public static function delete( $wp_user_id )
 123      {
 124          $wp_user = self::get_wp_user( $wp_user_id );
 125          if ( $wp_user ) 
 126          {
 127              $id = get_user_meta( $wp_user->ID, self::meta_key, true );
 128              if ( $id )
 129              {
 130                  $email = MP_User::get_email( $id );
 131                  if ( $email )
 132                  {
 133                      $count = self::count_emails( $email );
 134                      if ( ( 1 == $count ) && !apply_filters( 'MailPress_has_subscriptions', false, $id ) ) MP_User::delete( $id );
 135                  }
 136              }
 137          }
 138          return true;
 139      }
 140  
 141  	public static function profile_update( $wid )
 142      {
 143          $id = get_user_meta( $wid, self::meta_key, true );
 144          if ( $id )
 145          {
 146              $post_ = filter_input_array( INPUT_POST );
 147  
 148              $mp_user = MP_User::get( $id );
 149              if ( stripslashes( $post_['display_name'] ) != $mp_user->name )
 150              {
 151                  MP_User::update_name( $id, $post_['display_name'] );
 152              }
 153              if ( $post_['email'] != $mp_user->email )
 154              {
 155                  $wp_user = self::get_wp_user( $wid );
 156                  if ( MailPress::is_email( $post_['email'] ) ) self::sync( $wp_user );
 157              }
 158          }
 159          else
 160          {
 161              $wp_user = self::get_wp_user( $wid );
 162              self::sync( $wp_user );
 163          }
 164      }
 165  
 166  //// MP user management ////
 167  
 168  	public static function mp_insert_user( $mp_user_id )
 169      {
 170          $mp_email    = MP_User::get_email( $mp_user_id );
 171          $wp_users      = self::get_wp_users_by_email( $mp_email );
 172          if ( is_array( $wp_users ) ) foreach ( $wp_users as $wp_user ) update_user_meta( $wp_user->ID, self::meta_key, $mp_user_id );
 173      }
 174  
 175  	public static function mp_delete_user( $mp_user_id )
 176      {
 177          global $wpdb;
 178          $results = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = %s ;", self::meta_key, $mp_user_id ) );
 179      }
 180  
 181  // generic functions
 182  
 183  	public static function sync( $wp_user )
 184      {
 185  
 186   // Already a MailPress user ?
 187  
 188          $id = get_user_meta( $wp_user->ID, self::meta_key, true );
 189          if ( $id )
 190          {
 191              if ( MP_User::get_email( $id ) == $wp_user->user_email ) return true;
 192          }
 193  
 194  // Mail already in MailPress table ?
 195  
 196          $id =  MP_User::get_id_by_email( $wp_user->user_email );
 197          if ( $id ) 
 198          {
 199              update_user_meta( $wp_user->ID, self::meta_key, $id );
 200              MP_User::set_status( $id, 'active' );
 201              return true;                                          
 202          }
 203  
 204  // so insert !
 205  
 206          return self::insert( $wp_user );
 207      }
 208  
 209  	public static function insert( $wp_user, $type = 'activate' )
 210      {
 211          if ( !MailPress::is_email( $wp_user->user_email ) )    return false; // not an email
 212  
 213          if ( 'activate' == $type ) 
 214          {
 215              if ( !MP_User::insert( $wp_user->user_email, $wp_user->display_name, array( 'status' => 'active' ) ) )    return false; // user not inserted
 216          }
 217          else
 218          {
 219              $return = MP_User::add( $wp_user->user_email, $wp_user->display_name );
 220              if ( !$return['result'] )                 return false; // user not inserted
 221          }
 222          $id = MP_User::get_id_by_email( $wp_user->user_email );
 223          update_user_meta( $wp_user->ID, self::meta_key, $id );
 224          return true;
 225      }
 226  
 227  	public static function get_wp_user( $wp_user_id )
 228      {
 229          global $wpdb;
 230          return $wpdb->get_row( $wpdb->prepare( "SELECT ID, user_email, display_name FROM $wpdb->users WHERE ID = %d ", $wp_user_id ) );
 231      }
 232  
 233  	public static function get_wp_users_by_mp_user_id( $mp_user_id )
 234      {
 235          global $wpdb;
 236          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 ) );
 237      }
 238  
 239  	public static function get_wp_users_by_email( $email )
 240      {
 241          global $wpdb;
 242          $email = trim( $email );
 243          if ( !MailPress::is_email( $email ) ) return false;
 244  
 245          return $wpdb->get_results( $wpdb->prepare( "SELECT ID, user_email, display_name FROM $wpdb->users WHERE user_email = %s ;", $email ) );
 246      }
 247  
 248  	public static function count_emails( $email )
 249      {
 250          global $wpdb;
 251          return $wpdb->get_var( $wpdb->prepare( "SELECT count( * ) FROM $wpdb->users WHERE user_email = %s ;", $email ) );
 252      }
 253  
 254  ////  ADMIN  ////
 255  ////  ADMIN  ////
 256  ////  ADMIN  ////
 257  ////  ADMIN  ////
 258  
 259  // for install
 260  	public static function install()
 261      {
 262          $users = get_users();
 263          if ( $users ) foreach( $users as $user ) self::sync( $user );
 264      }
 265  
 266  // for link on plugin page
 267  	public static function plugin_action_links( $links, $file )
 268      {
 269          return MailPress::plugin_links( $links, $file, plugin_basename( __FILE__ ), 'general' );
 270      }
 271  
 272  // for role & capabilities
 273  	public static function capabilities( $capabilities ) 
 274      {
 275          $pu = ( current_user_can( 'edit_users' ) ) ? 'users.php' : 'profile.php';
 276  
 277          $capabilities['MailPress_manage_subscriptions'] = array(     'name'    => __( 'Your Subscriptions', 'MailPress' ), 
 278                                                  'group'    => 'admin', 
 279                                                  'menu'    => 33, 
 280      
 281                                                  'parent'    => $pu, 
 282                                                  'page_title'=> __( 'MailPress - Subscriptions', 'MailPress' ), 
 283                                                  'menu_title'=> __( 'Your Subscriptions', 'MailPress' ), 
 284                                                  'page'    => MailPress_page_subscriptions, 
 285                                                  'func'    => array( 'MP_AdminPage', 'body' )
 286                                               );
 287          return $capabilities;
 288      }
 289  
 290  	public static function roles_and_capabilities()
 291      {
 292          global $wp_roles;
 293          foreach( $wp_roles->role_names as $role => $name )
 294          {
 295              if ( 'administrator' == $role ) continue;
 296              $r = get_role( $role );
 297              $r->add_cap( 'MailPress_manage_subscriptions' );
 298          }
 299      }
 300  
 301  // for load admin page
 302  	public static function load_admin_page( $hub )
 303      {
 304          $hub[MailPress_page_subscriptions] = 'subscriptions';
 305          return $hub;
 306      }
 307  
 308  // for settings general
 309  	public static function settings_general_help( $content )
 310      {
 311          include  ( MP_ABSPATH . 'mp-admin/includes/settings/general/sync_wordpress_user/help.php' );
 312          return $content;
 313      }
 314  
 315  	public static function settings_general_update()
 316      {
 317          include  ( MP_ABSPATH . 'mp-admin/includes/settings/general/sync_wordpress_user/update.php' );
 318      }
 319  
 320  	public static function settings_general_form()
 321      {
 322          include  ( MP_ABSPATH . 'mp-admin/includes/settings/general/sync_wordpress_user/form.php' );
 323      }
 324  
 325  // for meta box in user page
 326  	public static function meta_boxes_user( $mp_user_id, $mp_screen )
 327      {
 328          add_meta_box( 'mp_user_syncwordpress', __( 'WP User sync', 'MailPress' ) , array( __CLASS__, 'meta_box' ), MP_AdminPage::screen, 'side', 'core' );
 329      }
 330  
 331  	public static function meta_box( $mp_user )
 332      {
 333          $wp_users = self::get_wp_users_by_mp_user_id( $mp_user->id );
 334          if ( $wp_users )
 335          {
 336              $count = 0;
 337              $total = count( $wp_users );
 338              $separator = ' style="border-bottom:1px solid #DFDFDF;"';
 339  ?>
 340  <div id="user-syncwordpress">
 341      <table class="form-table">
 342  <?php
 343  
 344              foreach ( $wp_users as $wp_user )
 345              {
 346                  $count++;
 347                  if ( $total == $count ) $separator = '';
 348  
 349                  $wp_user = get_userdata( $wp_user->ID );
 350                  if ( empty( $wp_user->first_name ) && empty( $wp_user->last_name ) && empty( $wp_user->nickname ) ) continue;
 351  ?>
 352          <tr><td style="padding:0px;" colspan="2"><strong><?php printf( __( 'WP User # %1$s', 'MailPress' ), $wp_user->ID ); ?></strong></td></tr>
 353  <?php if ( isset( $wp_user->first_name ) ) : ?>
 354          <tr><td style="padding:0px;"><?php _e( 'First name' ); ?></td><td style="padding:0px;"><?php echo $wp_user->first_name; ?></td></tr>
 355  <?php endif; ?>
 356  <?php if ( isset( $wp_user->last_name ) ) : ?>
 357          <tr><td style="padding:0px;"><?php _e( 'Last name' );  ?></td><td style="padding:0px;"><?php echo $wp_user->last_name; ?></td></tr>
 358  <?php endif; ?>
 359  <?php if ( isset( $wp_user->nickname ) ) : ?>
 360          <tr<?php echo $separator; ?>><td style="padding:0px;"><?php _e( 'Nickname' );   ?></td><td style="padding:0px;"><?php echo $wp_user->nickname; ?></td></tr>
 361  <?php endif; ?>
 362  <?php
 363              }
 364  ?>
 365      </table>
 366  </div>
 367  <?php
 368          }
 369          else 
 370              printf( __( '%1$s is not a WordPress user', 'MailPress' ), $mp_user->email );
 371      }
 372  }
 373  new MailPress_sync_wordpress_user();
 374  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1