wp/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    79 	/**
    79 	/**
    80 	 * Search and retrieve block patterns metadata
    80 	 * Search and retrieve block patterns metadata
    81 	 *
    81 	 *
    82 	 * @since 5.8.0
    82 	 * @since 5.8.0
    83 	 * @since 6.0.0 Added 'slug' to request.
    83 	 * @since 6.0.0 Added 'slug' to request.
       
    84 	 * @since 6.2.0 Added 'per_page', 'page', 'offset', 'order', and 'orderby' to request.
    84 	 *
    85 	 *
    85 	 * @param WP_REST_Request $request Full details about the request.
    86 	 * @param WP_REST_Request $request Full details about the request.
    86 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
    87 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
    87 	 */
    88 	 */
    88 	public function get_items( $request ) {
    89 	public function get_items( $request ) {
    91 		 * it. Some plugins modify the version in a misguided attempt to improve security by
    92 		 * it. Some plugins modify the version in a misguided attempt to improve security by
    92 		 * obscuring the version, which can cause invalid requests.
    93 		 * obscuring the version, which can cause invalid requests.
    93 		 */
    94 		 */
    94 		require ABSPATH . WPINC . '/version.php';
    95 		require ABSPATH . WPINC . '/version.php';
    95 
    96 
    96 		$query_args = array(
    97 		$valid_query_args = array(
    97 			'locale'     => get_user_locale(),
    98 			'offset'   => true,
    98 			'wp-version' => $wp_version,
    99 			'order'    => true,
    99 		);
   100 			'orderby'  => true,
   100 
   101 			'page'     => true,
   101 		$category_id = $request['category'];
   102 			'per_page' => true,
   102 		$keyword_id  = $request['keyword'];
   103 			'search'   => true,
   103 		$search_term = $request['search'];
   104 			'slug'     => true,
   104 		$slug        = $request['slug'];
   105 		);
   105 
   106 		$query_args       = array_intersect_key( $request->get_params(), $valid_query_args );
   106 		if ( $category_id ) {
   107 
   107 			$query_args['pattern-categories'] = $category_id;
   108 		$query_args['locale']             = get_user_locale();
   108 		}
   109 		$query_args['wp-version']         = $wp_version;
   109 
   110 		$query_args['pattern-categories'] = isset( $request['category'] ) ? $request['category'] : false;
   110 		if ( $keyword_id ) {
   111 		$query_args['pattern-keywords']   = isset( $request['keyword'] ) ? $request['keyword'] : false;
   111 			$query_args['pattern-keywords'] = $keyword_id;
   112 
   112 		}
   113 		$query_args = array_filter( $query_args );
   113 
       
   114 		if ( $search_term ) {
       
   115 			$query_args['search'] = $search_term;
       
   116 		}
       
   117 
       
   118 		if ( $slug ) {
       
   119 			$query_args['slug'] = $slug;
       
   120 		}
       
   121 
   114 
   122 		$transient_key = $this->get_transient_key( $query_args );
   115 		$transient_key = $this->get_transient_key( $query_args );
   123 
   116 
   124 		/*
   117 		/*
   125 		 * Use network-wide transient to improve performance. The locale is the only site
   118 		 * Use network-wide transient to improve performance. The locale is the only site
   198 	 * @param WP_REST_Request $request Request object.
   191 	 * @param WP_REST_Request $request Request object.
   199 	 * @return WP_REST_Response
   192 	 * @return WP_REST_Response
   200 	 */
   193 	 */
   201 	public function prepare_item_for_response( $item, $request ) {
   194 	public function prepare_item_for_response( $item, $request ) {
   202 		// Restores the more descriptive, specific name for use within this method.
   195 		// Restores the more descriptive, specific name for use within this method.
   203 		$raw_pattern      = $item;
   196 		$raw_pattern = $item;
       
   197 
   204 		$prepared_pattern = array(
   198 		$prepared_pattern = array(
   205 			'id'             => absint( $raw_pattern->id ),
   199 			'id'             => absint( $raw_pattern->id ),
   206 			'title'          => sanitize_text_field( $raw_pattern->title->rendered ),
   200 			'title'          => sanitize_text_field( $raw_pattern->title->rendered ),
   207 			'content'        => wp_kses_post( $raw_pattern->pattern_content ),
   201 			'content'        => wp_kses_post( $raw_pattern->pattern_content ),
   208 			'categories'     => array_map( 'sanitize_title', $raw_pattern->category_slugs ),
   202 			'categories'     => array_map( 'sanitize_title', $raw_pattern->category_slugs ),
   209 			'keywords'       => array_map( 'sanitize_text_field', explode( ',', $raw_pattern->meta->wpop_keywords ) ),
   203 			'keywords'       => array_map( 'sanitize_text_field', explode( ',', $raw_pattern->meta->wpop_keywords ) ),
   210 			'description'    => sanitize_text_field( $raw_pattern->meta->wpop_description ),
   204 			'description'    => sanitize_text_field( $raw_pattern->meta->wpop_description ),
   211 			'viewport_width' => absint( $raw_pattern->meta->wpop_viewport_width ),
   205 			'viewport_width' => absint( $raw_pattern->meta->wpop_viewport_width ),
       
   206 			'block_types'    => array_map( 'sanitize_text_field', $raw_pattern->meta->wpop_block_types ),
   212 		);
   207 		);
   213 
   208 
   214 		$prepared_pattern = $this->add_additional_fields_to_object( $prepared_pattern, $request );
   209 		$prepared_pattern = $this->add_additional_fields_to_object( $prepared_pattern, $request );
   215 
   210 
   216 		$response = new WP_REST_Response( $prepared_pattern );
   211 		$response = new WP_REST_Response( $prepared_pattern );
   229 
   224 
   230 	/**
   225 	/**
   231 	 * Retrieves the block pattern's schema, conforming to JSON Schema.
   226 	 * Retrieves the block pattern's schema, conforming to JSON Schema.
   232 	 *
   227 	 *
   233 	 * @since 5.8.0
   228 	 * @since 5.8.0
       
   229 	 * @since 6.2.0 Added `'block_types'` to schema.
   234 	 *
   230 	 *
   235 	 * @return array Item schema data.
   231 	 * @return array Item schema data.
   236 	 */
   232 	 */
   237 	public function get_item_schema() {
   233 	public function get_item_schema() {
   238 		if ( $this->schema ) {
   234 		if ( $this->schema ) {
   291 				'viewport_width' => array(
   287 				'viewport_width' => array(
   292 					'description' => __( 'The preferred width of the viewport when previewing a pattern, in pixels.' ),
   288 					'description' => __( 'The preferred width of the viewport when previewing a pattern, in pixels.' ),
   293 					'type'        => 'integer',
   289 					'type'        => 'integer',
   294 					'context'     => array( 'view', 'edit', 'embed' ),
   290 					'context'     => array( 'view', 'edit', 'embed' ),
   295 				),
   291 				),
       
   292 
       
   293 				'block_types'    => array(
       
   294 					'description' => __( 'The block types which can use this pattern.' ),
       
   295 					'type'        => 'array',
       
   296 					'uniqueItems' => true,
       
   297 					'items'       => array( 'type' => 'string' ),
       
   298 					'context'     => array( 'view', 'embed' ),
       
   299 				),
   296 			),
   300 			),
   297 		);
   301 		);
   298 
   302 
   299 		return $this->add_additional_fields_schema( $this->schema );
   303 		return $this->add_additional_fields_schema( $this->schema );
   300 	}
   304 	}
   301 
   305 
   302 	/**
   306 	/**
   303 	 * Retrieves the search parameters for the block pattern's collection.
   307 	 * Retrieves the search parameters for the block pattern's collection.
   304 	 *
   308 	 *
   305 	 * @since 5.8.0
   309 	 * @since 5.8.0
       
   310 	 * @since 6.2.0 Added 'per_page', 'page', 'offset', 'order', and 'orderby' to request.
   306 	 *
   311 	 *
   307 	 * @return array Collection parameters.
   312 	 * @return array Collection parameters.
   308 	 */
   313 	 */
   309 	public function get_collection_params() {
   314 	public function get_collection_params() {
   310 		$query_params = parent::get_collection_params();
   315 		$query_params = parent::get_collection_params();
   311 
   316 
   312 		// Pagination is not supported.
   317 		$query_params['per_page']['default'] = 100;
   313 		unset( $query_params['page'] );
       
   314 		unset( $query_params['per_page'] );
       
   315 
       
   316 		$query_params['search']['minLength'] = 1;
   318 		$query_params['search']['minLength'] = 1;
   317 		$query_params['context']['default']  = 'view';
   319 		$query_params['context']['default']  = 'view';
   318 
   320 
   319 		$query_params['category'] = array(
   321 		$query_params['category'] = array(
   320 			'description' => __( 'Limit results to those matching a category ID.' ),
   322 			'description' => __( 'Limit results to those matching a category ID.' ),
   329 		);
   331 		);
   330 
   332 
   331 		$query_params['slug'] = array(
   333 		$query_params['slug'] = array(
   332 			'description' => __( 'Limit results to those matching a pattern (slug).' ),
   334 			'description' => __( 'Limit results to those matching a pattern (slug).' ),
   333 			'type'        => 'array',
   335 			'type'        => 'array',
       
   336 		);
       
   337 
       
   338 		$query_params['offset'] = array(
       
   339 			'description' => __( 'Offset the result set by a specific number of items.' ),
       
   340 			'type'        => 'integer',
       
   341 		);
       
   342 
       
   343 		$query_params['order'] = array(
       
   344 			'description' => __( 'Order sort attribute ascending or descending.' ),
       
   345 			'type'        => 'string',
       
   346 			'default'     => 'desc',
       
   347 			'enum'        => array( 'asc', 'desc' ),
       
   348 		);
       
   349 
       
   350 		$query_params['orderby'] = array(
       
   351 			'description' => __( 'Sort collection by post attribute.' ),
       
   352 			'type'        => 'string',
       
   353 			'default'     => 'date',
       
   354 			'enum'        => array(
       
   355 				'author',
       
   356 				'date',
       
   357 				'id',
       
   358 				'include',
       
   359 				'modified',
       
   360 				'parent',
       
   361 				'relevance',
       
   362 				'slug',
       
   363 				'include_slugs',
       
   364 				'title',
       
   365 				'favorite_count',
       
   366 			),
   334 		);
   367 		);
   335 
   368 
   336 		/**
   369 		/**
   337 		 * Filter collection parameters for the block pattern directory controller.
   370 		 * Filter collection parameters for the block pattern directory controller.
   338 		 *
   371 		 *