Make WordPress Core


Ignore:
Timestamp:
08/18/2016 06:20:55 PM (10 years ago)
Author:
wonderboymusic
Message:

Query: add a protected field, $db, (composition, as it were) to WP_*_Query classes to hold the value for the database abstraction, instead of importing the global $wpdb into every method that uses it. Reduces the number of global imports by 32.

See #37699.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-user.php

    r37985 r38275  
    103103
    104104        /**
     105         * @since 4.7.0
     106         * @access protected
     107         * @var wpdb
     108         */
     109        protected $db;
     110
     111        /**
    105112         * Constructor.
    106113         *
     
    109116         * @since 2.0.0
    110117         * @access public
    111          *
    112          * @global wpdb $wpdb WordPress database abstraction object.
    113118         *
    114119         * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
     
    117122         */
    118123        public function __construct( $id = 0, $name = '', $blog_id = '' ) {
     124                $this->db = $GLOBALS['wpdb'];
     125
    119126                if ( ! isset( self::$back_compat_keys ) ) {
    120                         $prefix = $GLOBALS['wpdb']->prefix;
     127                        $prefix = $this->db->prefix;
    121128                        self::$back_compat_keys = array(
    122129                                'user_firstname' => 'first_name',
     
    233240
    234241                if ( !$user = $wpdb->get_row( $wpdb->prepare(
    235                         "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
    236                 ) ) )
     242                        "SELECT * FROM {$wpdb->users} WHERE $db_field = %s", $value
     243                ) ) ) {
    237244                        return false;
    238 
     245                }
    239246                update_user_caches( $user );
    240247
     
    443450         * @since 2.1.0
    444451         *
    445          * @global wpdb $wpdb WordPress database abstraction object.
    446          *
    447452         * @param string $cap_key Optional capability key
    448453         */
    449454        protected function _init_caps( $cap_key = '' ) {
    450                 global $wpdb;
    451 
    452                 if ( empty($cap_key) )
    453                         $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
    454                 else
     455                if ( empty( $cap_key ) ) {
     456                        $this->cap_key = $this->db->get_blog_prefix() . 'capabilities';
     457                } else {
    455458                        $this->cap_key = $cap_key;
    456 
     459                }
    457460                $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
    458461
     
    632635         * @since 2.0.0
    633636         * @access public
    634          *
    635          * @global wpdb $wpdb WordPress database abstraction object.
    636637         */
    637638        public function update_user_level_from_caps() {
    638                 global $wpdb;
    639639                $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
    640                 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
     640                update_user_meta( $this->ID, $this->db->get_blog_prefix() . 'user_level', $this->user_level );
    641641        }
    642642
     
    680680         * @since 2.1.0
    681681         * @access public
    682          *
    683          * @global wpdb $wpdb WordPress database abstraction object.
    684682         */
    685683        public function remove_all_caps() {
    686                 global $wpdb;
    687684                $this->caps = array();
    688685                delete_user_meta( $this->ID, $this->cap_key );
    689                 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
     686                delete_user_meta( $this->ID, $this->db->get_blog_prefix() . 'user_level' );
    690687                $this->get_role_caps();
    691688        }
     
    773770         * @since 3.0.0
    774771         *
    775          * @global wpdb $wpdb WordPress database abstraction object.
    776          *
    777772         * @param int $blog_id Optional. Site ID, defaults to current site.
    778773         */
    779774        public function for_blog( $blog_id = '' ) {
    780                 global $wpdb;
    781                 if ( ! empty( $blog_id ) )
    782                         $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
    783                 else
     775                if ( ! empty( $blog_id ) ) {
     776                        $cap_key = $this->db->get_blog_prefix( $blog_id ) . 'capabilities';
     777                } else {
    784778                        $cap_key = '';
     779                }
    785780                $this->_init_caps( $cap_key );
    786781        }
Note: See TracChangeset for help on using the changeset viewer.