0
votes

I registered a custom post type ("playlist") in my plugin. I've read a lot about roles and capabilities but this is quite difficult to understand completely... The thing I would like to achieve is to CLONE capabilities for a post type, by roles : In my case, I would like to get the capabilities from "post" (by role), and assign them to my custom post type ("playlist")

I don't want to use "capability_type" like below because I don't want to assign the "post" capabilities to my "playlist" post type, I want it to have their own capabilties but with the same default values for each role.

function register_post_type() {
    $args = array( 
        ...
        'capability_type' => 'post', //I think I don't need this
        ...
    );
    register_post_type( 'playlist', $args );
)

So I guess I need something more like that :

function register_post_type() {
    $args = array( 
        ...
        'capability_type'     => 'playlist',
        'map_meta_cap'        => true,
        'capabilities' => array(
            'edit_post'              => 'edit_playlist',
            'read_post'              => 'read_playlist',
            'delete_post'            => 'delete_playlist',
            'create_posts'           => 'create_playlists',
            'edit_posts'             => 'edit_playlists',
            'edit_others_posts'      => 'manage_playlists',
            'publish_posts'          => 'manage_playlists',
            'read_private_posts'     => 'read',
            'read'                   => 'read',
            'delete_posts'           => 'manage_playlists',
            'delete_private_posts'   => 'manage_playlists',
            'delete_published_posts' => 'manage_playlists',
            'delete_others_posts'    => 'manage_playlists',
            'edit_private_posts'     => 'edit_playlists',
            'edit_published_posts'   => 'edit_playlists'
        ),
        ...
    );
    register_post_type( 'playlist', $args );
)

but after that I would need to run a function to assign those capabilities to each role, based on the "post" capabilities". See what I mean ?

Do you have an idea of how I could achieve this ?

Thanks !

PS : useful link : http://justintadlock.com/archives/2013/09/13/register-post-type-cheat-sheet

1

1 Answers

0
votes

This is what I finally did.
It is not exactly the answer to the question (cloning a role), but it worked for what I needed.

    function register_post_type() {
        ...
        'capability_type' => 'playlist',
        'map_meta_cap'        => true,
        'capabilities' => array(

            // meta caps (don't assign these to roles)
            'edit_post'              => 'edit_playlist',
            'read_post'              => 'read_playlist',
            'delete_post'            => 'delete_playlist',

            // primitive/meta caps
            'create_posts'           => 'create_playlists',

            // primitive caps used outside of map_meta_cap()
            'edit_posts'             => 'edit_playlists',
            'edit_others_posts'      => 'manage_playlists',
            'publish_posts'          => 'manage_playlists',
            'read_private_posts'     => 'read',

            // primitive caps used inside of map_meta_cap()
            'read'                   => 'read',
            'delete_posts'           => 'manage_playlists',
            'delete_private_posts'   => 'manage_playlists',
            'delete_published_posts' => 'manage_playlists',
            'delete_others_posts'    => 'manage_playlists',
            'edit_private_posts'     => 'edit_playlists',
            'edit_published_posts'   => 'edit_playlists'
            ),
        ...
        );

        register_post_type( $this->post_type, $args );
    }

    function set_roles_capabilities(){

        global $wp_roles;
        if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles();

        //create a new role, based on the subscriber role 
        $role_name = 'playlist_author';
        $subscriber = $wp_roles->get_role('subscriber');
        $wp_roles->add_role($role_name,__('Playlist Author','xspfpl'), $subscriber->capabilities);

        //list of custom capabilities and which role should get it
        $wiki_caps=array(
            'manage_playlists'=>array('administrator','editor'),
            'edit_playlists'=>array('administrator','editor',$role_name),
            'create_playlists'=>array('administrator','editor',$role_name),
        );

        foreach ($wiki_caps as $wiki_cap=>$roles){
            foreach ($roles as $role){
                $wp_roles->add_cap( $role, $wiki_cap );
            }
        }

    }

    register_activation_hook( __FILE__ , 'set_roles_capabilities' );//roles & capabilities
    add_action( 'init', 'register_post_type');