Make WordPress Core

Ticket #36961: 36961.2.diff

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

     
    8888        public $allcaps = array();
    8989
    9090        /**
     91         * The site ID the roles of this user are initialized for.
     92         *
     93         * @since 4.8.0
     94         * @access private
     95         * @var int
     96         */
     97        private $blog_id = 0;
     98
     99        /**
    91100         * The filter context applied to user data fields.
    92101         *
    93102         * @since 2.9.0
     
    451460        protected function _init_caps( $cap_key = '' ) {
    452461                global $wpdb;
    453462
    454                 if ( empty($cap_key) )
    455                         $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
    456                 else
     463                // Backward-compatibility with calling this method directly.
     464                if ( empty( $this->blog_id ) ) {
     465                        if ( empty( $cap_key ) ) {
     466                                $this->blog_id = get_current_blog_id();
     467                        } else {
     468                                $base_prefix = $this->db->get_blog_prefix( 1 );
     469                                if ( $base_prefix . 'capabilities' === $cap_key ) {
     470                                        $this->blog_id = 1;
     471                                } else {
     472                                        $this->blog_id = absint( substr( $cap_key, strlen( $base_prefix ), - strlen( '_capabilities' ) ) );
     473                                }
     474                        }
     475                }
     476
     477                if ( empty($cap_key) ) {
     478                        $this->cap_key = $wpdb->get_blog_prefix( $this->blog_id ) . 'capabilities';
     479                } else {
    457480                        $this->cap_key = $cap_key;
     481                }
    458482
    459483                $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
    460484
     
    478502         * @return array List of all capabilities for the user.
    479503         */
    480504        public function get_role_caps() {
     505                $switch_site = false;
     506                if ( is_multisite() && $this->blog_id !== get_current_blog_id() ) {
     507                        $switch_site = $this->blog_id;
     508
     509                        switch_to_blog( $switch_site );
     510                }
     511
    481512                $wp_roles = wp_roles();
    482513
    483514                //Filter out caps that are not role names and assign to $this->roles
     
    492523                }
    493524                $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
    494525
     526                if ( $switch_site ) {
     527                        restore_current_blog();
     528                }
     529
    495530                return $this->allcaps;
    496531        }
    497532
     
    780815         */
    781816        public function for_blog( $blog_id = '' ) {
    782817                global $wpdb;
    783                 if ( ! empty( $blog_id ) )
    784                         $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
    785                 else
     818
     819                if ( ! empty( $blog_id ) ) {
     820                        $this->blog_id = absint( $blog_id );
     821                        $cap_key = $wpdb->get_blog_prefix( $this->blog_id ) . 'capabilities';
     822                } else {
     823                        $this->blog_id = get_current_blog_id();
    786824                        $cap_key = '';
     825                }
     826
    787827                $this->_init_caps( $cap_key );
    788828        }
    789829}
  • tests/phpunit/tests/user/capabilities.php

     
    17741774                $this->assertFalse( user_can( self::$users['contributor']->ID,   'remove_user', self::$users['contributor']->ID ) );
    17751775                $this->assertFalse( user_can( self::$users['subscriber']->ID,    'remove_user', self::$users['subscriber']->ID ) );
    17761776        }
     1777
     1778        /**
     1779         * @ticket 36961
     1780         */
     1781        function test_init_user_caps_for_different_site() {
     1782                global $wpdb;
     1783
     1784                if ( ! is_multisite() ) {
     1785                        $this->markTestSkipped( 'Test only runs in multisite' );
     1786                }
     1787
     1788                $site_id = self::factory()->blog->create( array( 'user_id' => self::$users['administrator']->ID ) );
     1789
     1790                switch_to_blog( $site_id );
     1791
     1792                $role_name = 'uploader';
     1793                add_role( $role_name, 'Uploader', array(
     1794                        'read'         => true,
     1795                        'upload_files' => true,
     1796                ) );
     1797                add_user_to_blog( $site_id, self::$users['subscriber']->ID, $role_name );
     1798
     1799                restore_current_blog();
     1800
     1801                $user = new WP_User( self::$users['subscriber']->ID, '', $site_id );
     1802                $this->assertTrue( $user->has_cap( 'upload_files' ) );
     1803        }
    17771804}
  • tests/phpunit/tests/user.php

     
    180180                $this->assertEquals( 'foo', $user->$key );
    181181                $this->assertEquals( 'foo', $user->data->$key );  // This will fail with WP < 3.3
    182182
    183                 foreach ( (array) $user as $key => $value ) {
     183                foreach ( get_object_vars( $user ) as $key => $value ) {
    184184                        $this->assertEquals( $value, $user->$key );
    185185                }
    186186        }