/** * Get a local date with its GMT equivalent, in MySQL datetime format * * @deprecated * @param string $date RFC3339 timestamp * @param boolean $force_utc Should we force UTC timestamp? * @return array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s), null on failure */ public function get_date_with_gmt($date, $force_utc = false) { _deprecated_function(__CLASS__ . '::' . __METHOD__, 'WPAPI-1.1', 'json_get_date_with_gmt'); return json_get_date_with_gmt($date, $force_utc); }
/** * Helper method for {@see create_post} and {@see edit_post}, containing shared logic. * * * @param array $data Post data to insert. * * @return int|WP_Error */ protected function insert_post(array $data) { $post = array(); $update = !empty($data['ID']); if ($update) { $current_post = get_post(absint($data['ID'])); if (!$current_post) { return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 400)); } $post['ID'] = absint($data['ID']); } else { // Defaults $post['post_author'] = 0; $post['post_password'] = ''; $post['post_excerpt'] = ''; $post['post_content'] = ''; $post['post_title'] = ''; } // Post type if (!empty($data['type'])) { // Changing post type $post_type = get_post_type_object($data['type']); if (!$post_type) { return new WP_Error('json_invalid_post_type', __('Invalid post type'), array('status' => 400)); } $post['post_type'] = $data['type']; } elseif ($update) { // Updating post, use existing post type $current_post = get_post($data['ID']); if (!$current_post) { return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 400)); } $post_type = get_post_type_object($current_post->post_type); $post['post_type'] = $current_post->post_type; } else { // Creating new post, use default type $post['post_type'] = apply_filters('json_insert_default_post_type', 'post'); $post_type = get_post_type_object($post['post_type']); if (!$post_type) { return new WP_Error('json_invalid_post_type', __('Invalid post type'), array('status' => 400)); } } // Permissions check if ($update) { if (!json_check_post_permission($post, 'edit')) { return new WP_Error('json_cannot_edit', __('Sorry, you are not allowed to edit this post.'), array('status' => 401)); } if ($post_type->name != get_post_type($data['ID'])) { return new WP_Error('json_cannot_change_post_type', __('The post type may not be changed.'), array('status' => 400)); } } else { if (!json_check_post_permission($post, 'create')) { return new WP_Error('json_cannot_create', __('Sorry, you are not allowed to post on this site.'), array('status' => 403)); } } // Post status if (!empty($data['status'])) { $post['post_status'] = $data['status']; switch ($post['post_status']) { case 'draft': case 'pending': break; case 'private': if (!json_check_post_permission($post, 'publish_posts')) { return new WP_Error('json_cannot_create_private', __('Sorry, you are not allowed to create private posts in this post type'), array('status' => 403)); } break; case 'publish': case 'future': if (!json_check_post_permission($post, 'publish_posts')) { return new WP_Error('json_cannot_publish', __('Sorry, you are not allowed to publish posts in this post type'), array('status' => 403)); } break; default: if (!get_post_status_object($post['post_status'])) { $post['post_status'] = 'draft'; } break; } } // Post title if (!empty($data['title'])) { $post['post_title'] = $data['title']; } // Post date if (!empty($data['date'])) { $date_data = json_get_date_with_gmt($data['date']); if (!empty($date_data)) { list($post['post_date'], $post['post_date_gmt']) = $date_data; } } elseif (!empty($data['date_gmt'])) { $date_data = json_get_date_with_gmt($data['date_gmt'], true); if (!empty($date_data)) { list($post['post_date'], $post['post_date_gmt']) = $date_data; } } // Post slug if (!empty($data['name'])) { $post['post_name'] = $data['name']; } // Author if (!empty($data['author'])) { // Allow passing an author object if (is_object($data['author'])) { if (empty($data['author']->ID)) { return new WP_Error('json_invalid_author', __('Invalid author object.'), array('status' => 400)); } $data['author'] = (int) $data['author']->ID; } else { $data['author'] = (int) $data['author']; } // Only check edit others' posts if we are another user if ($data['author'] !== get_current_user_id()) { if (!json_check_post_permission($post, 'edit_others_posts')) { return new WP_Error('json_cannot_edit_others', __('You are not allowed to edit posts as this user.'), array('status' => 401)); } $author = get_userdata($data['author']); if (!$author) { return new WP_Error('json_invalid_author', __('Invalid author ID.'), array('status' => 400)); } } $post['post_author'] = $data['author']; } // Post password if (!empty($data['password'])) { $post['post_password'] = $data['password']; if (!json_check_post_permission($post, 'publish_posts')) { return new WP_Error('json_cannot_create_passworded', __('Sorry, you are not allowed to create password protected posts in this post type'), array('status' => 401)); } } // Content and excerpt if (!empty($data['content_raw'])) { $post['post_content'] = $data['content_raw']; } if (!empty($data['excerpt_raw'])) { $post['post_excerpt'] = $data['excerpt_raw']; } // Parent if (!empty($data['parent'])) { $parent = get_post($data['parent']); if (empty($parent)) { return new WP_Error('json_post_invalid_id', __('Invalid post parent ID.'), array('status' => 400)); } $post['post_parent'] = $parent->ID; } // Menu order if (!empty($data['menu_order'])) { $post['menu_order'] = $data['menu_order']; } // Comment status if (!empty($data['comment_status'])) { $post['comment_status'] = $data['comment_status']; } // Ping status if (!empty($data['ping_status'])) { $post['ping_status'] = $data['ping_status']; } // Post format if (!empty($data['post_format'])) { $formats = get_post_format_slugs(); if (!in_array($data['post_format'], $formats)) { return new WP_Error('json_invalid_post_format', __('Invalid post format.'), array('status' => 400)); } $post['post_format'] = $data['post_format']; } // Pre-insert hook $can_insert = apply_filters('json_pre_insert_post', true, $post, $data, $update); if (is_wp_error($can_insert)) { return $can_insert; } // Post meta // TODO: implement this $post_ID = $update ? wp_update_post($post, true) : wp_insert_post($post, true); if (is_wp_error($post_ID)) { return $post_ID; } // If this is a new post, add the post ID to $post if (!$update) { $post['ID'] = $post_ID; } // Post meta if (!empty($data['post_meta'])) { $result = $this->handle_post_meta_action($post_ID, $data); if (is_wp_error($result)) { return $result; } } // Sticky if (isset($data['sticky'])) { if ($data['sticky']) { stick_post($post_ID); } else { unstick_post($post_ID); } } do_action('json_insert_post', $post, $data, $update); return $post_ID; }