Move post type object capabilities to a 'cap' object. Allow them to be initialized via the 'capabilities' key (an array) when registering support for the post type. Caps are now referred to by the name of the cap as if it was a post, i.e. ->cap->edit_post. see #13358.

git-svn-id: http://svn.automattic.com/wordpress/trunk@14585 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2010-05-12 20:45:18 +00:00
parent b2d3b7efa2
commit 7734a07cb2
13 changed files with 86 additions and 73 deletions

View File

@ -142,7 +142,7 @@ if ( post_type_supports($post_type, 'comments') )
if ( ('publish' == $post->post_status || 'private' == $post->post_status) && post_type_supports($post_type, 'comments') )
add_meta_box('commentsdiv', __('Comments'), 'post_comment_meta_box', $post_type, 'normal', 'core');
if ( !( 'pending' == $post->post_status && !current_user_can( $post_type_object->publish_cap ) ) )
if ( !( 'pending' == $post->post_status && !current_user_can( $post_type_object->cap->publish_posts ) ) )
add_meta_box('slugdiv', __('Slug'), 'post_slug_meta_box', $post_type, 'normal', 'core');
if ( post_type_supports($post_type, 'author') ) {
@ -221,7 +221,7 @@ $shortlink = wp_get_shortlink($post->ID, 'post');
if ( !empty($shortlink) )
$sample_permalink_html .= '<input id="shortlink" type="hidden" value="' . esc_attr($shortlink) . '" /><a href="#" class="button" onclick="prompt(&#39;URL:&#39;, jQuery(\'#shortlink\').val()); return false;">' . __('Get Shortlink') . '</a>';
if ( !( 'pending' == $post->post_status && !current_user_can( $post_type_object->publish_cap ) ) ) { ?>
if ( !( 'pending' == $post->post_status && !current_user_can( $post_type_object->cap->publish_posts ) ) ) { ?>
<div id="edit-slug-box">
<?php
if ( ! empty($post->ID) && ! empty($sample_permalink_html) && 'auto-draft' != $post->post_status )

View File

@ -19,7 +19,7 @@ $_GET['post_type'] = $post_type;
$post_type_object = get_post_type_object($post_type);
if ( !current_user_can($post_type_object->edit_type_cap) )
if ( !current_user_can($post_type_object->cap->edit_posts) )
wp_die(__('Cheatin&#8217; uh?'));
// Back-compat for viewing comments of an entry
@ -73,7 +73,7 @@ if ( isset($_GET['doaction']) || isset($_GET['doaction2']) || isset($_GET['delet
case 'trash':
$trashed = 0;
foreach( (array) $post_ids as $post_id ) {
if ( !current_user_can($post_type_object->delete_cap, $post_id) )
if ( !current_user_can($post_type_object->cap->delete_post, $post_id) )
wp_die( __('You are not allowed to move this item to the Trash.') );
if ( !wp_trash_post($post_id) )
@ -86,7 +86,7 @@ if ( isset($_GET['doaction']) || isset($_GET['doaction2']) || isset($_GET['delet
case 'untrash':
$untrashed = 0;
foreach( (array) $post_ids as $post_id ) {
if ( !current_user_can($post_type_object->delete_cap, $post_id) )
if ( !current_user_can($post_type_object->cap->delete_post, $post_id) )
wp_die( __('You are not allowed to restore this item from the Trash.') );
if ( !wp_untrash_post($post_id) )
@ -101,7 +101,7 @@ if ( isset($_GET['doaction']) || isset($_GET['doaction2']) || isset($_GET['delet
foreach( (array) $post_ids as $post_id ) {
$post_del = & get_post($post_id);
if ( !current_user_can($post_type_object->delete_cap, $post_id) )
if ( !current_user_can($post_type_object->cap->delete_post, $post_id) )
wp_die( __('You are not allowed to delete this item.') );
if ( $post_del->post_type == 'attachment' ) {
@ -140,7 +140,7 @@ if ( isset($_GET['doaction']) || isset($_GET['doaction2']) || isset($_GET['delet
wp_enqueue_script('inline-edit-post');
$user_posts = false;
if ( !current_user_can($post_type_object->edit_others_cap) ) {
if ( !current_user_can($post_type_object->cap->edit_other_posts) ) {
$user_posts_count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(1) FROM $wpdb->posts WHERE post_type = '%s' AND post_status NOT IN ('trash', 'auto-draft') AND post_author = %d", $post_type, $current_user->ID) );
$user_posts = true;
if ( $user_posts_count && empty($_GET['post_status']) && empty($_GET['all_posts']) && empty($_GET['author']) )
@ -345,7 +345,7 @@ do_action('restrict_manage_posts');
<input type="submit" id="post-query-submit" value="<?php esc_attr_e('Filter'); ?>" class="button-secondary" />
<?php }
if ( $is_trash && current_user_can($post_type_object->edit_others_cap) ) { ?>
if ( $is_trash && current_user_can($post_type_object->cap->edit_other_posts) ) { ?>
<input type="submit" name="delete_all" id="delete_all" value="<?php esc_attr_e('Empty Trash'); ?>" class="button-secondary apply" />
<?php } ?>
</div>
@ -396,7 +396,7 @@ if ( $page_links )
<?php } ?>
</select>
<input type="submit" value="<?php esc_attr_e('Apply'); ?>" name="doaction2" id="doaction2" class="button-secondary action" />
<?php if ( $is_trash && current_user_can($post_type_object->edit_others_cap) ) { ?>
<?php if ( $is_trash && current_user_can($post_type_object->cap->edit_other_posts) ) { ?>
<input type="submit" name="delete_all2" id="delete_all2" value="<?php esc_attr_e('Empty Trash'); ?>" class="button-secondary apply" />
<?php } ?>
<br class="clear" />

View File

@ -14,7 +14,7 @@ function post_submit_meta_box($post) {
$post_type = $post->post_type;
$post_type_object = get_post_type_object($post_type);
$can_publish = current_user_can($post_type_object->publish_cap);
$can_publish = current_user_can($post_type_object->cap->publish_posts);
?>
<div class="submitbox" id="submitpost">

View File

@ -46,7 +46,7 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
$ptype = get_post_type_object( $post_data['post_type'] );
if ( isset($post_data['user_ID']) && ($post_data['post_author'] != $post_data['user_ID']) ) {
if ( !current_user_can( $ptype->edit_others_cap ) ) {
if ( !current_user_can( $ptype->cap->edit_other_posts ) ) {
if ( 'page' == $post_data['post_type'] ) {
return new WP_Error( 'edit_others_pages', $update ?
__( 'You are not allowed to edit pages as this user.' ) :
@ -81,7 +81,7 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
// Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
// Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
if ( isset($post_data['post_status']) && ('publish' == $post_data['post_status'] && !current_user_can( $ptype->publish_cap )) )
if ( isset($post_data['post_status']) && ('publish' == $post_data['post_status'] && !current_user_can( $ptype->cap->publish_posts )) )
if ( $previous_status != 'publish' || !current_user_can( 'edit_post', $post_id ) )
$post_data['post_status'] = 'pending';
@ -138,7 +138,7 @@ function edit_post( $post_data = null ) {
$post_ID = (int) $post_data['post_ID'];
$ptype = get_post_type_object($post_data['post_type']);
if ( !current_user_can( $ptype->edit_cap, $post_ID ) ) {
if ( !current_user_can( $ptype->cap->edit_post, $post_ID ) ) {
if ( 'page' == $post_data['post_type'] )
wp_die( __('You are not allowed to edit this page.' ));
else
@ -237,7 +237,7 @@ function bulk_edit_posts( $post_data = null ) {
else
$ptype = get_post_type_object('post');
if ( !current_user_can( $ptype->edit_type_cap ) ) {
if ( !current_user_can( $ptype->cap->edit_posts ) ) {
if ( 'page' == $ptype->name )
wp_die( __('You are not allowed to edit pages.'));
else
@ -494,7 +494,7 @@ function wp_write_post() {
else
$ptype = get_post_type_object('post');
if ( !current_user_can( $ptype->edit_type_cap ) ) {
if ( !current_user_can( $ptype->cap->edit_posts ) ) {
if ( 'page' == $ptype->name )
return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) );
else

View File

@ -876,7 +876,7 @@ function inline_edit_row( $screen ) {
$hidden = array_intersect( array_keys( $columns ), array_filter( get_hidden_columns($screen) ) );
$col_count = count($columns) - count($hidden);
$m = ( isset($mode) && 'excerpt' == $mode ) ? 'excerpt' : 'list';
$can_publish = current_user_can($post_type_object->publish_cap);
$can_publish = current_user_can($post_type_object->cap->publish_posts);
$core_columns = array( 'cb' => true, 'date' => true, 'title' => true, 'categories' => true, 'tags' => true, 'comments' => true, 'author' => true );
?>
@ -1111,7 +1111,7 @@ endif; // post_type_supports comments or pings ?>
</select>
</label>
<?php if ( 'post' == $screen->post_type && $can_publish && current_user_can( $post_type_object->edit_others_cap ) ) : ?>
<?php if ( 'post' == $screen->post_type && $can_publish && current_user_can( $post_type_object->cap->edit_other_posts ) ) : ?>
<?php if ( $bulk ) : ?>
@ -1181,7 +1181,7 @@ endif; // post_type_supports comments or pings ?>
*/
function get_inline_data($post) {
$post_type_object = get_post_type_object($post->post_type);
if ( ! current_user_can($post_type_object->edit_cap, $post->ID) )
if ( ! current_user_can($post_type_object->cap->edit_post, $post->ID) )
return;
$title = esc_attr( get_the_title( $post->ID ) );
@ -1298,7 +1298,7 @@ function _post_row($a_post, $pending_comments, $mode) {
case 'cb':
?>
<th scope="row" class="check-column"><?php if ( current_user_can( $post_type_object->edit_cap, $post->ID ) ) { ?><input type="checkbox" name="post[]" value="<?php the_ID(); ?>" /><?php } ?></th>
<th scope="row" class="check-column"><?php if ( current_user_can( $post_type_object->cap->edit_post, $post->ID ) ) { ?><input type="checkbox" name="post[]" value="<?php the_ID(); ?>" /><?php } ?></th>
<?php
break;
@ -1341,17 +1341,17 @@ function _post_row($a_post, $pending_comments, $mode) {
case 'title':
$attributes = 'class="post-title column-title"' . $style;
?>
<td <?php echo $attributes ?>><strong><?php if ( current_user_can($post_type_object->edit_cap, $post->ID) && $post->post_status != 'trash' ) { ?><a class="row-title" href="<?php echo $edit_link; ?>" title="<?php echo esc_attr(sprintf(__('Edit &#8220;%s&#8221;'), $title)); ?>"><?php echo $title ?></a><?php } else { echo $title; }; _post_states($post); ?></strong>
<td <?php echo $attributes ?>><strong><?php if ( current_user_can($post_type_object->cap->edit_post, $post->ID) && $post->post_status != 'trash' ) { ?><a class="row-title" href="<?php echo $edit_link; ?>" title="<?php echo esc_attr(sprintf(__('Edit &#8220;%s&#8221;'), $title)); ?>"><?php echo $title ?></a><?php } else { echo $title; }; _post_states($post); ?></strong>
<?php
if ( 'excerpt' == $mode )
the_excerpt();
$actions = array();
if ( current_user_can($post_type_object->edit_cap, $post->ID) && 'trash' != $post->post_status ) {
if ( current_user_can($post_type_object->cap->edit_post, $post->ID) && 'trash' != $post->post_status ) {
$actions['edit'] = '<a href="' . get_edit_post_link($post->ID, true) . '" title="' . esc_attr(__('Edit this post')) . '">' . __('Edit') . '</a>';
$actions['inline hide-if-no-js'] = '<a href="#" class="editinline" title="' . esc_attr(__('Edit this post inline')) . '">' . __('Quick&nbsp;Edit') . '</a>';
}
if ( current_user_can($post_type_object->delete_cap, $post->ID) ) {
if ( current_user_can($post_type_object->cap->delete_post, $post->ID) ) {
if ( 'trash' == $post->post_status )
$actions['untrash'] = "<a title='" . esc_attr(__('Restore this post from the Trash')) . "' href='" . wp_nonce_url( admin_url( sprintf($post_type_object->_edit_link . '&amp;action=untrash', $post->ID) ), 'untrash-' . $post->post_type . '_' . $post->ID ) . "'>" . __('Restore') . "</a>";
elseif ( EMPTY_TRASH_DAYS )
@ -1360,7 +1360,7 @@ function _post_row($a_post, $pending_comments, $mode) {
$actions['delete'] = "<a class='submitdelete' title='" . esc_attr(__('Delete this post permanently')) . "' href='" . get_delete_post_link($post->ID, '', true) . "'>" . __('Delete Permanently') . "</a>";
}
if ( in_array($post->post_status, array('pending', 'draft')) ) {
if ( current_user_can($post_type_object->edit_cap, $post->ID) )
if ( current_user_can($post_type_object->cap->edit_post, $post->ID) )
$actions['view'] = '<a href="' . add_query_arg( 'preview', 'true', get_permalink($post->ID) ) . '" title="' . esc_attr(sprintf(__('Preview &#8220;%s&#8221;'), $title)) . '" rel="permalink">' . __('Preview') . '</a>';
} elseif ( 'trash' != $post->post_status ) {
$actions['view'] = '<a href="' . get_permalink($post->ID) . '" title="' . esc_attr(sprintf(__('View &#8220;%s&#8221;'), $title)) . '" rel="permalink">' . __('View') . '</a>';
@ -1443,13 +1443,13 @@ function _post_row($a_post, $pending_comments, $mode) {
case 'control_edit':
?>
<td><?php if ( current_user_can($post_type_object->edit_cap, $post->ID) ) { echo "<a href='$edit_link' class='edit'>" . __('Edit') . "</a>"; } ?></td>
<td><?php if ( current_user_can($post_type_object->cap->edit_post, $post->ID) ) { echo "<a href='$edit_link' class='edit'>" . __('Edit') . "</a>"; } ?></td>
<?php
break;
case 'control_delete':
?>
<td><?php if ( current_user_can($post_type_object->delete_cap, $post->ID) ) { echo "<a href='" . wp_nonce_url("post.php?action=delete&amp;post=$id", 'delete-post_' . $post->ID) . "' class='delete'>" . __('Delete') . "</a>"; } ?></td>
<td><?php if ( current_user_can($post_type_object->cap->delete_post, $post->ID) ) { echo "<a href='" . wp_nonce_url("post.php?action=delete&amp;post=$id", 'delete-post_' . $post->ID) . "' class='delete'>" . __('Delete') . "</a>"; } ?></td>
<?php
break;
@ -1566,14 +1566,14 @@ foreach ( $posts_columns as $column_name => $column_display_name ) {
$attributes = 'class="post-title page-title column-title"' . $style;
$edit_link = get_edit_post_link( $page->ID );
?>
<td <?php echo $attributes ?>><strong><?php if ( current_user_can($post_type_object->edit_cap, $page->ID) && $post->post_status != 'trash' ) { ?><a class="row-title" href="<?php echo $edit_link; ?>" title="<?php echo esc_attr(sprintf(__('Edit &#8220;%s&#8221;'), $title)); ?>"><?php echo $pad; echo $title ?></a><?php } else { echo $pad; echo $title; }; _post_states($page); echo isset($parent_name) ? ' | ' . $post_type_object->labels->parent . ' ' . esc_html($parent_name) : ''; ?></strong>
<td <?php echo $attributes ?>><strong><?php if ( current_user_can($post_type_object->cap->edit_post, $page->ID) && $post->post_status != 'trash' ) { ?><a class="row-title" href="<?php echo $edit_link; ?>" title="<?php echo esc_attr(sprintf(__('Edit &#8220;%s&#8221;'), $title)); ?>"><?php echo $pad; echo $title ?></a><?php } else { echo $pad; echo $title; }; _post_states($page); echo isset($parent_name) ? ' | ' . $post_type_object->labels->parent . ' ' . esc_html($parent_name) : ''; ?></strong>
<?php
$actions = array();
if ( current_user_can($post_type_object->edit_cap, $page->ID) && $post->post_status != 'trash' ) {
if ( current_user_can($post_type_object->cap->edit_post, $page->ID) && $post->post_status != 'trash' ) {
$actions['edit'] = '<a href="' . $edit_link . '" title="' . esc_attr(__('Edit this page')) . '">' . __('Edit') . '</a>';
$actions['inline'] = '<a href="#" class="editinline">' . __('Quick&nbsp;Edit') . '</a>';
}
if ( current_user_can($post_type_object->delete_cap, $page->ID) ) {
if ( current_user_can($post_type_object->cap->delete_post, $page->ID) ) {
if ( $post->post_status == 'trash' )
$actions['untrash'] = "<a title='" . esc_attr(__('Remove this page from the Trash')) . "' href='" . wp_nonce_url("post.php?post_type=$post_type&amp;action=untrash&amp;post=$page->ID", 'untrash-' . $post->post_type . '_' . $page->ID) . "'>" . __('Restore') . "</a>";
elseif ( EMPTY_TRASH_DAYS )
@ -1582,7 +1582,7 @@ foreach ( $posts_columns as $column_name => $column_display_name ) {
$actions['delete'] = "<a class='submitdelete' title='" . esc_attr(__('Delete this page permanently')) . "' href='" . wp_nonce_url("post.php?post_type=$post_type&amp;action=delete&amp;post=$page->ID", 'delete-' . $post->post_type . '_' . $page->ID) . "'>" . __('Delete Permanently') . "</a>";
}
if ( in_array($post->post_status, array('pending', 'draft')) ) {
if ( current_user_can($post_type_object->edit_cap, $page->ID) )
if ( current_user_can($post_type_object->cap->edit_post, $page->ID) )
$actions['view'] = '<a href="' . add_query_arg( 'preview', 'true', get_permalink($page->ID) ) . '" title="' . esc_attr(sprintf(__('Preview &#8220;%s&#8221;'), $title)) . '" rel="permalink">' . __('Preview') . '</a>';
} elseif ( $post->post_status != 'trash' ) {
$actions['view'] = '<a href="' . get_permalink($page->ID) . '" title="' . esc_attr(sprintf(__('View &#8220;%s&#8221;'), $title)) . '" rel="permalink">' . __('View') . '</a>';
@ -2010,7 +2010,7 @@ function _wp_comment_row( $comment_id, $mode, $comment_status, $checkbox = true,
$post = get_post($comment->comment_post_ID);
$the_comment_status = wp_get_comment_status($comment->comment_ID);
$post_type_object = get_post_type_object($post->post_type);
$user_can = current_user_can($post_type_object->edit_cap, $post->ID);
$user_can = current_user_can($post_type_object->cap->edit_post, $post->ID);
$comment_url = esc_url(get_comment_link($comment->comment_ID));
$author_url = get_comment_author_url();
@ -3328,10 +3328,10 @@ function favorite_actions( $screen = null ) {
if ( isset($post_type_object) ) {
switch ( $screen->id ) {
case $post_type_object->name:
$default_action = array('edit.php?post_type=' . $post_type_object->name => array($post_type_object->labels->edit_item, $post_type_object->edit_type_cap));
$default_action = array('edit.php?post_type=' . $post_type_object->name => array($post_type_object->labels->edit_item, $post_type_object->cap->edit_posts));
break;
case "edit-{$post_type_object->name}":
$default_action = array('post-new.php?post_type=' . $post_type_object->name => array($post_type_object->labels->new_item, $post_type_object->edit_type_cap));
$default_action = array('post-new.php?post_type=' . $post_type_object->name => array($post_type_object->labels->new_item, $post_type_object->cap->edit_posts));
break;
}
}

View File

@ -249,8 +249,8 @@ function get_editable_user_ids( $user_id, $exclude_zeros = true, $post_type = 'p
$user = new WP_User( $user_id );
$post_type_obj = get_post_type_object($post_type);
if ( ! $user->has_cap($post_type_obj->edit_others_cap) ) {
if ( $user->has_cap($post_type_obj->edit_type_cap) || ! $exclude_zeros )
if ( ! $user->has_cap($post_type_obj->cap->edit_other_posts) ) {
if ( $user->has_cap($post_type_obj->cap->edit_posts) || ! $exclude_zeros )
return array($user->id);
else
return array();

View File

@ -129,9 +129,9 @@ foreach ( (array) get_post_types( array('show_ui' => true) ) as $ptype ) {
while ( isset($menu[$ptype_menu_position]) || in_array($ptype_menu_position, $core_menu_positions) )
$ptype_menu_position++;
$menu[$ptype_menu_position] = array( esc_attr( $ptype_obj->labels->name ), $ptype_obj->edit_type_cap, "edit.php?post_type=$ptype", '', 'menu-top menu-icon-' . $ptype_class, 'menu-' . $ptype_class, $menu_icon );
$submenu["edit.php?post_type=$ptype"][5] = array( $ptype_obj->labels->edit, $ptype_obj->edit_type_cap, "edit.php?post_type=$ptype");
$submenu["edit.php?post_type=$ptype"][10] = array( $ptype_obj->labels->add_new, $ptype_obj->edit_type_cap, "post-new.php?post_type=$ptype" );
$menu[$ptype_menu_position] = array( esc_attr( $ptype_obj->labels->name ), $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype", '', 'menu-top menu-icon-' . $ptype_class, 'menu-' . $ptype_class, $menu_icon );
$submenu["edit.php?post_type=$ptype"][5] = array( $ptype_obj->labels->edit, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype");
$submenu["edit.php?post_type=$ptype"][10] = array( $ptype_obj->labels->add_new, $ptype_obj->cap->edit_posts, "post-new.php?post_type=$ptype" );
$i = 15;
foreach ( $wp_taxonomies as $tax ) {

View File

@ -46,7 +46,7 @@ When you&#8217;re promoted, just reload this page and you&#8217;ll be able to bl
wp_enqueue_script('autosave');
// Show post form.
if ( current_user_can($post_type_object->edit_type_cap) ) {
if ( current_user_can($post_type_object->cap->edit_posts) ) {
$post = get_default_post_to_edit( $post_type, true );
$post_ID = $post->ID;
include('edit-form-advanced.php');

View File

@ -152,7 +152,7 @@ case 'edit':
if ( empty($post->ID) )
wp_die( __('You attempted to edit an item that doesn&#8217;t exist. Perhaps it was deleted?') );
if ( !current_user_can($post_type_object->edit_cap, $post_id) )
if ( !current_user_can($post_type_object->cap->edit_post, $post_id) )
wp_die( __('You are not allowed to edit this item.') );
if ( 'trash' == $post->post_status )
@ -217,7 +217,7 @@ case 'trash':
$post = & get_post($post_id);
if ( !current_user_can($post_type_object->delete_cap, $post_id) )
if ( !current_user_can($post_type_object->cap->delete_post, $post_id) )
wp_die( __('You are not allowed to move this item to the Trash.') );
if ( ! wp_trash_post($post_id) )
@ -230,7 +230,7 @@ case 'trash':
case 'untrash':
check_admin_referer('untrash-' . $post_type . '_' . $post_id);
if ( !current_user_can($post_type_object->delete_cap, $post_id) )
if ( !current_user_can($post_type_object->cap->delete_post, $post_id) )
wp_die( __('You are not allowed to move this item out of the Trash.') );
if ( ! wp_untrash_post($post_id) )
@ -243,7 +243,7 @@ case 'untrash':
case 'delete':
check_admin_referer('delete-' . $post_type . '_' . $post_id);
if ( !current_user_can($post_type_object->delete_cap, $post_id) )
if ( !current_user_can($post_type_object->cap->delete_post, $post_id) )
wp_die( __('You are not allowed to delete this item.') );
$force = !EMPTY_TRASH_DAYS;

View File

@ -823,7 +823,7 @@ function map_meta_cap( $cap, $user_id ) {
$post = get_post( $args[0] );
$post_type = get_post_type_object( $post->post_type );
if ( $post_type && 'post' != $post_type->capability_type ) {
$args = array_merge( array( $post_type->delete_cap, $user_id ), $args );
$args = array_merge( array( $post_type->cap->delete_post, $user_id ), $args );
return call_user_func_array( 'map_meta_cap', $args );
}
@ -900,7 +900,7 @@ function map_meta_cap( $cap, $user_id ) {
$post = get_post( $args[0] );
$post_type = get_post_type_object( $post->post_type );
if ( $post_type && 'post' != $post_type->capability_type ) {
$args = array_merge( array( $post_type->edit_cap, $user_id ), $args );
$args = array_merge( array( $post_type->cap->edit_post, $user_id ), $args );
return call_user_func_array( 'map_meta_cap', $args );
}
$post_author_data = get_userdata( $post->post_author );
@ -959,7 +959,7 @@ function map_meta_cap( $cap, $user_id ) {
$post = get_post( $args[0] );
$post_type = get_post_type_object( $post->post_type );
if ( $post_type && 'post' != $post_type->capability_type ) {
$args = array_merge( array( $post_type->read_cap, $user_id ), $args );
$args = array_merge( array( $post_type->cap->read_post, $user_id ), $args );
return call_user_func_array( 'map_meta_cap', $args );
}

View File

@ -793,7 +793,7 @@ function get_edit_post_link( $id = 0, $context = 'display' ) {
if ( !$post_type_object )
return;
if ( !current_user_can( $post_type_object->edit_cap, $post->ID ) )
if ( !current_user_can( $post_type_object->cap->edit_post, $post->ID ) )
return;
return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
@ -846,7 +846,7 @@ function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false
if ( !$post_type_object )
return;
if ( !current_user_can( $post_type_object->delete_cap, $post->ID ) )
if ( !current_user_can( $post_type_object->cap->delete_post, $post->ID ) )
return;
$action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';

View File

@ -774,12 +774,7 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' )
* - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
* - inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
* - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
* - edit_cap - The capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post).
* - edit_type_cap - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
* - edit_others_cap - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
* - publish_others_cap - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
* - read_cap - The capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post).
* - delete_cap - The capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post).
* - capabilities - Array of capabilities for this post type. You can see accepted values in {@link get_post_type_capabilities()}. By default the capability_type is used to construct capabilities.
* - hierarchical - Whether the post type is hierarchical. Defaults to false.
* - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
* - register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
@ -802,7 +797,7 @@ function register_post_type($post_type, $args = array()) {
// Args prefixed with an underscore are reserved for internal use.
$defaults = array(
'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
'_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false,
'_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'capabilities' => array(), 'hierarchical' => false,
'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null,
'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
'permalink_epmask' => EP_PERMALINK, 'can_export' => true,
@ -827,20 +822,8 @@ function register_post_type($post_type, $args = array()) {
if ( empty($args->capability_type) )
$args->capability_type = 'post';
if ( empty($args->edit_cap) )
$args->edit_cap = 'edit_' . $args->capability_type;
if ( empty($args->edit_type_cap) )
$args->edit_type_cap = 'edit_' . $args->capability_type . 's';
if ( empty($args->edit_others_cap) )
$args->edit_others_cap = 'edit_others_' . $args->capability_type . 's';
if ( empty($args->publish_cap) )
$args->publish_cap = 'publish_' . $args->capability_type . 's';
if ( empty($args->read_cap) )
$args->read_cap = 'read_' . $args->capability_type;
if ( empty($args->read_private_cap) )
$args->read_private_cap = 'read_private_' . $args->capability_type . 's';
if ( empty($args->delete_cap) )
$args->delete_cap = 'delete_' . $args->capability_type;
$args->cap = get_post_type_capabilities( $args );
if ( ! empty($args->supports) ) {
add_post_type_support($post_type, $args->supports);
@ -892,6 +875,36 @@ function register_post_type($post_type, $args = array()) {
return $args;
}
/**
* Builds an object with all post type capabilities out of a post type object
*
* Accepted keys of the capabilities array in the post type object:
* - edit_post - The meta capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post).
* - edit_posts - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
* - edit_others_posts - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
* - publish_posts - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
* - read_post - The meta capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post).
* - read_private_posts - The capability that controls reading private posts. Defaults to "read_ . $capability_type . s" (read_private_posts).
* - delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post).
*
* @since 3.0.0
* @param object $args
* @return object object with all the capabilities as member variables
*/
function get_post_type_capabilities( $args ) {
$defaults = array(
'edit_post' => 'edit_' . $args->capabilities['capability_type'],
'edit_posts' => 'edit_' . $args->capabilities['capability_type'] . 's',
'edit_others_posts' => 'edit_others_' . $args->capabilities['capability_type'] . 's',
'publish_posts' => 'publish_' . $args->capabilities['capability_type'] . 's',
'read_post' => 'edit_' . $args->capabilities['capability_type'],
'read_private_posts' => 'read_private_' . $args->capabilities['capability_type'] . 's',
'delete_post' => 'delete_' . $args->capabilities['capability_type'],
);
$labels = array_merge( $defaults, $args->capabilities );
return (object) $labels;
}
/**
* Builds an object with all post type labels out of a post type object
*
@ -1525,7 +1538,7 @@ function wp_count_posts( $type = 'post', $perm = '' ) {
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
if ( 'readable' == $perm && is_user_logged_in() ) {
$post_type_object = get_post_type_object($type);
if ( !current_user_can( $post_type_object->read_private_cap ) ) {
if ( !current_user_can( $post_type_object->cap->read_private_posts ) ) {
$cache_key .= '_' . $perm . '_' . $user->ID;
$query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
}

View File

@ -2192,10 +2192,10 @@ class WP_Query {
if ( !empty($post_type_object) ) {
$post_type_cap = $post_type_object->capability_type;
$edit_cap = $post_type_object->edit_cap;
$read_cap = $post_type_object->read_cap;
$edit_others_cap = $post_type_object->edit_others_cap;
$read_private_cap = $post_type_object->read_private_cap;
$edit_cap = $post_type_object->cap->edit_post;
$read_cap = $post_type_object->cap->read_post;
$edit_others_cap = $post_type_object->cap->edit_other_posts;
$read_private_cap = $post_type_object->cap->read_private_posts;
} else {
$edit_cap = 'edit_' . $post_type_cap;
$read_cap = 'read_' . $post_type_cap;