Ticket #36961: 36961.6.diff
File 36961.6.diff, 8.5 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 … … 431 423 * used. 432 424 * 433 425 * @since 2.1.0 426 * @deprecated 4.9.0 Use WP_User::for_site() 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_site()' ); 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->site_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 } … … 465 458 * @return array List of all capabilities for the user. 466 459 */ 467 460 public function get_role_caps() { 461 $switch_site = false; 462 if ( is_multisite() && $this->site_id != get_current_blog_id() ) { 463 $switch_site = true; 464 465 switch_to_blog( $this->site_id ); 466 } 467 468 468 $wp_roles = wp_roles(); 469 469 470 470 //Filter out caps that are not role names and assign to $this->roles … … 479 479 } 480 480 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); 481 481 482 if ( $switch_site ) { 483 restore_current_blog(); 484 } 485 482 486 return $this->allcaps; 483 487 } 484 488 … … 753 757 * Set the site to operate on. Defaults to the current site. 754 758 * 755 759 * @since 3.0.0 760 * @deprecated 4.9.0 Use WP_User::for_site() 756 761 * 757 762 * @global wpdb $wpdb WordPress database abstraction object. 758 763 * 759 764 * @param int $blog_id Optional. Site ID, defaults to current site. 760 765 */ 761 766 public function for_blog( $blog_id = '' ) { 767 _deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' ); 768 769 $this->for_site( $blog_id ); 770 } 771 772 /** 773 * Sets the site to operate on. Defaults to the current site. 774 * 775 * @since 4.9.0 776 * 777 * @global wpdb $wpdb WordPress database abstraction object. 778 * 779 * @param int $site_id Site ID to initialize user capabilities for. Default is the current site. 780 */ 781 public function for_site( $site_id = '' ) { 762 782 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 ); 783 784 if ( ! empty( $site_id ) ) { 785 $this->site_id = absint( $site_id ); 786 } else { 787 $this->site_id = get_current_blog_id(); 788 } 789 790 $this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities'; 791 792 $this->caps = $this->get_caps_data(); 793 794 $this->get_role_caps(); 795 } 796 797 /** 798 * Gets the available user capabilities data. 799 * 800 * @since 4.9.0 801 * 802 * @return array User capabilities array. 803 */ 804 private function get_caps_data() { 805 $caps = get_user_meta( $this->ID, $this->cap_key, true ); 806 807 if ( ! is_array( $caps ) ) { 808 return array(); 809 } 810 811 return $caps; 768 812 } 769 813 } -
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
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 } 1867 1868 /** 1869 * @ticket 36961 1870 * @group ms-required 1871 */ 1872 function test_init_user_caps_for_different_site_by_user_switch() { 1873 global $wpdb; 1874 1875 $user = new WP_User( self::$users['subscriber']->ID ); 1876 1877 $site_id = self::factory()->blog->create( array( 'user_id' => self::$users['administrator']->ID ) ); 1878 1879 switch_to_blog( $site_id ); 1880 1881 $role_name = 'uploader'; 1882 add_role( $role_name, 'Uploader', array( 1883 'read' => true, 1884 'upload_files' => true, 1885 ) ); 1886 add_user_to_blog( $site_id, self::$users['subscriber']->ID, $role_name ); 1887 1888 restore_current_blog(); 1889 1890 $user->for_site( $site_id ); 1891 $this->assertTrue( $user->has_cap( 'upload_files' ) ); 1892 } 1843 1893 } -
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 }