Ticket #36961: 36961.3.diff
File 36961.3.diff, 4.6 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-user.php
88 88 public $allcaps = array(); 89 89 90 90 /** 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 /** 91 100 * The filter context applied to user data fields. 92 101 * 93 102 * @since 2.9.0 … … 247 256 } 248 257 249 258 /** 250 * Makes private/protected methods readable for backward compatibility.251 *252 * @since 4.3.0253 * @access public254 *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 /**267 259 * Magic method for checking the existence of a certain custom field. 268 260 * 269 261 * @since 3.3.0 … … 446 438 * 447 439 * @access protected 448 440 * @since 2.1.0 441 * @deprecated 4.8.0 449 442 * 450 443 * @global wpdb $wpdb WordPress database abstraction object. 451 444 * … … 454 447 protected function _init_caps( $cap_key = '' ) { 455 448 global $wpdb; 456 449 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 { 460 455 $this->cap_key = $cap_key; 456 } 461 457 462 458 $this->caps = get_user_meta( $this->ID, $this->cap_key, true ); 463 459 … … 481 477 * @return array List of all capabilities for the user. 482 478 */ 483 479 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 484 487 $wp_roles = wp_roles(); 485 488 486 489 //Filter out caps that are not role names and assign to $this->roles … … 495 498 } 496 499 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); 497 500 501 if ( $switch_site ) { 502 restore_current_blog(); 503 } 504 498 505 return $this->allcaps; 499 506 } 500 507 … … 783 790 */ 784 791 public function for_blog( $blog_id = '' ) { 785 792 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(); 791 809 } 792 810 } -
tests/phpunit/tests/user/capabilities.php
1774 1774 $this->assertFalse( user_can( self::$users['contributor']->ID, 'remove_user', self::$users['contributor']->ID ) ); 1775 1775 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1776 1776 } 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 } 1777 1804 } -
tests/phpunit/tests/user.php
180 180 $this->assertEquals( 'foo', $user->$key ); 181 181 $this->assertEquals( 'foo', $user->data->$key ); // This will fail with WP < 3.3 182 182 183 foreach ( (array) $useras $key => $value ) {183 foreach ( get_object_vars( $user ) as $key => $value ) { 184 184 $this->assertEquals( $value, $user->$key ); 185 185 } 186 186 }