Ticket #36961: 36961.8.diff
File 36961.8.diff, 9.1 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 $site_id = 0; 100 101 /** 94 102 * @static 95 103 * @since 3.3.0 96 104 * @var array … … 108 116 * 109 117 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB. 110 118 * @param string $name Optional. User's username 111 * @param int $ blog_id Optional Site ID, defaults to current site.119 * @param int $site_id Optional Site ID, defaults to current site. 112 120 */ 113 public function __construct( $id = 0, $name = '', $ blog_id = '' ) {121 public function __construct( $id = 0, $name = '', $site_id = '' ) { 114 122 if ( ! isset( self::$back_compat_keys ) ) { 115 123 $prefix = $GLOBALS['wpdb']->prefix; 116 124 self::$back_compat_keys = array( … … 124 132 } 125 133 126 134 if ( $id instanceof WP_User ) { 127 $this->init( $id->data, $ blog_id );135 $this->init( $id->data, $site_id ); 128 136 return; 129 137 } elseif ( is_object( $id ) ) { 130 $this->init( $id, $ blog_id );138 $this->init( $id, $site_id ); 131 139 return; 132 140 } 133 141 … … 143 151 } 144 152 145 153 if ( $data ) { 146 $this->init( $data, $ blog_id );154 $this->init( $data, $site_id ); 147 155 } else { 148 156 $this->data = new stdClass; 149 157 } … … 155 163 * @since 3.3.0 156 164 * 157 165 * @param object $data User DB row object. 158 * @param int $ blog_id Optional. The site ID to initialize for.166 * @param int $site_id Optional. The site ID to initialize for. 159 167 */ 160 public function init( $data, $ blog_id = '' ) {168 public function init( $data, $site_id = '' ) { 161 169 $this->data = $data; 162 170 $this->ID = (int) $data->ID; 163 171 164 $this->for_ blog( $blog_id );172 $this->for_site( $site_id ); 165 173 } 166 174 167 175 /** … … 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 … … 423 415 } 424 416 425 417 /** 418 * Makes private/protected methods readable for backward compatibility. 419 * 420 * @since 4.3.0 421 * 422 * @param callable $name Method to call. 423 * @param array $arguments Arguments to pass when calling. 424 * @return mixed|false Return value of the callback, false otherwise. 425 */ 426 public function __call( $name, $arguments ) { 427 if ( '_init_caps' === $name ) { 428 return call_user_func_array( array( $this, $name ), $arguments ); 429 } 430 return false; 431 } 432 433 /** 426 434 * Set up capability object properties. 427 435 * 428 436 * Will set the value for the 'cap_key' property to current database table … … 431 439 * used. 432 440 * 433 441 * @since 2.1.0 442 * @deprecated 4.9.0 Use WP_User::for_site() 434 443 * 435 444 * @global wpdb $wpdb WordPress database abstraction object. 436 445 * … … 439 448 protected function _init_caps( $cap_key = '' ) { 440 449 global $wpdb; 441 450 442 if ( empty($cap_key) ) 443 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities'; 444 else 445 $this->cap_key = $cap_key; 451 _deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' ); 446 452 447 $this->caps = get_user_meta( $this->ID, $this->cap_key, true ); 453 if ( empty( $cap_key ) ) { 454 $this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities'; 455 } else { 456 $this->cap_key = $cap_key; 457 } 448 458 449 if ( ! is_array( $this->caps ) ) 450 $this->caps = array(); 459 $this->caps = $this->get_caps_data(); 451 460 452 461 $this->get_role_caps(); 453 462 } … … 465 474 * @return array List of all capabilities for the user. 466 475 */ 467 476 public function get_role_caps() { 477 $switch_site = false; 478 if ( is_multisite() && $this->site_id != get_current_blog_id() ) { 479 $switch_site = true; 480 481 switch_to_blog( $this->site_id ); 482 } 483 468 484 $wp_roles = wp_roles(); 469 485 470 486 //Filter out caps that are not role names and assign to $this->roles … … 479 495 } 480 496 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); 481 497 498 if ( $switch_site ) { 499 restore_current_blog(); 500 } 501 482 502 return $this->allcaps; 483 503 } 484 504 … … 753 773 * Set the site to operate on. Defaults to the current site. 754 774 * 755 775 * @since 3.0.0 776 * @deprecated 4.9.0 Use WP_User::for_site() 756 777 * 757 778 * @global wpdb $wpdb WordPress database abstraction object. 758 779 * 759 780 * @param int $blog_id Optional. Site ID, defaults to current site. 760 781 */ 761 782 public function for_blog( $blog_id = '' ) { 783 _deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' ); 784 785 $this->for_site( $blog_id ); 786 } 787 788 /** 789 * Sets the site to operate on. Defaults to the current site. 790 * 791 * @since 4.9.0 792 * 793 * @global wpdb $wpdb WordPress database abstraction object. 794 * 795 * @param int $site_id Site ID to initialize user capabilities for. Default is the current site. 796 */ 797 public function for_site( $site_id = '' ) { 762 798 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 ); 799 800 if ( ! empty( $site_id ) ) { 801 $this->site_id = absint( $site_id ); 802 } else { 803 $this->site_id = get_current_blog_id(); 804 } 805 806 $this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities'; 807 808 $this->caps = $this->get_caps_data(); 809 810 $this->get_role_caps(); 811 } 812 813 /** 814 * Gets the available user capabilities data. 815 * 816 * @since 4.9.0 817 * 818 * @return array User capabilities array. 819 */ 820 private function get_caps_data() { 821 $caps = get_user_meta( $this->ID, $this->cap_key, true ); 822 823 if ( ! is_array( $caps ) ) { 824 return array(); 825 } 826 827 return $caps; 768 828 } 769 829 } -
src/wp-includes/ms-blogs.php
851 851 if ( did_action( 'init' ) ) { 852 852 $wp_roles = new WP_Roles(); 853 853 $current_user = wp_get_current_user(); 854 $current_user->for_ blog( $new_blog );854 $current_user->for_site( $new_blog ); 855 855 } 856 856 857 857 /** This filter is documented in wp-includes/ms-blogs.php */ … … 925 925 if ( did_action( 'init' ) ) { 926 926 $wp_roles = new WP_Roles(); 927 927 $current_user = wp_get_current_user(); 928 $current_user->for_ blog( $blog );928 $current_user->for_site( $blog ); 929 929 } 930 930 931 931 /** This filter is documented in wp-includes/ms-blogs.php */ -
tests/phpunit/tests/user/capabilities.php
1848 1848 $this->assertFalse( user_can( self::$users['contributor']->ID, 'remove_user', self::$users['contributor']->ID ) ); 1849 1849 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1850 1850 } 1851 1852 /** 1853 * @ticket 36961 1854 * @group ms-required 1855 */ 1856 function test_init_user_caps_for_different_site() { 1857 global $wpdb; 1858 1859 $site_id = self::factory()->blog->create( array( 'user_id' => self::$users['administrator']->ID ) ); 1860 1861 switch_to_blog( $site_id ); 1862 1863 $role_name = 'uploader'; 1864 add_role( $role_name, 'Uploader', array( 1865 'read' => true, 1866 'upload_files' => true, 1867 ) ); 1868 add_user_to_blog( $site_id, self::$users['subscriber']->ID, $role_name ); 1869 1870 restore_current_blog(); 1871 1872 $user = new WP_User( self::$users['subscriber']->ID, '', $site_id ); 1873 $this->assertTrue( $user->has_cap( 'upload_files' ) ); 1874 } 1875 1876 /** 1877 * @ticket 36961 1878 * @group ms-required 1879 */ 1880 function test_init_user_caps_for_different_site_by_user_switch() { 1881 global $wpdb; 1882 1883 $user = new WP_User( self::$users['subscriber']->ID ); 1884 1885 $site_id = self::factory()->blog->create( array( 'user_id' => self::$users['administrator']->ID ) ); 1886 1887 switch_to_blog( $site_id ); 1888 1889 $role_name = 'uploader'; 1890 add_role( $role_name, 'Uploader', array( 1891 'read' => true, 1892 'upload_files' => true, 1893 ) ); 1894 add_user_to_blog( $site_id, self::$users['subscriber']->ID, $role_name ); 1895 1896 restore_current_blog(); 1897 1898 $user->for_site( $site_id ); 1899 $this->assertTrue( $user->has_cap( 'upload_files' ) ); 1900 } 1851 1901 } -
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 }