Prepare a raw block pattern before it gets output in a REST API response.
Parameters
$item
objectrequired- Raw pattern from api.wordpress.org, before any changes.
$request
WP_REST_Requestrequired- Request object.
Source
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_text_field', explode( ',', $raw_pattern->meta->wpop_keywords ) ),
'description' => sanitize_text_field( $raw_pattern->meta->wpop_description ),
'viewport_width' => absint( $raw_pattern->meta->wpop_viewport_width ),
'block_types' => array_map( 'sanitize_text_field', $raw_pattern->meta->wpop_block_types ),
);
$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 );
}
Hooks
- apply_filters( ‘rest_prepare_block_pattern’,
WP_REST_Response $response ,object $raw_pattern ,WP_REST_Request $request ) Filters the REST API response for a block pattern.
User Contributed Notes
You must log in before being able to contribute a note or feedback.