From e79fa033291974cfab191ee5b1701c52def1478a Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Wed, 2 Nov 2016 00:31:32 +0000 Subject: [PATCH] Roles/Capabilities: Add a new `wp_roles_init` filter. Historically, it's been difficult to extend user roles, but reasonable to work around by waiting until after `init` has fired, to add custom roles and capabilities. With the addition of Locale Switching, Core now potentially loads roles before `init` has fired, leaving a window where custom roles and capabilities are not handled. The new filter allows plugins to add their own custom roles whenever they're initialised (on page load, or when switching sites, for example), so that they can always be obeyed. `WP_Roles` has also been tidied up a little bit, to remove duplicate code. Props johnjamesjacoby, pento. Fixes #23016. Built from https://develop.svn.wordpress.org/trunk@39082 git-svn-id: http://core.svn.wordpress.org/trunk@39024 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-roles.php | 31 ++++++++++++------------------- wp-includes/ms-blogs.php | 8 ++++---- wp-includes/version.php | 2 +- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/wp-includes/class-wp-roles.php b/wp-includes/class-wp-roles.php index b7558748db..a665191f4a 100644 --- a/wp-includes/class-wp-roles.php +++ b/wp-includes/class-wp-roles.php @@ -127,6 +127,15 @@ class WP_Roles { $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] ); $this->role_names[$role] = $this->roles[$role]['name']; } + + /** + * After the roles have been initialized, allow plugins to add their own roles. + * + * @since 4.7.0 + * + * @param WP_Roles A reference to the WP_Roles object. + */ + do_action( 'wp_roles_init', $this ); } /** @@ -136,28 +145,12 @@ class WP_Roles { * after switching wpdb to a new site ID. * * @since 3.5.0 + * @deprecated 4.7.0 Use new WP_Roles() * @access public */ public function reinit() { - global $wpdb; - - // There is no need to reinit if using the wp_user_roles global. - if ( ! $this->use_db ) { - return; - } - - // Duplicated from _init() to avoid an extra function call. - $this->role_key = $wpdb->get_blog_prefix() . 'user_roles'; - $this->roles = get_option( $this->role_key ); - if ( empty( $this->roles ) ) - return; - - $this->role_objects = array(); - $this->role_names = array(); - foreach ( array_keys( $this->roles ) as $role ) { - $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] ); - $this->role_names[$role] = $this->roles[$role]['name']; - } + __deprecated_function( __METHOD__, '4.7.0', 'new WP_Roles()' ); + $this->_init(); } /** diff --git a/wp-includes/ms-blogs.php b/wp-includes/ms-blogs.php index db3e4639c1..9188750386 100644 --- a/wp-includes/ms-blogs.php +++ b/wp-includes/ms-blogs.php @@ -766,7 +766,7 @@ function update_blog_option( $id, $option, $value, $deprecated = null ) { * @return true Always returns True. */ function switch_to_blog( $new_blog, $deprecated = null ) { - global $wpdb; + global $wpdb, $wp_roles; $blog_id = get_current_blog_id(); if ( empty( $new_blog ) ) { @@ -822,7 +822,7 @@ function switch_to_blog( $new_blog, $deprecated = null ) { } if ( did_action( 'init' ) ) { - wp_roles()->reinit(); + $wp_roles = new WP_Roles(); $current_user = wp_get_current_user(); $current_user->for_blog( $new_blog ); } @@ -850,7 +850,7 @@ function switch_to_blog( $new_blog, $deprecated = null ) { * @return bool True on success, false if we're already on the current blog */ function restore_current_blog() { - global $wpdb; + global $wpdb, $wp_roles; if ( empty( $GLOBALS['_wp_switched_stack'] ) ) { return false; @@ -896,7 +896,7 @@ function restore_current_blog() { } if ( did_action( 'init' ) ) { - wp_roles()->reinit(); + $wp_roles = new WP_Roles(); $current_user = wp_get_current_user(); $current_user->for_blog( $blog ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 661b3d0c68..523c431585 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-beta1-39081'; +$wp_version = '4.7-beta1-39082'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.