Make WordPress Core


Ignore:
Timestamp:
11/02/2016 12:30:29 AM (8 years ago)
Author:
pento
Message:

Roles/Capabilities: Add a new wp_roles_init filter.

Historically, it's been difficult to extend user roles, but reasonable to work around by waiting until after init has fired, to add custom roles and capabilities. With the addition of Locale Switching, Core now potentially loads roles before init has fired, leaving a window where custom roles and capabilities are not handled.

The new filter allows plugins to add their own custom roles whenever they're initialised (on page load, or when switching sites, for example), so that they can always be obeyed.

WP_Roles has also been tidied up a little bit, to remove duplicate code.

Props johnjamesjacoby, pento.
Fixes #23016.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-roles.php

    r38768 r39082  
    128128            $this->role_names[$role] = $this->roles[$role]['name'];
    129129        }
     130
     131        /**
     132         * After the roles have been initialized, allow plugins to add their own roles.
     133         *
     134         * @since 4.7.0
     135         *
     136         * @param WP_Roles A reference to the WP_Roles object.
     137         */
     138        do_action( 'wp_roles_init', $this );
    130139    }
    131140
     
    137146     *
    138147     * @since 3.5.0
     148     * @deprecated 4.7.0 Use new WP_Roles()
    139149     * @access public
    140150     */
    141151    public function reinit() {
    142         global $wpdb;
    143 
    144         // There is no need to reinit if using the wp_user_roles global.
    145         if ( ! $this->use_db ) {
    146             return;
    147         }
    148 
    149         // Duplicated from _init() to avoid an extra function call.
    150         $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
    151         $this->roles = get_option( $this->role_key );
    152         if ( empty( $this->roles ) )
    153             return;
    154 
    155         $this->role_objects = array();
    156         $this->role_names =  array();
    157         foreach ( array_keys( $this->roles ) as $role ) {
    158             $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
    159             $this->role_names[$role] = $this->roles[$role]['name'];
    160         }
     152        __deprecated_function( __METHOD__, '4.7.0', 'new WP_Roles()' );
     153        $this->_init();
    161154    }
    162155
Note: See TracChangeset for help on using the changeset viewer.