Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/20/2015 01:54:56 PM (10 years ago)
Author:
boonebgorges
Message:

Convert second parameter of bp_current_user_can() to an $args array.

This makes the syntax simpler for the vast majority of use cases, where an
explicit blog_id doesn't need to be specified. It also makes it possible to
pass additional arguments, such as an item_id, to bp_current_user_can().

An integer passed as the second parameter will trigger the backward-
compatibility layer.

Props thebrandonallen, r-a-y.
See #5121. Fixes #6501.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-caps.php

    r9945 r9957  
    264264 * Check whether the current user has a given capability.
    265265 *
    266  * Can be passed blog ID, or will use the root blog by default.
    267  *
    268  * @since BuddyPress (1.6.0)
    269  *
    270  * @param string $capability Capability or role name.
    271  * @param int $blog_id Optional. Blog ID. Defaults to the BP root blog.
    272  * @return bool True if the user has the cap for the given blog.
    273  */
    274 function bp_current_user_can( $capability, $blog_id = 0 ) {
     266 * @since BuddyPress (1.6.0)
     267 * @since BuddyPress (2.4.0) Second argument modified to accept an array, rather than `$blog_id`.
     268 *
     269 * @param string    $capability Capability or role name.
     270 * @param array|int $args {
     271 *     Array of extra arguments applicable to the capability check.
     272 *     @type int   $blog_id Optional. Blog ID. Defaults to the BP root blog.
     273 *     @type mixed $a,...   Optional. Extra arguments applicable to the capability check.
     274 * }
     275 * @return bool True if the user has the cap for the given parameters.
     276 */
     277function bp_current_user_can( $capability, $args = array() ) {
     278    $blog_id = 0;
     279
     280    // Backward compatibility for older $blog_id parameter.
     281    if ( is_int( $args ) ) {
     282        $blog_id = $args;
     283        $args = array();
     284
     285    // New format for second parameter.
     286    } elseif ( is_array( $args ) && isset( $args['blog_id'] ) ) {
     287        // Get the blog ID if set, but don't pass along to `current_user_can_for_blog()`.
     288        $blog_id = (int) $args['blog_id'];
     289        unset( $args['blog_id'] );
     290    }
    275291
    276292    // Use root blog if no ID passed
     
    279295    }
    280296
    281     $retval = current_user_can_for_blog( $blog_id, $capability );
     297    $args   = array( $blog_id, $capability, $args );
     298    $retval = call_user_func_array( 'current_user_can_for_blog', $args );
    282299
    283300    /**
     
    285302     *
    286303     * @since BuddyPress (1.6.0)
     304     * @since BuddyPress (2.4.0) Pass `$args` variable.
    287305     *
    288306     * @param bool   $retval     Whether or not the current user has the capability.
    289307     * @param string $capability The capability being checked for.
    290308     * @param int    $blog_id    Blog ID. Defaults to the BP root blog.
    291      */
    292     return (bool) apply_filters( 'bp_current_user_can', $retval, $capability, $blog_id );
     309     * @param array  $args       Array of extra arguments passed.
     310     */
     311    return (bool) apply_filters( 'bp_current_user_can', $retval, $capability, $blog_id, $args );
    293312}
    294313
Note: See TracChangeset for help on using the changeset viewer.