Skip to:
Content

BuddyPress.org

Changeset 9957


Ignore:
Timestamp:
06/20/2015 01:54:56 PM (8 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.

Location:
trunk
Files:
2 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
  • trunk/tests/phpunit/testcases/core/caps.php

    r9956 r9957  
    2525    }
    2626
     27    /**
     28     * @ticket BP6501
     29     */
     30    public function test_bp_current_user_can_should_respect_blog_id_passed_in_args_array() {
     31        if ( ! is_multisite() ) {
     32            $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
     33        }
     34
     35        $b = $this->factory->blog->create();
     36        $u = $this->factory->user->create();
     37
     38        $this->set_current_user( $u );
     39
     40        add_filter( 'user_has_cap', array( $this, 'grant_cap_foo' ), 10, 2 );
     41        $can  = bp_current_user_can( 'foo', array( 'blog_id' => bp_get_root_blog_id() ) );
     42        $cant = bp_current_user_can( 'foo', array( 'blog_id' => $b ) );
     43        remove_filter( 'user_has_cap', array( $this, 'grant_cap_foo' ), 10, 2 );
     44
     45        $this->assertTrue( $can );
     46        $this->assertFalse( $cant );
     47    }
     48
    2749    public function grant_cap_foo( $allcaps, $caps ) {
    2850        if ( bp_is_root_blog() ) {
Note: See TracChangeset for help on using the changeset viewer.