Make WordPress Core


Ignore:
Timestamp:
08/24/2011 07:32:59 PM (15 years ago)
Author:
ryan
Message:

Introduce metadata_exists(), WP_User::get_data_by(), WP_User::get(), WP_User::has_prop(). Don't fill user objects with meta. Eliminate data duplication in cache and memory. Props scribu. see #15458

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/capabilities.php

    r18515 r18597  
    421421        var $filter = null;
    422422
    423         /**
    424          * Constructor - Sets up the object properties.
    425          *
    426          * Retrieves the userdata and then assigns all of the data keys to direct
    427          * properties of the object. Calls {@link WP_User::_init_caps()} after
    428          * setting up the object's user data properties.
    429          *
    430          * @since 2.0.0
    431          * @access public
    432          *
    433          * @param int|string $id User's ID or username
    434          * @param int $name Optional. User's username
     423        private static $back_compat_keys = array(
     424                'user_firstname' => 'first_name',
     425                'user_lastname' => 'last_name',
     426                'user_description' => 'description'
     427        );
     428
     429        /**
     430         * Constructor
     431         *
     432         * Retrieves the userdata and passes it to {@link WP_User::init()}.
     433         *
     434         * @since 2.0.0
     435         * @access public
     436         *
     437         * @param int|string $id User's ID
     438         * @param string $name Optional. User's username
    435439         * @param int $blog_id Optional Blog ID, defaults to current blog.
    436440         * @return WP_User
    437441         */
    438         function __construct( $id, $name = '', $blog_id = '' ) {
    439                 if ( empty( $id ) && empty( $name ) )
    440                         return;
    441 
     442        function __construct( $id = 0, $name = '', $blog_id = '' ) {
    442443                if ( ! is_numeric( $id ) ) {
    443444                        $name = $id;
    … …  
    445446                }
    446447
    447                 if ( ! empty( $id ) )
    448                         $this->data = get_userdata( $id );
     448                if ( $id )
     449                        $data = self::get_data_by( 'id', $id );
    449450                else
    450                         $this->data = get_user_by('login', $name );
    451 
    452                 if ( empty( $this->data->ID ) )
    453                         return;
    454 
    455                 $this->ID = $this->data->ID;
     451                        $data = self::get_data_by( 'login', $name );
     452
     453                if ( $data )
     454                        $this->init( $data, $blog_id );
     455        }
     456
     457        /**
     458         * Sets up object properties, including capabilities.
     459         *
     460         * @param object $data User DB row object
     461         * @param int $blog_id Optional. The blog id to initialize for
     462         */
     463        function init( $data, $blog_id = '' ) {
     464                $this->data = $data;
     465                $this->ID = (int) $data->ID;
     466
    456467                $this->for_blog( $blog_id );
     468        }
     469
     470        /**
     471         * Return only the main user fields
     472         *
     473         * @since 3.3.0
     474         *
     475         * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
     476         * @param string|int $value The field value
     477         */
     478        static function get_data_by( $field, $value ) {
     479                global $wpdb;
     480
     481                if ( 'id' == $field )
     482                        $value = absint( $value );
     483                else
     484                        $value = trim( $value );
     485
     486                if ( !$value )
     487                        return false;
     488
     489                switch ( $field ) {
     490                        case 'id':
     491                                $user_id = $value;
     492                                $db_field = 'ID';
     493                                break;
     494                        case 'slug':
     495                                $user_id = wp_cache_get($value, 'userslugs');
     496                                $db_field = 'user_nicename';
     497                                break;
     498                        case 'email':
     499                                $user_id = wp_cache_get($value, 'useremail');
     500                                $db_field = 'user_email';
     501                                break;
     502                        case 'login':
     503                                $value = sanitize_user( $value );
     504                                $user_id = wp_cache_get($value, 'userlogins');
     505                                $db_field = 'user_login';
     506                                break;
     507                        default:
     508                                return false;
     509                }
     510
     511                if ( false !== $user_id ) {
     512                        if ( $user = wp_cache_get( $user_id, 'users' ) )
     513                                return $user;
     514                }
     515
     516                if ( !$user = $wpdb->get_row( $wpdb->prepare(
     517                        "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
     518                ) ) )
     519                        return false;
     520
     521                update_user_caches( $user );
     522
     523                return $user;
    457524        }
    458525
    … …  
    467534                        $key = 'ID';
    468535                }
    469                 return isset( $this->data->$key );
     536
     537                if ( isset( $this->data->$key ) )
     538                        return true;
     539
     540                if ( isset( self::$back_compat_keys[ $key ] ) )
     541                        $key = self::$back_compat_keys[ $key ];
     542
     543                return metadata_exists( 'user', $this->ID, $key );
    470544        }
    471545
    … …  
    481555                }
    482556
    483                 return $this->data->$key;
     557                if ( isset( $this->data->$key ) ) {
     558                        $value = $this->data->$key;
     559                } else {
     560                        if ( isset( self::$back_compat_keys[ $key ] ) )
     561                                $key = self::$back_compat_keys[ $key ];
     562                        $value = get_user_meta( $this->ID, $key, true );
     563                }
     564
     565                if ( $this->filter ) {
     566                        $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
     567                }
     568
     569                return $value;
    484570        }
    485571
    … …  
    500586
    501587        /**
     588         * Retrieve the value of a property or meta key.
     589         *
     590         * Retrieves from the users and usermeta table.
     591         *
     592         * @since 3.3.0
     593         *
     594         * @param string $key Property
     595         */
     596        function get( $key ) {
     597                return $this->__get( $key );
     598        }
     599
     600        /**
     601         * Determine whether a property or meta key is set
     602         *
     603         * Consults the users and usermeta tables.
     604         *
     605         * @since 3.3.0
     606         *
     607         * @param string $key Property
     608         */
     609        function has_prop( $key ) {
     610                return $this->__isset( $key );
     611        }
     612
     613        /**
    502614         * Set up capability object properties.
    503615         *
    … …  
    520632                        $this->cap_key = $cap_key;
    521633
    522                 $this->caps = &$this->data->{$this->cap_key};
     634                $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
     635
    523636                if ( ! is_array( $this->caps ) )
    524637                        $this->caps = array();
    … …  
    663776        function update_user_level_from_caps() {
    664777                global $wpdb;
    665                 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
     778                $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
    666779                update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
    667780        }
    … …  
    10671180        $args = array_merge( array( $capability ), $args );
    10681181
    1069         return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
     1182        return call_user_func_array( array( $current_user, 'has_cap' ), $args );
    10701183}
    10711184
Note: See TracChangeset for help on using the changeset viewer.