Changeset 59198
- Timestamp:
- 10/08/2024 10:30:57 PM (4 weeks ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/capabilities.php
r59125 r59198 914 914 * map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. 915 915 * 916 * This function replaces the current_user_can_for_blog() function. 917 * 916 918 * Example usage: 917 919 * 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 ); 921 * 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. 926 * 927 * @param int $blog_id Site ID. 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 ); 923 * 924 * @since 6.7.0 925 * 926 * @param int $site_id Site ID. 928 927 * @param string $capability Capability name. 929 928 * @param mixed ...$args Optional further parameters, typically starting with an object ID. 930 929 * @return bool Whether the user has the given capability. 931 930 */ 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; 934 933 935 934 $can = current_user_can( $capability, ...$args ); … … 1024 1023 * Example usage: 1025 1024 * 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 ); 1029 1028 * 1030 1029 * @since 6.7.0 1031 1030 * 1032 1031 * @param int|WP_User $user User ID or object. 1033 * @param int $ blog_id Site ID.1032 * @param int $site_id Site ID. 1034 1033 * @param string $capability Capability name. 1035 1034 * @param mixed ...$args Optional further parameters, typically starting with an object ID. 1036 1035 * @return bool Whether the user has the given capability. 1037 1036 */ 1038 function user_can_for_ blog( $user, $blog_id, $capability, ...$args ) {1037 function user_can_for_site( $user, $site_id, $capability, ...$args ) { 1039 1038 if ( ! is_object( $user ) ) { 1040 1039 $user = get_userdata( $user ); … … 1048 1047 1049 1048 // 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 ) { 1051 1050 return false; 1052 1051 } 1053 1052 1054 $switched = is_multisite() ? switch_to_blog( $ blog_id ) : false;1053 $switched = is_multisite() ? switch_to_blog( $site_id ) : false; 1055 1054 1056 1055 $can = user_can( $user->ID, $capability, ...$args ); -
trunk/src/wp-includes/deprecated.php
r59039 r59198 6407 6407 return $variation . '--' . md5( serialize( $block ) ); 6408 6408 } 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 } -
trunk/tests/phpunit/tests/user/capabilities.php
r59123 r59198 1655 1655 1656 1656 /** 1657 * @group can_for_ blog1658 */ 1659 public function test_current_user_can_for_ blog() {1657 * @group can_for_site 1658 */ 1659 public function test_current_user_can_for_site() { 1660 1660 global $wpdb; 1661 1661 … … 1664 1664 wp_set_current_user( $user->ID ); 1665 1665 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' ) ); 1668 1668 1669 1669 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' ) ); 1672 1672 return; 1673 1673 } 1674 1674 1675 1675 $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' ) ); 1677 1677 $wpdb->suppress_errors( $suppress ); 1678 1678 … … 1680 1680 1681 1681 $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' ) ); 1684 1684 1685 1685 $another_blog_id = self::factory()->blog->create( array( 'user_id' => self::$users['author']->ID ) ); … … 1688 1688 1689 1689 // 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' ) ); 1691 1691 1692 1692 // Add the current user to the site … … 1694 1694 1695 1695 // 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' ) ); 1697 1697 1698 1698 wp_set_current_user( $old_uid ); … … 1700 1700 1701 1701 /** 1702 * @group can_for_ blog1703 */ 1704 public function test_user_can_for_ blog() {1702 * @group can_for_site 1703 */ 1704 public function test_user_can_for_site() { 1705 1705 $user = self::$users['editor']; 1706 1706 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' ) ); 1709 1709 1710 1710 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' ) ); 1713 1713 return; 1714 1714 } … … 1717 1717 1718 1718 $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' ) ); 1721 1721 1722 1722 $author = self::$users['author']; … … 1724 1724 // Verify another user doesn't have a capability 1725 1725 $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' ) ); 1727 1727 1728 1728 // Add the author to the site … … 1731 1731 // Verify they now have the capability 1732 1732 $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' ) ); 1734 1734 1735 1735 // 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' ) ); 1737 1737 } 1738 1738 … … 1740 1740 * @group ms-required 1741 1741 */ 1742 public function test_borked_current_user_can_for_ blog() {1742 public function test_borked_current_user_can_for_site() { 1743 1743 $orig_blog_id = get_current_blog_id(); 1744 1744 $blog_id = self::factory()->blog->create(); … … 1748 1748 add_action( 'switch_blog', array( $this, 'nullify_current_user_and_keep_nullifying_user' ) ); 1749 1749 1750 current_user_can_for_ blog( $blog_id, 'edit_posts' );1750 current_user_can_for_site( $blog_id, 'edit_posts' ); 1751 1751 1752 1752 $this->assertSame( $orig_blog_id, get_current_blog_id() );
Note: See TracChangeset
for help on using the changeset viewer.