Changeset 32174
- Timestamp:
- 04/20/2015 07:29:01 AM (11 years ago)
- Location:
- branches/4.1
- Files:
-
- 2 edited
-
src/wp-includes/capabilities.php (modified) (2 diffs)
-
tests/phpunit/tests/user/capabilities.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/4.1/src/wp-includes/capabilities.php
r30681 r32174 1375 1375 */ 1376 1376 function current_user_can_for_blog( $blog_id, $capability ) { 1377 if ( is_multisite() ) 1378 switch_to_blog( $blog_id ); 1377 $switched = is_multisite() ? switch_to_blog( $blog_id ) : false; 1379 1378 1380 1379 $current_user = wp_get_current_user(); 1381 1380 1382 if ( empty( $current_user ) ) 1381 if ( empty( $current_user ) ) { 1382 if ( $switched ) { 1383 restore_current_blog(); 1384 } 1383 1385 return false; 1386 } 1384 1387 1385 1388 $args = array_slice( func_get_args(), 2 ); … … 1388 1391 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args ); 1389 1392 1390 if ( is_multisite() )1393 if ( $switched ) { 1391 1394 restore_current_blog(); 1395 } 1392 1396 1393 1397 return $can; -
branches/4.1/tests/phpunit/tests/user/capabilities.php
r27390 r32174 661 661 $author->remove_cap( 'foo' ); 662 662 $this->assertFalse ( isset( $author->caps['foo'] ) ); 663 } 664 665 function test_borked_current_user_can_for_blog() { 666 if ( ! is_multisite() ) { 667 $this->markTestSkipped( 'Test only runs in multisite' ); 668 return; 669 } 670 671 $orig_blog_id = get_current_blog_id(); 672 $blog_id = $this->factory->blog->create(); 673 674 $nullify_current_user = function() { 675 // Prevents fatal errors in ::tearDown()'s and other uses of restore_current_blog() 676 $function_stack = wp_debug_backtrace_summary( null, 0, false ); 677 if ( in_array( 'restore_current_blog', $function_stack ) ) { 678 return; 679 } 680 $GLOBALS['current_user'] = null; 681 }; 682 683 $nullify_current_user_and_keep_nullifying_user = function() use ( $nullify_current_user ) { 684 $nullify_current_user(); 685 686 add_action( 'set_current_user', $nullify_current_user ); 687 }; 688 689 $nullify_current_user(); 690 691 add_action( 'switch_blog', $nullify_current_user_and_keep_nullifying_user ); 692 693 current_user_can_for_blog( $blog_id, 'edit_posts' ); 694 695 $this->assertEquals( $orig_blog_id, get_current_blog_id() ); 663 696 } 664 697
Note: See TracChangeset
for help on using the changeset viewer.