WordPress.org

Making WordPress.org

Changeset 4080


Ignore:
Timestamp:
09/16/2016 03:50:43 PM (4 years ago)
Author:
ocean90
Message:

Rosetta: Add role manager for custom roles which are used on the main site.

See #2003, #1398.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/rosetta/inc
Files:
6 added
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/rosetta/inc/site/class-locale-main.php

    r4032 r4080  
    55use WordPressdotorg\Rosetta\Jetpack;
    66use WordPressdotorg\Rosetta\User;
     7use WordPressdotorg\Rosetta\User\Role;
    78use WP_Site;
     9use WP_User;
    810
    911class Locale_Main implements Site {
     
    4951        }
    5052
     53        $this->initialize_jetpack_customizations();
     54        $this->initialize_user_role_customizations();
     55    }
     56
     57    /**
     58     * Initializes customizations for Jetpack.
     59     */
     60    private function initialize_jetpack_customizations() {
    5161        $jetpack_module_manager = new Jetpack\Module_Manager( [
    5262            'stats',
     
    5868        $jetpack_module_manager->setup();
    5969
     70        // Options for Jetpack's sharing module.
    6071        $options = new Filter\Options();
    61         // Options for Jetpack's sharing module.
    6272        $options->add_option(
    6373            ( new Filter\Option() )
     
    8797        $options->setup();
    8898    }
     99
     100    /**
     101     * Initializes user role customizations.
     102     */
     103    private function initialize_user_role_customizations() {
     104        $role_manager = new User\Role_Manager();
     105        $role_manager->add_role( new Role\Locale_Manager() );
     106        $role_manager->add_role( new Role\General_Translation_Editor() );
     107        $role_manager->add_role( new Role\Translation_Editor() );
     108        $role_manager->setup();
     109
     110        add_action( 'set_user_role', [ $this, 'restore_translation_editor_role' ], 10, 3 );
     111        add_filter( 'editable_roles', [ $this, 'remove_administrator_from_editable_roles' ] );
     112    }
     113
     114    /**
     115     * Restores the "(General) Translation Editor" role if an user is promoted.
     116     *
     117     * @param int    $user_id   The user ID.
     118     * @param string $role      The new role.
     119     * @param array  $old_roles An array of the user's previous roles.
     120     */
     121    public function restore_translation_editor_role( $user_id, $role, $old_roles ) {
     122        if (
     123            Role\General_Translation_Editor::get_name() !== $role &&
     124            in_array( Role\Translation_Editor::get_name(), $old_roles, true )
     125        ) {
     126            $user = new WP_User( $user_id );
     127            $user->add_role( Role\Translation_Editor::get_name() );
     128        }
     129
     130        if (
     131            Role\Translation_Editor::get_name() !== $role &&
     132            in_array( Role\General_Translation_Editor::get_name(), $old_roles, true )
     133        ) {
     134            $user = new WP_User( $user_id );
     135            $user->add_role( Role\General_Translation_Editor::get_name() );
     136        }
     137    }
     138
     139    /**
     140     * Removes "Administrator" role from the list of editable roles.
     141     *
     142     * @param array $roles List of roles.
     143     * @return array Filtered list of editable roles.
     144     */
     145    public function remove_administrator_from_editable_roles( $roles ) {
     146        if ( ! is_super_admin() && ! is_main_site() ) {
     147            unset( $roles['administrator'] );
     148        }
     149
     150        return $roles;
     151    }
    89152}
Note: See TracChangeset for help on using the changeset viewer.