Make WordPress Core

Ticket #38681: 38681.2.diff

File 38681.2.diff, 4.8 KB (added by flixos90, 8 years ago)
  • src/wp-includes/class-wp-user.php

     
    113113         *
    114114         * @global wpdb $wpdb WordPress database abstraction object.
    115115         *
    116          * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
    117          * @param string $name Optional. User's username
    118          * @param int $blog_id Optional Site ID, defaults to current site.
     116         * @param int|string|stdClass|WP_User $id   User's ID, a WP_User object, or a user object from the DB.
     117         * @param string                      $name Optional. User's username
     118         * @param int $site_id Optional Site ID, defaults to current site.
    119119         */
    120         public function __construct( $id = 0, $name = '', $blog_id = '' ) {
     120        public function __construct( $id = 0, $name = '', $site_id = '' ) {
    121121                if ( ! isset( self::$back_compat_keys ) ) {
    122122                        $prefix = $GLOBALS['wpdb']->prefix;
    123123                        self::$back_compat_keys = array(
     
    131131                }
    132132
    133133                if ( $id instanceof WP_User ) {
    134                         $this->init( $id->data, $blog_id );
     134                        $this->init( $id->data, $site_id );
    135135                        return;
    136136                } elseif ( is_object( $id ) ) {
    137                         $this->init( $id, $blog_id );
     137                        $this->init( $id, $site_id );
    138138                        return;
    139139                }
    140140
     
    150150                }
    151151
    152152                if ( $data ) {
    153                         $this->init( $data, $blog_id );
     153                        $this->init( $data, $site_id );
    154154                } else {
    155155                        $this->data = new stdClass;
    156156                }
     
    160160         * Sets up object properties, including capabilities.
    161161         *
    162162         * @param object $data    User DB row object.
    163          * @param int    $blog_id Optional. The site ID to initialize for.
     163         * @param int    $site_id Optional. The site ID to initialize for.
    164164         */
    165         public function init( $data, $blog_id = '' ) {
     165        public function init( $data, $site_id = '' ) {
    166166                $this->data = $data;
    167167                $this->ID = (int) $data->ID;
    168168
    169                 $this->for_blog( $blog_id );
     169                $this->for_site( $site_id );
    170170        }
    171171
    172172        /**
     
    254254         * @return mixed|false Return value of the callback, false otherwise.
    255255         */
    256256        public function __call( $name, $arguments ) {
    257                 if ( '_init_caps' === $name ) {
    258                         return call_user_func_array( array( $this, $name ), $arguments );
     257                switch ( $name ) {
     258                        case 'for_blog':
     259                                return call_user_func_array( array( $this, 'for_site' ), $arguments );
     260                        case '_init_caps':
     261                                return call_user_func_array( array( $this, $name ), $arguments );
    259262                }
     263
    260264                return false;
    261265        }
    262266
     
    772776        /**
    773777         * Set the site to operate on. Defaults to the current site.
    774778         *
    775          * @since 3.0.0
     779         * @since 4.8.0 Replaces for_blog() method.
     780         * @access public
    776781         *
    777782         * @global wpdb $wpdb WordPress database abstraction object.
    778783         *
    779          * @param int $blog_id Optional. Site ID, defaults to current site.
     784         * @param int $site_id Optional. Site ID, defaults to current site.
    780785         */
    781         public function for_blog( $blog_id = '' ) {
     786        public function for_site( $site_id = '' ) {
    782787                global $wpdb;
    783                 if ( ! empty( $blog_id ) )
    784                         $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
    785                 else
     788
     789                if ( ! empty( $site_id ) ) {
     790                        $cap_key = $wpdb->get_blog_prefix( $site_id ) . 'capabilities';
     791                } else {
    786792                        $cap_key = '';
     793                }
     794
    787795                $this->_init_caps( $cap_key );
    788796        }
    789797}
  • src/wp-includes/ms-blogs.php

     
    824824        if ( did_action( 'init' ) ) {
    825825                $wp_roles = new WP_Roles();
    826826                $current_user = wp_get_current_user();
    827                 $current_user->for_blog( $new_blog );
     827                $current_user->for_site( $new_blog );
    828828        }
    829829
    830830        /** This filter is documented in wp-includes/ms-blogs.php */
     
    898898        if ( did_action( 'init' ) ) {
    899899                $wp_roles = new WP_Roles();
    900900                $current_user = wp_get_current_user();
    901                 $current_user->for_blog( $blog );
     901                $current_user->for_site( $blog );
    902902        }
    903903
    904904        /** This filter is documented in wp-includes/ms-blogs.php */
  • tests/phpunit/tests/user/multisite.php

     
    424424
    425425                $wp_roles->remove_role( $role );
    426426        }
     427
     428        public function data_test_switch_site_for_user() {
     429                return array(
     430                        'for_site' => array( 'for_site' ),
     431                        'for_blog' => array( 'for_blog' ),
     432                );
     433        }
     434
     435        /**
     436         * @ticket 38681
     437         * @dataProvider data_test_switch_site_for_user
     438         */
     439        public function test_switch_site_for_user( $method_name ) {
     440                $user = self::factory()->user->create_and_get();
     441                $site_id = self::factory()->blog->create();
     442
     443                call_user_func( array( $user, $method_name ), $site_id );
     444
     445                wpmu_delete_blog( $site_id );
     446                wpmu_delete_user( $user->ID );
     447
     448                $this->assertFalse( $user->has_cap( 'read' ) );
     449        }
    427450}
    428451
    429452endif ;