Ticket #38645: 38645.diff
File 38645.diff, 6.2 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-roles.php
70 70 public $use_db = true; 71 71 72 72 /** 73 * What role type to get from the database 74 * 75 * @since 4.8.0 76 * 77 * @access public 78 * @var string 79 */ 80 public $role_type = 'user_roles'; 81 82 /** 73 83 * Constructor 74 84 * 75 85 * @since 2.0.0 86 * 87 * @param int $site_id Optional. The site ID to initialize for. Default is the current site. 76 88 */ 77 public function __construct() { 78 $this->_init(); 89 public function __construct( $site_id = null ) { 90 $this->use_db = empty( $GLOBALS['wp_user_roles'] ); 91 92 $this->for_site( $site_id ); 79 93 } 80 94 81 95 /** … … 99 113 * Set up the object properties. 100 114 * 101 115 * The role key is set to the current prefix for the $wpdb object with 102 * 'user_roles'appended. If the $wp_user_roles global is set, then it will116 * the role_type appended. If the $wp_user_roles global is set, then it will 103 117 * be used and the role option will not be updated or used. 104 118 * 105 119 * @since 2.1.0 106 120 * @access protected 107 121 * 108 * @ global array $wp_user_roles Used to set the 'roles' property value.122 * @param string $role_key Optional role key. 109 123 */ 110 protected function _init( ) {111 global $wp _user_roles, $wpdb;124 protected function _init( $role_key = '' ) { 125 global $wpdb; 112 126 113 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles'; 114 if ( ! empty( $wp_user_roles ) ) { 115 $this->roles = $wp_user_roles; 116 $this->use_db = false; 127 // Fallback on current site's prefix if no role key was passed 128 if ( empty( $role_key ) ) { 129 $this->role_key = $wpdb->get_blog_prefix() . $this->role_type; 117 130 } else { 118 $this->role s = get_option( $this->role_key );131 $this->role_key = $role_key; 119 132 } 120 133 121 if ( empty( $this->roles ) ) 122 return; 134 // Use the $wp_user_roles global if one was set 135 if ( false === $this->use_db ) { 136 $this->roles = $GLOBALS['wp_user_roles']; 137 } else { 138 $this->roles = get_option( $this->role_key ); 139 } 123 140 124 $this->role_objects = array(); 125 $this->role_names = array(); 126 foreach ( array_keys( $this->roles ) as $role ) { 127 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] ); 128 $this->role_names[$role] = $this->roles[$role]['name']; 141 // Set role names & objects from the roles array 142 if ( ! empty( $this->roles ) ) { 143 $this->role_objects = array(); 144 $this->role_names = array(); 145 146 foreach ( array_keys( $this->roles ) as $role ) { 147 $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] ); 148 $this->role_names[ $role ] = $this->roles[ $role ]['name']; 149 } 129 150 } 130 151 131 152 /** … … 142 163 * Reinitialize the object 143 164 * 144 165 * Recreates the role objects. This is typically called only by switch_to_blog() 145 * after switching wpdb to a new site ID. 166 * after switching wpdb to a new site ID, and is skipped entirely when the 167 * $wp_user_roles omega global is used. 146 168 * 147 169 * @since 3.5.0 148 * @deprecated 4.7.0 Use new WP_Roles()149 170 * @access public 150 171 */ 151 172 public function reinit() { 152 _deprecated_function( __METHOD__, '4.7.0', 'new WP_Roles()' ); 153 $this->_init(); 173 _deprecated_function( __METHOD__, '4.7.0', 'WP_Roles::for_site()' ); 174 if ( $this->use_db ) { 175 $this->_init(); 176 } 177 } 178 179 /** 180 * Set the site to operate on. Defaults to the current site. 181 * 182 * @since 4.8.0 183 * 184 * @global WPDB $wpdb 185 * 186 * @param int $site_id Optional. The site ID to initialize for. Default is the current site. 187 */ 188 public function for_site( $site_id = null ) { 189 global $wpdb; 190 191 if ( ! $site_id ) { 192 $site_id = get_current_blog_id(); 193 } 194 195 // Set the role_key based on the prefix for the blog_id 196 $role_key = $wpdb->get_blog_prefix( $site_id ) . $this->role_type; 197 198 // Initialize roles based on key 199 $this->_init( $role_key ); 154 200 } 155 201 156 202 /** -
src/wp-includes/ms-blogs.php
766 766 * @return true Always returns True. 767 767 */ 768 768 function switch_to_blog( $new_blog, $deprecated = null ) { 769 global $wpdb , $wp_roles;769 global $wpdb; 770 770 771 771 $blog_id = get_current_blog_id(); 772 772 if ( empty( $new_blog ) ) { … … 821 821 } 822 822 } 823 823 824 if ( did_action( 'init' ) ) {825 $wp_roles = new WP_Roles();826 $current_user = wp_get_current_user();827 $current_user->for_blog( $new_blog );828 }829 830 824 /** This filter is documented in wp-includes/ms-blogs.php */ 831 825 do_action( 'switch_blog', $new_blog, $prev_blog_id ); 832 826 $GLOBALS['switched'] = true; … … 850 844 * @return bool True on success, false if we're already on the current blog 851 845 */ 852 846 function restore_current_blog() { 853 global $wpdb , $wp_roles;847 global $wpdb; 854 848 855 849 if ( empty( $GLOBALS['_wp_switched_stack'] ) ) { 856 850 return false; … … 895 889 } 896 890 } 897 891 898 if ( did_action( 'init' ) ) {899 $wp_roles = new WP_Roles();900 $current_user = wp_get_current_user();901 $current_user->for_blog( $blog );902 }903 904 892 /** This filter is documented in wp-includes/ms-blogs.php */ 905 893 do_action( 'switch_blog', $blog, $prev_blog_id ); 906 894 … … 911 899 } 912 900 913 901 /** 902 * Switch WP_Roles & current_user roles & caps, usually when switching sites. 903 * 904 * @since 4.8.0 905 * 906 * @param int $new_site_id 907 * @param int $old_site_id 908 */ 909 function switch_wp_roles( $new_site_id = 0, $old_site_id = 0 ) { 910 if ( $new_site_id != $old_site_id ) { 911 wp_roles()->for_site( $new_site_id ); 912 wp_get_current_user()->for_blog( $new_site_id ); 913 } 914 } 915 916 /** 914 917 * Determines if switch_to_blog() is in effect 915 918 * 916 919 * @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', 'switch_wp_roles', 8, 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 );