Make WordPress Core


Ignore:
Timestamp:
10/08/2024 10:30:57 PM (16 months ago)
Author:
johnbillion
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/capabilities.php

    r59125 r59198  
    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 );
    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.
    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;
     931function 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 );
     
    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 ) {
     1037function user_can_for_site( $user, $site_id, $capability, ...$args ) {
    10391038    if ( ! is_object( $user ) ) {
    10401039        $user = get_userdata( $user );
     
    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 );
Note: See TracChangeset for help on using the changeset viewer.