mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@39082 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -41,8 +41,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
||||
// this will flush everything and reload it from the db
|
||||
unset($GLOBALS['wp_user_roles']);
|
||||
global $wp_roles;
|
||||
if ( is_object( $wp_roles ) )
|
||||
$wp_roles->_init();
|
||||
$wp_roles = new WP_Roles();
|
||||
}
|
||||
|
||||
function _meta_yes_you_can( $can, $key, $post_id, $user_id, $cap, $caps ) {
|
||||
@@ -1626,4 +1625,33 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
||||
$this->assertFalse( current_user_can( 'do_not_allow' ), "Non-logged-in user should not have the do_not_allow capability" );
|
||||
}
|
||||
|
||||
protected $_role_test_wp_roles_role;
|
||||
/**
|
||||
* @ticket 23016
|
||||
*/
|
||||
public function test_wp_roles_init_action() {
|
||||
$this->_role_test_wp_roles_init = array(
|
||||
'role' => 'test_wp_roles_init',
|
||||
'info' => array(
|
||||
'name' => 'Test WP Roles Init',
|
||||
'capabilities' => array( 'testing_magic' => true ),
|
||||
),
|
||||
);
|
||||
add_action( 'wp_roles_init', array( $this, '_hook_wp_roles_init' ), 10, 1 );
|
||||
|
||||
$wp_roles = new WP_Roles();
|
||||
|
||||
remove_action( 'wp_roles_init', array( $this, '_hook_wp_roles_init' ) );
|
||||
|
||||
$expected = new WP_Role( $this->_role_test_wp_roles_init['role'], $this->_role_test_wp_roles_init['info']['capabilities'] );
|
||||
|
||||
$role = $wp_roles->get_role( $this->_role_test_wp_roles_init['role'] );
|
||||
|
||||
$this->assertEquals( $expected, $role );
|
||||
$this->assertContains( $this->_role_test_wp_roles_init['info']['name'], $wp_roles->role_names );
|
||||
}
|
||||
|
||||
public function _hook_wp_roles_init( $wp_roles ) {
|
||||
$wp_roles->add_role( $this->_role_test_wp_roles_init['role'], $this->_role_test_wp_roles_init['info']['name'], $this->_role_test_wp_roles_init['info']['capabilities'] );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,6 +396,34 @@ class Tests_Multisite_User extends WP_UnitTestCase {
|
||||
|
||||
$this->assertWPError( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 23016
|
||||
*/
|
||||
public function test_wp_roles_global_is_reset() {
|
||||
global $wp_roles;
|
||||
$role = 'test_global_is_reset';
|
||||
$role_name = 'Test Global Is Reset';
|
||||
$blog_id = self::factory()->blog->create();
|
||||
|
||||
$wp_roles->add_role( $role, $role_name, array() );
|
||||
|
||||
$this->assertNotEmpty( $wp_roles->get_role( $role ) );
|
||||
|
||||
switch_to_blog( $blog_id );
|
||||
|
||||
$this->assertEmpty( $wp_roles->get_role( $role ) );
|
||||
|
||||
$wp_roles->add_role( $role, $role_name, array() );
|
||||
|
||||
$this->assertNotEmpty( $wp_roles->get_role( $role ) );
|
||||
|
||||
restore_current_blog();
|
||||
|
||||
$this->assertNotEmpty( $wp_roles->get_role( $role ) );
|
||||
|
||||
$wp_roles->remove_role( $role );
|
||||
}
|
||||
}
|
||||
|
||||
endif ;
|
||||
|
||||
Reference in New Issue
Block a user