Make WordPress Core

Ticket #36961: 36961.3.diff

File 36961.3.diff, 4.6 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
     
    247256        }
    248257
    249258        /**
    250          * Makes private/protected methods readable for backward compatibility.
    251          *
    252          * @since 4.3.0
    253          * @access public
    254          *
    255          * @param callable $name      Method to call.
    256          * @param array    $arguments Arguments to pass when calling.
    257          * @return mixed|false Return value of the callback, false otherwise.
    258          */
    259         public function __call( $name, $arguments ) {
    260                 if ( '_init_caps' === $name ) {
    261                         return call_user_func_array( array( $this, $name ), $arguments );
    262                 }
    263                 return false;
    264         }
    265 
    266         /**
    267259         * Magic method for checking the existence of a certain custom field.
    268260         *
    269261         * @since 3.3.0
     
    446438         *
    447439         * @access protected
    448440         * @since 2.1.0
     441         * @deprecated 4.8.0
    449442         *
    450443         * @global wpdb $wpdb WordPress database abstraction object.
    451444         *
     
    454447        protected function _init_caps( $cap_key = '' ) {
    455448                global $wpdb;
    456449
    457                 if ( empty($cap_key) )
    458                         $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
    459                 else
     450                _deprecated_function( __METHOD__, '4.8.0' );
     451
     452                if ( empty($cap_key) ) {
     453                        $this->cap_key = $wpdb->get_blog_prefix( $this->blog_id ) . 'capabilities';
     454                } else {
    460455                        $this->cap_key = $cap_key;
     456                }
    461457
    462458                $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
    463459
     
    481477         * @return array List of all capabilities for the user.
    482478         */
    483479        public function get_role_caps() {
     480                $switch_site = false;
     481                if ( is_multisite() && $this->blog_id !== get_current_blog_id() ) {
     482                        $switch_site = $this->blog_id;
     483
     484                        switch_to_blog( $switch_site );
     485                }
     486
    484487                $wp_roles = wp_roles();
    485488
    486489                //Filter out caps that are not role names and assign to $this->roles
     
    495498                }
    496499                $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
    497500
     501                if ( $switch_site ) {
     502                        restore_current_blog();
     503                }
     504
    498505                return $this->allcaps;
    499506        }
    500507
     
    783790         */
    784791        public function for_blog( $blog_id = '' ) {
    785792                global $wpdb;
    786                 if ( ! empty( $blog_id ) )
    787                         $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
    788                 else
    789                         $cap_key = '';
    790                 $this->_init_caps( $cap_key );
     793
     794                if ( ! empty( $blog_id ) ) {
     795                        $this->blog_id = absint( $blog_id );
     796                } else {
     797                        $this->blog_id = get_current_blog_id();
     798                }
     799
     800                $this->cap_key = $wpdb->get_blog_prefix( $this->blog_id ) . 'capabilities';
     801
     802                $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
     803
     804                if ( ! is_array( $this->caps ) ) {
     805                        $this->caps = array();
     806                }
     807
     808                $this->get_role_caps();
    791809        }
    792810}
  • 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        }