itthinx / groups

Groups provides group-based user membership management, group-based capabilities and content access control. It integrates standard WordPress capabilities and application-specific capabilities along with an extensive API.
GNU General Public License v3.0
49 stars 35 forks source link

Added filter for the potential of pre-selecting available capabilities #43

Closed CatEntangler closed 8 years ago

CatEntangler commented 8 years ago

Filter: groups_edit_post_selected_caps Purpose: Allow for the automatic pre-selection of capabilities for posts. Example use: Setting up a custom post type that requires default read capabilities without requiring site/content managers to have to remember to edit the meta box every time. Variables: 4 example usage:

add_filter( 'groups_edit_post_selected_caps', 'select_default_capabilities', 10, 4);
/*
* @param string $selected The text added to the option element within the select. Either an empty string or '  selected="selected" ' are default to return. Other option element attributes could be passed back as well. This param must be returned or previously selected capabilities won't display
* @param string $capability_name the name of the capability as set in capabilities editor
* @param string $capability_id the escaped ID of the capability being evaluated
* @param array $previous_selected An array of capabilities already selected in a previously saved post.
*/
function select_default_capabilities($selected, $capability_name, $capability_id, $previous_selected) {
    global $post;
    if( $post->post_type === 'my_custom_post_type' && $capability_name === 'groups_read_post' && empty($post->post_title) ) {

        // This is a new post because the post title is empty

        $selected = ' selected="selected" ';

    }

    return $selected;
}
proaktion commented 8 years ago

Many thanks for your contribution, I've added this and released 1.10.3 with some modifications, the filter is now:

    $selected = apply_filters(
        'groups_access_restrictions_capability_selected',
        in_array( $capability->capability, $read_caps ),
        $capability->capability,
        $capability->capability_id,
        $read_caps,
        $post_id,
        $post_type
    );

where the first argument is boolean instead of the text and it takes two additional ones for the post ID and type.

CatEntangler commented 8 years ago

@proaktion works great! thanks for the pull request acceptance.