wp/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 <?php
       
     2 /**
       
     3  * REST API: WP_REST_Posts_Controller class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage REST_API
       
     7  * @since 4.7.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core class to access posts via the REST API.
       
    12  *
       
    13  * @since 4.7.0
       
    14  *
       
    15  * @see WP_REST_Controller
       
    16  */
       
    17 class WP_REST_Posts_Controller extends WP_REST_Controller {
       
    18 
       
    19 	/**
       
    20 	 * Post type.
       
    21 	 *
       
    22 	 * @since 4.7.0
       
    23 	 * @var string
       
    24 	 */
       
    25 	protected $post_type;
       
    26 
       
    27 	/**
       
    28 	 * Instance of a post meta fields object.
       
    29 	 *
       
    30 	 * @since 4.7.0
       
    31 	 * @var WP_REST_Post_Meta_Fields
       
    32 	 */
       
    33 	protected $meta;
       
    34 
       
    35 	/**
       
    36 	 * Constructor.
       
    37 	 *
       
    38 	 * @since 4.7.0
       
    39 	 *
       
    40 	 * @param string $post_type Post type.
       
    41 	 */
       
    42 	public function __construct( $post_type ) {
       
    43 		$this->post_type = $post_type;
       
    44 		$this->namespace = 'wp/v2';
       
    45 		$obj = get_post_type_object( $post_type );
       
    46 		$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
       
    47 
       
    48 		$this->meta = new WP_REST_Post_Meta_Fields( $this->post_type );
       
    49 	}
       
    50 
       
    51 	/**
       
    52 	 * Registers the routes for the objects of the controller.
       
    53 	 *
       
    54 	 * @since 4.7.0
       
    55 	 *
       
    56 	 * @see register_rest_route()
       
    57 	 */
       
    58 	public function register_routes() {
       
    59 
       
    60 		register_rest_route( $this->namespace, '/' . $this->rest_base, array(
       
    61 			array(
       
    62 				'methods'             => WP_REST_Server::READABLE,
       
    63 				'callback'            => array( $this, 'get_items' ),
       
    64 				'permission_callback' => array( $this, 'get_items_permissions_check' ),
       
    65 				'args'                => $this->get_collection_params(),
       
    66 			),
       
    67 			array(
       
    68 				'methods'             => WP_REST_Server::CREATABLE,
       
    69 				'callback'            => array( $this, 'create_item' ),
       
    70 				'permission_callback' => array( $this, 'create_item_permissions_check' ),
       
    71 				'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
       
    72 			),
       
    73 			'schema' => array( $this, 'get_public_item_schema' ),
       
    74 		) );
       
    75 
       
    76 		$schema = $this->get_item_schema();
       
    77 		$get_item_args = array(
       
    78 			'context'  => $this->get_context_param( array( 'default' => 'view' ) ),
       
    79 		);
       
    80 		if ( isset( $schema['properties']['password'] ) ) {
       
    81 			$get_item_args['password'] = array(
       
    82 				'description' => __( 'The password for the post if it is password protected.' ),
       
    83 				'type'        => 'string',
       
    84 			);
       
    85 		}
       
    86 		register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
       
    87 			'args' => array(
       
    88 				'id' => array(
       
    89 					'description' => __( 'Unique identifier for the object.' ),
       
    90 					'type'        => 'integer',
       
    91 				),
       
    92 			),
       
    93 			array(
       
    94 				'methods'             => WP_REST_Server::READABLE,
       
    95 				'callback'            => array( $this, 'get_item' ),
       
    96 				'permission_callback' => array( $this, 'get_item_permissions_check' ),
       
    97 				'args'                => $get_item_args,
       
    98 			),
       
    99 			array(
       
   100 				'methods'             => WP_REST_Server::EDITABLE,
       
   101 				'callback'            => array( $this, 'update_item' ),
       
   102 				'permission_callback' => array( $this, 'update_item_permissions_check' ),
       
   103 				'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
       
   104 			),
       
   105 			array(
       
   106 				'methods'             => WP_REST_Server::DELETABLE,
       
   107 				'callback'            => array( $this, 'delete_item' ),
       
   108 				'permission_callback' => array( $this, 'delete_item_permissions_check' ),
       
   109 				'args'                => array(
       
   110 					'force' => array(
       
   111 						'type'        => 'boolean',
       
   112 						'default'     => false,
       
   113 						'description' => __( 'Whether to bypass trash and force deletion.' ),
       
   114 					),
       
   115 				),
       
   116 			),
       
   117 			'schema' => array( $this, 'get_public_item_schema' ),
       
   118 		) );
       
   119 	}
       
   120 
       
   121 	/**
       
   122 	 * Checks if a given request has access to read posts.
       
   123 	 *
       
   124 	 * @since 4.7.0
       
   125 	 *
       
   126 	 * @param  WP_REST_Request $request Full details about the request.
       
   127 	 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
       
   128 	 */
       
   129 	public function get_items_permissions_check( $request ) {
       
   130 
       
   131 		$post_type = get_post_type_object( $this->post_type );
       
   132 
       
   133 		if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) {
       
   134 			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() ) );
       
   135 		}
       
   136 
       
   137 		return true;
       
   138 	}
       
   139 
       
   140 	/**
       
   141 	 * Retrieves a collection of posts.
       
   142 	 *
       
   143 	 * @since 4.7.0
       
   144 	 *
       
   145 	 * @param WP_REST_Request $request Full details about the request.
       
   146 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
       
   147 	 */
       
   148 	public function get_items( $request ) {
       
   149 
       
   150 		// Ensure a search string is set in case the orderby is set to 'relevance'.
       
   151 		if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
       
   152 			return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.' ), array( 'status' => 400 ) );
       
   153 		}
       
   154 
       
   155 		// Ensure an include parameter is set in case the orderby is set to 'include'.
       
   156 		if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
       
   157 			return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.' ), array( 'status' => 400 ) );
       
   158 		}
       
   159 
       
   160 		// Retrieve the list of registered collection query parameters.
       
   161 		$registered = $this->get_collection_params();
       
   162 		$args = array();
       
   163 
       
   164 		/*
       
   165 		 * This array defines mappings between public API query parameters whose
       
   166 		 * values are accepted as-passed, and their internal WP_Query parameter
       
   167 		 * name equivalents (some are the same). Only values which are also
       
   168 		 * present in $registered will be set.
       
   169 		 */
       
   170 		$parameter_mappings = array(
       
   171 			'author'         => 'author__in',
       
   172 			'author_exclude' => 'author__not_in',
       
   173 			'exclude'        => 'post__not_in',
       
   174 			'include'        => 'post__in',
       
   175 			'menu_order'     => 'menu_order',
       
   176 			'offset'         => 'offset',
       
   177 			'order'          => 'order',
       
   178 			'orderby'        => 'orderby',
       
   179 			'page'           => 'paged',
       
   180 			'parent'         => 'post_parent__in',
       
   181 			'parent_exclude' => 'post_parent__not_in',
       
   182 			'search'         => 's',
       
   183 			'slug'           => 'post_name__in',
       
   184 			'status'         => 'post_status',
       
   185 		);
       
   186 
       
   187 		/*
       
   188 		 * For each known parameter which is both registered and present in the request,
       
   189 		 * set the parameter's value on the query $args.
       
   190 		 */
       
   191 		foreach ( $parameter_mappings as $api_param => $wp_param ) {
       
   192 			if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
       
   193 				$args[ $wp_param ] = $request[ $api_param ];
       
   194 			}
       
   195 		}
       
   196 
       
   197 		// Check for & assign any parameters which require special handling or setting.
       
   198 		$args['date_query'] = array();
       
   199 
       
   200 		// Set before into date query. Date query must be specified as an array of an array.
       
   201 		if ( isset( $registered['before'], $request['before'] ) ) {
       
   202 			$args['date_query'][0]['before'] = $request['before'];
       
   203 		}
       
   204 
       
   205 		// Set after into date query. Date query must be specified as an array of an array.
       
   206 		if ( isset( $registered['after'], $request['after'] ) ) {
       
   207 			$args['date_query'][0]['after'] = $request['after'];
       
   208 		}
       
   209 
       
   210 		// Ensure our per_page parameter overrides any provided posts_per_page filter.
       
   211 		if ( isset( $registered['per_page'] ) ) {
       
   212 			$args['posts_per_page'] = $request['per_page'];
       
   213 		}
       
   214 
       
   215 		if ( isset( $registered['sticky'], $request['sticky'] ) ) {
       
   216 			$sticky_posts = get_option( 'sticky_posts', array() );
       
   217 			if ( ! is_array( $sticky_posts ) ) {
       
   218 				$sticky_posts = array();
       
   219 			}
       
   220 			if ( $request['sticky'] ) {
       
   221 				/*
       
   222 				 * As post__in will be used to only get sticky posts,
       
   223 				 * we have to support the case where post__in was already
       
   224 				 * specified.
       
   225 				 */
       
   226 				$args['post__in'] = $args['post__in'] ? array_intersect( $sticky_posts, $args['post__in'] ) : $sticky_posts;
       
   227 
       
   228 				/*
       
   229 				 * If we intersected, but there are no post ids in common,
       
   230 				 * WP_Query won't return "no posts" for post__in = array()
       
   231 				 * so we have to fake it a bit.
       
   232 				 */
       
   233 				if ( ! $args['post__in'] ) {
       
   234 					$args['post__in'] = array( 0 );
       
   235 				}
       
   236 			} elseif ( $sticky_posts ) {
       
   237 				/*
       
   238 				 * As post___not_in will be used to only get posts that
       
   239 				 * are not sticky, we have to support the case where post__not_in
       
   240 				 * was already specified.
       
   241 				 */
       
   242 				$args['post__not_in'] = array_merge( $args['post__not_in'], $sticky_posts );
       
   243 			}
       
   244 		}
       
   245 
       
   246 		// Force the post_type argument, since it's not a user input variable.
       
   247 		$args['post_type'] = $this->post_type;
       
   248 
       
   249 		/**
       
   250 		 * Filters the query arguments for a request.
       
   251 		 *
       
   252 		 * Enables adding extra arguments or setting defaults for a post collection request.
       
   253 		 *
       
   254 		 * @since 4.7.0
       
   255 		 *
       
   256 		 * @link https://developer.wordpress.org/reference/classes/wp_query/
       
   257 		 *
       
   258 		 * @param array           $args    Key value array of query var to query value.
       
   259 		 * @param WP_REST_Request $request The request used.
       
   260 		 */
       
   261 		$args = apply_filters( "rest_{$this->post_type}_query", $args, $request );
       
   262 		$query_args = $this->prepare_items_query( $args, $request );
       
   263 
       
   264 		$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
       
   265 
       
   266 		foreach ( $taxonomies as $taxonomy ) {
       
   267 			$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
       
   268 			$tax_exclude = $base . '_exclude';
       
   269 
       
   270 			if ( ! empty( $request[ $base ] ) ) {
       
   271 				$query_args['tax_query'][] = array(
       
   272 					'taxonomy'         => $taxonomy->name,
       
   273 					'field'            => 'term_id',
       
   274 					'terms'            => $request[ $base ],
       
   275 					'include_children' => false,
       
   276 				);
       
   277 			}
       
   278 
       
   279 			if ( ! empty( $request[ $tax_exclude ] ) ) {
       
   280 				$query_args['tax_query'][] = array(
       
   281 					'taxonomy'         => $taxonomy->name,
       
   282 					'field'            => 'term_id',
       
   283 					'terms'            => $request[ $tax_exclude ],
       
   284 					'include_children' => false,
       
   285 					'operator'         => 'NOT IN',
       
   286 				);
       
   287 			}
       
   288 		}
       
   289 
       
   290 		$posts_query  = new WP_Query();
       
   291 		$query_result = $posts_query->query( $query_args );
       
   292 
       
   293 		// Allow access to all password protected posts if the context is edit.
       
   294 		if ( 'edit' === $request['context'] ) {
       
   295 			add_filter( 'post_password_required', '__return_false' );
       
   296 		}
       
   297 
       
   298 		$posts = array();
       
   299 
       
   300 		foreach ( $query_result as $post ) {
       
   301 			if ( ! $this->check_read_permission( $post ) ) {
       
   302 				continue;
       
   303 			}
       
   304 
       
   305 			$data    = $this->prepare_item_for_response( $post, $request );
       
   306 			$posts[] = $this->prepare_response_for_collection( $data );
       
   307 		}
       
   308 
       
   309 		// Reset filter.
       
   310 		if ( 'edit' === $request['context'] ) {
       
   311 			remove_filter( 'post_password_required', '__return_false' );
       
   312 		}
       
   313 
       
   314 		$page = (int) $query_args['paged'];
       
   315 		$total_posts = $posts_query->found_posts;
       
   316 
       
   317 		if ( $total_posts < 1 ) {
       
   318 			// Out-of-bounds, run the query again without LIMIT for total count.
       
   319 			unset( $query_args['paged'] );
       
   320 
       
   321 			$count_query = new WP_Query();
       
   322 			$count_query->query( $query_args );
       
   323 			$total_posts = $count_query->found_posts;
       
   324 		}
       
   325 
       
   326 		$max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] );
       
   327 
       
   328 		if ( $page > $max_pages && $total_posts > 0 ) {
       
   329 			return new WP_Error( 'rest_post_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) );
       
   330 		}
       
   331 
       
   332 		$response  = rest_ensure_response( $posts );
       
   333 
       
   334 		$response->header( 'X-WP-Total', (int) $total_posts );
       
   335 		$response->header( 'X-WP-TotalPages', (int) $max_pages );
       
   336 
       
   337 		$request_params = $request->get_query_params();
       
   338 		$base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
       
   339 
       
   340 		if ( $page > 1 ) {
       
   341 			$prev_page = $page - 1;
       
   342 
       
   343 			if ( $prev_page > $max_pages ) {
       
   344 				$prev_page = $max_pages;
       
   345 			}
       
   346 
       
   347 			$prev_link = add_query_arg( 'page', $prev_page, $base );
       
   348 			$response->link_header( 'prev', $prev_link );
       
   349 		}
       
   350 		if ( $max_pages > $page ) {
       
   351 			$next_page = $page + 1;
       
   352 			$next_link = add_query_arg( 'page', $next_page, $base );
       
   353 
       
   354 			$response->link_header( 'next', $next_link );
       
   355 		}
       
   356 
       
   357 		return $response;
       
   358 	}
       
   359 
       
   360 	/**
       
   361 	 * Get the post, if the ID is valid.
       
   362 	 *
       
   363 	 * @since 4.7.2
       
   364 	 *
       
   365 	 * @param int $id Supplied ID.
       
   366 	 * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
       
   367 	 */
       
   368 	protected function get_post( $id ) {
       
   369 		$error = new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
       
   370 		if ( (int) $id <= 0 ) {
       
   371 			return $error;
       
   372 		}
       
   373 
       
   374 		$post = get_post( (int) $id );
       
   375 		if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
       
   376 			return $error;
       
   377 		}
       
   378 
       
   379 		return $post;
       
   380 	}
       
   381 
       
   382 	/**
       
   383 	 * Checks if a given request has access to read a post.
       
   384 	 *
       
   385 	 * @since 4.7.0
       
   386 	 *
       
   387 	 * @param WP_REST_Request $request Full details about the request.
       
   388 	 * @return bool|WP_Error True if the request has read access for the item, WP_Error object otherwise.
       
   389 	 */
       
   390 	public function get_item_permissions_check( $request ) {
       
   391 		$post = $this->get_post( $request['id'] );
       
   392 		if ( is_wp_error( $post ) ) {
       
   393 			return $post;
       
   394 		}
       
   395 
       
   396 		if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) {
       
   397 			return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit this post.' ), array( 'status' => rest_authorization_required_code() ) );
       
   398 		}
       
   399 
       
   400 		if ( $post && ! empty( $request['password'] ) ) {
       
   401 			// Check post password, and return error if invalid.
       
   402 			if ( ! hash_equals( $post->post_password, $request['password'] ) ) {
       
   403 				return new WP_Error( 'rest_post_incorrect_password', __( 'Incorrect post password.' ), array( 'status' => 403 ) );
       
   404 			}
       
   405 		}
       
   406 
       
   407 		// Allow access to all password protected posts if the context is edit.
       
   408 		if ( 'edit' === $request['context'] ) {
       
   409 			add_filter( 'post_password_required', '__return_false' );
       
   410 		}
       
   411 
       
   412 		if ( $post ) {
       
   413 			return $this->check_read_permission( $post );
       
   414 		}
       
   415 
       
   416 		return true;
       
   417 	}
       
   418 
       
   419 	/**
       
   420 	 * Checks if the user can access password-protected content.
       
   421 	 *
       
   422 	 * This method determines whether we need to override the regular password
       
   423 	 * check in core with a filter.
       
   424 	 *
       
   425 	 * @since 4.7.0
       
   426 	 *
       
   427 	 * @param WP_Post         $post    Post to check against.
       
   428 	 * @param WP_REST_Request $request Request data to check.
       
   429 	 * @return bool True if the user can access password-protected content, otherwise false.
       
   430 	 */
       
   431 	public function can_access_password_content( $post, $request ) {
       
   432 		if ( empty( $post->post_password ) ) {
       
   433 			// No filter required.
       
   434 			return false;
       
   435 		}
       
   436 
       
   437 		// Edit context always gets access to password-protected posts.
       
   438 		if ( 'edit' === $request['context'] ) {
       
   439 			return true;
       
   440 		}
       
   441 
       
   442 		// No password, no auth.
       
   443 		if ( empty( $request['password'] ) ) {
       
   444 			return false;
       
   445 		}
       
   446 
       
   447 		// Double-check the request password.
       
   448 		return hash_equals( $post->post_password, $request['password'] );
       
   449 	}
       
   450 
       
   451 	/**
       
   452 	 * Retrieves a single post.
       
   453 	 *
       
   454 	 * @since 4.7.0
       
   455 	 *
       
   456 	 * @param WP_REST_Request $request Full details about the request.
       
   457 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
       
   458 	 */
       
   459 	public function get_item( $request ) {
       
   460 		$post = $this->get_post( $request['id'] );
       
   461 		if ( is_wp_error( $post ) ) {
       
   462 			return $post;
       
   463 		}
       
   464 
       
   465 		$data     = $this->prepare_item_for_response( $post, $request );
       
   466 		$response = rest_ensure_response( $data );
       
   467 
       
   468 		if ( is_post_type_viewable( get_post_type_object( $post->post_type ) ) ) {
       
   469 			$response->link_header( 'alternate',  get_permalink( $post->ID ), array( 'type' => 'text/html' ) );
       
   470 		}
       
   471 
       
   472 		return $response;
       
   473 	}
       
   474 
       
   475 	/**
       
   476 	 * Checks if a given request has access to create a post.
       
   477 	 *
       
   478 	 * @since 4.7.0
       
   479 	 *
       
   480 	 * @param WP_REST_Request $request Full details about the request.
       
   481 	 * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
       
   482 	 */
       
   483 	public function create_item_permissions_check( $request ) {
       
   484 		if ( ! empty( $request['id'] ) ) {
       
   485 			return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
       
   486 		}
       
   487 
       
   488 		$post_type = get_post_type_object( $this->post_type );
       
   489 
       
   490 		if ( ! empty( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( $post_type->cap->edit_others_posts ) ) {
       
   491 			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() ) );
       
   492 		}
       
   493 
       
   494 		if ( ! empty( $request['sticky'] ) && ! current_user_can( $post_type->cap->edit_others_posts ) ) {
       
   495 			return new WP_Error( 'rest_cannot_assign_sticky', __( 'Sorry, you are not allowed to make posts sticky.' ), array( 'status' => rest_authorization_required_code() ) );
       
   496 		}
       
   497 
       
   498 		if ( ! current_user_can( $post_type->cap->create_posts ) ) {
       
   499 			return new WP_Error( 'rest_cannot_create', __( 'Sorry, you are not allowed to create posts as this user.' ), array( 'status' => rest_authorization_required_code() ) );
       
   500 		}
       
   501 
       
   502 		if ( ! $this->check_assign_terms_permission( $request ) ) {
       
   503 			return new WP_Error( 'rest_cannot_assign_term', __( 'Sorry, you are not allowed to assign the provided terms.' ), array( 'status' => rest_authorization_required_code() ) );
       
   504 		}
       
   505 
       
   506 		return true;
       
   507 	}
       
   508 
       
   509 	/**
       
   510 	 * Creates a single post.
       
   511 	 *
       
   512 	 * @since 4.7.0
       
   513 	 *
       
   514 	 * @param WP_REST_Request $request Full details about the request.
       
   515 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
       
   516 	 */
       
   517 	public function create_item( $request ) {
       
   518 		if ( ! empty( $request['id'] ) ) {
       
   519 			return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
       
   520 		}
       
   521 
       
   522 		$prepared_post = $this->prepare_item_for_database( $request );
       
   523 
       
   524 		if ( is_wp_error( $prepared_post ) ) {
       
   525 			return $prepared_post;
       
   526 		}
       
   527 
       
   528 		$prepared_post->post_type = $this->post_type;
       
   529 
       
   530 		$post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true );
       
   531 
       
   532 		if ( is_wp_error( $post_id ) ) {
       
   533 
       
   534 			if ( 'db_insert_error' === $post_id->get_error_code() ) {
       
   535 				$post_id->add_data( array( 'status' => 500 ) );
       
   536 			} else {
       
   537 				$post_id->add_data( array( 'status' => 400 ) );
       
   538 			}
       
   539 
       
   540 			return $post_id;
       
   541 		}
       
   542 
       
   543 		$post = get_post( $post_id );
       
   544 
       
   545 		/**
       
   546 		 * Fires after a single post is created or updated via the REST API.
       
   547 		 *
       
   548 		 * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
       
   549 		 *
       
   550 		 * @since 4.7.0
       
   551 		 *
       
   552 		 * @param WP_Post         $post     Inserted or updated post object.
       
   553 		 * @param WP_REST_Request $request  Request object.
       
   554 		 * @param bool            $creating True when creating a post, false when updating.
       
   555 		 */
       
   556 		do_action( "rest_insert_{$this->post_type}", $post, $request, true );
       
   557 
       
   558 		$schema = $this->get_item_schema();
       
   559 
       
   560 		if ( ! empty( $schema['properties']['sticky'] ) ) {
       
   561 			if ( ! empty( $request['sticky'] ) ) {
       
   562 				stick_post( $post_id );
       
   563 			} else {
       
   564 				unstick_post( $post_id );
       
   565 			}
       
   566 		}
       
   567 
       
   568 		if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
       
   569 			$this->handle_featured_media( $request['featured_media'], $post_id );
       
   570 		}
       
   571 
       
   572 		if ( ! empty( $schema['properties']['format'] ) && ! empty( $request['format'] ) ) {
       
   573 			set_post_format( $post, $request['format'] );
       
   574 		}
       
   575 
       
   576 		if ( ! empty( $schema['properties']['template'] ) && isset( $request['template'] ) ) {
       
   577 			$this->handle_template( $request['template'], $post_id, true );
       
   578 		}
       
   579 
       
   580 		$terms_update = $this->handle_terms( $post_id, $request );
       
   581 
       
   582 		if ( is_wp_error( $terms_update ) ) {
       
   583 			return $terms_update;
       
   584 		}
       
   585 
       
   586 		if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
       
   587 			$meta_update = $this->meta->update_value( $request['meta'], $post_id );
       
   588 
       
   589 			if ( is_wp_error( $meta_update ) ) {
       
   590 				return $meta_update;
       
   591 			}
       
   592 		}
       
   593 
       
   594 		$post = get_post( $post_id );
       
   595 		$fields_update = $this->update_additional_fields_for_object( $post, $request );
       
   596 
       
   597 		if ( is_wp_error( $fields_update ) ) {
       
   598 			return $fields_update;
       
   599 		}
       
   600 
       
   601 		$request->set_param( 'context', 'edit' );
       
   602 
       
   603 		$response = $this->prepare_item_for_response( $post, $request );
       
   604 		$response = rest_ensure_response( $response );
       
   605 
       
   606 		$response->set_status( 201 );
       
   607 		$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $post_id ) ) );
       
   608 
       
   609 		return $response;
       
   610 	}
       
   611 
       
   612 	/**
       
   613 	 * Checks if a given request has access to update a post.
       
   614 	 *
       
   615 	 * @since 4.7.0
       
   616 	 *
       
   617 	 * @param WP_REST_Request $request Full details about the request.
       
   618 	 * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
       
   619 	 */
       
   620 	public function update_item_permissions_check( $request ) {
       
   621 		$post = $this->get_post( $request['id'] );
       
   622 		if ( is_wp_error( $post ) ) {
       
   623 			return $post;
       
   624 		}
       
   625 
       
   626 		$post_type = get_post_type_object( $this->post_type );
       
   627 
       
   628 		if ( $post && ! $this->check_update_permission( $post ) ) {
       
   629 			return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this post.' ), array( 'status' => rest_authorization_required_code() ) );
       
   630 		}
       
   631 
       
   632 		if ( ! empty( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( $post_type->cap->edit_others_posts ) ) {
       
   633 			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() ) );
       
   634 		}
       
   635 
       
   636 		if ( ! empty( $request['sticky'] ) && ! current_user_can( $post_type->cap->edit_others_posts ) ) {
       
   637 			return new WP_Error( 'rest_cannot_assign_sticky', __( 'Sorry, you are not allowed to make posts sticky.' ), array( 'status' => rest_authorization_required_code() ) );
       
   638 		}
       
   639 
       
   640 		if ( ! $this->check_assign_terms_permission( $request ) ) {
       
   641 			return new WP_Error( 'rest_cannot_assign_term', __( 'Sorry, you are not allowed to assign the provided terms.' ), array( 'status' => rest_authorization_required_code() ) );
       
   642 		}
       
   643 
       
   644 		return true;
       
   645 	}
       
   646 
       
   647 	/**
       
   648 	 * Updates a single post.
       
   649 	 *
       
   650 	 * @since 4.7.0
       
   651 	 *
       
   652 	 * @param WP_REST_Request $request Full details about the request.
       
   653 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
       
   654 	 */
       
   655 	public function update_item( $request ) {
       
   656 		$valid_check = $this->get_post( $request['id'] );
       
   657 		if ( is_wp_error( $valid_check ) ) {
       
   658 			return $valid_check;
       
   659 		}
       
   660 
       
   661 		$post = $this->prepare_item_for_database( $request );
       
   662 
       
   663 		if ( is_wp_error( $post ) ) {
       
   664 			return $post;
       
   665 		}
       
   666 
       
   667 		// convert the post object to an array, otherwise wp_update_post will expect non-escaped input.
       
   668 		$post_id = wp_update_post( wp_slash( (array) $post ), true );
       
   669 
       
   670 		if ( is_wp_error( $post_id ) ) {
       
   671 			if ( 'db_update_error' === $post_id->get_error_code() ) {
       
   672 				$post_id->add_data( array( 'status' => 500 ) );
       
   673 			} else {
       
   674 				$post_id->add_data( array( 'status' => 400 ) );
       
   675 			}
       
   676 			return $post_id;
       
   677 		}
       
   678 
       
   679 		$post = get_post( $post_id );
       
   680 
       
   681 		/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
       
   682 		do_action( "rest_insert_{$this->post_type}", $post, $request, false );
       
   683 
       
   684 		$schema = $this->get_item_schema();
       
   685 
       
   686 		if ( ! empty( $schema['properties']['format'] ) && ! empty( $request['format'] ) ) {
       
   687 			set_post_format( $post, $request['format'] );
       
   688 		}
       
   689 
       
   690 		if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
       
   691 			$this->handle_featured_media( $request['featured_media'], $post_id );
       
   692 		}
       
   693 
       
   694 		if ( ! empty( $schema['properties']['sticky'] ) && isset( $request['sticky'] ) ) {
       
   695 			if ( ! empty( $request['sticky'] ) ) {
       
   696 				stick_post( $post_id );
       
   697 			} else {
       
   698 				unstick_post( $post_id );
       
   699 			}
       
   700 		}
       
   701 
       
   702 		if ( ! empty( $schema['properties']['template'] ) && isset( $request['template'] ) ) {
       
   703 			$this->handle_template( $request['template'], $post->ID );
       
   704 		}
       
   705 
       
   706 		$terms_update = $this->handle_terms( $post->ID, $request );
       
   707 
       
   708 		if ( is_wp_error( $terms_update ) ) {
       
   709 			return $terms_update;
       
   710 		}
       
   711 
       
   712 		if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
       
   713 			$meta_update = $this->meta->update_value( $request['meta'], $post->ID );
       
   714 
       
   715 			if ( is_wp_error( $meta_update ) ) {
       
   716 				return $meta_update;
       
   717 			}
       
   718 		}
       
   719 
       
   720 		$post = get_post( $post_id );
       
   721 		$fields_update = $this->update_additional_fields_for_object( $post, $request );
       
   722 
       
   723 		if ( is_wp_error( $fields_update ) ) {
       
   724 			return $fields_update;
       
   725 		}
       
   726 
       
   727 		$request->set_param( 'context', 'edit' );
       
   728 
       
   729 		$response = $this->prepare_item_for_response( $post, $request );
       
   730 
       
   731 		return rest_ensure_response( $response );
       
   732 	}
       
   733 
       
   734 	/**
       
   735 	 * Checks if a given request has access to delete a post.
       
   736 	 *
       
   737 	 * @since 4.7.0
       
   738 	 *
       
   739 	 * @param WP_REST_Request $request Full details about the request.
       
   740 	 * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
       
   741 	 */
       
   742 	public function delete_item_permissions_check( $request ) {
       
   743 		$post = $this->get_post( $request['id'] );
       
   744 		if ( is_wp_error( $post ) ) {
       
   745 			return $post;
       
   746 		}
       
   747 
       
   748 		if ( $post && ! $this->check_delete_permission( $post ) ) {
       
   749 			return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete this post.' ), array( 'status' => rest_authorization_required_code() ) );
       
   750 		}
       
   751 
       
   752 		return true;
       
   753 	}
       
   754 
       
   755 	/**
       
   756 	 * Deletes a single post.
       
   757 	 *
       
   758 	 * @since 4.7.0
       
   759 	 *
       
   760 	 * @param WP_REST_Request $request Full details about the request.
       
   761 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
       
   762 	 */
       
   763 	public function delete_item( $request ) {
       
   764 		$post = $this->get_post( $request['id'] );
       
   765 		if ( is_wp_error( $post ) ) {
       
   766 			return $post;
       
   767 		}
       
   768 
       
   769 		$id    = $post->ID;
       
   770 		$force = (bool) $request['force'];
       
   771 
       
   772 		$supports_trash = ( EMPTY_TRASH_DAYS > 0 );
       
   773 
       
   774 		if ( 'attachment' === $post->post_type ) {
       
   775 			$supports_trash = $supports_trash && MEDIA_TRASH;
       
   776 		}
       
   777 
       
   778 		/**
       
   779 		 * Filters whether a post is trashable.
       
   780 		 *
       
   781 		 * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
       
   782 		 *
       
   783 		 * Pass false to disable trash support for the post.
       
   784 		 *
       
   785 		 * @since 4.7.0
       
   786 		 *
       
   787 		 * @param bool    $supports_trash Whether the post type support trashing.
       
   788 		 * @param WP_Post $post           The Post object being considered for trashing support.
       
   789 		 */
       
   790 		$supports_trash = apply_filters( "rest_{$this->post_type}_trashable", $supports_trash, $post );
       
   791 
       
   792 		if ( ! $this->check_delete_permission( $post ) ) {
       
   793 			return new WP_Error( 'rest_user_cannot_delete_post', __( 'Sorry, you are not allowed to delete this post.' ), array( 'status' => rest_authorization_required_code() ) );
       
   794 		}
       
   795 
       
   796 		$request->set_param( 'context', 'edit' );
       
   797 
       
   798 
       
   799 		// If we're forcing, then delete permanently.
       
   800 		if ( $force ) {
       
   801 			$previous = $this->prepare_item_for_response( $post, $request );
       
   802 			$result = wp_delete_post( $id, true );
       
   803 			$response = new WP_REST_Response();
       
   804 			$response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
       
   805 		} else {
       
   806 			// If we don't support trashing for this type, error out.
       
   807 			if ( ! $supports_trash ) {
       
   808 				/* translators: %s: force=true */
       
   809 				return new WP_Error( 'rest_trash_not_supported', sprintf( __( "The post does not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
       
   810 			}
       
   811 
       
   812 			// Otherwise, only trash if we haven't already.
       
   813 			if ( 'trash' === $post->post_status ) {
       
   814 				return new WP_Error( 'rest_already_trashed', __( 'The post has already been deleted.' ), array( 'status' => 410 ) );
       
   815 			}
       
   816 
       
   817 			// (Note that internally this falls through to `wp_delete_post` if
       
   818 			// the trash is disabled.)
       
   819 			$result = wp_trash_post( $id );
       
   820 			$post = get_post( $id );
       
   821 			$response = $this->prepare_item_for_response( $post, $request );
       
   822 		}
       
   823 
       
   824 		if ( ! $result ) {
       
   825 			return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
       
   826 		}
       
   827 
       
   828 		/**
       
   829 		 * Fires immediately after a single post is deleted or trashed via the REST API.
       
   830 		 *
       
   831 		 * They dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
       
   832 		 *
       
   833 		 * @since 4.7.0
       
   834 		 *
       
   835 		 * @param object           $post     The deleted or trashed post.
       
   836 		 * @param WP_REST_Response $response The response data.
       
   837 		 * @param WP_REST_Request  $request  The request sent to the API.
       
   838 		 */
       
   839 		do_action( "rest_delete_{$this->post_type}", $post, $response, $request );
       
   840 
       
   841 		return $response;
       
   842 	}
       
   843 
       
   844 	/**
       
   845 	 * Determines the allowed query_vars for a get_items() response and prepares
       
   846 	 * them for WP_Query.
       
   847 	 *
       
   848 	 * @since 4.7.0
       
   849 	 *
       
   850 	 * @param array           $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
       
   851 	 * @param WP_REST_Request $request       Optional. Full details about the request.
       
   852 	 * @return array Items query arguments.
       
   853 	 */
       
   854 	protected function prepare_items_query( $prepared_args = array(), $request = null ) {
       
   855 		$query_args = array();
       
   856 
       
   857 		foreach ( $prepared_args as $key => $value ) {
       
   858 			/**
       
   859 			 * Filters the query_vars used in get_items() for the constructed query.
       
   860 			 *
       
   861 			 * The dynamic portion of the hook name, `$key`, refers to the query_var key.
       
   862 			 *
       
   863 			 * @since 4.7.0
       
   864 			 *
       
   865 			 * @param string $value The query_var value.
       
   866 			 */
       
   867 			$query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value );
       
   868 		}
       
   869 
       
   870 		if ( 'post' !== $this->post_type || ! isset( $query_args['ignore_sticky_posts'] ) ) {
       
   871 			$query_args['ignore_sticky_posts'] = true;
       
   872 		}
       
   873 
       
   874 		// Map to proper WP_Query orderby param.
       
   875 		if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
       
   876 			$orderby_mappings = array(
       
   877 				'id'            => 'ID',
       
   878 				'include'       => 'post__in',
       
   879 				'slug'          => 'post_name',
       
   880 				'include_slugs' => 'post_name__in',
       
   881 			);
       
   882 
       
   883 			if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
       
   884 				$query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
       
   885 			}
       
   886 		}
       
   887 
       
   888 		return $query_args;
       
   889 	}
       
   890 
       
   891 	/**
       
   892 	 * Checks the post_date_gmt or modified_gmt and prepare any post or
       
   893 	 * modified date for single post output.
       
   894 	 *
       
   895 	 * @since 4.7.0
       
   896 	 *
       
   897 	 * @param string      $date_gmt GMT publication time.
       
   898 	 * @param string|null $date     Optional. Local publication time. Default null.
       
   899 	 * @return string|null ISO8601/RFC3339 formatted datetime.
       
   900 	 */
       
   901 	protected function prepare_date_response( $date_gmt, $date = null ) {
       
   902 		// Use the date if passed.
       
   903 		if ( isset( $date ) ) {
       
   904 			return mysql_to_rfc3339( $date );
       
   905 		}
       
   906 
       
   907 		// Return null if $date_gmt is empty/zeros.
       
   908 		if ( '0000-00-00 00:00:00' === $date_gmt ) {
       
   909 			return null;
       
   910 		}
       
   911 
       
   912 		// Return the formatted datetime.
       
   913 		return mysql_to_rfc3339( $date_gmt );
       
   914 	}
       
   915 
       
   916 	/**
       
   917 	 * Prepares a single post for create or update.
       
   918 	 *
       
   919 	 * @since 4.7.0
       
   920 	 *
       
   921 	 * @param WP_REST_Request $request Request object.
       
   922 	 * @return stdClass|WP_Error Post object or WP_Error.
       
   923 	 */
       
   924 	protected function prepare_item_for_database( $request ) {
       
   925 		$prepared_post = new stdClass;
       
   926 
       
   927 		// Post ID.
       
   928 		if ( isset( $request['id'] ) ) {
       
   929 			$existing_post = $this->get_post( $request['id'] );
       
   930 			if ( is_wp_error( $existing_post ) ) {
       
   931 				return $existing_post;
       
   932 			}
       
   933 
       
   934 			$prepared_post->ID = $existing_post->ID;
       
   935 		}
       
   936 
       
   937 		$schema = $this->get_item_schema();
       
   938 
       
   939 		// Post title.
       
   940 		if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
       
   941 			if ( is_string( $request['title'] ) ) {
       
   942 				$prepared_post->post_title = $request['title'];
       
   943 			} elseif ( ! empty( $request['title']['raw'] ) ) {
       
   944 				$prepared_post->post_title = $request['title']['raw'];
       
   945 			}
       
   946 		}
       
   947 
       
   948 		// Post content.
       
   949 		if ( ! empty( $schema['properties']['content'] ) && isset( $request['content'] ) ) {
       
   950 			if ( is_string( $request['content'] ) ) {
       
   951 				$prepared_post->post_content = $request['content'];
       
   952 			} elseif ( isset( $request['content']['raw'] ) ) {
       
   953 				$prepared_post->post_content = $request['content']['raw'];
       
   954 			}
       
   955 		}
       
   956 
       
   957 		// Post excerpt.
       
   958 		if ( ! empty( $schema['properties']['excerpt'] ) && isset( $request['excerpt'] ) ) {
       
   959 			if ( is_string( $request['excerpt'] ) ) {
       
   960 				$prepared_post->post_excerpt = $request['excerpt'];
       
   961 			} elseif ( isset( $request['excerpt']['raw'] ) ) {
       
   962 				$prepared_post->post_excerpt = $request['excerpt']['raw'];
       
   963 			}
       
   964 		}
       
   965 
       
   966 		// Post type.
       
   967 		if ( empty( $request['id'] ) ) {
       
   968 			// Creating new post, use default type for the controller.
       
   969 			$prepared_post->post_type = $this->post_type;
       
   970 		} else {
       
   971 			// Updating a post, use previous type.
       
   972 			$prepared_post->post_type = get_post_type( $request['id'] );
       
   973 		}
       
   974 
       
   975 		$post_type = get_post_type_object( $prepared_post->post_type );
       
   976 
       
   977 		// Post status.
       
   978 		if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) {
       
   979 			$status = $this->handle_status_param( $request['status'], $post_type );
       
   980 
       
   981 			if ( is_wp_error( $status ) ) {
       
   982 				return $status;
       
   983 			}
       
   984 
       
   985 			$prepared_post->post_status = $status;
       
   986 		}
       
   987 
       
   988 		// Post date.
       
   989 		if ( ! empty( $schema['properties']['date'] ) && ! empty( $request['date'] ) ) {
       
   990 			$date_data = rest_get_date_with_gmt( $request['date'] );
       
   991 
       
   992 			if ( ! empty( $date_data ) ) {
       
   993 				list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data;
       
   994 				$prepared_post->edit_date = true;
       
   995 			}
       
   996 		} elseif ( ! empty( $schema['properties']['date_gmt'] ) && ! empty( $request['date_gmt'] ) ) {
       
   997 			$date_data = rest_get_date_with_gmt( $request['date_gmt'], true );
       
   998 
       
   999 			if ( ! empty( $date_data ) ) {
       
  1000 				list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data;
       
  1001 				$prepared_post->edit_date = true;
       
  1002 			}
       
  1003 		}
       
  1004 
       
  1005 		// Post slug.
       
  1006 		if ( ! empty( $schema['properties']['slug'] ) && isset( $request['slug'] ) ) {
       
  1007 			$prepared_post->post_name = $request['slug'];
       
  1008 		}
       
  1009 
       
  1010 		// Author.
       
  1011 		if ( ! empty( $schema['properties']['author'] ) && ! empty( $request['author'] ) ) {
       
  1012 			$post_author = (int) $request['author'];
       
  1013 
       
  1014 			if ( get_current_user_id() !== $post_author ) {
       
  1015 				$user_obj = get_userdata( $post_author );
       
  1016 
       
  1017 				if ( ! $user_obj ) {
       
  1018 					return new WP_Error( 'rest_invalid_author', __( 'Invalid author ID.' ), array( 'status' => 400 ) );
       
  1019 				}
       
  1020 			}
       
  1021 
       
  1022 			$prepared_post->post_author = $post_author;
       
  1023 		}
       
  1024 
       
  1025 		// Post password.
       
  1026 		if ( ! empty( $schema['properties']['password'] ) && isset( $request['password'] ) ) {
       
  1027 			$prepared_post->post_password = $request['password'];
       
  1028 
       
  1029 			if ( '' !== $request['password'] ) {
       
  1030 				if ( ! empty( $schema['properties']['sticky'] ) && ! empty( $request['sticky'] ) ) {
       
  1031 					return new WP_Error( 'rest_invalid_field', __( 'A post can not be sticky and have a password.' ), array( 'status' => 400 ) );
       
  1032 				}
       
  1033 
       
  1034 				if ( ! empty( $prepared_post->ID ) && is_sticky( $prepared_post->ID ) ) {
       
  1035 					return new WP_Error( 'rest_invalid_field', __( 'A sticky post can not be password protected.' ), array( 'status' => 400 ) );
       
  1036 				}
       
  1037 			}
       
  1038 		}
       
  1039 
       
  1040 		if ( ! empty( $schema['properties']['sticky'] ) && ! empty( $request['sticky'] ) ) {
       
  1041 			if ( ! empty( $prepared_post->ID ) && post_password_required( $prepared_post->ID ) ) {
       
  1042 				return new WP_Error( 'rest_invalid_field', __( 'A password protected post can not be set to sticky.' ), array( 'status' => 400 ) );
       
  1043 			}
       
  1044 		}
       
  1045 
       
  1046 		// Parent.
       
  1047 		if ( ! empty( $schema['properties']['parent'] ) && isset( $request['parent'] ) ) {
       
  1048 			if ( 0 === (int) $request['parent'] ) {
       
  1049 				$prepared_post->post_parent = 0;
       
  1050 			} else {
       
  1051 				$parent = get_post( (int) $request['parent'] );
       
  1052 				if ( empty( $parent ) ) {
       
  1053 					return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post parent ID.' ), array( 'status' => 400 ) );
       
  1054 				}
       
  1055 				$prepared_post->post_parent = (int) $parent->ID;
       
  1056 			}
       
  1057 		}
       
  1058 
       
  1059 		// Menu order.
       
  1060 		if ( ! empty( $schema['properties']['menu_order'] ) && isset( $request['menu_order'] ) ) {
       
  1061 			$prepared_post->menu_order = (int) $request['menu_order'];
       
  1062 		}
       
  1063 
       
  1064 		// Comment status.
       
  1065 		if ( ! empty( $schema['properties']['comment_status'] ) && ! empty( $request['comment_status'] ) ) {
       
  1066 			$prepared_post->comment_status = $request['comment_status'];
       
  1067 		}
       
  1068 
       
  1069 		// Ping status.
       
  1070 		if ( ! empty( $schema['properties']['ping_status'] ) && ! empty( $request['ping_status'] ) ) {
       
  1071 			$prepared_post->ping_status = $request['ping_status'];
       
  1072 		}
       
  1073 
       
  1074 		if ( ! empty( $schema['properties']['template'] ) ) {
       
  1075 			// Force template to null so that it can be handled exclusively by the REST controller.
       
  1076 			$prepared_post->page_template = null;
       
  1077 		}
       
  1078 
       
  1079 		/**
       
  1080 		 * Filters a post before it is inserted via the REST API.
       
  1081 		 *
       
  1082 		 * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
       
  1083 		 *
       
  1084 		 * @since 4.7.0
       
  1085 		 *
       
  1086 		 * @param stdClass        $prepared_post An object representing a single post prepared
       
  1087 		 *                                       for inserting or updating the database.
       
  1088 		 * @param WP_REST_Request $request       Request object.
       
  1089 		 */
       
  1090 		return apply_filters( "rest_pre_insert_{$this->post_type}", $prepared_post, $request );
       
  1091 
       
  1092 	}
       
  1093 
       
  1094 	/**
       
  1095 	 * Determines validity and normalizes the given status parameter.
       
  1096 	 *
       
  1097 	 * @since 4.7.0
       
  1098 	 *
       
  1099 	 * @param string $post_status Post status.
       
  1100 	 * @param object $post_type   Post type.
       
  1101 	 * @return string|WP_Error Post status or WP_Error if lacking the proper permission.
       
  1102 	 */
       
  1103 	protected function handle_status_param( $post_status, $post_type ) {
       
  1104 
       
  1105 		switch ( $post_status ) {
       
  1106 			case 'draft':
       
  1107 			case 'pending':
       
  1108 				break;
       
  1109 			case 'private':
       
  1110 				if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
       
  1111 					return new WP_Error( 'rest_cannot_publish', __( 'Sorry, you are not allowed to create private posts in this post type.' ), array( 'status' => rest_authorization_required_code() ) );
       
  1112 				}
       
  1113 				break;
       
  1114 			case 'publish':
       
  1115 			case 'future':
       
  1116 				if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
       
  1117 					return new WP_Error( 'rest_cannot_publish', __( 'Sorry, you are not allowed to publish posts in this post type.' ), array( 'status' => rest_authorization_required_code() ) );
       
  1118 				}
       
  1119 				break;
       
  1120 			default:
       
  1121 				if ( ! get_post_status_object( $post_status ) ) {
       
  1122 					$post_status = 'draft';
       
  1123 				}
       
  1124 				break;
       
  1125 		}
       
  1126 
       
  1127 		return $post_status;
       
  1128 	}
       
  1129 
       
  1130 	/**
       
  1131 	 * Determines the featured media based on a request param.
       
  1132 	 *
       
  1133 	 * @since 4.7.0
       
  1134 	 *
       
  1135 	 * @param int $featured_media Featured Media ID.
       
  1136 	 * @param int $post_id        Post ID.
       
  1137 	 * @return bool|WP_Error Whether the post thumbnail was successfully deleted, otherwise WP_Error.
       
  1138 	 */
       
  1139 	protected function handle_featured_media( $featured_media, $post_id ) {
       
  1140 
       
  1141 		$featured_media = (int) $featured_media;
       
  1142 		if ( $featured_media ) {
       
  1143 			$result = set_post_thumbnail( $post_id, $featured_media );
       
  1144 			if ( $result ) {
       
  1145 				return true;
       
  1146 			} else {
       
  1147 				return new WP_Error( 'rest_invalid_featured_media', __( 'Invalid featured media ID.' ), array( 'status' => 400 ) );
       
  1148 			}
       
  1149 		} else {
       
  1150 			return delete_post_thumbnail( $post_id );
       
  1151 		}
       
  1152 
       
  1153 	}
       
  1154 
       
  1155 	/**
       
  1156 	 * Check whether the template is valid for the given post.
       
  1157 	 *
       
  1158 	 * @since 4.9.0
       
  1159 	 *
       
  1160 	 * @param string          $template Page template filename.
       
  1161 	 * @param WP_REST_Request $request  Request.
       
  1162 	 * @return bool|WP_Error True if template is still valid or if the same as existing value, or false if template not supported.
       
  1163 	 */
       
  1164 	public function check_template( $template, $request ) {
       
  1165 
       
  1166 		if ( ! $template ) {
       
  1167 			return true;
       
  1168 		}
       
  1169 
       
  1170 		if ( $request['id'] ) {
       
  1171 			$current_template = get_page_template_slug( $request['id'] );
       
  1172 		} else {
       
  1173 			$current_template = '';
       
  1174 		}
       
  1175 
       
  1176 		// Always allow for updating a post to the same template, even if that template is no longer supported.
       
  1177 		if ( $template === $current_template ) {
       
  1178 			return true;
       
  1179 		}
       
  1180 
       
  1181 		// If this is a create request, get_post() will return null and wp theme will fallback to the passed post type.
       
  1182 		$allowed_templates = wp_get_theme()->get_page_templates( get_post( $request['id'] ), $this->post_type );
       
  1183 
       
  1184 		if ( isset( $allowed_templates[ $template ] ) ) {
       
  1185 			return true;
       
  1186 		}
       
  1187 
       
  1188 		/* translators: 1: parameter, 2: list of valid values */
       
  1189 		return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not one of %2$s.' ), 'template', implode( ', ', array_keys( $allowed_templates ) ) ) );
       
  1190 	}
       
  1191 
       
  1192 	/**
       
  1193 	 * Sets the template for a post.
       
  1194 	 *
       
  1195 	 * @since 4.7.0
       
  1196 	 * @since 4.9.0 Introduced the $validate parameter.
       
  1197 	 *
       
  1198 	 * @param string  $template Page template filename.
       
  1199 	 * @param integer $post_id  Post ID.
       
  1200 	 * @param bool    $validate Whether to validate that the template selected is valid.
       
  1201 	 */
       
  1202 	public function handle_template( $template, $post_id, $validate = false ) {
       
  1203 
       
  1204 		if ( $validate && ! array_key_exists( $template, wp_get_theme()->get_page_templates( get_post( $post_id ) ) ) ) {
       
  1205 			$template = '';
       
  1206 		}
       
  1207 
       
  1208 		update_post_meta( $post_id, '_wp_page_template', $template );
       
  1209 	}
       
  1210 
       
  1211 	/**
       
  1212 	 * Updates the post's terms from a REST request.
       
  1213 	 *
       
  1214 	 * @since 4.7.0
       
  1215 	 *
       
  1216 	 * @param int             $post_id The post ID to update the terms form.
       
  1217 	 * @param WP_REST_Request $request The request object with post and terms data.
       
  1218 	 * @return null|WP_Error WP_Error on an error assigning any of the terms, otherwise null.
       
  1219 	 */
       
  1220 	protected function handle_terms( $post_id, $request ) {
       
  1221 		$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
       
  1222 
       
  1223 		foreach ( $taxonomies as $taxonomy ) {
       
  1224 			$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
       
  1225 
       
  1226 			if ( ! isset( $request[ $base ] ) ) {
       
  1227 				continue;
       
  1228 			}
       
  1229 
       
  1230 			$result = wp_set_object_terms( $post_id, $request[ $base ], $taxonomy->name );
       
  1231 
       
  1232 			if ( is_wp_error( $result ) ) {
       
  1233 				return $result;
       
  1234 			}
       
  1235 		}
       
  1236 	}
       
  1237 
       
  1238 	/**
       
  1239 	 * Checks whether current user can assign all terms sent with the current request.
       
  1240 	 *
       
  1241 	 * @since 4.7.0
       
  1242 	 *
       
  1243 	 * @param WP_REST_Request $request The request object with post and terms data.
       
  1244 	 * @return bool Whether the current user can assign the provided terms.
       
  1245 	 */
       
  1246 	protected function check_assign_terms_permission( $request ) {
       
  1247 		$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
       
  1248 		foreach ( $taxonomies as $taxonomy ) {
       
  1249 			$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
       
  1250 
       
  1251 			if ( ! isset( $request[ $base ] ) ) {
       
  1252 				continue;
       
  1253 			}
       
  1254 
       
  1255 			foreach ( $request[ $base ] as $term_id ) {
       
  1256 				// Invalid terms will be rejected later.
       
  1257 				if ( ! get_term( $term_id, $taxonomy->name ) ) {
       
  1258 					continue;
       
  1259 				}
       
  1260 
       
  1261 				if ( ! current_user_can( 'assign_term', (int) $term_id ) ) {
       
  1262 					return false;
       
  1263 				}
       
  1264 			}
       
  1265 		}
       
  1266 
       
  1267 		return true;
       
  1268 	}
       
  1269 
       
  1270 	/**
       
  1271 	 * Checks if a given post type can be viewed or managed.
       
  1272 	 *
       
  1273 	 * @since 4.7.0
       
  1274 	 *
       
  1275 	 * @param object|string $post_type Post type name or object.
       
  1276 	 * @return bool Whether the post type is allowed in REST.
       
  1277 	 */
       
  1278 	protected function check_is_post_type_allowed( $post_type ) {
       
  1279 		if ( ! is_object( $post_type ) ) {
       
  1280 			$post_type = get_post_type_object( $post_type );
       
  1281 		}
       
  1282 
       
  1283 		if ( ! empty( $post_type ) && ! empty( $post_type->show_in_rest ) ) {
       
  1284 			return true;
       
  1285 		}
       
  1286 
       
  1287 		return false;
       
  1288 	}
       
  1289 
       
  1290 	/**
       
  1291 	 * Checks if a post can be read.
       
  1292 	 *
       
  1293 	 * Correctly handles posts with the inherit status.
       
  1294 	 *
       
  1295 	 * @since 4.7.0
       
  1296 	 *
       
  1297 	 * @param object $post Post object.
       
  1298 	 * @return bool Whether the post can be read.
       
  1299 	 */
       
  1300 	public function check_read_permission( $post ) {
       
  1301 		$post_type = get_post_type_object( $post->post_type );
       
  1302 		if ( ! $this->check_is_post_type_allowed( $post_type ) ) {
       
  1303 			return false;
       
  1304 		}
       
  1305 
       
  1306 		// Is the post readable?
       
  1307 		if ( 'publish' === $post->post_status || current_user_can( $post_type->cap->read_post, $post->ID ) ) {
       
  1308 			return true;
       
  1309 		}
       
  1310 
       
  1311 		$post_status_obj = get_post_status_object( $post->post_status );
       
  1312 		if ( $post_status_obj && $post_status_obj->public ) {
       
  1313 			return true;
       
  1314 		}
       
  1315 
       
  1316 		// Can we read the parent if we're inheriting?
       
  1317 		if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) {
       
  1318 			$parent = get_post( $post->post_parent );
       
  1319 			if ( $parent ) {
       
  1320 				return $this->check_read_permission( $parent );
       
  1321 			}
       
  1322 		}
       
  1323 
       
  1324 		/*
       
  1325 		 * If there isn't a parent, but the status is set to inherit, assume
       
  1326 		 * it's published (as per get_post_status()).
       
  1327 		 */
       
  1328 		if ( 'inherit' === $post->post_status ) {
       
  1329 			return true;
       
  1330 		}
       
  1331 
       
  1332 		return false;
       
  1333 	}
       
  1334 
       
  1335 	/**
       
  1336 	 * Checks if a post can be edited.
       
  1337 	 *
       
  1338 	 * @since 4.7.0
       
  1339 	 *
       
  1340 	 * @param object $post Post object.
       
  1341 	 * @return bool Whether the post can be edited.
       
  1342 	 */
       
  1343 	protected function check_update_permission( $post ) {
       
  1344 		$post_type = get_post_type_object( $post->post_type );
       
  1345 
       
  1346 		if ( ! $this->check_is_post_type_allowed( $post_type ) ) {
       
  1347 			return false;
       
  1348 		}
       
  1349 
       
  1350 		return current_user_can( $post_type->cap->edit_post, $post->ID );
       
  1351 	}
       
  1352 
       
  1353 	/**
       
  1354 	 * Checks if a post can be created.
       
  1355 	 *
       
  1356 	 * @since 4.7.0
       
  1357 	 *
       
  1358 	 * @param object $post Post object.
       
  1359 	 * @return bool Whether the post can be created.
       
  1360 	 */
       
  1361 	protected function check_create_permission( $post ) {
       
  1362 		$post_type = get_post_type_object( $post->post_type );
       
  1363 
       
  1364 		if ( ! $this->check_is_post_type_allowed( $post_type ) ) {
       
  1365 			return false;
       
  1366 		}
       
  1367 
       
  1368 		return current_user_can( $post_type->cap->create_posts );
       
  1369 	}
       
  1370 
       
  1371 	/**
       
  1372 	 * Checks if a post can be deleted.
       
  1373 	 *
       
  1374 	 * @since 4.7.0
       
  1375 	 *
       
  1376 	 * @param object $post Post object.
       
  1377 	 * @return bool Whether the post can be deleted.
       
  1378 	 */
       
  1379 	protected function check_delete_permission( $post ) {
       
  1380 		$post_type = get_post_type_object( $post->post_type );
       
  1381 
       
  1382 		if ( ! $this->check_is_post_type_allowed( $post_type ) ) {
       
  1383 			return false;
       
  1384 		}
       
  1385 
       
  1386 		return current_user_can( $post_type->cap->delete_post, $post->ID );
       
  1387 	}
       
  1388 
       
  1389 	/**
       
  1390 	 * Prepares a single post output for response.
       
  1391 	 *
       
  1392 	 * @since 4.7.0
       
  1393 	 *
       
  1394 	 * @param WP_Post         $post    Post object.
       
  1395 	 * @param WP_REST_Request $request Request object.
       
  1396 	 * @return WP_REST_Response Response object.
       
  1397 	 */
       
  1398 	public function prepare_item_for_response( $post, $request ) {
       
  1399 		$GLOBALS['post'] = $post;
       
  1400 
       
  1401 		setup_postdata( $post );
       
  1402 
       
  1403 		$fields = $this->get_fields_for_response( $request );
       
  1404 
       
  1405 		// Base fields for every post.
       
  1406 		$data = array();
       
  1407 
       
  1408 		if ( in_array( 'id', $fields, true ) ) {
       
  1409 			$data['id'] = $post->ID;
       
  1410 		}
       
  1411 
       
  1412 		if ( in_array( 'date', $fields, true ) ) {
       
  1413 			$data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
       
  1414 		}
       
  1415 
       
  1416 		if ( in_array( 'date_gmt', $fields, true ) ) {
       
  1417 			// For drafts, `post_date_gmt` may not be set, indicating that the
       
  1418 			// date of the draft should be updated each time it is saved (see
       
  1419 			// #38883).  In this case, shim the value based on the `post_date`
       
  1420 			// field with the site's timezone offset applied.
       
  1421 			if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
       
  1422 				$post_date_gmt = get_gmt_from_date( $post->post_date );
       
  1423 			} else {
       
  1424 				$post_date_gmt = $post->post_date_gmt;
       
  1425 			}
       
  1426 			$data['date_gmt'] = $this->prepare_date_response( $post_date_gmt );
       
  1427 		}
       
  1428 
       
  1429 		if ( in_array( 'guid', $fields, true ) ) {
       
  1430 			$data['guid'] = array(
       
  1431 				/** This filter is documented in wp-includes/post-template.php */
       
  1432 				'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
       
  1433 				'raw'      => $post->guid,
       
  1434 			);
       
  1435 		}
       
  1436 
       
  1437 		if ( in_array( 'modified', $fields, true ) ) {
       
  1438 			$data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
       
  1439 		}
       
  1440 
       
  1441 		if ( in_array( 'modified_gmt', $fields, true ) ) {
       
  1442 			// For drafts, `post_modified_gmt` may not be set (see
       
  1443 			// `post_date_gmt` comments above).  In this case, shim the value
       
  1444 			// based on the `post_modified` field with the site's timezone
       
  1445 			// offset applied.
       
  1446 			if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
       
  1447 				$post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) );
       
  1448 			} else {
       
  1449 				$post_modified_gmt = $post->post_modified_gmt;
       
  1450 			}
       
  1451 			$data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt );
       
  1452 		}
       
  1453 
       
  1454 		if ( in_array( 'password', $fields, true ) ) {
       
  1455 			$data['password'] = $post->post_password;
       
  1456 		}
       
  1457 
       
  1458 		if ( in_array( 'slug', $fields, true ) ) {
       
  1459 			$data['slug'] = $post->post_name;
       
  1460 		}
       
  1461 
       
  1462 		if ( in_array( 'status', $fields, true ) ) {
       
  1463 			$data['status'] = $post->post_status;
       
  1464 		}
       
  1465 
       
  1466 		if ( in_array( 'type', $fields, true ) ) {
       
  1467 			$data['type'] = $post->post_type;
       
  1468 		}
       
  1469 
       
  1470 		if ( in_array( 'link', $fields, true ) ) {
       
  1471 			$data['link'] = get_permalink( $post->ID );
       
  1472 		}
       
  1473 
       
  1474 		if ( in_array( 'title', $fields, true ) ) {
       
  1475 			add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
       
  1476 
       
  1477 			$data['title'] = array(
       
  1478 				'raw'      => $post->post_title,
       
  1479 				'rendered' => get_the_title( $post->ID ),
       
  1480 			);
       
  1481 
       
  1482 			remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
       
  1483 		}
       
  1484 
       
  1485 		$has_password_filter = false;
       
  1486 
       
  1487 		if ( $this->can_access_password_content( $post, $request ) ) {
       
  1488 			// Allow access to the post, permissions already checked before.
       
  1489 			add_filter( 'post_password_required', '__return_false' );
       
  1490 
       
  1491 			$has_password_filter = true;
       
  1492 		}
       
  1493 
       
  1494 		if ( in_array( 'content', $fields, true ) ) {
       
  1495 			$data['content'] = array(
       
  1496 				'raw'       => $post->post_content,
       
  1497 				/** This filter is documented in wp-includes/post-template.php */
       
  1498 				'rendered'  => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
       
  1499 				'protected' => (bool) $post->post_password,
       
  1500 			);
       
  1501 		}
       
  1502 
       
  1503 		if ( in_array( 'excerpt', $fields, true ) ) {
       
  1504 			/** This filter is documented in wp-includes/post-template.php */
       
  1505 			$excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
       
  1506 			$data['excerpt'] = array(
       
  1507 				'raw'       => $post->post_excerpt,
       
  1508 				'rendered'  => post_password_required( $post ) ? '' : $excerpt,
       
  1509 				'protected' => (bool) $post->post_password,
       
  1510 			);
       
  1511 		}
       
  1512 
       
  1513 		if ( $has_password_filter ) {
       
  1514 			// Reset filter.
       
  1515 			remove_filter( 'post_password_required', '__return_false' );
       
  1516 		}
       
  1517 
       
  1518 		if ( in_array( 'author', $fields, true ) ) {
       
  1519 			$data['author'] = (int) $post->post_author;
       
  1520 		}
       
  1521 
       
  1522 		if ( in_array( 'featured_media', $fields, true ) ) {
       
  1523 			$data['featured_media'] = (int) get_post_thumbnail_id( $post->ID );
       
  1524 		}
       
  1525 
       
  1526 		if ( in_array( 'parent', $fields, true ) ) {
       
  1527 			$data['parent'] = (int) $post->post_parent;
       
  1528 		}
       
  1529 
       
  1530 		if ( in_array( 'menu_order', $fields, true ) ) {
       
  1531 			$data['menu_order'] = (int) $post->menu_order;
       
  1532 		}
       
  1533 
       
  1534 		if ( in_array( 'comment_status', $fields, true ) ) {
       
  1535 			$data['comment_status'] = $post->comment_status;
       
  1536 		}
       
  1537 
       
  1538 		if ( in_array( 'ping_status', $fields, true ) ) {
       
  1539 			$data['ping_status'] = $post->ping_status;
       
  1540 		}
       
  1541 
       
  1542 		if ( in_array( 'sticky', $fields, true ) ) {
       
  1543 			$data['sticky'] = is_sticky( $post->ID );
       
  1544 		}
       
  1545 
       
  1546 		if ( in_array( 'template', $fields, true ) ) {
       
  1547 			if ( $template = get_page_template_slug( $post->ID ) ) {
       
  1548 				$data['template'] = $template;
       
  1549 			} else {
       
  1550 				$data['template'] = '';
       
  1551 			}
       
  1552 		}
       
  1553 
       
  1554 		if ( in_array( 'format', $fields, true ) ) {
       
  1555 			$data['format'] = get_post_format( $post->ID );
       
  1556 
       
  1557 			// Fill in blank post format.
       
  1558 			if ( empty( $data['format'] ) ) {
       
  1559 				$data['format'] = 'standard';
       
  1560 			}
       
  1561 		}
       
  1562 
       
  1563 		if ( in_array( 'meta', $fields, true ) ) {
       
  1564 			$data['meta'] = $this->meta->get_value( $post->ID, $request );
       
  1565 		}
       
  1566 
       
  1567 		$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
       
  1568 
       
  1569 		foreach ( $taxonomies as $taxonomy ) {
       
  1570 			$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
       
  1571 
       
  1572 			if ( in_array( $base, $fields, true ) ) {
       
  1573 				$terms = get_the_terms( $post, $taxonomy->name );
       
  1574 				$data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
       
  1575 			}
       
  1576 		}
       
  1577 
       
  1578 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
       
  1579 		$data    = $this->add_additional_fields_to_object( $data, $request );
       
  1580 		$data    = $this->filter_response_by_context( $data, $context );
       
  1581 
       
  1582 		// Wrap the data in a response object.
       
  1583 		$response = rest_ensure_response( $data );
       
  1584 
       
  1585 		$links = $this->prepare_links( $post );
       
  1586 		$response->add_links( $links );
       
  1587 
       
  1588 		if ( ! empty( $links['self']['href'] ) ) {
       
  1589 			$actions = $this->get_available_actions( $post, $request );
       
  1590 
       
  1591 			$self = $links['self']['href'];
       
  1592 
       
  1593 			foreach ( $actions as $rel ) {
       
  1594 				$response->add_link( $rel, $self );
       
  1595 			}
       
  1596 		}
       
  1597 
       
  1598 		/**
       
  1599 		 * Filters the post data for a response.
       
  1600 		 *
       
  1601 		 * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
       
  1602 		 *
       
  1603 		 * @since 4.7.0
       
  1604 		 *
       
  1605 		 * @param WP_REST_Response $response The response object.
       
  1606 		 * @param WP_Post          $post     Post object.
       
  1607 		 * @param WP_REST_Request  $request  Request object.
       
  1608 		 */
       
  1609 		return apply_filters( "rest_prepare_{$this->post_type}", $response, $post, $request );
       
  1610 	}
       
  1611 
       
  1612 	/**
       
  1613 	 * Overwrites the default protected title format.
       
  1614 	 *
       
  1615 	 * By default, WordPress will show password protected posts with a title of
       
  1616 	 * "Protected: %s", as the REST API communicates the protected status of a post
       
  1617 	 * in a machine readable format, we remove the "Protected: " prefix.
       
  1618 	 *
       
  1619 	 * @since 4.7.0
       
  1620 	 *
       
  1621 	 * @return string Protected title format.
       
  1622 	 */
       
  1623 	public function protected_title_format() {
       
  1624 		return '%s';
       
  1625 	}
       
  1626 
       
  1627 	/**
       
  1628 	 * Prepares links for the request.
       
  1629 	 *
       
  1630 	 * @since 4.7.0
       
  1631 	 *
       
  1632 	 * @param WP_Post $post Post object.
       
  1633 	 * @return array Links for the given post.
       
  1634 	 */
       
  1635 	protected function prepare_links( $post ) {
       
  1636 		$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
       
  1637 
       
  1638 		// Entity meta.
       
  1639 		$links = array(
       
  1640 			'self' => array(
       
  1641 				'href'   => rest_url( trailingslashit( $base ) . $post->ID ),
       
  1642 			),
       
  1643 			'collection' => array(
       
  1644 				'href'   => rest_url( $base ),
       
  1645 			),
       
  1646 			'about'      => array(
       
  1647 				'href'   => rest_url( 'wp/v2/types/' . $this->post_type ),
       
  1648 			),
       
  1649 		);
       
  1650 
       
  1651 		if ( ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'author' ) )
       
  1652 			&& ! empty( $post->post_author ) ) {
       
  1653 			$links['author'] = array(
       
  1654 				'href'       => rest_url( 'wp/v2/users/' . $post->post_author ),
       
  1655 				'embeddable' => true,
       
  1656 			);
       
  1657 		}
       
  1658 
       
  1659 		if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'comments' ) ) {
       
  1660 			$replies_url = rest_url( 'wp/v2/comments' );
       
  1661 			$replies_url = add_query_arg( 'post', $post->ID, $replies_url );
       
  1662 
       
  1663 			$links['replies'] = array(
       
  1664 				'href'       => $replies_url,
       
  1665 				'embeddable' => true,
       
  1666 			);
       
  1667 		}
       
  1668 
       
  1669 		if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
       
  1670 			$revisions       = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
       
  1671 			$revisions_count = count( $revisions );
       
  1672 
       
  1673 			$links['version-history'] = array(
       
  1674 				'href'  => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
       
  1675 				'count' => $revisions_count,
       
  1676 			);
       
  1677 
       
  1678 			if ( $revisions_count > 0 ) {
       
  1679 				$last_revision = array_shift( $revisions );
       
  1680 
       
  1681 				$links['predecessor-version'] = array(
       
  1682 					'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ),
       
  1683 					'id'   => $last_revision,
       
  1684 				);
       
  1685 			}
       
  1686 
       
  1687 		}
       
  1688 
       
  1689 		$post_type_obj = get_post_type_object( $post->post_type );
       
  1690 
       
  1691 		if ( $post_type_obj->hierarchical && ! empty( $post->post_parent ) ) {
       
  1692 			$links['up'] = array(
       
  1693 				'href'       => rest_url( trailingslashit( $base ) . (int) $post->post_parent ),
       
  1694 				'embeddable' => true,
       
  1695 			);
       
  1696 		}
       
  1697 
       
  1698 		// If we have a featured media, add that.
       
  1699 		if ( $featured_media = get_post_thumbnail_id( $post->ID ) ) {
       
  1700 			$image_url = rest_url( 'wp/v2/media/' . $featured_media );
       
  1701 
       
  1702 			$links['https://api.w.org/featuredmedia'] = array(
       
  1703 				'href'       => $image_url,
       
  1704 				'embeddable' => true,
       
  1705 			);
       
  1706 		}
       
  1707 
       
  1708 		if ( ! in_array( $post->post_type, array( 'attachment', 'nav_menu_item', 'revision' ), true ) ) {
       
  1709 			$attachments_url = rest_url( 'wp/v2/media' );
       
  1710 			$attachments_url = add_query_arg( 'parent', $post->ID, $attachments_url );
       
  1711 
       
  1712 			$links['https://api.w.org/attachment'] = array(
       
  1713 				'href' => $attachments_url,
       
  1714 			);
       
  1715 		}
       
  1716 
       
  1717 		$taxonomies = get_object_taxonomies( $post->post_type );
       
  1718 
       
  1719 		if ( ! empty( $taxonomies ) ) {
       
  1720 			$links['https://api.w.org/term'] = array();
       
  1721 
       
  1722 			foreach ( $taxonomies as $tax ) {
       
  1723 				$taxonomy_obj = get_taxonomy( $tax );
       
  1724 
       
  1725 				// Skip taxonomies that are not public.
       
  1726 				if ( empty( $taxonomy_obj->show_in_rest ) ) {
       
  1727 					continue;
       
  1728 				}
       
  1729 
       
  1730 				$tax_base = ! empty( $taxonomy_obj->rest_base ) ? $taxonomy_obj->rest_base : $tax;
       
  1731 
       
  1732 				$terms_url = add_query_arg(
       
  1733 					'post',
       
  1734 					$post->ID,
       
  1735 					rest_url( 'wp/v2/' . $tax_base )
       
  1736 				);
       
  1737 
       
  1738 				$links['https://api.w.org/term'][] = array(
       
  1739 					'href'       => $terms_url,
       
  1740 					'taxonomy'   => $tax,
       
  1741 					'embeddable' => true,
       
  1742 				);
       
  1743 			}
       
  1744 		}
       
  1745 
       
  1746 		return $links;
       
  1747 	}
       
  1748 
       
  1749 	/**
       
  1750 	 * Get the link relations available for the post and current user.
       
  1751 	 *
       
  1752 	 * @since 4.9.8
       
  1753 	 *
       
  1754 	 * @param WP_Post $post Post object.
       
  1755 	 * @param WP_REST_Request Request object.
       
  1756 	 *
       
  1757 	 * @return array List of link relations.
       
  1758 	 */
       
  1759 	protected function get_available_actions( $post, $request ) {
       
  1760 
       
  1761 		if ( 'edit' !== $request['context'] ) {
       
  1762 			return array();
       
  1763 		}
       
  1764 
       
  1765 		$rels = array();
       
  1766 
       
  1767 		$post_type = get_post_type_object( $post->post_type );
       
  1768 
       
  1769 		if ( 'attachment' !== $this->post_type && current_user_can( $post_type->cap->publish_posts ) ) {
       
  1770 			$rels[] = 'https://api.w.org/action-publish';
       
  1771 		}
       
  1772 
       
  1773 		if ( 'post' === $post_type->name ) {
       
  1774 			if ( current_user_can( $post_type->cap->edit_others_posts ) && current_user_can( $post_type->cap->publish_posts ) ) {
       
  1775 				$rels[] = 'https://api.w.org/action-sticky';
       
  1776 			}
       
  1777 		}
       
  1778 
       
  1779 		if ( post_type_supports( $post_type->name, 'author' ) ) {
       
  1780 			if ( current_user_can( $post_type->cap->edit_others_posts ) ) {
       
  1781 				$rels[] = 'https://api.w.org/action-assign-author';
       
  1782 			}
       
  1783 		}
       
  1784 
       
  1785 		$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
       
  1786 
       
  1787 		foreach ( $taxonomies as $tax ) {
       
  1788 			$tax_base   = ! empty( $tax->rest_base ) ? $tax->rest_base : $tax->name;
       
  1789 			$create_cap = is_taxonomy_hierarchical( $tax->name ) ? $tax->cap->edit_terms : $tax->cap->assign_terms;
       
  1790 
       
  1791 			if ( current_user_can( $create_cap ) ) {
       
  1792 				$rels[] = 'https://api.w.org/action-create-' . $tax_base;
       
  1793 			}
       
  1794 
       
  1795 			if ( current_user_can( $tax->cap->assign_terms ) ) {
       
  1796 				$rels[] = 'https://api.w.org/action-assign-' . $tax_base;
       
  1797 			}
       
  1798 		}
       
  1799 
       
  1800 		return $rels;
       
  1801 	}
       
  1802 
       
  1803 	/**
       
  1804 	 * Retrieves the post's schema, conforming to JSON Schema.
       
  1805 	 *
       
  1806 	 * @since 4.7.0
       
  1807 	 *
       
  1808 	 * @return array Item schema data.
       
  1809 	 */
       
  1810 	public function get_item_schema() {
       
  1811 
       
  1812 		$schema = array(
       
  1813 			'$schema'    => 'http://json-schema.org/draft-04/schema#',
       
  1814 			'title'      => $this->post_type,
       
  1815 			'type'       => 'object',
       
  1816 			// Base properties for every Post.
       
  1817 			'properties' => array(
       
  1818 				'date'            => array(
       
  1819 					'description' => __( "The date the object was published, in the site's timezone." ),
       
  1820 					'type'        => 'string',
       
  1821 					'format'      => 'date-time',
       
  1822 					'context'     => array( 'view', 'edit', 'embed' ),
       
  1823 				),
       
  1824 				'date_gmt'        => array(
       
  1825 					'description' => __( 'The date the object was published, as GMT.' ),
       
  1826 					'type'        => 'string',
       
  1827 					'format'      => 'date-time',
       
  1828 					'context'     => array( 'view', 'edit' ),
       
  1829 				),
       
  1830 				'guid'            => array(
       
  1831 					'description' => __( 'The globally unique identifier for the object.' ),
       
  1832 					'type'        => 'object',
       
  1833 					'context'     => array( 'view', 'edit' ),
       
  1834 					'readonly'    => true,
       
  1835 					'properties'  => array(
       
  1836 						'raw'      => array(
       
  1837 							'description' => __( 'GUID for the object, as it exists in the database.' ),
       
  1838 							'type'        => 'string',
       
  1839 							'context'     => array( 'edit' ),
       
  1840 							'readonly'    => true,
       
  1841 						),
       
  1842 						'rendered' => array(
       
  1843 							'description' => __( 'GUID for the object, transformed for display.' ),
       
  1844 							'type'        => 'string',
       
  1845 							'context'     => array( 'view', 'edit' ),
       
  1846 							'readonly'    => true,
       
  1847 						),
       
  1848 					),
       
  1849 				),
       
  1850 				'id'              => array(
       
  1851 					'description' => __( 'Unique identifier for the object.' ),
       
  1852 					'type'        => 'integer',
       
  1853 					'context'     => array( 'view', 'edit', 'embed' ),
       
  1854 					'readonly'    => true,
       
  1855 				),
       
  1856 				'link'            => array(
       
  1857 					'description' => __( 'URL to the object.' ),
       
  1858 					'type'        => 'string',
       
  1859 					'format'      => 'uri',
       
  1860 					'context'     => array( 'view', 'edit', 'embed' ),
       
  1861 					'readonly'    => true,
       
  1862 				),
       
  1863 				'modified'        => array(
       
  1864 					'description' => __( "The date the object was last modified, in the site's timezone." ),
       
  1865 					'type'        => 'string',
       
  1866 					'format'      => 'date-time',
       
  1867 					'context'     => array( 'view', 'edit' ),
       
  1868 					'readonly'    => true,
       
  1869 				),
       
  1870 				'modified_gmt'    => array(
       
  1871 					'description' => __( 'The date the object was last modified, as GMT.' ),
       
  1872 					'type'        => 'string',
       
  1873 					'format'      => 'date-time',
       
  1874 					'context'     => array( 'view', 'edit' ),
       
  1875 					'readonly'    => true,
       
  1876 				),
       
  1877 				'slug'            => array(
       
  1878 					'description' => __( 'An alphanumeric identifier for the object unique to its type.' ),
       
  1879 					'type'        => 'string',
       
  1880 					'context'     => array( 'view', 'edit', 'embed' ),
       
  1881 					'arg_options' => array(
       
  1882 						'sanitize_callback' => array( $this, 'sanitize_slug' ),
       
  1883 					),
       
  1884 				),
       
  1885 				'status'          => array(
       
  1886 					'description' => __( 'A named status for the object.' ),
       
  1887 					'type'        => 'string',
       
  1888 					'enum'        => array_keys( get_post_stati( array( 'internal' => false ) ) ),
       
  1889 					'context'     => array( 'view', 'edit' ),
       
  1890 				),
       
  1891 				'type'            => array(
       
  1892 					'description' => __( 'Type of Post for the object.' ),
       
  1893 					'type'        => 'string',
       
  1894 					'context'     => array( 'view', 'edit', 'embed' ),
       
  1895 					'readonly'    => true,
       
  1896 				),
       
  1897 				'password'        => array(
       
  1898 					'description' => __( 'A password to protect access to the content and excerpt.' ),
       
  1899 					'type'        => 'string',
       
  1900 					'context'     => array( 'edit' ),
       
  1901 				),
       
  1902 			),
       
  1903 		);
       
  1904 
       
  1905 		$post_type_obj = get_post_type_object( $this->post_type );
       
  1906 
       
  1907 		if ( $post_type_obj->hierarchical ) {
       
  1908 			$schema['properties']['parent'] = array(
       
  1909 				'description' => __( 'The ID for the parent of the object.' ),
       
  1910 				'type'        => 'integer',
       
  1911 				'context'     => array( 'view', 'edit' ),
       
  1912 			);
       
  1913 		}
       
  1914 
       
  1915 		$post_type_attributes = array(
       
  1916 			'title',
       
  1917 			'editor',
       
  1918 			'author',
       
  1919 			'excerpt',
       
  1920 			'thumbnail',
       
  1921 			'comments',
       
  1922 			'revisions',
       
  1923 			'page-attributes',
       
  1924 			'post-formats',
       
  1925 			'custom-fields',
       
  1926 		);
       
  1927 		$fixed_schemas = array(
       
  1928 			'post' => array(
       
  1929 				'title',
       
  1930 				'editor',
       
  1931 				'author',
       
  1932 				'excerpt',
       
  1933 				'thumbnail',
       
  1934 				'comments',
       
  1935 				'revisions',
       
  1936 				'post-formats',
       
  1937 				'custom-fields',
       
  1938 			),
       
  1939 			'page' => array(
       
  1940 				'title',
       
  1941 				'editor',
       
  1942 				'author',
       
  1943 				'excerpt',
       
  1944 				'thumbnail',
       
  1945 				'comments',
       
  1946 				'revisions',
       
  1947 				'page-attributes',
       
  1948 				'custom-fields',
       
  1949 			),
       
  1950 			'attachment' => array(
       
  1951 				'title',
       
  1952 				'author',
       
  1953 				'comments',
       
  1954 				'revisions',
       
  1955 				'custom-fields',
       
  1956 			),
       
  1957 		);
       
  1958 		foreach ( $post_type_attributes as $attribute ) {
       
  1959 			if ( isset( $fixed_schemas[ $this->post_type ] ) && ! in_array( $attribute, $fixed_schemas[ $this->post_type ], true ) ) {
       
  1960 				continue;
       
  1961 			} elseif ( ! isset( $fixed_schemas[ $this->post_type ] ) && ! post_type_supports( $this->post_type, $attribute ) ) {
       
  1962 				continue;
       
  1963 			}
       
  1964 
       
  1965 			switch ( $attribute ) {
       
  1966 
       
  1967 				case 'title':
       
  1968 					$schema['properties']['title'] = array(
       
  1969 						'description' => __( 'The title for the object.' ),
       
  1970 						'type'        => 'object',
       
  1971 						'context'     => array( 'view', 'edit', 'embed' ),
       
  1972 						'arg_options' => array(
       
  1973 							'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database()
       
  1974 							'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database()
       
  1975 						),
       
  1976 						'properties'  => array(
       
  1977 							'raw' => array(
       
  1978 								'description' => __( 'Title for the object, as it exists in the database.' ),
       
  1979 								'type'        => 'string',
       
  1980 								'context'     => array( 'edit' ),
       
  1981 							),
       
  1982 							'rendered' => array(
       
  1983 								'description' => __( 'HTML title for the object, transformed for display.' ),
       
  1984 								'type'        => 'string',
       
  1985 								'context'     => array( 'view', 'edit', 'embed' ),
       
  1986 								'readonly'    => true,
       
  1987 							),
       
  1988 						),
       
  1989 					);
       
  1990 					break;
       
  1991 
       
  1992 				case 'editor':
       
  1993 					$schema['properties']['content'] = array(
       
  1994 						'description' => __( 'The content for the object.' ),
       
  1995 						'type'        => 'object',
       
  1996 						'context'     => array( 'view', 'edit' ),
       
  1997 						'arg_options' => array(
       
  1998 							'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database()
       
  1999 							'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database()
       
  2000 						),
       
  2001 						'properties'  => array(
       
  2002 							'raw' => array(
       
  2003 								'description' => __( 'Content for the object, as it exists in the database.' ),
       
  2004 								'type'        => 'string',
       
  2005 								'context'     => array( 'edit' ),
       
  2006 							),
       
  2007 							'rendered' => array(
       
  2008 								'description' => __( 'HTML content for the object, transformed for display.' ),
       
  2009 								'type'        => 'string',
       
  2010 								'context'     => array( 'view', 'edit' ),
       
  2011 								'readonly'    => true,
       
  2012 							),
       
  2013 							'protected'       => array(
       
  2014 								'description' => __( 'Whether the content is protected with a password.' ),
       
  2015 								'type'        => 'boolean',
       
  2016 								'context'     => array( 'view', 'edit', 'embed' ),
       
  2017 								'readonly'    => true,
       
  2018 							),
       
  2019 						),
       
  2020 					);
       
  2021 					break;
       
  2022 
       
  2023 				case 'author':
       
  2024 					$schema['properties']['author'] = array(
       
  2025 						'description' => __( 'The ID for the author of the object.' ),
       
  2026 						'type'        => 'integer',
       
  2027 						'context'     => array( 'view', 'edit', 'embed' ),
       
  2028 					);
       
  2029 					break;
       
  2030 
       
  2031 				case 'excerpt':
       
  2032 					$schema['properties']['excerpt'] = array(
       
  2033 						'description' => __( 'The excerpt for the object.' ),
       
  2034 						'type'        => 'object',
       
  2035 						'context'     => array( 'view', 'edit', 'embed' ),
       
  2036 						'arg_options' => array(
       
  2037 							'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database()
       
  2038 							'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database()
       
  2039 						),
       
  2040 						'properties'  => array(
       
  2041 							'raw' => array(
       
  2042 								'description' => __( 'Excerpt for the object, as it exists in the database.' ),
       
  2043 								'type'        => 'string',
       
  2044 								'context'     => array( 'edit' ),
       
  2045 							),
       
  2046 							'rendered' => array(
       
  2047 								'description' => __( 'HTML excerpt for the object, transformed for display.' ),
       
  2048 								'type'        => 'string',
       
  2049 								'context'     => array( 'view', 'edit', 'embed' ),
       
  2050 								'readonly'    => true,
       
  2051 							),
       
  2052 							'protected'       => array(
       
  2053 								'description' => __( 'Whether the excerpt is protected with a password.' ),
       
  2054 								'type'        => 'boolean',
       
  2055 								'context'     => array( 'view', 'edit', 'embed' ),
       
  2056 								'readonly'    => true,
       
  2057 							),
       
  2058 						),
       
  2059 					);
       
  2060 					break;
       
  2061 
       
  2062 				case 'thumbnail':
       
  2063 					$schema['properties']['featured_media'] = array(
       
  2064 						'description' => __( 'The ID of the featured media for the object.' ),
       
  2065 						'type'        => 'integer',
       
  2066 						'context'     => array( 'view', 'edit', 'embed' ),
       
  2067 					);
       
  2068 					break;
       
  2069 
       
  2070 				case 'comments':
       
  2071 					$schema['properties']['comment_status'] = array(
       
  2072 						'description' => __( 'Whether or not comments are open on the object.' ),
       
  2073 						'type'        => 'string',
       
  2074 						'enum'        => array( 'open', 'closed' ),
       
  2075 						'context'     => array( 'view', 'edit' ),
       
  2076 					);
       
  2077 					$schema['properties']['ping_status'] = array(
       
  2078 						'description' => __( 'Whether or not the object can be pinged.' ),
       
  2079 						'type'        => 'string',
       
  2080 						'enum'        => array( 'open', 'closed' ),
       
  2081 						'context'     => array( 'view', 'edit' ),
       
  2082 					);
       
  2083 					break;
       
  2084 
       
  2085 				case 'page-attributes':
       
  2086 					$schema['properties']['menu_order'] = array(
       
  2087 						'description' => __( 'The order of the object in relation to other object of its type.' ),
       
  2088 						'type'        => 'integer',
       
  2089 						'context'     => array( 'view', 'edit' ),
       
  2090 					);
       
  2091 					break;
       
  2092 
       
  2093 				case 'post-formats':
       
  2094 					// Get the native post formats and remove the array keys.
       
  2095 					$formats = array_values( get_post_format_slugs() );
       
  2096 
       
  2097 					$schema['properties']['format'] = array(
       
  2098 						'description' => __( 'The format for the object.' ),
       
  2099 						'type'        => 'string',
       
  2100 						'enum'        => $formats,
       
  2101 						'context'     => array( 'view', 'edit' ),
       
  2102 					);
       
  2103 					break;
       
  2104 
       
  2105 				case 'custom-fields':
       
  2106 					$schema['properties']['meta'] = $this->meta->get_field_schema();
       
  2107 					break;
       
  2108 
       
  2109 			}
       
  2110 		}
       
  2111 
       
  2112 		if ( 'post' === $this->post_type ) {
       
  2113 			$schema['properties']['sticky'] = array(
       
  2114 				'description' => __( 'Whether or not the object should be treated as sticky.' ),
       
  2115 				'type'        => 'boolean',
       
  2116 				'context'     => array( 'view', 'edit' ),
       
  2117 			);
       
  2118 		}
       
  2119 
       
  2120 		$schema['properties']['template'] = array(
       
  2121 			'description' => __( 'The theme file to use to display the object.' ),
       
  2122 			'type'        => 'string',
       
  2123 			'context'     => array( 'view', 'edit' ),
       
  2124 			'arg_options' => array(
       
  2125 				'validate_callback' => array( $this, 'check_template' ),
       
  2126 			),
       
  2127 		);
       
  2128 
       
  2129 		$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
       
  2130 		foreach ( $taxonomies as $taxonomy ) {
       
  2131 			$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
       
  2132 			$schema['properties'][ $base ] = array(
       
  2133 				/* translators: %s: taxonomy name */
       
  2134 				'description' => sprintf( __( 'The terms assigned to the object in the %s taxonomy.' ), $taxonomy->name ),
       
  2135 				'type'        => 'array',
       
  2136 				'items'       => array(
       
  2137 					'type'    => 'integer',
       
  2138 				),
       
  2139 				'context'     => array( 'view', 'edit' ),
       
  2140 			);
       
  2141 		}
       
  2142 
       
  2143 		$schema_links = $this->get_schema_links();
       
  2144 
       
  2145 		if ( $schema_links ) {
       
  2146 			$schema['links'] = $schema_links;
       
  2147 		}
       
  2148 
       
  2149 		return $this->add_additional_fields_schema( $schema );
       
  2150 	}
       
  2151 
       
  2152 	/**
       
  2153 	 * Retrieve Link Description Objects that should be added to the Schema for the posts collection.
       
  2154 	 *
       
  2155 	 * @since 4.9.8
       
  2156 	 *
       
  2157 	 * @return array
       
  2158 	 */
       
  2159 	protected function get_schema_links() {
       
  2160 
       
  2161 		$href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" );
       
  2162 
       
  2163 		$links = array();
       
  2164 
       
  2165 		if ( 'attachment' !== $this->post_type ) {
       
  2166 			$links[] = array(
       
  2167 				'rel'          => 'https://api.w.org/action-publish',
       
  2168 				'title'        => __( 'The current user can publish this post.' ),
       
  2169 				'href'         => $href,
       
  2170 				'targetSchema' => array(
       
  2171 					'type'       => 'object',
       
  2172 					'properties' => array(
       
  2173 						'status' => array(
       
  2174 							'type' => 'string',
       
  2175 							'enum' => array( 'publish', 'future' ),
       
  2176 						),
       
  2177 					),
       
  2178 				),
       
  2179 			);
       
  2180 		}
       
  2181 
       
  2182 		if ( 'post' === $this->post_type ) {
       
  2183 			$links[] = array(
       
  2184 				'rel'          => 'https://api.w.org/action-sticky',
       
  2185 				'title'        => __( 'The current user can sticky this post.' ),
       
  2186 				'href'         => $href,
       
  2187 				'targetSchema' => array(
       
  2188 					'type'       => 'object',
       
  2189 					'properties' => array(
       
  2190 						'sticky' => array(
       
  2191 							'type' => 'boolean',
       
  2192 						),
       
  2193 					),
       
  2194 				),
       
  2195 			);
       
  2196 		}
       
  2197 
       
  2198 		if ( post_type_supports( $this->post_type, 'author' ) ) {
       
  2199 			$links[] = array(
       
  2200 				'rel'          => 'https://api.w.org/action-assign-author',
       
  2201 				'title'        => __( 'The current user can change the author on this post.' ),
       
  2202 				'href'         => $href,
       
  2203 				'targetSchema' => array(
       
  2204 					'type'       => 'object',
       
  2205 					'properties' => array(
       
  2206 						'author' => array(
       
  2207 							'type' => 'integer',
       
  2208 						),
       
  2209 					),
       
  2210 				),
       
  2211 			);
       
  2212 		}
       
  2213 
       
  2214 		$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
       
  2215 
       
  2216 		foreach ( $taxonomies as $tax ) {
       
  2217 			$tax_base = ! empty( $tax->rest_base ) ? $tax->rest_base : $tax->name;
       
  2218 
       
  2219 			/* translators: %s: taxonomy name */
       
  2220 			$assign_title = sprintf( __( 'The current user can assign terms in the %s taxonomy.' ), $tax->name );
       
  2221 			/* translators: %s: taxonomy name */
       
  2222 			$create_title = sprintf( __( 'The current user can create terms in the %s taxonomy.' ), $tax->name );
       
  2223 
       
  2224 			$links[] = array(
       
  2225 				'rel'          => 'https://api.w.org/action-assign-' . $tax_base,
       
  2226 				'title'        => $assign_title,
       
  2227 				'href'         => $href,
       
  2228 				'targetSchema' => array(
       
  2229 					'type'       => 'object',
       
  2230 					'properties' => array(
       
  2231 						$tax_base => array(
       
  2232 							'type'  => 'array',
       
  2233 							'items' => array(
       
  2234 								'type' => 'integer',
       
  2235 							),
       
  2236 						),
       
  2237 					),
       
  2238 				),
       
  2239 			);
       
  2240 
       
  2241 			$links[] = array(
       
  2242 				'rel'          => 'https://api.w.org/action-create-' . $tax_base,
       
  2243 				'title'        => $create_title,
       
  2244 				'href'         => $href,
       
  2245 				'targetSchema' => array(
       
  2246 					'type'       => 'object',
       
  2247 					'properties' => array(
       
  2248 						$tax_base => array(
       
  2249 							'type'  => 'array',
       
  2250 							'items' => array(
       
  2251 								'type' => 'integer',
       
  2252 							),
       
  2253 						),
       
  2254 					),
       
  2255 				),
       
  2256 			);
       
  2257 		}
       
  2258 
       
  2259 		return $links;
       
  2260 	}
       
  2261 
       
  2262 	/**
       
  2263 	 * Retrieves the query params for the posts collection.
       
  2264 	 *
       
  2265 	 * @since 4.7.0
       
  2266 	 *
       
  2267 	 * @return array Collection parameters.
       
  2268 	 */
       
  2269 	public function get_collection_params() {
       
  2270 		$query_params = parent::get_collection_params();
       
  2271 
       
  2272 		$query_params['context']['default'] = 'view';
       
  2273 
       
  2274 		$query_params['after'] = array(
       
  2275 			'description'        => __( 'Limit response to posts published after a given ISO8601 compliant date.' ),
       
  2276 			'type'               => 'string',
       
  2277 			'format'             => 'date-time',
       
  2278 		);
       
  2279 
       
  2280 		if ( post_type_supports( $this->post_type, 'author' ) ) {
       
  2281 			$query_params['author'] = array(
       
  2282 				'description'         => __( 'Limit result set to posts assigned to specific authors.' ),
       
  2283 				'type'                => 'array',
       
  2284 				'items'               => array(
       
  2285 					'type'            => 'integer',
       
  2286 				),
       
  2287 				'default'             => array(),
       
  2288 			);
       
  2289 			$query_params['author_exclude'] = array(
       
  2290 				'description'         => __( 'Ensure result set excludes posts assigned to specific authors.' ),
       
  2291 				'type'                => 'array',
       
  2292 				'items'               => array(
       
  2293 					'type'            => 'integer',
       
  2294 				),
       
  2295 				'default'             => array(),
       
  2296 			);
       
  2297 		}
       
  2298 
       
  2299 		$query_params['before'] = array(
       
  2300 			'description'        => __( 'Limit response to posts published before a given ISO8601 compliant date.' ),
       
  2301 			'type'               => 'string',
       
  2302 			'format'             => 'date-time',
       
  2303 		);
       
  2304 
       
  2305 		$query_params['exclude'] = array(
       
  2306 			'description'        => __( 'Ensure result set excludes specific IDs.' ),
       
  2307 			'type'               => 'array',
       
  2308 			'items'              => array(
       
  2309 				'type'           => 'integer',
       
  2310 			),
       
  2311 			'default'            => array(),
       
  2312 		);
       
  2313 
       
  2314 		$query_params['include'] = array(
       
  2315 			'description'        => __( 'Limit result set to specific IDs.' ),
       
  2316 			'type'               => 'array',
       
  2317 			'items'              => array(
       
  2318 				'type'           => 'integer',
       
  2319 			),
       
  2320 			'default'            => array(),
       
  2321 		);
       
  2322 
       
  2323 		if ( 'page' === $this->post_type || post_type_supports( $this->post_type, 'page-attributes' ) ) {
       
  2324 			$query_params['menu_order'] = array(
       
  2325 				'description'        => __( 'Limit result set to posts with a specific menu_order value.' ),
       
  2326 				'type'               => 'integer',
       
  2327 			);
       
  2328 		}
       
  2329 
       
  2330 		$query_params['offset'] = array(
       
  2331 			'description'        => __( 'Offset the result set by a specific number of items.' ),
       
  2332 			'type'               => 'integer',
       
  2333 		);
       
  2334 
       
  2335 		$query_params['order'] = array(
       
  2336 			'description'        => __( 'Order sort attribute ascending or descending.' ),
       
  2337 			'type'               => 'string',
       
  2338 			'default'            => 'desc',
       
  2339 			'enum'               => array( 'asc', 'desc' ),
       
  2340 		);
       
  2341 
       
  2342 		$query_params['orderby'] = array(
       
  2343 			'description'        => __( 'Sort collection by object attribute.' ),
       
  2344 			'type'               => 'string',
       
  2345 			'default'            => 'date',
       
  2346 			'enum'               => array(
       
  2347 				'author',
       
  2348 				'date',
       
  2349 				'id',
       
  2350 				'include',
       
  2351 				'modified',
       
  2352 				'parent',
       
  2353 				'relevance',
       
  2354 				'slug',
       
  2355 				'include_slugs',
       
  2356 				'title',
       
  2357 			),
       
  2358 		);
       
  2359 
       
  2360 		if ( 'page' === $this->post_type || post_type_supports( $this->post_type, 'page-attributes' ) ) {
       
  2361 			$query_params['orderby']['enum'][] = 'menu_order';
       
  2362 		}
       
  2363 
       
  2364 		$post_type = get_post_type_object( $this->post_type );
       
  2365 
       
  2366 		if ( $post_type->hierarchical || 'attachment' === $this->post_type ) {
       
  2367 			$query_params['parent'] = array(
       
  2368 				'description'       => __( 'Limit result set to items with particular parent IDs.' ),
       
  2369 				'type'              => 'array',
       
  2370 				'items'             => array(
       
  2371 					'type'          => 'integer',
       
  2372 				),
       
  2373 				'default'           => array(),
       
  2374 			);
       
  2375 			$query_params['parent_exclude'] = array(
       
  2376 				'description'       => __( 'Limit result set to all items except those of a particular parent ID.' ),
       
  2377 				'type'              => 'array',
       
  2378 				'items'             => array(
       
  2379 					'type'          => 'integer',
       
  2380 				),
       
  2381 				'default'           => array(),
       
  2382 			);
       
  2383 		}
       
  2384 
       
  2385 		$query_params['slug'] = array(
       
  2386 			'description'       => __( 'Limit result set to posts with one or more specific slugs.' ),
       
  2387 			'type'              => 'array',
       
  2388 			'items'             => array(
       
  2389 				'type'          => 'string',
       
  2390 			),
       
  2391 			'sanitize_callback' => 'wp_parse_slug_list',
       
  2392 		);
       
  2393 
       
  2394 		$query_params['status'] = array(
       
  2395 			'default'           => 'publish',
       
  2396 			'description'       => __( 'Limit result set to posts assigned one or more statuses.' ),
       
  2397 			'type'              => 'array',
       
  2398 			'items'             => array(
       
  2399 				'enum'          => array_merge( array_keys( get_post_stati() ), array( 'any' ) ),
       
  2400 				'type'          => 'string',
       
  2401 			),
       
  2402 			'sanitize_callback' => array( $this, 'sanitize_post_statuses' ),
       
  2403 		);
       
  2404 
       
  2405 		$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
       
  2406 
       
  2407 		foreach ( $taxonomies as $taxonomy ) {
       
  2408 			$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
       
  2409 
       
  2410 			$query_params[ $base ] = array(
       
  2411 				/* translators: %s: taxonomy name */
       
  2412 				'description'       => sprintf( __( 'Limit result set to all items that have the specified term assigned in the %s taxonomy.' ), $base ),
       
  2413 				'type'              => 'array',
       
  2414 				'items'             => array(
       
  2415 					'type'          => 'integer',
       
  2416 				),
       
  2417 				'default'           => array(),
       
  2418 			);
       
  2419 
       
  2420 			$query_params[ $base . '_exclude' ] = array(
       
  2421 				/* translators: %s: taxonomy name */
       
  2422 				'description' => sprintf( __( 'Limit result set to all items except those that have the specified term assigned in the %s taxonomy.' ), $base ),
       
  2423 				'type'        => 'array',
       
  2424 				'items'       => array(
       
  2425 					'type'    => 'integer',
       
  2426 				),
       
  2427 				'default'           => array(),
       
  2428 			);
       
  2429 		}
       
  2430 
       
  2431 		if ( 'post' === $this->post_type ) {
       
  2432 			$query_params['sticky'] = array(
       
  2433 				'description'       => __( 'Limit result set to items that are sticky.' ),
       
  2434 				'type'              => 'boolean',
       
  2435 			);
       
  2436 		}
       
  2437 
       
  2438 		/**
       
  2439 		 * Filter collection parameters for the posts controller.
       
  2440 		 *
       
  2441 		 * The dynamic part of the filter `$this->post_type` refers to the post
       
  2442 		 * type slug for the controller.
       
  2443 		 *
       
  2444 		 * This filter registers the collection parameter, but does not map the
       
  2445 		 * collection parameter to an internal WP_Query parameter. Use the
       
  2446 		 * `rest_{$this->post_type}_query` filter to set WP_Query parameters.
       
  2447 		 *
       
  2448 		 * @since 4.7.0
       
  2449 		 *
       
  2450 		 * @param array        $query_params JSON Schema-formatted collection parameters.
       
  2451 		 * @param WP_Post_Type $post_type    Post type object.
       
  2452 		 */
       
  2453 		return apply_filters( "rest_{$this->post_type}_collection_params", $query_params, $post_type );
       
  2454 	}
       
  2455 
       
  2456 	/**
       
  2457 	 * Sanitizes and validates the list of post statuses, including whether the
       
  2458 	 * user can query private statuses.
       
  2459 	 *
       
  2460 	 * @since 4.7.0
       
  2461 	 *
       
  2462 	 * @param  string|array    $statuses  One or more post statuses.
       
  2463 	 * @param  WP_REST_Request $request   Full details about the request.
       
  2464 	 * @param  string          $parameter Additional parameter to pass to validation.
       
  2465 	 * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
       
  2466 	 */
       
  2467 	public function sanitize_post_statuses( $statuses, $request, $parameter ) {
       
  2468 		$statuses = wp_parse_slug_list( $statuses );
       
  2469 
       
  2470 		// The default status is different in WP_REST_Attachments_Controller
       
  2471 		$attributes = $request->get_attributes();
       
  2472 		$default_status = $attributes['args']['status']['default'];
       
  2473 
       
  2474 		foreach ( $statuses as $status ) {
       
  2475 			if ( $status === $default_status ) {
       
  2476 				continue;
       
  2477 			}
       
  2478 
       
  2479 			$post_type_obj = get_post_type_object( $this->post_type );
       
  2480 
       
  2481 			if ( current_user_can( $post_type_obj->cap->edit_posts ) ) {
       
  2482 				$result = rest_validate_request_arg( $status, $request, $parameter );
       
  2483 				if ( is_wp_error( $result ) ) {
       
  2484 					return $result;
       
  2485 				}
       
  2486 			} else {
       
  2487 				return new WP_Error( 'rest_forbidden_status', __( 'Status is forbidden.' ), array( 'status' => rest_authorization_required_code() ) );
       
  2488 			}
       
  2489 		}
       
  2490 
       
  2491 		return $statuses;
       
  2492 	}
       
  2493 }