Возможность delete_others_posts как false не работает

Я пытаюсь определить новую роль пользователя, основываясь на возможностях пользовательских типов записей. Помимо других возможностей, я хочу, чтобы эта роль просто удаляла свои посты, а не посты других пользователей.

Для этого я определил delete_others_posts(delete_others_dictionary_entry) как false но почему-то это не работает, и каждый пользователь с этой новой ролью может удалять сообщения других пользователей.

add_action('init', 'setup_dictionary_post');

//Register custom post type
function setup_dictionary_post() {

$capabilities = array(
'publish_posts' => 'publish_dictionary_entry',
'edit_posts' => 'edit_dictionary_entry',
'edit_others_posts' => 'edit_others_dictionary_entry',
'delete_posts' => 'delete_dictionary_entry',
'delete_others_posts' => 'delete_others_dictionary_entry',
'read_private_posts' => 'read_private_dictionary_entry'
);

$labels = array(
'name' => 'Dictionary Entries',
'singular_name' => 'Dictionary Entry',
'menu_name' => 'Dictionary Entries',
'add_new' => 'Add New',
'add_new_item' => 'Add New Dictionary Entry',
'edit' => 'Edit entry',
'edit_item' => 'Edit Dictionary Entry',
'new_item' => 'New Dictionary Entry',
'view' => 'View Dictionary Entry',
'view_item' => 'View Dictionary Entry',
'search_items' => 'Search Dictionary Entries',
'not_found' => 'No Dictionary Entries Found',
'not_found_in_trash' => 'No Dictionary Entries Found in Trash',
'parent' => 'Parent Dictionary Entry');

register_post_type('dictionary_entry',
array(
'label' => 'Dictionary Entries',
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'dictionary_entry',
'capabilities'=>$capabilities,
'hierarchical' => false,
'rewrite' => array('slug' => ''),
'query_var' => true,
'supports' => array('title','comments','revisions','thumbnail','author','page-attributes',),
'labels' => $labels,
)
);

flush_rewrite_rules(false);

/********************** CUSTOM ROLE *****************************/
remove_role('dictionary_entry_author');
add_role('dictionary_entry_author',
'Dictionary Helper', array(
'publish_dictionary_entry' => true,
'edit_dictionary_entry' => true,
'edit_others_dictionary_entry' => true,
'delete_dictionary_entry' => true,
'delete_others_dictionary_entry' => false,
'read_private_dictionary_entry' => true,
'read_dictionary_entry' => true,
'read' => true
));
}

0

Решение

Сначала вы должны сопоставить ваши возможности с реальными возможностями.
Что-то вроде:

add_filter('map_meta_cap', function($caps, $cap, $user_id, $args) {
$cap_type = 'dictionary_entry';

/* If editing, deleting, or reading a cpt, get the post and post type object. */
if ( 'edit_' . $cap_type == $cap || 'delete_' . $cap_type == $cap || 'read_' . $cap_type == $cap ) {
$post = \get_post($args[0]);
$post_type = \get_post_type_object( $post->post_type );

/* Set an empty array for the caps. */
$caps = array();
}

/* If editing a cpt, assign the required capability. */
if ( 'edit_' . $cap_type == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
}

/* If deleting a cpt, assign the required capability. */
elseif ( 'delete_' . $cap_type == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->delete_posts;
else
$caps[] = $post_type->cap->delete_others_posts;
}

/* If reading a private cpt, assign the required capability. */
elseif ( 'read_' . $cap_type == $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 the capabilities required by the user. */
return $caps;
}, 10, 4);

Также проверьте https://codex.wordpress.org/Function_Reference/map_meta_cap (хотя это довольно тонкий) и http://mannieschumpert.com/blog/wordpress-capabilities-magic-with-map_meta_cap/

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector