Ticket #38645: 38645.5.diff
File 38645.5.diff, 8.0 KB (added by , 8 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; 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 } 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 } 120 _deprecated_function( __METHOD__, '4.9.0', 'WP_Roles::for_site()' ); 123 121 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 available roles data. 324 * 325 * @since 4.9.0 326 * 327 * @global array $wp_user_roles Used to set the 'roles' property value. 328 * 329 * @return array Roles array. 330 */ 331 protected function get_roles_data() { 332 global $wp_user_roles; 333 334 if ( ! empty( $wp_user_roles ) ) { 335 return $wp_user_roles; 336 } 337 338 if ( $this->site_id != get_current_blog_id() ) { 339 remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 ); 340 341 $roles = get_blog_option( $this->site_id, $this->role_key, array() ); 342 343 add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 ); 344 345 return $roles; 346 } 347 348 return get_option( $this->role_key, array() ); 349 } 273 350 } -
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
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 38645 1854 * @group ms-required 1855 */ 1856 function test_init_roles_for_different_site() { 1857 global $wpdb; 1858 1859 $site_id = self::factory()->blog->create(); 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 1869 restore_current_blog(); 1870 1871 $wp_roles = wp_roles(); 1872 $wp_roles->for_blog( $site_id ); 1873 1874 $this->assertTrue( isset( $wp_roles->role_objects[ $role_name ] ) ); 1875 } 1851 1876 }