Skip to content

Commit 8eec9f2

Browse files
committed
Role/Capability: Introduce the current_user_can_for_site() and user_can_for_site() functions.
The `current_user_can_for_site()` function is a replacement for `current_user_can_for_blog()` which is now deprecated. `user_can_for_site()` is a renaming of the `user_can_for_blog()` function which was introduced in [59123]. The intention of this change is to prevent the introduction of a new function which uses the old "blog" naming structure. Props swissspidy, spacedmonkey, flixos90, johnjamesjacoby Fixes #45197 git-svn-id: https://develop.svn.wordpress.org/trunk@59198 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ee61e60 commit 8eec9f2

File tree

3 files changed

+58
-41
lines changed

3 files changed

+58
-41
lines changed

src/wp-includes/capabilities.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -913,24 +913,23 @@ function current_user_can( $capability, ...$args ) {
913913
* capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to
914914
* map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`.
915915
*
916+
* This function replaces the current_user_can_for_blog() function.
917+
*
916918
* Example usage:
917919
*
918-
* current_user_can_for_blog( $blog_id, 'edit_posts' );
919-
* current_user_can_for_blog( $blog_id, 'edit_post', $post->ID );
920-
* current_user_can_for_blog( $blog_id, 'edit_post_meta', $post->ID, $meta_key );
920+
* current_user_can_for_site( $site_id, 'edit_posts' );
921+
* current_user_can_for_site( $site_id, 'edit_post', $post->ID );
922+
* current_user_can_for_site( $site_id, 'edit_post_meta', $post->ID, $meta_key );
921923
*
922-
* @since 3.0.0
923-
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
924-
* by adding it to the function signature.
925-
* @since 5.8.0 Wraps current_user_can() after switching to blog.
924+
* @since 6.7.0
926925
*
927-
* @param int $blog_id Site ID.
926+
* @param int $site_id Site ID.
928927
* @param string $capability Capability name.
929928
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
930929
* @return bool Whether the user has the given capability.
931930
*/
932-
function current_user_can_for_blog( $blog_id, $capability, ...$args ) {
933-
$switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
931+
function current_user_can_for_site( $site_id, $capability, ...$args ) {
932+
$switched = is_multisite() ? switch_to_blog( $site_id ) : false;
934933

935934
$can = current_user_can( $capability, ...$args );
936935

@@ -1023,19 +1022,19 @@ function user_can( $user, $capability, ...$args ) {
10231022
*
10241023
* Example usage:
10251024
*
1026-
* user_can_for_blog( $user->ID, $blog_id, 'edit_posts' );
1027-
* user_can_for_blog( $user->ID, $blog_id, 'edit_post', $post->ID );
1028-
* user_can_for_blog( $user->ID, $blog_id, 'edit_post_meta', $post->ID, $meta_key );
1025+
* user_can_for_site( $user->ID, $site_id, 'edit_posts' );
1026+
* user_can_for_site( $user->ID, $site_id, 'edit_post', $post->ID );
1027+
* user_can_for_site( $user->ID, $site_id, 'edit_post_meta', $post->ID, $meta_key );
10291028
*
10301029
* @since 6.7.0
10311030
*
10321031
* @param int|WP_User $user User ID or object.
1033-
* @param int $blog_id Site ID.
1032+
* @param int $site_id Site ID.
10341033
* @param string $capability Capability name.
10351034
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
10361035
* @return bool Whether the user has the given capability.
10371036
*/
1038-
function user_can_for_blog( $user, $blog_id, $capability, ...$args ) {
1037+
function user_can_for_site( $user, $site_id, $capability, ...$args ) {
10391038
if ( ! is_object( $user ) ) {
10401039
$user = get_userdata( $user );
10411040
}
@@ -1047,11 +1046,11 @@ function user_can_for_blog( $user, $blog_id, $capability, ...$args ) {
10471046
}
10481047

10491048
// Check if the blog ID is valid.
1050-
if ( ! is_numeric( $blog_id ) || $blog_id <= 0 ) {
1049+
if ( ! is_numeric( $site_id ) || $site_id <= 0 ) {
10511050
return false;
10521051
}
10531052

1054-
$switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
1053+
$switched = is_multisite() ? switch_to_blog( $site_id ) : false;
10551054

10561055
$can = user_can( $user->ID, $capability, ...$args );
10571056

src/wp-includes/deprecated.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6406,3 +6406,21 @@ function wp_create_block_style_variation_instance_name( $block, $variation ) {
64066406
_deprecated_function( __FUNCTION__, '6.7.0', 'wp_unique_id' );
64076407
return $variation . '--' . md5( serialize( $block ) );
64086408
}
6409+
6410+
/**
6411+
* Returns whether the current user has the specified capability for a given site.
6412+
*
6413+
* @since 3.0.0
6414+
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
6415+
* by adding it to the function signature.
6416+
* @since 5.8.0 Wraps current_user_can() after switching to blog.
6417+
* @deprecated 6.7.0 Use current_user_can_for_site() instead.
6418+
*
6419+
* @param int $blog_id Site ID.
6420+
* @param string $capability Capability name.
6421+
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
6422+
* @return bool Whether the user has the given capability.
6423+
*/
6424+
function current_user_can_for_blog( $blog_id, $capability, ...$args ) {
6425+
return current_user_can_for_site( $blog_id, $capability, ...$args );
6426+
}

tests/phpunit/tests/user/capabilities.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,100 +1654,100 @@ public function test_set_role_fires_remove_user_role_and_add_user_role_hooks() {
16541654
}
16551655

16561656
/**
1657-
* @group can_for_blog
1657+
* @group can_for_site
16581658
*/
1659-
public function test_current_user_can_for_blog() {
1659+
public function test_current_user_can_for_site() {
16601660
global $wpdb;
16611661

16621662
$user = self::$users['administrator'];
16631663
$old_uid = get_current_user_id();
16641664
wp_set_current_user( $user->ID );
16651665

1666-
$this->assertTrue( current_user_can_for_blog( get_current_blog_id(), 'edit_posts' ) );
1667-
$this->assertFalse( current_user_can_for_blog( get_current_blog_id(), 'foo_the_bar' ) );
1666+
$this->assertTrue( current_user_can_for_site( get_current_blog_id(), 'edit_posts' ) );
1667+
$this->assertFalse( current_user_can_for_site( get_current_blog_id(), 'foo_the_bar' ) );
16681668

16691669
if ( ! is_multisite() ) {
1670-
$this->assertTrue( current_user_can_for_blog( 12345, 'edit_posts' ) );
1671-
$this->assertFalse( current_user_can_for_blog( 12345, 'foo_the_bar' ) );
1670+
$this->assertTrue( current_user_can_for_site( 12345, 'edit_posts' ) );
1671+
$this->assertFalse( current_user_can_for_site( 12345, 'foo_the_bar' ) );
16721672
return;
16731673
}
16741674

16751675
$suppress = $wpdb->suppress_errors();
1676-
$this->assertFalse( current_user_can_for_blog( 12345, 'edit_posts' ) );
1676+
$this->assertFalse( current_user_can_for_site( 12345, 'edit_posts' ) );
16771677
$wpdb->suppress_errors( $suppress );
16781678

16791679
$blog_id = self::factory()->blog->create( array( 'user_id' => $user->ID ) );
16801680

16811681
$this->assertNotWPError( $blog_id );
1682-
$this->assertTrue( current_user_can_for_blog( $blog_id, 'edit_posts' ) );
1683-
$this->assertFalse( current_user_can_for_blog( $blog_id, 'foo_the_bar' ) );
1682+
$this->assertTrue( current_user_can_for_site( $blog_id, 'edit_posts' ) );
1683+
$this->assertFalse( current_user_can_for_site( $blog_id, 'foo_the_bar' ) );
16841684

16851685
$another_blog_id = self::factory()->blog->create( array( 'user_id' => self::$users['author']->ID ) );
16861686

16871687
$this->assertNotWPError( $another_blog_id );
16881688

16891689
// Verify the user doesn't have a capability
1690-
$this->assertFalse( current_user_can_for_blog( $another_blog_id, 'edit_posts' ) );
1690+
$this->assertFalse( current_user_can_for_site( $another_blog_id, 'edit_posts' ) );
16911691

16921692
// Add the current user to the site
16931693
add_user_to_blog( $another_blog_id, $user->ID, 'author' );
16941694

16951695
// Verify they now have the capability
1696-
$this->assertTrue( current_user_can_for_blog( $another_blog_id, 'edit_posts' ) );
1696+
$this->assertTrue( current_user_can_for_site( $another_blog_id, 'edit_posts' ) );
16971697

16981698
wp_set_current_user( $old_uid );
16991699
}
17001700

17011701
/**
1702-
* @group can_for_blog
1702+
* @group can_for_site
17031703
*/
1704-
public function test_user_can_for_blog() {
1704+
public function test_user_can_for_site() {
17051705
$user = self::$users['editor'];
17061706

1707-
$this->assertTrue( user_can_for_blog( $user->ID, get_current_blog_id(), 'edit_posts' ) );
1708-
$this->assertFalse( user_can_for_blog( $user->ID, get_current_blog_id(), 'foo_the_bar' ) );
1707+
$this->assertTrue( user_can_for_site( $user->ID, get_current_blog_id(), 'edit_posts' ) );
1708+
$this->assertFalse( user_can_for_site( $user->ID, get_current_blog_id(), 'foo_the_bar' ) );
17091709

17101710
if ( ! is_multisite() ) {
1711-
$this->assertTrue( user_can_for_blog( $user->ID, 12345, 'edit_posts' ) );
1712-
$this->assertFalse( user_can_for_blog( $user->ID, 12345, 'foo_the_bar' ) );
1711+
$this->assertTrue( user_can_for_site( $user->ID, 12345, 'edit_posts' ) );
1712+
$this->assertFalse( user_can_for_site( $user->ID, 12345, 'foo_the_bar' ) );
17131713
return;
17141714
}
17151715

17161716
$blog_id = self::factory()->blog->create( array( 'user_id' => $user->ID ) );
17171717

17181718
$this->assertNotWPError( $blog_id );
1719-
$this->assertTrue( user_can_for_blog( $user->ID, $blog_id, 'edit_posts' ) );
1720-
$this->assertFalse( user_can_for_blog( $user->ID, $blog_id, 'foo_the_bar' ) );
1719+
$this->assertTrue( user_can_for_site( $user->ID, $blog_id, 'edit_posts' ) );
1720+
$this->assertFalse( user_can_for_site( $user->ID, $blog_id, 'foo_the_bar' ) );
17211721

17221722
$author = self::$users['author'];
17231723

17241724
// Verify another user doesn't have a capability
17251725
$this->assertFalse( is_user_member_of_blog( $author->ID, $blog_id ) );
1726-
$this->assertFalse( user_can_for_blog( $author->ID, $blog_id, 'edit_posts' ) );
1726+
$this->assertFalse( user_can_for_site( $author->ID, $blog_id, 'edit_posts' ) );
17271727

17281728
// Add the author to the site
17291729
add_user_to_blog( $blog_id, $author->ID, 'author' );
17301730

17311731
// Verify they now have the capability
17321732
$this->assertTrue( is_user_member_of_blog( $author->ID, $blog_id ) );
1733-
$this->assertTrue( user_can_for_blog( $author->ID, $blog_id, 'edit_posts' ) );
1733+
$this->assertTrue( user_can_for_site( $author->ID, $blog_id, 'edit_posts' ) );
17341734

17351735
// Verify the user doesn't have a capability for a non-existent site
1736-
$this->assertFalse( user_can_for_blog( $user->ID, -1, 'edit_posts' ) );
1736+
$this->assertFalse( user_can_for_site( $user->ID, -1, 'edit_posts' ) );
17371737
}
17381738

17391739
/**
17401740
* @group ms-required
17411741
*/
1742-
public function test_borked_current_user_can_for_blog() {
1742+
public function test_borked_current_user_can_for_site() {
17431743
$orig_blog_id = get_current_blog_id();
17441744
$blog_id = self::factory()->blog->create();
17451745

17461746
$this->nullify_current_user();
17471747

17481748
add_action( 'switch_blog', array( $this, 'nullify_current_user_and_keep_nullifying_user' ) );
17491749

1750-
current_user_can_for_blog( $blog_id, 'edit_posts' );
1750+
current_user_can_for_site( $blog_id, 'edit_posts' );
17511751

17521752
$this->assertSame( $orig_blog_id, get_current_blog_id() );
17531753
}

0 commit comments

Comments
 (0)