Skip to:
Content

BuddyPress.org

Changeset 11052


Ignore:
Timestamp:
09/01/2016 12:50:06 AM (9 years ago)
Author:
dcavins
Message:

Improve bp_current_user_can().

  • Deprecate $args['blog_id’] in favor of $args['site_id’].
  • Improve the structure of the passed $args array in the

bp_current_user_can filter.

Props r-a-y, dcavins.

Fixes #7192.

Location:
trunk
Files:
3 edited

Legend:

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

    r10909 r11052  
    244244 * @since 1.6.0
    245245 * @since 2.4.0 Second argument modified to accept an array, rather than `$blog_id`.
     246 * @since 2.7.0 Deprecated $args['blog_id'] in favor of $args['site_id'].
    246247 *
    247248 * @param string    $capability Capability or role name.
    248249 * @param array|int $args {
    249250 *     Array of extra arguments applicable to the capability check.
    250  *     @type int   $blog_id Optional. Blog ID. Defaults to the BP root blog.
     251 *     @type int   $site_id Optional. Blog ID. Defaults to the BP root blog.
     252 *     @type int   $blog_id Deprecated. Use $site_id instead.
    251253 *     @type mixed $a,...   Optional. Extra arguments applicable to the capability check.
    252254 * }
     
    254256 */
    255257function bp_current_user_can( $capability, $args = array() ) {
    256     $blog_id = 0;
    257 
    258258    // Backward compatibility for older $blog_id parameter.
    259259    if ( is_int( $args ) ) {
    260260        $blog_id = $args;
    261261        $args = array();
     262        $args['site_id'] = $args;
    262263
    263264    // New format for second parameter.
    264265    } elseif ( is_array( $args ) && isset( $args['blog_id'] ) ) {
    265266        // Get the blog ID if set, but don't pass along to `current_user_can_for_blog()`.
    266         $blog_id = (int) $args['blog_id'];
     267        $args['site_id'] = (int) $args['blog_id'];
    267268        unset( $args['blog_id'] );
    268269    }
    269270
    270     // Backward compatibility for older bp_current_user_can() checks.
    271     if ( empty( $args ) ) {
    272         $args = null;
    273     }
     271    // Cast $args as an array.
     272    $args = (array) $args;
    274273
    275274    // Use root blog if no ID passed.
    276     if ( empty( $blog_id ) ) {
    277         $blog_id = bp_get_root_blog_id();
    278     }
    279 
    280     $args   = array( $blog_id, $capability, $args );
    281     $retval = call_user_func_array( 'current_user_can_for_blog', $args );
     275    if ( empty( $args['site_id'] ) ) {
     276        $args['site_id'] = bp_get_root_blog_id();
     277    }
     278
     279    // Call bp_user_can().
     280    $retval = bp_user_can( bp_loggedin_user_id(), $capability, $args );
    282281
    283282    /**
     
    286285     * @since 1.6.0
    287286     * @since 2.4.0 Pass `$args` variable.
     287     * @since 2.7.0 Change format of $args variable array.
    288288     *
    289289     * @param bool   $retval     Whether or not the current user has the capability.
    290290     * @param string $capability The capability being checked for.
    291291     * @param int    $blog_id    Blog ID. Defaults to the BP root blog.
    292      * @param array  $args       Array of extra arguments passed.
    293      */
    294     return (bool) apply_filters( 'bp_current_user_can', $retval, $capability, $blog_id, $args );
     292     * @param array  $args       Array of extra arguments as originally passed.
     293     */
     294    return (bool) apply_filters( 'bp_current_user_can', $retval, $capability, $args['site_id'], $args );
    295295}
    296296
     
    320320
    321321    $switched = is_multisite() ? switch_to_blog( $site_id ) : false;
    322     $args     = array( $user_id, $capability, $args );
    323     $retval   = call_user_func_array( 'user_can', $args );
     322    $retval   = call_user_func_array( 'user_can', array( $user_id, $capability, $args ) );
    324323
    325324    /**
  • trunk/src/bp-xprofile/bp-xprofile-caps.php

    r10825 r11052  
    2929
    3030            // You may pass args manually: $field_id, $profile_user_id.
    31             $field_id        = isset( $args[0] ) ? (int)$args[0] : bp_get_the_profile_field_id();
    32             $profile_user_id = isset( $args[1] ) ? (int)$args[1] : bp_displayed_user_id();
     31            $field_id        = ! empty( $args[0] ) ? (int) $args[0] : bp_get_the_profile_field_id();
     32            $profile_user_id = isset( $args[1] )   ? (int) $args[1] : bp_displayed_user_id();
    3333
    3434            // Visibility on the fullname field is not editable.
  • trunk/tests/phpunit/testcases/core/caps.php

    r10377 r11052  
    4747    }
    4848
    49     /**
    50      * @group bp_xprofile_change_field_visibility
    51      */
    52     public function test_bp_current_user_can_should_pass_null_in_args_parameter_if_empty() {
    53         $u = $this->factory->user->create();
    54         $this->set_current_user( $u );
    55 
    56         /**
    57          * Fake bp_get_the_profile_field_id() to pretend we're in the field loop and
    58          * to avoid notices when checking 'bp_xprofile_change_field_visibility' cap
    59          */
    60         $GLOBALS['field'] = new stdClass;
    61         $GLOBALS['field']->id = 1;
    62 
    63         // Capture the cap's $args
    64         add_filter( 'bp_xprofile_map_meta_caps', array( $this, 'check_cap_args' ), 10, 4 );
    65 
    66         // Use a cap check that depends on a null value for a cap's args
    67         bp_current_user_can( 'bp_xprofile_change_field_visibility' );
    68 
    69         // Assert!
    70         $this->assertEquals( null, $this->test_args[0] );
    71 
    72         // Reset
    73         remove_filter( 'bp_xprofile_map_meta_caps', array( $this, 'check_cap_args' ), 10, 4 );
    74         unset( $GLOBALS['field'], $this->test_args );
    75     }
    76 
    7749    public function grant_cap_foo( $allcaps, $caps ) {
    7850        if ( bp_is_root_blog() ) {
Note: See TracChangeset for help on using the changeset viewer.