Skip to:
Content

BuddyPress.org

Ticket #7192: 7192.03.patch

File 7192.03.patch, 5.4 KB (added by dcavins, 3 years ago)

Add note about changed filter arg.

  • src/bp-core/bp-core-caps.php

    diff --git src/bp-core/bp-core-caps.php src/bp-core/bp-core-caps.php
    index 2963f9f..9b59eec 100644
    function bp_set_current_user_default_role() { 
    243243 *
    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 * }
    253255 * @return bool True if the user has the cap for the given parameters.
    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();
     275        if ( empty( $args['site_id'] ) ) {
     276                $args['site_id'] = bp_get_root_blog_id();
    278277        }
    279278
    280         $args   = array( $blog_id, $capability, $args );
    281         $retval = call_user_func_array( 'current_user_can_for_blog', $args );
     279        // Call bp_user_can().
     280        $retval = bp_user_can( bp_loggedin_user_id(), $capability, $args );
    282281
    283282        /**
    284283         * Filters whether or not the current user has a given capability.
    285284         *
    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.
     292         * @param array  $args       Array of extra arguments as originally passed.
    293293         */
    294         return (bool) apply_filters( 'bp_current_user_can', $retval, $capability, $blog_id, $args );
     294        return (bool) apply_filters( 'bp_current_user_can', $retval, $capability, $args['site_id'], $args );
    295295}
    296296
    297297/**
    function bp_user_can( $user_id, $capability, $args = array() ) { 
    319319        }
    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        /**
    326325         * Filters whether or not the specified user has a given capability on a given site.
  • src/bp-xprofile/bp-xprofile-caps.php

    diff --git src/bp-xprofile/bp-xprofile-caps.php src/bp-xprofile/bp-xprofile-caps.php
    index 8d9b28c..c69e060 100644
    function bp_xprofile_map_meta_caps( $caps, $cap, $user_id, $args ) { 
    2828                        $caps = array( 'exist' ); // Must allow for logged-out users during registration.
    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.
    3535                        if ( 1 == $field_id ) {
  • tests/phpunit/testcases/core/caps.php

    diff --git tests/phpunit/testcases/core/caps.php tests/phpunit/testcases/core/caps.php
    index 0a90318..0c01dd8 100644
    class BP_Tests_Core_Caps extends BP_UnitTestCase { 
    4646                $this->assertFalse( $cant );
    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() ) {
    7951                        $allcaps['foo'] = 1;