Changeset 9957
- Timestamp:
- 06/20/2015 01:54:56 PM (8 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-caps.php
r9945 r9957 264 264 * Check whether the current user has a given capability. 265 265 * 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 */ 277 function 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 } 275 291 276 292 // Use root blog if no ID passed … … 279 295 } 280 296 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 ); 282 299 283 300 /** … … 285 302 * 286 303 * @since BuddyPress (1.6.0) 304 * @since BuddyPress (2.4.0) Pass `$args` variable. 287 305 * 288 306 * @param bool $retval Whether or not the current user has the capability. 289 307 * @param string $capability The capability being checked for. 290 308 * @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 ); 293 312 } 294 313 -
trunk/tests/phpunit/testcases/core/caps.php
r9956 r9957 25 25 } 26 26 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 27 49 public function grant_cap_foo( $allcaps, $caps ) { 28 50 if ( bp_is_root_blog() ) {
Note: See TracChangeset
for help on using the changeset viewer.