In case someone is looking for a way to map custom capabilities to Download Monitor’s custom post type, here is how I did it:
/**
* Filter the cpt arguments to add capabilitie_type, capabilities and apply the map_meta_cap filter
*/
add_filter( 'dlm_cpt_dlm_download_args', 'edit_dlm_download_capabilities' );
function edit_dlm_download_capabilities( $args ) {
$args['capability_type'] = 'download';
$args['capabilities'] = array(
'edit_post' => 'edit_download',
'read_post' => 'read_download',
'delete_post' => 'delete_download',
'edit_posts' => 'edit_downloads',
'edit_others_posts' => 'edit_others_downloads',
'delete_posts' => 'delete_downloads',
'delete_others_posts' => 'delete_others_downloads',
'publish_posts' => 'publish_downloads',
'read_private_posts' => 'read_private_downloads'
);
$args['map_meta_cap'] = true;
return $args;
}
Then you can use the map_meta_cap
filter to apply your custom capabilities:
/**
* Map les capacités aux ressources
*/
add_filter( 'map_meta_cap', 'map_dlm_downloads_capabilities', 10, 4 );
function map_dlm_downloads_capabilities( $caps, $cap, $user_id, $args ) {
if ( 'edit_download' == $cap || 'delete_download' == $cap || 'read_download' == $cap ) {
$post = get_post( $args[0] );
$post_type = get_post_type_object( $post->post_type );
$caps = array();
}
if ( 'edit_download' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
}
elseif ( 'delete_download' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->delete_posts;
else
$caps[] = $post_type->cap->delete_others_posts;
}
elseif ( 'read_download' == $cap ) {
if ( 'private' != $post->post_status )
$caps[] = 'read';
elseif ( $user_id == $post->post_author )
$caps[] = 'read';
else
$caps[] = $post_type->cap->read_private_posts;
}
return $caps;
}
Then you can use any wordpress plugin that allows you to assign user capabilities like User Role Editor or Members.
-
This reply was modified 5 years, 1 month ago by Etienne.