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

Built from https://develop.svn.wordpress.org/trunk@59198


git-svn-id: http://core.svn.wordpress.org/trunk@58593 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
John Blackbourn 2024-10-08 22:32:09 +00:00
parent 58c1f27c40
commit 324f713adf
3 changed files with 35 additions and 18 deletions

View File

@ -913,24 +913,23 @@ function current_user_can( $capability, ...$args ) {
* capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to * capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to
* map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. * map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`.
* *
* This function replaces the current_user_can_for_blog() function.
*
* Example usage: * Example usage:
* *
* current_user_can_for_blog( $blog_id, 'edit_posts' ); * current_user_can_for_site( $site_id, 'edit_posts' );
* current_user_can_for_blog( $blog_id, 'edit_post', $post->ID ); * current_user_can_for_site( $site_id, 'edit_post', $post->ID );
* current_user_can_for_blog( $blog_id, 'edit_post_meta', $post->ID, $meta_key ); * current_user_can_for_site( $site_id, 'edit_post_meta', $post->ID, $meta_key );
* *
* @since 3.0.0 * @since 6.7.0
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
* by adding it to the function signature.
* @since 5.8.0 Wraps current_user_can() after switching to blog.
* *
* @param int $blog_id Site ID. * @param int $site_id Site ID.
* @param string $capability Capability name. * @param string $capability Capability name.
* @param mixed ...$args Optional further parameters, typically starting with an object ID. * @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return bool Whether the user has the given capability. * @return bool Whether the user has the given capability.
*/ */
function current_user_can_for_blog( $blog_id, $capability, ...$args ) { function current_user_can_for_site( $site_id, $capability, ...$args ) {
$switched = is_multisite() ? switch_to_blog( $blog_id ) : false; $switched = is_multisite() ? switch_to_blog( $site_id ) : false;
$can = current_user_can( $capability, ...$args ); $can = current_user_can( $capability, ...$args );
@ -1023,19 +1022,19 @@ function user_can( $user, $capability, ...$args ) {
* *
* Example usage: * Example usage:
* *
* user_can_for_blog( $user->ID, $blog_id, 'edit_posts' ); * user_can_for_site( $user->ID, $site_id, 'edit_posts' );
* user_can_for_blog( $user->ID, $blog_id, 'edit_post', $post->ID ); * user_can_for_site( $user->ID, $site_id, 'edit_post', $post->ID );
* user_can_for_blog( $user->ID, $blog_id, 'edit_post_meta', $post->ID, $meta_key ); * user_can_for_site( $user->ID, $site_id, 'edit_post_meta', $post->ID, $meta_key );
* *
* @since 6.7.0 * @since 6.7.0
* *
* @param int|WP_User $user User ID or object. * @param int|WP_User $user User ID or object.
* @param int $blog_id Site ID. * @param int $site_id Site ID.
* @param string $capability Capability name. * @param string $capability Capability name.
* @param mixed ...$args Optional further parameters, typically starting with an object ID. * @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return bool Whether the user has the given capability. * @return bool Whether the user has the given capability.
*/ */
function user_can_for_blog( $user, $blog_id, $capability, ...$args ) { function user_can_for_site( $user, $site_id, $capability, ...$args ) {
if ( ! is_object( $user ) ) { if ( ! is_object( $user ) ) {
$user = get_userdata( $user ); $user = get_userdata( $user );
} }
@ -1047,11 +1046,11 @@ function user_can_for_blog( $user, $blog_id, $capability, ...$args ) {
} }
// Check if the blog ID is valid. // Check if the blog ID is valid.
if ( ! is_numeric( $blog_id ) || $blog_id <= 0 ) { if ( ! is_numeric( $site_id ) || $site_id <= 0 ) {
return false; return false;
} }
$switched = is_multisite() ? switch_to_blog( $blog_id ) : false; $switched = is_multisite() ? switch_to_blog( $site_id ) : false;
$can = user_can( $user->ID, $capability, ...$args ); $can = user_can( $user->ID, $capability, ...$args );

View File

@ -6406,3 +6406,21 @@ function wp_create_block_style_variation_instance_name( $block, $variation ) {
_deprecated_function( __FUNCTION__, '6.7.0', 'wp_unique_id' ); _deprecated_function( __FUNCTION__, '6.7.0', 'wp_unique_id' );
return $variation . '--' . md5( serialize( $block ) ); return $variation . '--' . md5( serialize( $block ) );
} }
/**
* Returns whether the current user has the specified capability for a given site.
*
* @since 3.0.0
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
* by adding it to the function signature.
* @since 5.8.0 Wraps current_user_can() after switching to blog.
* @deprecated 6.7.0 Use current_user_can_for_site() instead.
*
* @param int $blog_id Site ID.
* @param string $capability Capability name.
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
* @return bool Whether the user has the given capability.
*/
function current_user_can_for_blog( $blog_id, $capability, ...$args ) {
return current_user_can_for_site( $blog_id, $capability, ...$args );
}

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.7-beta2-59197'; $wp_version = '6.7-beta2-59198';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.