WP_REST_Pattern_Directory_Controller::prepare_item_for_response() WordPress Method

The WP_REST_Pattern_Directory_Controller::prepare_item_for_response() method is used to prepare an individual item for a response. This is done by setting the item's data member to an array of values, which are then serialized by the controller's prepare_response() method.

WP_REST_Pattern_Directory_Controller::prepare_item_for_response( object $item, WP_REST_Request $request ) #

Prepare a raw block pattern before it gets output in a REST API response.


Parameters

$item

(object)(Required)Raw pattern from api.wordpress.org, before any changes.

$request

(WP_REST_Request)(Required)Request object.


Top ↑

Return

(WP_REST_Response)


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php

	public function prepare_item_for_response( $item, $request ) {
		// Restores the more descriptive, specific name for use within this method.
		$raw_pattern      = $item;
		$prepared_pattern = array(
			'id'             => absint( $raw_pattern->id ),
			'title'          => sanitize_text_field( $raw_pattern->title->rendered ),
			'content'        => wp_kses_post( $raw_pattern->pattern_content ),
			'categories'     => array_map( 'sanitize_title', $raw_pattern->category_slugs ),
			'keywords'       => array_map( 'sanitize_title', $raw_pattern->keyword_slugs ),
			'description'    => sanitize_text_field( $raw_pattern->meta->wpop_description ),
			'viewport_width' => absint( $raw_pattern->meta->wpop_viewport_width ),
		);

		$prepared_pattern = $this->add_additional_fields_to_object( $prepared_pattern, $request );

		$response = new WP_REST_Response( $prepared_pattern );

		/**
		 * Filters the REST API response for a block pattern.
		 *
		 * @since 5.8.0
		 *
		 * @param WP_REST_Response $response    The response object.
		 * @param object           $raw_pattern The unprepared block pattern.
		 * @param WP_REST_Request  $request     The request object.
		 */
		return apply_filters( 'rest_prepare_block_pattern', $response, $raw_pattern, $request );
	}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Renamed $raw_pattern to $item to match parent class for PHP 8 named parameter support.
5.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.