Making WordPress.org

Changeset 6567


Ignore:
Timestamp:
02/07/2018 09:46:19 PM (7 years ago)
Author:
obenland
Message:

Trac: Allow Cotributors+ to edit their components

Allows component maintainers to edit their component page if they have at least the role of Contributor.

Fixes #1540.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-components.php

    r6566 r6567  
    1919        add_action( 'manage_component_posts_custom_column', array( $this, 'manage_posts_custom_column' ), 10, 2 );
    2020        add_filter( 'wp_nav_menu_objects', array( $this, 'highlight_menu_component_link' ) );
     21        add_filter( 'map_meta_cap', [ $this, 'map_meta_cap' ], 10, 4 );
    2122    }
    2223
     
    210211
    211212        return $menu_items;
     213    }
     214
     215    /**
     216     * Allows component maintainers to edit their components if they are at least a Contributor.
     217     *
     218     * @param array  $required_caps The user's actual capabilities.
     219     * @param string $cap           Capability name.
     220     * @param int    $user_id       The user ID.
     221     * @param array  $context       Context to the cap. Typically the object ID.
     222     * @return array Primitive caps.
     223     */
     224    public function map_meta_cap( $required_caps, $cap, $user_id, $context ) {
     225        if ( in_array( $cap, [ 'edit_post', 'publish_post', 'edit_others_posts' ], true ) ) {
     226            if ( empty( $context[0] ) ) {
     227                $context[0] = isset( $_POST['post_ID'] ) ? absint( $_POST['post_ID'] ) : 0;
     228            }
     229
     230            if ( 'component' === get_post_type( $context[0] ) ) {
     231                $user_name   = get_user_by( 'id', $user_id )->user_login;
     232                $maintainers = array_map( 'trim', explode( ',', get_post_meta( $context[0], '_active_maintainers', true ) ) );
     233
     234                if ( in_array( $user_name, $maintainers, true ) ) {
     235                    $required_caps = ['edit_posts'];
     236                }
     237            }
     238        }
     239
     240        return $required_caps;
    212241    }
    213242
Note: See TracChangeset for help on using the changeset viewer.