Making WordPress.org


Ignore:
Timestamp:
10/24/2014 07:16:04 AM (12 years ago)
Author:
coffee2code
Message:

Code Reference: add new role for handbook editors. Fixes #621

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/functions.php

    r929 r930  
    6666        register_post_types();
    6767        register_taxonomies();
     68
     69        add_action( 'after_switch_theme', __NAMESPACE__ . '\\add_roles' );
     70        add_filter( 'user_has_cap', __NAMESPACE__ . '\\adjust_handbook_editor_caps', 11 );
    6871        add_action( 'widgets_init', __NAMESPACE__ . '\\widgets_init' );
    6972        add_action( 'pre_get_posts', __NAMESPACE__ . '\\pre_get_posts' );
     
    8588        add_filter( 'breadcrumb_trail_items',  __NAMESPACE__ . '\\breadcrumb_trail_items', 10, 2 );
    8689
     90}
     91
     92
     93/**
     94 * Create the handbook_editor role which can only edit handbooks.
     95 *
     96 * @access public
     97 *
     98 */
     99function add_roles() {
     100        add_role(
     101                'handbook_editor',
     102                __( 'Handbook Editor', 'wporg' ),
     103                array(
     104                        'moderate_comments'             => true,
     105                        'upload_files'                  => true,
     106                        'unfiltered_html'               => true,
     107                        'read'                          => true,
     108                        'edit_handbook_pages'           => true,
     109                        'edit_others_handbook_pages'    => true,
     110                        'edit_published_handbook_pages' => true,
     111                        'edit_private_handbook_pages'   => true,
     112                        'read_private_handbook_pages'   => true,
     113                )
     114        );
     115}
     116
     117/**
     118 * Adjusts handbook capabilities for roles.
     119 *
     120 * Undoes some capability assignments by the handbook plugin since only
     121 * administrators, editors, and handbook_editors can manipulate handbooks.
     122 *
     123 * @access public
     124 *
     125 * @param  array $caps Array of user capabilities.
     126 * @return array
     127 */
     128function adjust_handbook_editor_caps( $caps ) {
     129        if ( ! is_user_member_of_blog() || ! class_exists( 'WPorg_Handbook' ) ) {
     130                return $caps;
     131        }
     132
     133        // Get current user's role.
     134        $role = wp_get_current_user()->roles[0];
     135
     136        // Unset caps set by handbook plugin.
     137        // Only administrators, editors, and handbook_editors can manipulate handbooks.
     138        if ( ! in_array( $role, array( 'administrator', 'editor', 'handbook_editor' ) ) ) {
     139                foreach ( \WPorg_Handbook::caps() as $cap ) {
     140                        unset( $caps[ $cap ] );
     141                }
     142
     143                foreach ( \WPorg_Handbook::editor_caps() as $cap ) {
     144                        unset( $caps[ $cap ] );
     145                }
     146        }
     147
     148        return $caps;
    87149}
    88150
Note: See TracChangeset for help on using the changeset viewer.