/**
* Get a specific post status
*
* @param WP_REST_Request $request
* @return array|WP_Error
*/
public function get_item($request)
{
$obj = get_post_status_object($request['status']);
if (empty($obj)) {
return new WP_Error('rest_status_invalid', __('Invalid status.'), array('status' => 404));
}
return $this->prepare_item_for_response($obj, $request);
}
/**
* Hooks the WP get_comments_number filter to get the number of comments
* across all posts in the translation group.
*
* @param int $count The number of comments on the single translation
* @param int $post_id The post ID of the single translation
* @return int The count of all comments on published posts in this translation group
**/
public function get_comments_number($count, $post_id)
{
$translations = bbl_get_post_translations($post_id);
$count = 0;
foreach ($translations as &$translation) {
$post_status = get_post_status_object($translation->post_status);
// FIXME: I'm not entirely sure about using publicly_queryable here… what I want to avoid is draft, private, etc statii.
if ($post_status->publicly_queryable) {
$count += $translation->comment_count;
}
}
return $count;
}
/**
* Helper function for checking if a user can read forums, topics, or replies. We need this to handle
* users who are not logged in but should have permission to read (e.g, non-private forums). This
* function is meant to be used in conjunction with a filter on `map_meta_cap`.
*
* @since 1.0.0
* @access public
* @param int $user_id
* @param string $cap
* @param int $post_id
* @return bool
*/
function mb_user_can($user_id, $cap, $post_id)
{
// @todo Check hierarchy.
if (in_array($cap, array('read_forum', 'read_topic', 'read_reply'))) {
if ('read_forum' === $cap) {
$status_obj = get_post_status_object(mb_get_forum_status($post_id));
} elseif ('read_topic' === $cap) {
$status_obj = get_post_status_object(mb_get_topic_status($post_id));
} elseif ('read_forum' === $cap) {
$status_obj = get_post_status_object(mb_get_reply_status($post_id));
}
if (false === $status_obj->private && false === $status_obj->protected) {
return true;
}
}
return user_can($user_id, $cap, $post_id);
}
/**
* Rename $_POST data from form names to DB post columns.
*
* Manipulates $_POST directly.
*
* @package WordPress
* @since 2.6.0
*
* @param bool $update Are we updating a pre-existing post?
* @param array $post_data Array of post data. Defaults to the contents of $_POST.
* @return object|bool WP_Error on failure, true on success.
*/
function _wp_translate_postdata($update = false, $post_data = null)
{
if (empty($post_data)) {
$post_data =& $_POST;
}
if ($update) {
$post_data['ID'] = (int) $post_data['post_ID'];
}
$ptype = get_post_type_object($post_data['post_type']);
if ($update && !current_user_can('edit_post', $post_data['ID'])) {
if ('page' == $post_data['post_type']) {
return new WP_Error('edit_others_pages', __('Sorry, you are not allowed to edit pages as this user.'));
} else {
return new WP_Error('edit_others_posts', __('Sorry, you are not allowed to edit posts as this user.'));
}
} elseif (!$update && !current_user_can($ptype->cap->create_posts)) {
if ('page' == $post_data['post_type']) {
return new WP_Error('edit_others_pages', __('Sorry, you are not allowed to create pages as this user.'));
} else {
return new WP_Error('edit_others_posts', __('Sorry, you are not allowed to create posts as this user.'));
}
}
if (isset($post_data['content'])) {
$post_data['post_content'] = $post_data['content'];
}
if (isset($post_data['excerpt'])) {
$post_data['post_excerpt'] = $post_data['excerpt'];
}
if (isset($post_data['parent_id'])) {
$post_data['post_parent'] = (int) $post_data['parent_id'];
}
if (isset($post_data['trackback_url'])) {
$post_data['to_ping'] = $post_data['trackback_url'];
}
$post_data['user_ID'] = get_current_user_id();
if (!empty($post_data['post_author_override'])) {
$post_data['post_author'] = (int) $post_data['post_author_override'];
} else {
if (!empty($post_data['post_author'])) {
$post_data['post_author'] = (int) $post_data['post_author'];
} else {
$post_data['post_author'] = (int) $post_data['user_ID'];
}
}
if (isset($post_data['user_ID']) && $post_data['post_author'] != $post_data['user_ID'] && !current_user_can($ptype->cap->edit_others_posts)) {
if ($update) {
if ('page' == $post_data['post_type']) {
return new WP_Error('edit_others_pages', __('Sorry, you are not allowed to edit pages as this user.'));
} else {
return new WP_Error('edit_others_posts', __('Sorry, you are not allowed to edit posts as this user.'));
}
} else {
if ('page' == $post_data['post_type']) {
return new WP_Error('edit_others_pages', __('Sorry, you are not allowed to create pages as this user.'));
} else {
return new WP_Error('edit_others_posts', __('Sorry, you are not allowed to create posts as this user.'));
}
}
}
if (!empty($post_data['post_status'])) {
$post_data['post_status'] = sanitize_key($post_data['post_status']);
// No longer an auto-draft
if ('auto-draft' === $post_data['post_status']) {
$post_data['post_status'] = 'draft';
}
if (!get_post_status_object($post_data['post_status'])) {
unset($post_data['post_status']);
}
}
// What to do based on which button they pressed
if (isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft']) {
$post_data['post_status'] = 'draft';
}
if (isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate']) {
$post_data['post_status'] = 'private';
}
if (isset($post_data['publish']) && '' != $post_data['publish'] && (!isset($post_data['post_status']) || $post_data['post_status'] != 'private')) {
$post_data['post_status'] = 'publish';
}
if (isset($post_data['advanced']) && '' != $post_data['advanced']) {
$post_data['post_status'] = 'draft';
}
if (isset($post_data['pending']) && '' != $post_data['pending']) {
$post_data['post_status'] = 'pending';
}
if (isset($post_data['ID'])) {
$post_id = $post_data['ID'];
} else {
//.........这里部分代码省略.........
//.........这里部分代码省略.........
}
} else {
// The user is trying to edit someone else's post.
$caps[] = $post_type->cap->edit_others_posts;
// The post is published, extra cap required.
if ('publish' == $post->post_status) {
$caps[] = $post_type->cap->edit_published_posts;
} elseif ('private' == $post->post_status) {
$caps[] = $post_type->cap->edit_private_posts;
}
}
break;
case 'read_post':
case 'read_page':
$post = get_post($args[0]);
if ('revision' == $post->post_type) {
$post = get_post($post->post_parent);
}
$post_type = get_post_type_object($post->post_type);
if (!$post_type) {
/* translators: 1: post type, 2: capability name */
_doing_it_wrong(__FUNCTION__, sprintf(__('The post type %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post of that type.'), $post->post_type, $cap), '4.4.0');
$caps[] = 'edit_others_posts';
break;
}
if (!$post_type->map_meta_cap) {
$caps[] = $post_type->cap->{$cap};
// Prior to 3.1 we would re-call map_meta_cap here.
if ('read_post' == $cap) {
$cap = $post_type->cap->{$cap};
}
break;
}
$status_obj = get_post_status_object($post->post_status);
if ($status_obj->public) {
$caps[] = $post_type->cap->read;
break;
}
if ($post->post_author && $user_id == $post->post_author) {
$caps[] = $post_type->cap->read;
} elseif ($status_obj->private) {
$caps[] = $post_type->cap->read_private_posts;
} else {
$caps = map_meta_cap('edit_post', $user_id, $post->ID);
}
break;
case 'publish_post':
$post = get_post($args[0]);
$post_type = get_post_type_object($post->post_type);
if (!$post_type) {
/* translators: 1: post type, 2: capability name */
_doing_it_wrong(__FUNCTION__, sprintf(__('The post type %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post of that type.'), $post->post_type, $cap), '4.4.0');
$caps[] = 'edit_others_posts';
break;
}
$caps[] = $post_type->cap->publish_posts;
break;
case 'edit_post_meta':
case 'delete_post_meta':
case 'add_post_meta':
$post = get_post($args[0]);
$caps = map_meta_cap('edit_post', $user_id, $post->ID);
$meta_key = isset($args[1]) ? $args[1] : false;
if ($meta_key && has_filter("auth_post_meta_{$meta_key}")) {
/**
* Filter whether the user is allowed to add post meta to a post.
请发表评论