Ticket #36961: 36961.5.diff
File 36961.5.diff, 5.0 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-user.php
91 91 public $filter = null; 92 92 93 93 /** 94 * The site ID the capabilities of this user are initialized for. 95 * 96 * @since 4.9.0 97 * @var int 98 */ 99 private $blog_id = 0; 100 101 /** 94 102 * @static 95 103 * @since 3.3.0 96 104 * @var array … … 239 247 } 240 248 241 249 /** 242 * Makes private/protected methods readable for backward compatibility.243 *244 * @since 4.3.0245 *246 * @param callable $name Method to call.247 * @param array $arguments Arguments to pass when calling.248 * @return mixed|false Return value of the callback, false otherwise.249 */250 public function __call( $name, $arguments ) {251 if ( '_init_caps' === $name ) {252 return call_user_func_array( array( $this, $name ), $arguments );253 }254 return false;255 }256 257 /**258 250 * Magic method for checking the existence of a certain custom field. 259 251 * 260 252 * @since 3.3.0 … … 431 423 * used. 432 424 * 433 425 * @since 2.1.0 426 * @deprecated 4.9.0 Use WP_User::for_blog() 434 427 * 435 428 * @global wpdb $wpdb WordPress database abstraction object. 436 429 * … … 439 432 protected function _init_caps( $cap_key = '' ) { 440 433 global $wpdb; 441 434 442 if ( empty($cap_key) ) 443 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities'; 444 else 445 $this->cap_key = $cap_key; 435 _deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_blog()' ); 446 436 447 $this->caps = get_user_meta( $this->ID, $this->cap_key, true ); 437 if ( empty( $cap_key ) ) { 438 $this->cap_key = $wpdb->get_blog_prefix( $this->blog_id ) . 'capabilities'; 439 } else { 440 $this->cap_key = $cap_key; 441 } 448 442 449 if ( ! is_array( $this->caps ) ) 450 $this->caps = array(); 443 $this->caps = $this->get_caps_data(); 451 444 452 445 $this->get_role_caps(); 453 446 } … … 467 460 public function get_role_caps() { 468 461 $wp_roles = wp_roles(); 469 462 463 $switch_roles = false; 464 if ( is_multisite() && $this->blog_id != get_current_blog_id() ) { 465 $switch_roles = get_current_blog_id(); 466 467 $wp_roles->for_blog( $this->blog_id ); 468 } 469 470 470 //Filter out caps that are not role names and assign to $this->roles 471 471 if ( is_array( $this->caps ) ) 472 472 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) ); … … 479 479 } 480 480 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); 481 481 482 if ( $switch_roles ) { 483 $wp_roles->for_blog( $switch_roles ); 484 } 485 482 486 return $this->allcaps; 483 487 } 484 488 … … 760 764 */ 761 765 public function for_blog( $blog_id = '' ) { 762 766 global $wpdb; 763 if ( ! empty( $blog_id ) ) 764 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; 765 else 766 $cap_key = ''; 767 $this->_init_caps( $cap_key ); 767 768 if ( ! empty( $blog_id ) ) { 769 $this->blog_id = absint( $blog_id ); 770 } else { 771 $this->blog_id = get_current_blog_id(); 772 } 773 774 $this->cap_key = $wpdb->get_blog_prefix( $this->blog_id ) . 'capabilities'; 775 776 $this->caps = $this->get_caps_data(); 777 778 $this->get_role_caps(); 779 } 780 781 /** 782 * Gets the available user capabilities data. 783 * 784 * @since 4.9.0 785 * 786 * @return array User capabilities array. 787 */ 788 private function get_caps_data() { 789 $caps = get_user_meta( $this->ID, $this->cap_key, true ); 790 791 if ( ! is_array( $caps ) ) { 792 return array(); 793 } 794 795 return $caps; 768 796 } 769 797 } -
tests/phpunit/tests/user/capabilities.php
1840 1840 $this->assertFalse( user_can( self::$users['contributor']->ID, 'remove_user', self::$users['contributor']->ID ) ); 1841 1841 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1842 1842 } 1843 1844 /** 1845 * @ticket 36961 1846 * @group ms-required 1847 */ 1848 function test_init_user_caps_for_different_site() { 1849 global $wpdb; 1850 1851 $site_id = self::factory()->blog->create( array( 'user_id' => self::$users['administrator']->ID ) ); 1852 1853 switch_to_blog( $site_id ); 1854 1855 $role_name = 'uploader'; 1856 add_role( $role_name, 'Uploader', array( 1857 'read' => true, 1858 'upload_files' => true, 1859 ) ); 1860 add_user_to_blog( $site_id, self::$users['subscriber']->ID, $role_name ); 1861 1862 restore_current_blog(); 1863 1864 $user = new WP_User( self::$users['subscriber']->ID, '', $site_id ); 1865 $this->assertTrue( $user->has_cap( 'upload_files' ) ); 1866 } 1843 1867 } -
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 }