Ticket #38645: 38645.6.diff
File 38645.6.diff, 9.1 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-roles.php
65 65 public $use_db = true; 66 66 67 67 /** 68 * The site ID the roles are initialized for. 69 * 70 * @since 4.9.0 71 * @var int 72 */ 73 protected $site_id = 0; 74 75 /** 68 76 * Constructor 69 77 * 70 78 * @since 2.0.0 79 * @since 4.9.0 The $site_id argument was added. 80 * 81 * @global array $wp_user_roles Used to set the 'roles' property value. 82 * 83 * @param int $site_id Site ID to initialize roles for. Default is the current site. 71 84 */ 72 public function __construct() { 73 $this->_init(); 85 public function __construct( $site_id = null ) { 86 global $wp_user_roles; 87 88 $this->use_db = empty( $wp_user_roles ); 89 90 $this->for_site( $site_id ); 74 91 } 75 92 76 93 /** … … 97 114 * be used and the role option will not be updated or used. 98 115 * 99 116 * @since 2.1.0 100 * 101 * @global array $wp_user_roles Used to set the 'roles' property value. 117 * @deprecated 4.9.0 Use WP_Roles::for_site() 102 118 */ 103 119 protected function _init() { 104 global $wp_user_roles, $wpdb;120 _deprecated_function( __METHOD__, '4.9.0', 'WP_Roles::for_site()' ); 105 121 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 } 113 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 ); 122 $this->for_site(); 132 123 } 133 124 134 125 /** … … 138 129 * after switching wpdb to a new site ID. 139 130 * 140 131 * @since 3.5.0 141 * @deprecated 4.7.0 Use new WP_Roles()132 * @deprecated 4.7.0 Use WP_Roles::for_site() 142 133 */ 143 134 public function reinit() { 144 _deprecated_function( __METHOD__, '4.7.0', 'new WP_Roles()' ); 145 $this->_init(); 135 _deprecated_function( __METHOD__, '4.7.0', 'WP_Roles::for_site()' ); 136 137 $this->for_site(); 146 138 } 147 139 148 140 /** … … 270 262 public function is_role( $role ) { 271 263 return isset( $this->role_names[$role] ); 272 264 } 265 266 /** 267 * Initializes all of the available roles. 268 * 269 * @since 4.9.0 270 */ 271 public function init_roles() { 272 if ( empty( $this->roles ) ) { 273 return; 274 } 275 276 $this->role_objects = array(); 277 $this->role_names = array(); 278 foreach ( array_keys( $this->roles ) as $role ) { 279 $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] ); 280 $this->role_names[ $role ] = $this->roles[ $role ]['name']; 281 } 282 283 /** 284 * After the roles have been initialized, allow plugins to add their own roles. 285 * 286 * @since 4.7.0 287 * 288 * @param WP_Roles $this A reference to the WP_Roles object. 289 */ 290 do_action( 'wp_roles_init', $this ); 291 } 292 293 /** 294 * Sets the site to operate on. Defaults to the current site. 295 * 296 * @since 4.9.0 297 * 298 * @global wpdb $wpdb WordPress database abstraction object. 299 * 300 * @param int $site_id Site ID to initialize roles for. Default is the current site. 301 */ 302 public function for_site( $site_id = null ) { 303 global $wpdb; 304 305 if ( ! empty( $site_id ) ) { 306 $this->site_id = absint( $site_id ); 307 } else { 308 $this->site_id = get_current_blog_id(); 309 } 310 311 $this->role_key = $wpdb->get_blog_prefix( $this->site_id ) . 'user_roles'; 312 313 if ( ! empty( $this->roles ) && ! $this->use_db ) { 314 return; 315 } 316 317 $this->roles = $this->get_roles_data(); 318 319 $this->init_roles(); 320 } 321 322 /** 323 * Gets the ID of the site for which roles are currently initialized. 324 * 325 * @since 4.9.0 326 * 327 * @return int Site ID. 328 */ 329 public function get_site_id() { 330 return $this->site_id; 331 } 332 333 /** 334 * Gets the available roles data. 335 * 336 * @since 4.9.0 337 * 338 * @global array $wp_user_roles Used to set the 'roles' property value. 339 * 340 * @return array Roles array. 341 */ 342 protected function get_roles_data() { 343 global $wp_user_roles; 344 345 if ( ! empty( $wp_user_roles ) ) { 346 return $wp_user_roles; 347 } 348 349 if ( is_multisite() && $this->site_id != get_current_blog_id() ) { 350 remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 ); 351 352 $roles = get_blog_option( $this->site_id, $this->role_key, array() ); 353 354 add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 ); 355 356 return $roles; 357 } 358 359 return get_option( $this->role_key, array() ); 360 } 273 361 } -
src/wp-includes/ms-blogs.php
792 792 * @return true Always returns True. 793 793 */ 794 794 function switch_to_blog( $new_blog, $deprecated = null ) { 795 global $wpdb , $wp_roles;795 global $wpdb; 796 796 797 797 $blog_id = get_current_blog_id(); 798 798 if ( empty( $new_blog ) ) { … … 847 847 } 848 848 } 849 849 850 if ( did_action( 'init' ) ) {851 $wp_roles = new WP_Roles();852 $current_user = wp_get_current_user();853 $current_user->for_site( $new_blog );854 }855 856 850 /** This filter is documented in wp-includes/ms-blogs.php */ 857 851 do_action( 'switch_blog', $new_blog, $prev_blog_id ); 858 852 $GLOBALS['switched'] = true; … … 876 870 * @return bool True on success, false if we're already on the current blog 877 871 */ 878 872 function restore_current_blog() { 879 global $wpdb , $wp_roles;873 global $wpdb; 880 874 881 875 if ( empty( $GLOBALS['_wp_switched_stack'] ) ) { 882 876 return false; … … 921 915 } 922 916 } 923 917 924 if ( did_action( 'init' ) ) {925 $wp_roles = new WP_Roles();926 $current_user = wp_get_current_user();927 $current_user->for_site( $blog );928 }929 930 918 /** This filter is documented in wp-includes/ms-blogs.php */ 931 919 do_action( 'switch_blog', $blog, $prev_blog_id ); 932 920 … … 937 925 } 938 926 939 927 /** 928 * Switches the initialized roles and current user capabilities to another site. 929 * 930 * @since 4.9.0 931 * 932 * @param int $new_site_id New site ID. 933 * @param int $old_site_id Old site ID. 934 */ 935 function wp_switch_roles_and_user( $new_site_id, $old_site_id ) { 936 if ( $new_site_id == $old_site_id ) { 937 return; 938 } 939 940 if ( ! did_action( 'init' ) ) { 941 return; 942 } 943 944 wp_roles()->for_site( $new_site_id ); 945 wp_get_current_user()->for_site( $new_site_id ); 946 } 947 948 /** 940 949 * Determines if switch_to_blog() is in effect 941 950 * 942 951 * @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
1939 1939 1940 1940 $this->assertSame( 333, $user->get_site_id() ); 1941 1941 } 1942 1943 /** 1944 * @ticket 38645 1945 * @group ms-required 1946 */ 1947 function test_init_roles_for_different_site() { 1948 global $wpdb; 1949 1950 $site_id = self::factory()->blog->create(); 1951 1952 switch_to_blog( $site_id ); 1953 1954 $role_name = 'uploader'; 1955 add_role( $role_name, 'Uploader', array( 1956 'read' => true, 1957 'upload_files' => true, 1958 ) ); 1959 1960 restore_current_blog(); 1961 1962 $wp_roles = wp_roles(); 1963 $wp_roles->for_site( $site_id ); 1964 1965 $this->assertTrue( isset( $wp_roles->role_objects[ $role_name ] ) ); 1966 } 1967 1968 /** 1969 * @ticket 38645 1970 */ 1971 function test_get_roles_data() { 1972 global $wpdb; 1973 1974 $custom_roles = array( 1975 'test_role' => array( 1976 'name' => 'Test Role', 1977 'capabilities' => array( 1978 'do_foo' => true, 1979 'do_bar' => false, 1980 ), 1981 ), 1982 ); 1983 1984 // Test `WP_Roles::get_roles_data()` by manually setting the roles option. 1985 update_option( $wpdb->get_blog_prefix( get_current_blog_id() ) . 'user_roles', $custom_roles ); 1986 1987 $roles = new WP_Roles(); 1988 $this->assertSame( $custom_roles, $roles->roles ); 1989 } 1990 1991 /** 1992 * @ticket 38645 1993 */ 1994 function test_roles_get_site_id_default() { 1995 $roles = new WP_Roles(); 1996 $this->assertSame( get_current_blog_id(), $roles->get_site_id() ); 1997 } 1998 1999 /** 2000 * @ticket 38645 2001 */ 2002 function test_roles_get_site_id() { 2003 global $wpdb; 2004 2005 // Suppressing errors here allows to get around creating an actual site, 2006 // which is unnecessary for this test. 2007 $suppress = $wpdb->suppress_errors(); 2008 $roles = new WP_Roles( 333 ); 2009 $wpdb->suppress_errors( $suppress ); 2010 2011 $this->assertSame( 333, $roles->get_site_id() ); 2012 } 1942 2013 }