Ticket #38645: 38645.4.diff
File 38645.4.diff, 8.5 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-roles.php
65 65 public $use_db = true; 66 66 67 67 /** 68 * Constructor68 * The site ID the roles are initialized for. 69 69 * 70 * @since 2.0.0 70 * @since 4.9.0 71 * @var int 71 72 */ 72 public function __construct() { 73 $this->_init(); 74 } 73 protected $blog_id = 0; 75 74 76 75 /** 77 * Make private/protected methods readable for backward compatibility.76 * Constructor 78 77 * 79 * @since 4.0.0 78 * @since 2.0.0 79 * @since 4.9.0 The $blog_id argument was added. 80 80 * 81 * @ param callable $name Method to call.82 * @param array $arguments Arguments to pass when calling.83 * @ return mixed|false Return value of the callback, false otherwise.81 * @global array $wp_user_roles Used to set the 'roles' property value. 82 * 83 * @param int $blog_id Site ID to initialize roles for. Default is the current site. 84 84 */ 85 public function __call( $name, $arguments ) { 86 if ( '_init' === $name ) { 87 return call_user_func_array( array( $this, $name ), $arguments ); 88 } 89 return false; 85 public function __construct( $blog_id = null ) { 86 global $wp_user_roles; 87 88 $this->use_db = empty( $wp_user_roles ); 89 90 $this->for_blog( $blog_id ); 90 91 } 91 92 92 93 /** … … 97 98 * be used and the role option will not be updated or used. 98 99 * 99 100 * @since 2.1.0 100 * 101 * @global array $wp_user_roles Used to set the 'roles' property value. 101 * @deprecated 4.9.0 Use WP_Roles::for_blog() 102 102 */ 103 103 protected function _init() { 104 global $wp_user_roles, $wpdb; 105 106 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles'; 107 if ( ! empty( $wp_user_roles ) ) { 108 $this->roles = $wp_user_roles; 109 $this->use_db = false; 110 } else { 111 $this->roles = get_option( $this->role_key ); 112 } 104 _deprecated_function( __METHOD__, '4.9.0', 'WP_Roles::for_blog()' ); 113 105 114 if ( empty( $this->roles ) ) 115 return; 116 117 $this->role_objects = array(); 118 $this->role_names = array(); 119 foreach ( array_keys( $this->roles ) as $role ) { 120 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] ); 121 $this->role_names[$role] = $this->roles[$role]['name']; 122 } 123 124 /** 125 * After the roles have been initialized, allow plugins to add their own roles. 126 * 127 * @since 4.7.0 128 * 129 * @param WP_Roles $this A reference to the WP_Roles object. 130 */ 131 do_action( 'wp_roles_init', $this ); 106 $this->for_blog(); 132 107 } 133 108 134 109 /** … … 138 113 * after switching wpdb to a new site ID. 139 114 * 140 115 * @since 3.5.0 141 * @deprecated 4.7.0 Use new WP_Roles()116 * @deprecated 4.7.0 Use WP_Roles::for_blog() 142 117 */ 143 118 public function reinit() { 144 _deprecated_function( __METHOD__, '4.7.0', 'new WP_Roles()' ); 145 $this->_init(); 119 _deprecated_function( __METHOD__, '4.7.0', 'WP_Roles::for_blog()' ); 120 121 $this->for_blog(); 146 122 } 147 123 148 124 /** … … 270 246 public function is_role( $role ) { 271 247 return isset( $this->role_names[$role] ); 272 248 } 249 250 /** 251 * Initializes all of the available roles. 252 * 253 * @since 4.9.0 254 */ 255 public function init_roles() { 256 if ( empty( $this->roles ) ) { 257 return; 258 } 259 260 $this->role_objects = array(); 261 $this->role_names = array(); 262 foreach ( array_keys( $this->roles ) as $role ) { 263 $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] ); 264 $this->role_names[ $role ] = $this->roles[ $role ]['name']; 265 } 266 267 /** 268 * After the roles have been initialized, allow plugins to add their own roles. 269 * 270 * @since 4.7.0 271 * 272 * @param WP_Roles $this A reference to the WP_Roles object. 273 */ 274 do_action( 'wp_roles_init', $this ); 275 } 276 277 /** 278 * Sets the site to operate on. Defaults to the current site. 279 * 280 * @since 4.9.0 281 * 282 * @global wpdb $wpdb WordPress database abstraction object. 283 * 284 * @param int $blog_id Site ID to initialize roles for. Default is the current site. 285 */ 286 public function for_blog( $blog_id = null ) { 287 global $wpdb; 288 289 if ( ! empty( $blog_id ) ) { 290 $this->blog_id = absint( $blog_id ); 291 } else { 292 $this->blog_id = get_current_blog_id(); 293 } 294 295 $this->role_key = $wpdb->get_blog_prefix( $this->blog_id ) . 'user_roles'; 296 297 if ( ! empty( $this->roles ) && ! $this->use_db ) { 298 return; 299 } 300 301 $this->roles = $this->get_roles_data(); 302 303 $this->init_roles(); 304 } 305 306 /** 307 * Gets the available roles data. 308 * 309 * @since 4.9.0 310 * 311 * @global array $wp_user_roles Used to set the 'roles' property value. 312 * 313 * @return array Roles array. 314 */ 315 protected function get_roles_data() { 316 global $wp_user_roles; 317 318 if ( ! empty( $wp_user_roles ) ) { 319 return $wp_user_roles; 320 } 321 322 if ( $this->blog_id != get_current_blog_id() ) { 323 remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 ); 324 325 $roles = get_blog_option( $this->blog_id, $this->role_key, array() ); 326 327 add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 ); 328 329 return $roles; 330 } 331 332 return get_option( $this->role_key, array() ); 333 } 273 334 } -
src/wp-includes/ms-blogs.php
793 793 * @return true Always returns True. 794 794 */ 795 795 function switch_to_blog( $new_blog, $deprecated = null ) { 796 global $wpdb , $wp_roles;796 global $wpdb; 797 797 798 798 $blog_id = get_current_blog_id(); 799 799 if ( empty( $new_blog ) ) { … … 848 848 } 849 849 } 850 850 851 if ( did_action( 'init' ) ) {852 $wp_roles = new WP_Roles();853 $current_user = wp_get_current_user();854 $current_user->for_blog( $new_blog );855 }856 857 851 /** This filter is documented in wp-includes/ms-blogs.php */ 858 852 do_action( 'switch_blog', $new_blog, $prev_blog_id ); 859 853 $GLOBALS['switched'] = true; … … 877 871 * @return bool True on success, false if we're already on the current blog 878 872 */ 879 873 function restore_current_blog() { 880 global $wpdb , $wp_roles;874 global $wpdb; 881 875 882 876 if ( empty( $GLOBALS['_wp_switched_stack'] ) ) { 883 877 return false; … … 922 916 } 923 917 } 924 918 925 if ( did_action( 'init' ) ) {926 $wp_roles = new WP_Roles();927 $current_user = wp_get_current_user();928 $current_user->for_blog( $blog );929 }930 931 919 /** This filter is documented in wp-includes/ms-blogs.php */ 932 920 do_action( 'switch_blog', $blog, $prev_blog_id ); 933 921 … … 938 926 } 939 927 940 928 /** 929 * Switches the initialized roles and current user capabilities to another site. 930 * 931 * @since 4.9.0 932 * 933 * @param int $new_site_id New site ID. 934 * @param int $old_site_id Old site ID. 935 */ 936 function wp_switch_roles_and_user( $new_site_id, $old_site_id ) { 937 if ( $new_site_id == $old_site_id ) { 938 return; 939 } 940 941 if ( ! did_action( 'init' ) ) { 942 return; 943 } 944 945 wp_roles()->for_blog( $new_site_id ); 946 wp_get_current_user()->for_blog( $new_site_id ); 947 } 948 949 /** 941 950 * Determines if switch_to_blog() is in effect 942 951 * 943 952 * @since 3.5.0 -
src/wp-includes/ms-default-filters.php
32 32 add_action( 'network_user_new_created_user', 'wp_send_new_user_notifications' ); 33 33 add_filter( 'sanitize_user', 'strtolower' ); 34 34 35 // Roles 36 add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 ); 37 35 38 // Blogs 36 39 add_filter( 'wpmu_validate_blog_signup', 'signup_nonce_check' ); 37 40 add_action( 'wpmu_new_blog', 'wpmu_log_new_registrations', 10, 2 ); -
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 38645 1846 * @group ms-required 1847 */ 1848 function test_init_roles_for_different_site() { 1849 global $wpdb; 1850 1851 $site_id = self::factory()->blog->create(); 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 1861 restore_current_blog(); 1862 1863 $wp_roles = wp_roles(); 1864 $wp_roles->for_blog( $site_id ); 1865 1866 $this->assertTrue( isset( $wp_roles->role_objects[ $role_name ] ) ); 1867 } 1843 1868 }