Skip to:
Content

bbPress.org

Changeset 6118


Ignore:
Timestamp:
11/09/2016 09:51:11 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Roles: Fix infinite loops from load order changes in WordPress 4.7.

  • Do not translate "role names" which are actually role IDs
  • Add dummy function so literal role names are part of the pomo dictionary
  • Introduce common/locale.php for future localization code
  • Introduce roles variable to main bbPress class, and store loaded roles there
  • Introduce bbp_translate_user_role() to help with outputting literal role names in the proper language

See #3017. For trunk (2.6)

Location:
trunk/src
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bbpress.php

    r6092 r6118  
    6767     * @var array Topic views
    6868     */
    69     public $views        = array();
     69    public $views = array();
    7070
    7171    /**
    7272     * @var array Overloads get_option()
    7373     */
    74     public $options      = array();
     74    public $options = array();
    7575
    7676    /**
     
    7878     */
    7979    public $user_options = array();
     80
     81    /**
     82     * @var array Dynamically initialized user roles
     83     */
     84    public $roles = array();
    8085
    8186    /** Singleton *************************************************************/
     
    316321        require $this->includes_dir . 'common/functions.php';
    317322        require $this->includes_dir . 'common/formatting.php';
     323        require $this->includes_dir . 'common/locale.php';
    318324        require $this->includes_dir . 'common/template.php';
    319325        require $this->includes_dir . 'common/widgets.php';
     
    383389            'setup_theme',              // Setup the default theme compat
    384390            'setup_current_user',       // Setup currently logged in user
     391            'roles_init',               // User roles init
    385392            'register_post_types',      // Register post types (forum|topic|reply)
    386393            'register_post_statuses',   // Register post statuses (closed|spam|orphan|hidden)
     
    745752     * Setup the currently logged-in user
    746753     *
    747      * Do not to call this prematurely, I.E. before the 'init' action has
    748      * started. This function is naturally hooked into 'init' to ensure proper
    749      * execution. get_currentuserinfo() is used to check for XMLRPC_REQUEST to
    750      * avoid xmlrpc errors.
    751      *
    752754     * @since 2.0.0 bbPress (r2697)
    753755     *
     
    756758    public function setup_current_user() {
    757759        $this->current_user = wp_get_current_user();
     760    }
     761
     762    /**
     763     * Initialize forum-specific roles
     764     *
     765     * @since 2.6.0
     766     */
     767    public function roles_init() {
     768
     769        // Get role IDs
     770        $keymaster   = bbp_get_keymaster_role();
     771        $moderator   = bbp_get_moderator_role();
     772        $participant = bbp_get_participant_role();
     773        $spectator   = bbp_get_spectator_role();
     774        $blocked     = bbp_get_blocked_role();
     775
     776        // Build the roles into one useful array
     777        $this->roles[ $keymaster   ] = new WP_Role( 'Keymaster',   bbp_get_caps_for_role( $keymaster   ) );
     778        $this->roles[ $moderator   ] = new WP_Role( 'Moderator',   bbp_get_caps_for_role( $moderator   ) );
     779        $this->roles[ $participant ] = new WP_Role( 'Participant', bbp_get_caps_for_role( $participant ) );
     780        $this->roles[ $spectator   ] = new WP_Role( 'Spectator',   bbp_get_caps_for_role( $spectator   ) );
     781        $this->roles[ $blocked     ] = new WP_Role( 'Blocked',     bbp_get_caps_for_role( $blocked     ) );
    758782    }
    759783
  • trunk/src/includes/admin/settings.php

    r6089 r6118  
    536536        <?php foreach ( bbp_get_dynamic_roles() as $role => $details ) : ?>
    537537
    538             <option <?php selected( $default_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option>
     538            <option <?php selected( $default_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
    539539
    540540        <?php endforeach; ?>
  • trunk/src/includes/admin/users.php

    r6061 r6118  
    119119                            <?php foreach ( $dynamic_roles as $role => $details ) : ?>
    120120
    121                                 <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option>
     121                                <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
    122122
    123123                            <?php endforeach; ?>
     
    170170            <option value=''><?php esc_html_e( 'Change forum role to&hellip;', 'bbpress' ) ?></option>
    171171            <?php foreach ( $dynamic_roles as $role => $details ) : ?>
    172                 <option value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option>
     172                <option value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
    173173            <?php endforeach; ?>
    174174        </select><?php submit_button( __( 'Change', 'bbpress' ), 'secondary', $button_id, false );
     
    289289            if ( ! empty( $user_role ) ) {
    290290                $roles  = bbp_get_dynamic_roles();
    291                 $retval = translate_user_role( $roles[ $user_role ]['name'] );
     291                $retval = bbp_translate_user_role( $roles[ $user_role ]['name'] );
    292292            }
    293293        }
  • trunk/src/includes/core/actions.php

    r6106 r6118  
    9191 *                                                    v---Load order
    9292 */
    93 add_action( 'bbp_roles_init', 'bbp_add_forums_roles', 1 );
     93add_action( 'bbp_roles_init', 'bbp_add_forums_roles', 8 );
    9494
    9595/**
  • trunk/src/includes/core/capabilities.php

    r6108 r6118  
    288288function bbp_add_forums_roles( $wp_roles = null ) {
    289289
    290     // Attempt to get global roles if not passed in & not mid-initialization
    291     if ( ( null === $wp_roles ) && ! doing_action( 'wp_roles_init' ) ) {
    292         $wp_roles = bbp_get_wp_roles();
    293     }
     290    // Get the dynamic roles
     291    $bbp_roles = bbp_get_dynamic_roles();
    294292
    295293    // Loop through dynamic roles and add them to the $wp_roles array
    296     foreach ( bbp_get_dynamic_roles() as $role_id => $details ) {
     294    foreach ( $bbp_roles as $role_id => $details ) {
    297295        $wp_roles->roles[ $role_id ]        = $details;
    298296        $wp_roles->role_objects[ $role_id ] = new WP_Role( $role_id, $details['capabilities'] );
     
    363361 *
    364362 * @since 2.2.0 bbPress (r4284)
     363 * @since 2.6.0 bbPress (r6117) Use bbpress()->roles
    365364 *
    366365 * @return array
    367366 */
    368367function bbp_get_dynamic_roles() {
    369     static $bbp_roles = null;
    370 
    371     // Only run once
    372     if ( null === $bbp_roles ) {
    373         $bbp_roles = array(
    374 
    375             // Keymaster
    376             bbp_get_keymaster_role() => array(
    377                 'name'         => __( 'Keymaster', 'bbpress' ),
    378                 'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
    379             ),
    380 
    381             // Moderator
    382             bbp_get_moderator_role() => array(
    383                 'name'         => __( 'Moderator', 'bbpress' ),
    384                 'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
    385             ),
    386 
    387             // Participant
    388             bbp_get_participant_role() => array(
    389                 'name'         => __( 'Participant', 'bbpress' ),
    390                 'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
    391             ),
    392 
    393             // Spectator
    394             bbp_get_spectator_role() => array(
    395                 'name'         => __( 'Spectator', 'bbpress' ),
    396                 'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
    397             ),
    398 
    399             // Blocked
    400             bbp_get_blocked_role() => array(
    401                 'name'         => __( 'Blocked', 'bbpress' ),
    402                 'capabilities' => bbp_get_caps_for_role( bbp_get_blocked_role() )
    403             )
    404         );
     368
     369    // Defaults
     370    $to_array = array();
     371    $roles    = bbpress()->roles;
     372
     373    // Convert WP_Roles objects to arrays
     374    foreach ( $roles as $role_id => $wp_role ) {
     375        $to_array[ $role_id ] = (array) $wp_role;
    405376    }
    406377
    407378    // Filter & return
    408     return (array) apply_filters( 'bbp_get_dynamic_roles', $bbp_roles );
     379    return (array) apply_filters( 'bbp_get_dynamic_roles', $to_array, $roles );
    409380}
    410381
     
    413384 *
    414385 * @since 2.3.0 bbPress (r4792)
     386 * @since 2.6.0 bbPress (r6117) Use bbp_translate_user_role()
    415387 *
    416388 * @param string $role_id
     
    419391function bbp_get_dynamic_role_name( $role_id = '' ) {
    420392    $roles = bbp_get_dynamic_roles();
    421     $role  = isset( $roles[ $role_id ] ) ? $roles[ $role_id ]['name'] : '';
     393    $role  = isset( $roles[ $role_id ] )
     394        ? bbp_translate_user_role( $roles[ $role_id ]['name'] )
     395        : '';
    422396
    423397    return apply_filters( 'bbp_get_dynamic_role_name', $role, $role_id, $roles );
     
    433407 *
    434408 * @param array $all_roles All registered roles
    435  * @return array 
     409 * @return array
    436410 */
    437411function bbp_filter_blog_editable_roles( $all_roles = array() ) {
     
    527501 * Removes bbPress-specific user roles from the `wp_user_roles` array.
    528502 *
    529  * This is currently only used when updating, uninstalling, or resetting bbPress. 
     503 * This is currently only used when updating, uninstalling, or resetting bbPress.
    530504 *
    531505 * @see bbp_admin_reset_handler()
  • trunk/src/includes/users/template.php

    r6062 r6118  
    13261326        <?php foreach ( $blog_roles as $role => $details ) : ?>
    13271327
    1328             <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option>
     1328            <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
    13291329
    13301330        <?php endforeach; ?>
     
    13631363        <?php foreach ( $dynamic_roles as $role => $details ) : ?>
    13641364
    1365             <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option>
     1365            <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
    13661366
    13671367        <?php endforeach; ?>
Note: See TracChangeset for help on using the changeset viewer.