Make WordPress Core

Ticket #38645: 38645.5.diff

File 38645.5.diff, 8.0 KB (added by flixos90, 8 years ago)
  • src/wp-includes/class-wp-roles.php

     
    6565        public $use_db = true;
    6666
    6767        /**
     68         * The site ID the roles are initialized for.
     69         *
     70         * @since 4.9.0
     71         * @var int
     72         */
     73        protected $site_id = 0;
     74
     75        /**
    6876         * Constructor
    6977         *
    7078         * @since 2.0.0
     79         * @since 4.9.0 The $site_id argument was added.
     80         *
     81         * @global array $wp_user_roles Used to set the 'roles' property value.
     82         *
     83         * @param int $site_id Site ID to initialize roles for. Default is the current site.
    7184         */
    72         public function __construct() {
    73                 $this->_init();
     85        public function __construct( $site_id = null ) {
     86                global $wp_user_roles;
     87
     88                $this->use_db = empty( $wp_user_roles );
     89
     90                $this->for_site( $site_id );
    7491        }
    7592
    7693        /**
     
    97114         * be used and the role option will not be updated or used.
    98115         *
    99116         * @since 2.1.0
    100          *
    101          * @global array $wp_user_roles Used to set the 'roles' property value.
     117         * @deprecated 4.9.0 Use WP_Roles::for_site()
    102118         */
    103119        protected function _init() {
    104                 global $wp_user_roles, $wpdb;
    105 
    106                 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
    107                 if ( ! empty( $wp_user_roles ) ) {
    108                         $this->roles = $wp_user_roles;
    109                         $this->use_db = false;
    110                 } else {
    111                         $this->roles = get_option( $this->role_key );
    112                 }
    113 
    114                 if ( empty( $this->roles ) )
    115                         return;
    116 
    117                 $this->role_objects = array();
    118                 $this->role_names =  array();
    119                 foreach ( array_keys( $this->roles ) as $role ) {
    120                         $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
    121                         $this->role_names[$role] = $this->roles[$role]['name'];
    122                 }
     120                _deprecated_function( __METHOD__, '4.9.0', 'WP_Roles::for_site()' );
    123121
    124                 /**
    125                  * After the roles have been initialized, allow plugins to add their own roles.
    126                  *
    127                  * @since 4.7.0
    128                  *
    129                  * @param WP_Roles $this A reference to the WP_Roles object.
    130                  */
    131                 do_action( 'wp_roles_init', $this );
     122                $this->for_site();
    132123        }
    133124
    134125        /**
     
    138129         * after switching wpdb to a new site ID.
    139130         *
    140131         * @since 3.5.0
    141          * @deprecated 4.7.0 Use new WP_Roles()
     132         * @deprecated 4.7.0 Use WP_Roles::for_site()
    142133         */
    143134        public function reinit() {
    144                 _deprecated_function( __METHOD__, '4.7.0', 'new WP_Roles()' );
    145                 $this->_init();
     135                _deprecated_function( __METHOD__, '4.7.0', 'WP_Roles::for_site()' );
     136
     137                $this->for_site();
    146138        }
    147139
    148140        /**
     
    270262        public function is_role( $role ) {
    271263                return isset( $this->role_names[$role] );
    272264        }
     265
     266        /**
     267         * Initializes all of the available roles.
     268         *
     269         * @since 4.9.0
     270         */
     271        public function init_roles() {
     272                if ( empty( $this->roles ) ) {
     273                        return;
     274                }
     275
     276                $this->role_objects = array();
     277                $this->role_names =  array();
     278                foreach ( array_keys( $this->roles ) as $role ) {
     279                        $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
     280                        $this->role_names[ $role ] = $this->roles[ $role ]['name'];
     281                }
     282
     283                /**
     284                 * After the roles have been initialized, allow plugins to add their own roles.
     285                 *
     286                 * @since 4.7.0
     287                 *
     288                 * @param WP_Roles $this A reference to the WP_Roles object.
     289                 */
     290                do_action( 'wp_roles_init', $this );
     291        }
     292
     293        /**
     294         * Sets the site to operate on. Defaults to the current site.
     295         *
     296         * @since 4.9.0
     297         *
     298         * @global wpdb $wpdb WordPress database abstraction object.
     299         *
     300         * @param int $site_id Site ID to initialize roles for. Default is the current site.
     301         */
     302        public function for_site( $site_id = null ) {
     303                global $wpdb;
     304
     305                if ( ! empty( $site_id ) ) {
     306                        $this->site_id = absint( $site_id );
     307                } else {
     308                        $this->site_id = get_current_blog_id();
     309                }
     310
     311                $this->role_key = $wpdb->get_blog_prefix( $this->site_id ) . 'user_roles';
     312
     313                if ( ! empty( $this->roles ) && ! $this->use_db ) {
     314                        return;
     315                }
     316
     317                $this->roles = $this->get_roles_data();
     318
     319                $this->init_roles();
     320        }
     321
     322        /**
     323         * Gets the available roles data.
     324         *
     325         * @since 4.9.0
     326         *
     327         * @global array $wp_user_roles Used to set the 'roles' property value.
     328         *
     329         * @return array Roles array.
     330         */
     331        protected function get_roles_data() {
     332                global $wp_user_roles;
     333
     334                if ( ! empty( $wp_user_roles ) ) {
     335                        return $wp_user_roles;
     336                }
     337
     338                if ( $this->site_id != get_current_blog_id() ) {
     339                        remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
     340
     341                        $roles = get_blog_option( $this->site_id, $this->role_key, array() );
     342
     343                        add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
     344
     345                        return $roles;
     346                }
     347
     348                return get_option( $this->role_key, array() );
     349        }
    273350}
  • src/wp-includes/ms-blogs.php

     
    793793 * @return true Always returns True.
    794794 */
    795795function switch_to_blog( $new_blog, $deprecated = null ) {
    796         global $wpdb, $wp_roles;
     796        global $wpdb;
    797797
    798798        $blog_id = get_current_blog_id();
    799799        if ( empty( $new_blog ) ) {
     
    848848                }
    849849        }
    850850
    851         if ( did_action( 'init' ) ) {
    852                 $wp_roles = new WP_Roles();
    853                 $current_user = wp_get_current_user();
    854                 $current_user->for_blog( $new_blog );
    855         }
    856 
    857851        /** This filter is documented in wp-includes/ms-blogs.php */
    858852        do_action( 'switch_blog', $new_blog, $prev_blog_id );
    859853        $GLOBALS['switched'] = true;
     
    877871 * @return bool True on success, false if we're already on the current blog
    878872 */
    879873function restore_current_blog() {
    880         global $wpdb, $wp_roles;
     874        global $wpdb;
    881875
    882876        if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
    883877                return false;
     
    922916                }
    923917        }
    924918
    925         if ( did_action( 'init' ) ) {
    926                 $wp_roles = new WP_Roles();
    927                 $current_user = wp_get_current_user();
    928                 $current_user->for_blog( $blog );
    929         }
    930 
    931919        /** This filter is documented in wp-includes/ms-blogs.php */
    932920        do_action( 'switch_blog', $blog, $prev_blog_id );
    933921
     
    938926}
    939927
    940928/**
     929 * Switches the initialized roles and current user capabilities to another site.
     930 *
     931 * @since 4.9.0
     932 *
     933 * @param int $new_site_id New site ID.
     934 * @param int $old_site_id Old site ID.
     935 */
     936function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
     937        if ( $new_site_id == $old_site_id ) {
     938                return;
     939        }
     940
     941        if ( ! did_action( 'init' ) ) {
     942                return;
     943        }
     944
     945        wp_roles()->for_blog( $new_site_id );
     946        wp_get_current_user()->for_blog( $new_site_id );
     947}
     948
     949/**
    941950 * Determines if switch_to_blog() is in effect
    942951 *
    943952 * @since 3.5.0
  • src/wp-includes/ms-default-filters.php

     
    3232add_action( 'network_user_new_created_user',   'wp_send_new_user_notifications' );
    3333add_filter( 'sanitize_user', 'strtolower' );
    3434
     35// Roles
     36add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
     37
    3538// Blogs
    3639add_filter( 'wpmu_validate_blog_signup', 'signup_nonce_check' );
    3740add_action( 'wpmu_new_blog', 'wpmu_log_new_registrations', 10, 2 );
  • tests/phpunit/tests/user/capabilities.php

     
    18481848                $this->assertFalse( user_can( self::$users['contributor']->ID,   'remove_user', self::$users['contributor']->ID ) );
    18491849                $this->assertFalse( user_can( self::$users['subscriber']->ID,    'remove_user', self::$users['subscriber']->ID ) );
    18501850        }
     1851
     1852        /**
     1853         * @ticket 38645
     1854         * @group ms-required
     1855         */
     1856        function test_init_roles_for_different_site() {
     1857                global $wpdb;
     1858
     1859                $site_id = self::factory()->blog->create();
     1860
     1861                switch_to_blog( $site_id );
     1862
     1863                $role_name = 'uploader';
     1864                add_role( $role_name, 'Uploader', array(
     1865                        'read'         => true,
     1866                        'upload_files' => true,
     1867                ) );
     1868
     1869                restore_current_blog();
     1870
     1871                $wp_roles = wp_roles();
     1872                $wp_roles->for_blog( $site_id );
     1873
     1874                $this->assertTrue( isset( $wp_roles->role_objects[ $role_name ] ) );
     1875        }
    18511876}