Making WordPress.org


Ignore:
Timestamp:
03/28/2016 07:06:41 PM (10 years ago)
Author:
obenland
Message:

Plugin Directory: Capability improvements.

  • Fill built-in roles with custom caps.
  • Let Plugin Reviewers be able to manage their own plugins like regular authors.

See #1570, #1571.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-capabilities.php

    r2777 r2830  
    99class Capabilities {
    1010
     11        /**
     12         * Filters a user's capabilities depending on specific context and/or privilege.
     13         *
     14         * @param array  $required_caps Returns the user's actual capabilities.
     15         * @param string $cap           Capability name.
     16         * @param int    $user_id       The user ID.
     17         * @param array  $context       Adds the context to the cap. Typically the object ID.
     18         */
    1119        public static function map_meta_cap( $required_caps, $cap, $user_id, $context ) {
    1220                switch( $cap ) {
    1321
     22                        case 'plugin_edit':
    1423                        case 'plugin_add_committer':
    1524                        case 'plugin_remove_committer':
    16                         case 'plugin_edit':
    1725                                $required_caps = array();
    18                                 $post = self::get_post_from_context( $context );
    19                                 if ( ! $post ) {
     26                                $post = get_post( $context[0] );
     27                                if ( ! $post instanceof \WP_Post ) {
    2028                                        $required_caps[] = 'do_not_allow';
    2129                                        break;
    2230                                }
    2331
    24                                 $user = new \WP_User( $user_id );
     32                                $user       = new \WP_User( $user_id );
    2533                                $committers = Tools::get_plugin_committers( $post->post_name );
    2634
    27                                 if ( $post->post_author === $user_id ) {
     35                                if ( $post->post_author === $user_id || in_array( $user->user_login, $committers, true ) ) {
    2836                                        $required_caps[] = 'plugin_edit_own';
    29                                 } elseif ( in_array( $user->user_login, $committers, true ) ) {
    30                                         $required_caps[] = 'plugin_edit_own';
     37
    3138                                } else {
    3239                                        if ( 'pending' == $post->post_status ) {
    3340                                                $required_caps[] = 'plugin_edit_pending';
     41
    3442                                        } else {
    3543                                                $required_caps[] = 'plugin_edit_others';
     
    3846                                break;
    3947
    40                         // Don't allow any users to alter the post meta for plugins
     48                        // Don't allow any users to alter the post meta for plugins.
    4149                        case 'add_post_meta':
    4250                        case 'edit_post_meta':
     
    4553                                if ( $post && 'plugin' == $post->post_type ) {
    4654                                        $required_caps[] = 'do_not_allow';
    47                                         break;
    4855                                }
    4956                                break;
     
    6370        }
    6471
    65         protected static function get_post_from_context( $context ) {
    66                 if ( ! $context ) {
    67                         return false;
    68                 }
    69                 $context = $context[0];
    70 
    71                 $post = false;
    72                 if ( is_int( $context ) ) {
    73                         $post = get_post( $context );
    74                 } elseif ( $context instanceof \WP_Post ) {
    75                         $post = $context;
    76                 } elseif ( is_string( $context ) ) {
    77                         $post = Plugin_Directory::get_plugin_post( $context );
    78                 }
    79                 if ( ! $post || 'plugin' != $post->post_type ) {
    80                         return false;
    81                 }
    82                 return $post;
    83         }
    84 
    8572        public static function add_roles() {
    8673                $committer = array(
     
    9279                );
    9380
    94                 $reviewer = array(
    95                         'read' => true,
    96                         'plugin_dashboard_access' => true,
     81                $reviewer = array_merge( $committer, array(
    9782                        'plugin_edit_pending' => true,
    9883                        'plugin_approve' => true,
    9984                        'plugin_reject' => true,
    100                 );
     85                ) );
    10186
    10287                $admin = array_merge( $reviewer, array(
     
    115100                add_role( 'plugin_reviewer',  'Plugin Reviewer',  $reviewer );
    116101                add_role( 'plugin_admin',     'Plugin Admin',     $admin );
     102
     103                foreach( array( 'contributor', 'author', 'editor', 'administrator' ) as $role ) {
     104                        $wp_role = get_role( $role );
     105
     106                        foreach ( $committer as $committer_cap ) {
     107                                $wp_role->add_cap( $committer_cap );
     108                        }
     109
     110                        if ( in_array( $role, array( 'editor', 'administrator' ) ) ) {
     111                                foreach ( $admin as $admin_cap ) {
     112                                        $wp_role->add_cap( $admin_cap );
     113                                }
     114                        }
     115                }
    117116        }
    118117}
Note: See TracChangeset for help on using the changeset viewer.