Ticket #36961: 36961.2.diff
File 36961.2.diff, 4.1 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-user.php
88 88 public $allcaps = array(); 89 89 90 90 /** 91 * The site ID the roles of this user are initialized for. 92 * 93 * @since 4.8.0 94 * @access private 95 * @var int 96 */ 97 private $blog_id = 0; 98 99 /** 91 100 * The filter context applied to user data fields. 92 101 * 93 102 * @since 2.9.0 … … 451 460 protected function _init_caps( $cap_key = '' ) { 452 461 global $wpdb; 453 462 454 if ( empty($cap_key) ) 455 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities'; 456 else 463 // Backward-compatibility with calling this method directly. 464 if ( empty( $this->blog_id ) ) { 465 if ( empty( $cap_key ) ) { 466 $this->blog_id = get_current_blog_id(); 467 } else { 468 $base_prefix = $this->db->get_blog_prefix( 1 ); 469 if ( $base_prefix . 'capabilities' === $cap_key ) { 470 $this->blog_id = 1; 471 } else { 472 $this->blog_id = absint( substr( $cap_key, strlen( $base_prefix ), - strlen( '_capabilities' ) ) ); 473 } 474 } 475 } 476 477 if ( empty($cap_key) ) { 478 $this->cap_key = $wpdb->get_blog_prefix( $this->blog_id ) . 'capabilities'; 479 } else { 457 480 $this->cap_key = $cap_key; 481 } 458 482 459 483 $this->caps = get_user_meta( $this->ID, $this->cap_key, true ); 460 484 … … 478 502 * @return array List of all capabilities for the user. 479 503 */ 480 504 public function get_role_caps() { 505 $switch_site = false; 506 if ( is_multisite() && $this->blog_id !== get_current_blog_id() ) { 507 $switch_site = $this->blog_id; 508 509 switch_to_blog( $switch_site ); 510 } 511 481 512 $wp_roles = wp_roles(); 482 513 483 514 //Filter out caps that are not role names and assign to $this->roles … … 492 523 } 493 524 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); 494 525 526 if ( $switch_site ) { 527 restore_current_blog(); 528 } 529 495 530 return $this->allcaps; 496 531 } 497 532 … … 780 815 */ 781 816 public function for_blog( $blog_id = '' ) { 782 817 global $wpdb; 783 if ( ! empty( $blog_id ) ) 784 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; 785 else 818 819 if ( ! empty( $blog_id ) ) { 820 $this->blog_id = absint( $blog_id ); 821 $cap_key = $wpdb->get_blog_prefix( $this->blog_id ) . 'capabilities'; 822 } else { 823 $this->blog_id = get_current_blog_id(); 786 824 $cap_key = ''; 825 } 826 787 827 $this->_init_caps( $cap_key ); 788 828 } 789 829 } -
tests/phpunit/tests/user/capabilities.php
1774 1774 $this->assertFalse( user_can( self::$users['contributor']->ID, 'remove_user', self::$users['contributor']->ID ) ); 1775 1775 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1776 1776 } 1777 1778 /** 1779 * @ticket 36961 1780 */ 1781 function test_init_user_caps_for_different_site() { 1782 global $wpdb; 1783 1784 if ( ! is_multisite() ) { 1785 $this->markTestSkipped( 'Test only runs in multisite' ); 1786 } 1787 1788 $site_id = self::factory()->blog->create( array( 'user_id' => self::$users['administrator']->ID ) ); 1789 1790 switch_to_blog( $site_id ); 1791 1792 $role_name = 'uploader'; 1793 add_role( $role_name, 'Uploader', array( 1794 'read' => true, 1795 'upload_files' => true, 1796 ) ); 1797 add_user_to_blog( $site_id, self::$users['subscriber']->ID, $role_name ); 1798 1799 restore_current_blog(); 1800 1801 $user = new WP_User( self::$users['subscriber']->ID, '', $site_id ); 1802 $this->assertTrue( $user->has_cap( 'upload_files' ) ); 1803 } 1777 1804 } -
tests/phpunit/tests/user.php
180 180 $this->assertEquals( 'foo', $user->$key ); 181 181 $this->assertEquals( 'foo', $user->data->$key ); // This will fail with WP < 3.3 182 182 183 foreach ( (array) $useras $key => $value ) {183 foreach ( get_object_vars( $user ) as $key => $value ) { 184 184 $this->assertEquals( $value, $user->$key ); 185 185 } 186 186 }