Skip to content

Commit

Permalink
Add magic get/set/isset methods to WP_User to avoid data duplication.…
Browse files Browse the repository at this point in the history
… Standardize on WP_User::ID. Props scribu. see #15458

git-svn-id: http://svn.automattic.com/wordpress/trunk@18504 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
ryan committed Aug 4, 2011
1 parent 6e3e6da commit 6c81d02
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 37 deletions.
2 changes: 1 addition & 1 deletion wp-admin/includes/deprecated.php
Expand Up @@ -255,7 +255,7 @@ function get_editable_user_ids( $user_id, $exclude_zeros = true, $post_type = 'p

if ( ! $user->has_cap($post_type_obj->cap->edit_others_posts) ) {
if ( $user->has_cap($post_type_obj->cap->edit_posts) || ! $exclude_zeros )
return array($user->id);
return array($user->ID);
else
return array();
}
Expand Down
2 changes: 1 addition & 1 deletion wp-admin/includes/ms.php
Expand Up @@ -250,7 +250,7 @@ function send_confirmation_on_profile_email() {
if ( ! is_object($errors) )
$errors = new WP_Error();

if ( $current_user->id != $_POST['user_id'] )
if ( $current_user->ID != $_POST['user_id'] )
return false;

if ( $current_user->user_email != $_POST['email'] ) {
Expand Down
4 changes: 2 additions & 2 deletions wp-admin/my-sites.php
Expand Up @@ -17,7 +17,7 @@

$action = isset( $_POST['action'] ) ? $_POST['action'] : 'splash';

$blogs = get_blogs_of_user( $current_user->id );
$blogs = get_blogs_of_user( $current_user->ID );

if ( empty( $blogs ) )
wp_die( __( 'You must be a member of at least one site to use this page.' ) );
Expand All @@ -28,7 +28,7 @@

$blog = get_blog_details( (int) $_POST['primary_blog'] );
if ( $blog && isset( $blog->domain ) ) {
update_user_option( $current_user->id, 'primary_blog', (int) $_POST['primary_blog'], true );
update_user_option( $current_user->ID, 'primary_blog', (int) $_POST['primary_blog'], true );
$updated = true;
} else {
wp_die( __( 'The primary site you chose does not exist.' ) );
Expand Down
4 changes: 2 additions & 2 deletions wp-admin/users.php
Expand Up @@ -216,7 +216,7 @@
$update = 'remove';
foreach ( $userids as $id ) {
$id = (int) $id;
if ( $id == $current_user->id && !is_super_admin() ) {
if ( $id == $current_user->ID && !is_super_admin() ) {
$update = 'err_admin_remove';
continue;
}
Expand Down Expand Up @@ -269,7 +269,7 @@
foreach ( $userids as $id ) {
$id = (int) $id;
$user = new WP_User($id);
if ( $id == $current_user->id && !is_super_admin() ) {
if ( $id == $current_user->ID && !is_super_admin() ) {
echo "<li>" . sprintf(__('ID #%1s: %2s <strong>The current user will not be removed.</strong>'), $id, $user->user_login) . "</li>\n";
} elseif ( !current_user_can('remove_user', $id) ) {
echo "<li>" . sprintf(__('ID #%1s: %2s <strong>You don\'t have permission to remove this user.</strong>'), $id, $user->user_login) . "</li>\n";
Expand Down
65 changes: 41 additions & 24 deletions wp-includes/capabilities.php
Expand Up @@ -360,8 +360,6 @@ class WP_User {
/**
* User data container.
*
* This will be set as properties of the object.
*
* @since 2.0.0
* @access private
* @var array
Expand All @@ -377,17 +375,6 @@ class WP_User {
*/
var $ID = 0;

/**
* The deprecated user's ID.
*
* @since 2.0.0
* @access public
* @deprecated Use WP_User::$ID
* @see WP_User::$ID
* @var int
*/
var $id = 0;

/**
* The individual capabilities the user has been given.
*
Expand Down Expand Up @@ -471,7 +458,6 @@ class WP_User {
* @return WP_User
*/
function __construct( $id, $name = '', $blog_id = '' ) {

if ( empty( $id ) && empty( $name ) )
return;

Expand All @@ -488,12 +474,40 @@ function __construct( $id, $name = '', $blog_id = '' ) {
if ( empty( $this->data->ID ) )
return;

foreach ( get_object_vars( $this->data ) as $key => $value ) {
$this->{$key} = $value;
$this->ID = $this->data->ID;
$this->for_blog( $blog_id );
}

/**
* Magic method for checking the existance of a certain custom field
*
* @since 3.3.0
*/
function __isset( $key ) {
return isset( $this->data->$key );
}

/**
* Magic method for accessing custom fields
*
* @since 3.3.0
*/
function __get( $key ) {
if ( 'id' == $key ) {
_deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
return $this->ID;
}

$this->id = $this->ID;
$this->for_blog( $blog_id );
return $this->data->$key;
}

/**
* Magic method for setting custom fields
*
* @since 3.3.0
*/
function __set( $key, $value ) {
$this->data->$key = $value;
}

/**
Expand All @@ -511,13 +525,16 @@ function __construct( $id, $name = '', $blog_id = '' ) {
*/
function _init_caps( $cap_key = '' ) {
global $wpdb;

if ( empty($cap_key) )
$this->cap_key = $wpdb->prefix . 'capabilities';
else
$this->cap_key = $cap_key;
$this->caps = &$this->{$this->cap_key};

$this->caps = &$this->data->{$this->cap_key};
if ( ! is_array( $this->caps ) )
$this->caps = array();

$this->get_role_caps();
}

Expand Down Expand Up @@ -956,10 +973,10 @@ function map_meta_cap( $cap, $user_id ) {
case 'add_post_meta':
$post = get_post( $args[0] );
$post_type_object = get_post_type_object( $post->post_type );
$caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
$caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );

$meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;

$meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;

if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
$allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
if ( ! $allowed )
Expand Down Expand Up @@ -1080,7 +1097,7 @@ function current_user_can_for_blog( $blog_id, $capability ) {
return false;

// Create new object to avoid stomping the global current_user.
$user = new WP_User( $current_user->id) ;
$user = new WP_User( $current_user->ID) ;

// Set the blog id. @todo add blog id arg to WP_User constructor?
$user->for_blog( $blog_id );
Expand Down Expand Up @@ -1225,7 +1242,7 @@ function is_super_admin( $user_id = false ) {
else
$user = wp_get_current_user();

if ( empty( $user->id ) )
if ( empty( $user->ID ) )
return false;

if ( is_multisite() ) {
Expand Down
6 changes: 3 additions & 3 deletions wp-includes/pluggable.php
Expand Up @@ -783,7 +783,7 @@ function wp_clear_auth_cookie() {
function is_user_logged_in() {
$user = wp_get_current_user();

if ( $user->id == 0 )
if ( $user->ID == 0 )
return false;

return true;
Expand Down Expand Up @@ -1308,7 +1308,7 @@ function wp_nonce_tick() {
*/
function wp_verify_nonce($nonce, $action = -1) {
$user = wp_get_current_user();
$uid = (int) $user->id;
$uid = (int) $user->ID;

$i = wp_nonce_tick();

Expand All @@ -1334,7 +1334,7 @@ function wp_verify_nonce($nonce, $action = -1) {
*/
function wp_create_nonce($action = -1) {
$user = wp_get_current_user();
$uid = (int) $user->id;
$uid = (int) $user->ID;

$i = wp_nonce_tick();

Expand Down
2 changes: 1 addition & 1 deletion wp-includes/user.php
Expand Up @@ -1566,7 +1566,7 @@ function wp_update_user($userdata) {

// Update the cookies if the password changed.
$current_user = wp_get_current_user();
if ( $current_user->id == $ID ) {
if ( $current_user->ID == $ID ) {
if ( isset($plaintext_pass) ) {
wp_clear_auth_cookie();
wp_set_auth_cookie($ID);
Expand Down
4 changes: 2 additions & 2 deletions wp-login.php
Expand Up @@ -586,10 +586,10 @@ function register_new_user( $user_login, $user_email ) {

if ( ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) ) {
// If the user doesn't belong to a blog, send them to user admin. If the user can't edit posts, send them to their profile.
if ( is_multisite() && !get_active_blog_for_user($user->id) && !is_super_admin( $user->id ) )
if ( is_multisite() && !get_active_blog_for_user($user->ID) && !is_super_admin( $user->ID ) )
$redirect_to = user_admin_url();
elseif ( is_multisite() && !$user->has_cap('read') )
$redirect_to = get_dashboard_url( $user->id );
$redirect_to = get_dashboard_url( $user->ID );
elseif ( !$user->has_cap('edit_posts') )
$redirect_to = admin_url('profile.php');
}
Expand Down
2 changes: 1 addition & 1 deletion wp-signup.php
Expand Up @@ -213,7 +213,7 @@ function validate_another_blog_signup() {
$meta = apply_filters( 'signup_create_blog_meta', array( 'lang_id' => 1, 'public' => $public ) ); // deprecated
$meta = apply_filters( 'add_signup_meta', $meta );

wpmu_create_blog( $domain, $path, $blog_title, $current_user->id, $meta, $wpdb->siteid );
wpmu_create_blog( $domain, $path, $blog_title, $current_user->ID, $meta, $wpdb->siteid );
confirm_another_blog_signup($domain, $path, $blog_title, $current_user->user_login, $current_user->user_email, $meta);
return true;
}
Expand Down

0 comments on commit 6c81d02

Please sign in to comment.