aboutsummaryrefslogtreecommitdiff
path: root/srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
diff options
context:
space:
mode:
Diffstat (limited to 'srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php')
-rw-r--r--srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php2616
1 files changed, 2616 insertions, 0 deletions
diff --git a/srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
new file mode 100644
index 0000000..e2a4224
--- /dev/null
+++ b/srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
@@ -0,0 +1,2616 @@
+<?php
+/**
+ * REST API: WP_REST_Posts_Controller class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 4.7.0
+ */
+
+/**
+ * Core class to access posts via the REST API.
+ *
+ * @since 4.7.0
+ *
+ * @see WP_REST_Controller
+ */
+class WP_REST_Posts_Controller extends WP_REST_Controller {
+ /**
+ * Post type.
+ *
+ * @since 4.7.0
+ * @var string
+ */
+ protected $post_type;
+
+ /**
+ * Instance of a post meta fields object.
+ *
+ * @since 4.7.0
+ * @var WP_REST_Post_Meta_Fields
+ */
+ protected $meta;
+
+ /**
+ * Constructor.
+ *
+ * @since 4.7.0
+ *
+ * @param string $post_type Post type.
+ */
+ public function __construct( $post_type ) {
+ $this->post_type = $post_type;
+ $this->namespace = 'wp/v2';
+ $obj = get_post_type_object( $post_type );
+ $this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
+
+ $this->meta = new WP_REST_Post_Meta_Fields( $this->post_type );
+ }
+
+ /**
+ * Registers the routes for the objects of the controller.
+ *
+ * @since 4.7.0
+ *
+ * @see register_rest_route()
+ */
+ public function register_routes() {
+
+ register_rest_route(
+ $this->namespace,
+ '/' . $this->rest_base,
+ array(
+ array(
+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'get_items' ),
+ 'permission_callback' => array( $this, 'get_items_permissions_check' ),
+ 'args' => $this->get_collection_params(),
+ ),
+ array(
+ 'methods' => WP_REST_Server::CREATABLE,
+ 'callback' => array( $this, 'create_item' ),
+ 'permission_callback' => array( $this, 'create_item_permissions_check' ),
+ 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
+ ),
+ 'schema' => array( $this, 'get_public_item_schema' ),
+ )
+ );
+
+ $schema = $this->get_item_schema();
+ $get_item_args = array(
+ 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
+ );
+ if ( isset( $schema['properties']['password'] ) ) {
+ $get_item_args['password'] = array(
+ 'description' => __( 'The password for the post if it is password protected.' ),
+ 'type' => 'string',
+ );
+ }
+ register_rest_route(
+ $this->namespace,
+ '/' . $this->rest_base . '/(?P<id>[\d]+)',
+ array(
+ 'args' => array(
+ 'id' => array(
+ 'description' => __( 'Unique identifier for the object.' ),
+ 'type' => 'integer',
+ ),
+ ),
+ array(
+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'get_item' ),
+ 'permission_callback' => array( $this, 'get_item_permissions_check' ),
+ 'args' => $get_item_args,
+ ),
+ array(
+ 'methods' => WP_REST_Server::EDITABLE,
+ 'callback' => array( $this, 'update_item' ),
+ 'permission_callback' => array( $this, 'update_item_permissions_check' ),
+ 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
+ ),
+ array(
+ 'methods' => WP_REST_Server::DELETABLE,
+ 'callback' => array( $this, 'delete_item' ),
+ 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
+ 'args' => array(
+ 'force' => array(
+ 'type' => 'boolean',
+ 'default' => false,
+ 'description' => __( 'Whether to bypass trash and force deletion.' ),
+ ),
+ ),
+ ),
+ 'schema' => array( $this, 'get_public_item_schema' ),
+ )
+ );
+ }
+
+ /**
+ * Checks if a given request has access to read posts.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
+ */
+ public function get_items_permissions_check( $request ) {
+
+ $post_type = get_post_type_object( $this->post_type );
+
+ if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) {
+ return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit posts in this post type.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ return true;
+ }
+
+ /**
+ * Retrieves a collection of posts.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
+ */
+ public function get_items( $request ) {
+
+ // Ensure a search string is set in case the orderby is set to 'relevance'.
+ if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
+ return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.' ), array( 'status' => 400 ) );
+ }
+
+ // Ensure an include parameter is set in case the orderby is set to 'include'.
+ if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
+ return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.' ), array( 'status' => 400 ) );
+ }
+
+ // Retrieve the list of registered collection query parameters.
+ $registered = $this->get_collection_params();
+ $args = array();
+
+ /*
+ * This array defines mappings between public API query parameters whose
+ * values are accepted as-passed, and their internal WP_Query parameter
+ * name equivalents (some are the same). Only values which are also
+ * present in $registered will be set.
+ */
+ $parameter_mappings = array(
+ 'author' => 'author__in',
+ 'author_exclude' => 'author__not_in',
+ 'exclude' => 'post__not_in',
+ 'include' => 'post__in',
+ 'menu_order' => 'menu_order',
+ 'offset' => 'offset',
+ 'order' => 'order',
+ 'orderby' => 'orderby',
+ 'page' => 'paged',
+ 'parent' => 'post_parent__in',
+ 'parent_exclude' => 'post_parent__not_in',
+ 'search' => 's',
+ 'slug' => 'post_name__in',
+ 'status' => 'post_status',
+ );
+
+ /*
+ * For each known parameter which is both registered and present in the request,
+ * set the parameter's value on the query $args.
+ */
+ foreach ( $parameter_mappings as $api_param => $wp_param ) {
+ if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
+ $args[ $wp_param ] = $request[ $api_param ];
+ }
+ }
+
+ // Check for & assign any parameters which require special handling or setting.
+ $args['date_query'] = array();
+
+ // Set before into date query. Date query must be specified as an array of an array.
+ if ( isset( $registered['before'], $request['before'] ) ) {
+ $args['date_query'][0]['before'] = $request['before'];
+ }
+
+ // Set after into date query. Date query must be specified as an array of an array.
+ if ( isset( $registered['after'], $request['after'] ) ) {
+ $args['date_query'][0]['after'] = $request['after'];
+ }
+
+ // Ensure our per_page parameter overrides any provided posts_per_page filter.
+ if ( isset( $registered['per_page'] ) ) {
+ $args['posts_per_page'] = $request['per_page'];
+ }
+
+ if ( isset( $registered['sticky'], $request['sticky'] ) ) {
+ $sticky_posts = get_option( 'sticky_posts', array() );
+ if ( ! is_array( $sticky_posts ) ) {
+ $sticky_posts = array();
+ }
+ if ( $request['sticky'] ) {
+ /*
+ * As post__in will be used to only get sticky posts,
+ * we have to support the case where post__in was already
+ * specified.
+ */
+ $args['post__in'] = $args['post__in'] ? array_intersect( $sticky_posts, $args['post__in'] ) : $sticky_posts;
+
+ /*
+ * If we intersected, but there are no post ids in common,
+ * WP_Query won't return "no posts" for post__in = array()
+ * so we have to fake it a bit.
+ */
+ if ( ! $args['post__in'] ) {
+ $args['post__in'] = array( 0 );
+ }
+ } elseif ( $sticky_posts ) {
+ /*
+ * As post___not_in will be used to only get posts that
+ * are not sticky, we have to support the case where post__not_in
+ * was already specified.
+ */
+ $args['post__not_in'] = array_merge( $args['post__not_in'], $sticky_posts );
+ }
+ }
+
+ // Force the post_type argument, since it's not a user input variable.
+ $args['post_type'] = $this->post_type;
+
+ /**
+ * Filters the query arguments for a request.
+ *
+ * Enables adding extra arguments or setting defaults for a post collection request.
+ *
+ * @since 4.7.0
+ *
+ * @link https://developer.wordpress.org/reference/classes/wp_query/
+ *
+ * @param array $args Key value array of query var to query value.
+ * @param WP_REST_Request $request The request used.
+ */
+ $args = apply_filters( "rest_{$this->post_type}_query", $args, $request );
+ $query_args = $this->prepare_items_query( $args, $request );
+
+ $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
+
+ foreach ( $taxonomies as $taxonomy ) {
+ $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
+ $tax_exclude = $base . '_exclude';
+
+ if ( ! empty( $request[ $base ] ) ) {
+ $query_args['tax_query'][] = array(
+ 'taxonomy' => $taxonomy->name,
+ 'field' => 'term_id',
+ 'terms' => $request[ $base ],
+ 'include_children' => false,
+ );
+ }
+
+ if ( ! empty( $request[ $tax_exclude ] ) ) {
+ $query_args['tax_query'][] = array(
+ 'taxonomy' => $taxonomy->name,
+ 'field' => 'term_id',
+ 'terms' => $request[ $tax_exclude ],
+ 'include_children' => false,
+ 'operator' => 'NOT IN',
+ );
+ }
+ }
+
+ $posts_query = new WP_Query();
+ $query_result = $posts_query->query( $query_args );
+
+ // Allow access to all password protected posts if the context is edit.
+ if ( 'edit' === $request['context'] ) {
+ add_filter( 'post_password_required', '__return_false' );
+ }
+
+ $posts = array();
+
+ foreach ( $query_result as $post ) {
+ if ( ! $this->check_read_permission( $post ) ) {
+ continue;
+ }
+
+ $data = $this->prepare_item_for_response( $post, $request );
+ $posts[] = $this->prepare_response_for_collection( $data );
+ }
+
+ // Reset filter.
+ if ( 'edit' === $request['context'] ) {
+ remove_filter( 'post_password_required', '__return_false' );
+ }
+
+ $page = (int) $query_args['paged'];
+ $total_posts = $posts_query->found_posts;
+
+ if ( $total_posts < 1 ) {
+ // Out-of-bounds, run the query again without LIMIT for total count.
+ unset( $query_args['paged'] );
+
+ $count_query = new WP_Query();
+ $count_query->query( $query_args );
+ $total_posts = $count_query->found_posts;
+ }
+
+ $max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] );
+
+ if ( $page > $max_pages && $total_posts > 0 ) {
+ return new WP_Error( 'rest_post_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) );
+ }
+
+ $response = rest_ensure_response( $posts );
+
+ $response->header( 'X-WP-Total', (int) $total_posts );
+ $response->header( 'X-WP-TotalPages', (int) $max_pages );
+
+ $request_params = $request->get_query_params();
+ $base = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
+
+ if ( $page > 1 ) {
+ $prev_page = $page - 1;
+
+ if ( $prev_page > $max_pages ) {
+ $prev_page = $max_pages;
+ }
+
+ $prev_link = add_query_arg( 'page', $prev_page, $base );
+ $response->link_header( 'prev', $prev_link );
+ }
+ if ( $max_pages > $page ) {
+ $next_page = $page + 1;
+ $next_link = add_query_arg( 'page', $next_page, $base );
+
+ $response->link_header( 'next', $next_link );
+ }
+
+ return $response;
+ }
+
+ /**
+ * Get the post, if the ID is valid.
+ *
+ * @since 4.7.2
+ *
+ * @param int $id Supplied ID.
+ * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
+ */
+ protected function get_post( $id ) {
+ $error = new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
+ if ( (int) $id <= 0 ) {
+ return $error;
+ }
+
+ $post = get_post( (int) $id );
+ if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
+ return $error;
+ }
+
+ return $post;
+ }
+
+ /**
+ * Checks if a given request has access to read a post.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return bool|WP_Error True if the request has read access for the item, WP_Error object otherwise.
+ */
+ public function get_item_permissions_check( $request ) {
+ $post = $this->get_post( $request['id'] );
+ if ( is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) {
+ return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit this post.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ if ( $post && ! empty( $request['password'] ) ) {
+ // Check post password, and return error if invalid.
+ if ( ! hash_equals( $post->post_password, $request['password'] ) ) {
+ return new WP_Error( 'rest_post_incorrect_password', __( 'Incorrect post password.' ), array( 'status' => 403 ) );
+ }
+ }
+
+ // Allow access to all password protected posts if the context is edit.
+ if ( 'edit' === $request['context'] ) {
+ add_filter( 'post_password_required', '__return_false' );
+ }
+
+ if ( $post ) {
+ return $this->check_read_permission( $post );
+ }
+
+ return true;
+ }
+
+ /**
+ * Checks if the user can access password-protected content.
+ *
+ * This method determines whether we need to override the regular password
+ * check in core with a filter.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_Post $post Post to check against.
+ * @param WP_REST_Request $request Request data to check.
+ * @return bool True if the user can access password-protected content, otherwise false.
+ */
+ public function can_access_password_content( $post, $request ) {
+ if ( empty( $post->post_password ) ) {
+ // No filter required.
+ return false;
+ }
+
+ // Edit context always gets access to password-protected posts.
+ if ( 'edit' === $request['context'] ) {
+ return true;
+ }
+
+ // No password, no auth.
+ if ( empty( $request['password'] ) ) {
+ return false;
+ }
+
+ // Double-check the request password.
+ return hash_equals( $post->post_password, $request['password'] );
+ }
+
+ /**
+ * Retrieves a single post.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
+ */
+ public function get_item( $request ) {
+ $post = $this->get_post( $request['id'] );
+ if ( is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ $data = $this->prepare_item_for_response( $post, $request );
+ $response = rest_ensure_response( $data );
+
+ if ( is_post_type_viewable( get_post_type_object( $post->post_type ) ) ) {
+ $response->link_header( 'alternate', get_permalink( $post->ID ), array( 'type' => 'text/html' ) );
+ }
+
+ return $response;
+ }
+
+ /**
+ * Checks if a given request has access to create a post.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
+ */
+ public function create_item_permissions_check( $request ) {
+ if ( ! empty( $request['id'] ) ) {
+ return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
+ }
+
+ $post_type = get_post_type_object( $this->post_type );
+
+ if ( ! empty( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( $post_type->cap->edit_others_posts ) ) {
+ return new WP_Error( 'rest_cannot_edit_others', __( 'Sorry, you are not allowed to create posts as this user.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ if ( ! empty( $request['sticky'] ) && ! current_user_can( $post_type->cap->edit_others_posts ) && ! current_user_can( $post_type->cap->publish_posts ) ) {
+ return new WP_Error( 'rest_cannot_assign_sticky', __( 'Sorry, you are not allowed to make posts sticky.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ if ( ! current_user_can( $post_type->cap->create_posts ) ) {
+ return new WP_Error( 'rest_cannot_create', __( 'Sorry, you are not allowed to create posts as this user.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ if ( ! $this->check_assign_terms_permission( $request ) ) {
+ return new WP_Error( 'rest_cannot_assign_term', __( 'Sorry, you are not allowed to assign the provided terms.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ return true;
+ }
+
+ /**
+ * Creates a single post.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
+ */
+ public function create_item( $request ) {
+ if ( ! empty( $request['id'] ) ) {
+ return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
+ }
+
+ $prepared_post = $this->prepare_item_for_database( $request );
+
+ if ( is_wp_error( $prepared_post ) ) {
+ return $prepared_post;
+ }
+
+ $prepared_post->post_type = $this->post_type;
+
+ $post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true );
+
+ if ( is_wp_error( $post_id ) ) {
+
+ if ( 'db_insert_error' === $post_id->get_error_code() ) {
+ $post_id->add_data( array( 'status' => 500 ) );
+ } else {
+ $post_id->add_data( array( 'status' => 400 ) );
+ }
+
+ return $post_id;
+ }
+
+ $post = get_post( $post_id );
+
+ /**
+ * Fires after a single post is created or updated via the REST API.
+ *
+ * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_Post $post Inserted or updated post object.
+ * @param WP_REST_Request $request Request object.
+ * @param bool $creating True when creating a post, false when updating.
+ */
+ do_action( "rest_insert_{$this->post_type}", $post, $request, true );
+
+ $schema = $this->get_item_schema();
+
+ if ( ! empty( $schema['properties']['sticky'] ) ) {
+ if ( ! empty( $request['sticky'] ) ) {
+ stick_post( $post_id );
+ } else {
+ unstick_post( $post_id );
+ }
+ }
+
+ if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
+ $this->handle_featured_media( $request['featured_media'], $post_id );
+ }
+
+ if ( ! empty( $schema['properties']['format'] ) && ! empty( $request['format'] ) ) {
+ set_post_format( $post, $request['format'] );
+ }
+
+ if ( ! empty( $schema['properties']['template'] ) && isset( $request['template'] ) ) {
+ $this->handle_template( $request['template'], $post_id, true );
+ }
+
+ $terms_update = $this->handle_terms( $post_id, $request );
+
+ if ( is_wp_error( $terms_update ) ) {
+ return $terms_update;
+ }
+
+ if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
+ $meta_update = $this->meta->update_value( $request['meta'], $post_id );
+
+ if ( is_wp_error( $meta_update ) ) {
+ return $meta_update;
+ }
+ }
+
+ $post = get_post( $post_id );
+ $fields_update = $this->update_additional_fields_for_object( $post, $request );
+
+ if ( is_wp_error( $fields_update ) ) {
+ return $fields_update;
+ }
+
+ $request->set_param( 'context', 'edit' );
+
+ /**
+ * Fires after a single post is completely created or updated via the REST API.
+ *
+ * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
+ *
+ * @since 5.0.0
+ *
+ * @param WP_Post $post Inserted or updated post object.
+ * @param WP_REST_Request $request Request object.
+ * @param bool $creating True when creating a post, false when updating.
+ */
+ do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );
+
+ $response = $this->prepare_item_for_response( $post, $request );
+ $response = rest_ensure_response( $response );
+
+ $response->set_status( 201 );
+ $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $post_id ) ) );
+
+ return $response;
+ }
+
+ /**
+ * Checks if a given request has access to update a post.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
+ */
+ public function update_item_permissions_check( $request ) {
+ $post = $this->get_post( $request['id'] );
+ if ( is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ $post_type = get_post_type_object( $this->post_type );
+
+ if ( $post && ! $this->check_update_permission( $post ) ) {
+ return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this post.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ if ( ! empty( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( $post_type->cap->edit_others_posts ) ) {
+ return new WP_Error( 'rest_cannot_edit_others', __( 'Sorry, you are not allowed to update posts as this user.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ if ( ! empty( $request['sticky'] ) && ! current_user_can( $post_type->cap->edit_others_posts ) && ! current_user_can( $post_type->cap->publish_posts ) ) {
+ return new WP_Error( 'rest_cannot_assign_sticky', __( 'Sorry, you are not allowed to make posts sticky.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ if ( ! $this->check_assign_terms_permission( $request ) ) {
+ return new WP_Error( 'rest_cannot_assign_term', __( 'Sorry, you are not allowed to assign the provided terms.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ return true;
+ }
+
+ /**
+ * Updates a single post.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
+ */
+ public function update_item( $request ) {
+ $valid_check = $this->get_post( $request['id'] );
+ if ( is_wp_error( $valid_check ) ) {
+ return $valid_check;
+ }
+
+ $post = $this->prepare_item_for_database( $request );
+
+ if ( is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ // convert the post object to an array, otherwise wp_update_post will expect non-escaped input.
+ $post_id = wp_update_post( wp_slash( (array) $post ), true );
+
+ if ( is_wp_error( $post_id ) ) {
+ if ( 'db_update_error' === $post_id->get_error_code() ) {
+ $post_id->add_data( array( 'status' => 500 ) );
+ } else {
+ $post_id->add_data( array( 'status' => 400 ) );
+ }
+ return $post_id;
+ }
+
+ $post = get_post( $post_id );
+
+ /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
+ do_action( "rest_insert_{$this->post_type}", $post, $request, false );
+
+ $schema = $this->get_item_schema();
+
+ if ( ! empty( $schema['properties']['format'] ) && ! empty( $request['format'] ) ) {
+ set_post_format( $post, $request['format'] );
+ }
+
+ if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
+ $this->handle_featured_media( $request['featured_media'], $post_id );
+ }
+
+ if ( ! empty( $schema['properties']['sticky'] ) && isset( $request['sticky'] ) ) {
+ if ( ! empty( $request['sticky'] ) ) {
+ stick_post( $post_id );
+ } else {
+ unstick_post( $post_id );
+ }
+ }
+
+ if ( ! empty( $schema['properties']['template'] ) && isset( $request['template'] ) ) {
+ $this->handle_template( $request['template'], $post->ID );
+ }
+
+ $terms_update = $this->handle_terms( $post->ID, $request );
+
+ if ( is_wp_error( $terms_update ) ) {
+ return $terms_update;
+ }
+
+ if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
+ $meta_update = $this->meta->update_value( $request['meta'], $post->ID );
+
+ if ( is_wp_error( $meta_update ) ) {
+ return $meta_update;
+ }
+ }
+
+ $post = get_post( $post_id );
+ $fields_update = $this->update_additional_fields_for_object( $post, $request );
+
+ if ( is_wp_error( $fields_update ) ) {
+ return $fields_update;
+ }
+
+ $request->set_param( 'context', 'edit' );
+
+ // Filter is fired in WP_REST_Attachments_Controller subclass.
+ if ( 'attachment' === $this->post_type ) {
+ $response = $this->prepare_item_for_response( $post, $request );
+ return rest_ensure_response( $response );
+ }
+
+ /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
+ do_action( "rest_after_insert_{$this->post_type}", $post, $request, false );
+
+ $response = $this->prepare_item_for_response( $post, $request );
+
+ return rest_ensure_response( $response );
+ }
+
+ /**
+ * Checks if a given request has access to delete a post.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
+ */
+ public function delete_item_permissions_check( $request ) {
+ $post = $this->get_post( $request['id'] );
+ if ( is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ if ( $post && ! $this->check_delete_permission( $post ) ) {
+ return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete this post.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ return true;
+ }
+
+ /**
+ * Deletes a single post.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
+ */
+ public function delete_item( $request ) {
+ $post = $this->get_post( $request['id'] );
+ if ( is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ $id = $post->ID;
+ $force = (bool) $request['force'];
+
+ $supports_trash = ( EMPTY_TRASH_DAYS > 0 );
+
+ if ( 'attachment' === $post->post_type ) {
+ $supports_trash = $supports_trash && MEDIA_TRASH;
+ }
+
+ /**
+ * Filters whether a post is trashable.
+ *
+ * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
+ *
+ * Pass false to disable trash support for the post.
+ *
+ * @since 4.7.0
+ *
+ * @param bool $supports_trash Whether the post type support trashing.
+ * @param WP_Post $post The Post object being considered for trashing support.
+ */
+ $supports_trash = apply_filters( "rest_{$this->post_type}_trashable", $supports_trash, $post );
+
+ if ( ! $this->check_delete_permission( $post ) ) {
+ return new WP_Error( 'rest_user_cannot_delete_post', __( 'Sorry, you are not allowed to delete this post.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
+
+ $request->set_param( 'context', 'edit' );
+
+ // If we're forcing, then delete permanently.
+ if ( $force ) {
+ $previous = $this->prepare_item_for_response( $post, $request );
+ $result = wp_delete_post( $id, true );
+ $response = new WP_REST_Response();
+ $response->set_data(
+ array(
+ 'deleted' => true,
+ 'previous' => $previous->get_data(),
+ )
+ );
+ } else {
+ // If we don't support trashing for this type, error out.
+ if ( ! $supports_trash ) {
+ /* translators: %s: force=true */
+ return new WP_Error( 'rest_trash_not_supported', sprintf( __( "The post does not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
+ }
+
+ // Otherwise, only trash if we haven't already.
+ if ( 'trash' === $post->post_status ) {
+ return new WP_Error( 'rest_already_trashed', __( 'The post has already been deleted.' ), array( 'status' => 410 ) );
+ }
+
+ // (Note that internally this falls through to `wp_delete_post` if
+ // the trash is disabled.)
+ $result = wp_trash_post( $id );
+ $post = get_post( $id );
+ $response = $this->prepare_item_for_response( $post, $request );
+ }
+
+ if ( ! $result ) {
+ return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
+ }
+
+ /**
+ * Fires immediately after a single post is deleted or trashed via the REST API.
+ *
+ * They dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
+ *
+ * @since 4.7.0
+ *
+ * @param object $post The deleted or trashed post.
+ * @param WP_REST_Response $response The response data.
+ * @param WP_REST_Request $request The request sent to the API.
+ */
+ do_action( "rest_delete_{$this->post_type}", $post, $response, $request );
+
+ return $response;
+ }
+
+ /**
+ * Determines the allowed query_vars for a get_items() response and prepares
+ * them for WP_Query.
+ *
+ * @since 4.7.0
+ *
+ * @param array $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
+ * @param WP_REST_Request $request Optional. Full details about the request.
+ * @return array Items query arguments.
+ */
+ protected function prepare_items_query( $prepared_args = array(), $request = null ) {
+ $query_args = array();
+
+ foreach ( $prepared_args as $key => $value ) {
+ /**
+ * Filters the query_vars used in get_items() for the constructed query.
+ *
+ * The dynamic portion of the hook name, `$key`, refers to the query_var key.
+ *
+ * @since 4.7.0
+ *
+ * @param string $value The query_var value.
+ */
+ $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
+ }
+
+ if ( 'post' !== $this->post_type || ! isset( $query_args['ignore_sticky_posts'] ) ) {
+ $query_args['ignore_sticky_posts'] = true;
+ }
+
+ // Map to proper WP_Query orderby param.
+ if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
+ $orderby_mappings = array(
+ 'id' => 'ID',
+ 'include' => 'post__in',
+ 'slug' => 'post_name',
+ 'include_slugs' => 'post_name__in',
+ );
+
+ if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
+ $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
+ }
+ }
+
+ return $query_args;
+ }
+
+ /**
+ * Checks the post_date_gmt or modified_gmt and prepare any post or
+ * modified date for single post output.
+ *
+ * @since 4.7.0
+ *
+ * @param string $date_gmt GMT publication time.
+ * @param string|null $date Optional. Local publication time. Default null.
+ * @return string|null ISO8601/RFC3339 formatted datetime.
+ */
+ protected function prepare_date_response( $date_gmt, $date = null ) {
+ // Use the date if passed.
+ if ( isset( $date ) ) {
+ return mysql_to_rfc3339( $date );
+ }
+
+ // Return null if $date_gmt is empty/zeros.
+ if ( '0000-00-00 00:00:00' === $date_gmt ) {
+ return null;
+ }
+
+ // Return the formatted datetime.
+ return mysql_to_rfc3339( $date_gmt );
+ }
+
+ /**
+ * Prepares a single post for create or update.
+ *
+ * @since 4.7.0
+ *
+ * @param WP_REST_Request $request Request object.
+ * @return stdClass|WP_Error Post object or WP_Error.
+ */
+ protected function prepare_item_for_database( $request ) {
+ $prepared_post = new stdClass();
+
+ // Post ID.
+ if ( isset( $request['id'] ) ) {
+ $existing_post = $this->get_post( $request['id'] );
+ if ( is_wp_error( $existing_post ) ) {
+ return $existing_post;
+ }
+
+ $prepared_post->ID = $existing_post->ID;
+ }
+
+ $schema = $this->get_item_schema();
+
+ // Post title.
+ if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
+ if ( is_string( $request['title'] ) ) {
+ $prepared_post->post_title = $request['title'];
+ } elseif ( ! empty( $request['title']['raw'] ) ) {
+ $prepared_post->post_title = $request['title']['raw'];
+ }
+ }
+
+ // Post content.
+ if ( ! empty( $schema['properties']['content'] ) && isset( $request['content'] ) ) {
+ if ( is_string( $request['content'] ) ) {
+ $prepared_post->post_content = $request['content'];
+ } elseif ( isset( $request['content']['raw'] ) ) {
+ $prepared_post->post_content = $request['content']['raw'];
+ }
+ }
+
+ // Post excerpt.
+ if ( ! empty( $schema['properties']['excerpt'] ) && isset( $request['excerpt'] ) ) {
+ if ( is_string( $request['excerpt'] ) ) {
+ $prepared_post->post_excerpt = $request['excerpt'];
+ } elseif ( isset( $request['excerpt']['raw'] ) ) {
+ $prepared_post->post_excerpt = $request['excerpt']['raw'];
+ }
+ }
+
+ // Post type.
+ if ( empty( $request['id'] ) ) {
+ // Creating new post, use default type for the controller.
+ $prepared_post->post_type = $this->post_type;
+ } else {
+ // Updating a post, use previous type.
+ $prepared_post->post_type = get_post_type( $request['id'] );
+ }
+
+ $post_type = get_post_type_object( $prepared_post->post_type );
+
+ // Post status.
+ if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) {
+ $status = $this->handle_status_param( $request['status'], $post_type );
+
+ if ( is_wp_error( $status ) ) {
+ return $status;
+ }
+
+ $prepared_post->post_status = $status;
+ }
+
+ // Post date.
+ if ( ! empty( $schema['properties']['date'] ) && ! empty( $request['date'] ) ) {
+ $current_date = isset( $prepared_post->ID ) ? get_post( $prepared_post->ID )->post_date : false