Prepare a raw block pattern before it gets output in a REST API response.
Parameters
$item
arrayrequired- Raw pattern as registered, before any changes.
$request
WP_REST_Requestrequired- Request object.
Source
public function prepare_item_for_response( $item, $request ) {
// Resolve pattern blocks so they don't need to be resolved client-side
// in the editor, improving performance.
$blocks = parse_blocks( $item['content'] );
$blocks = resolve_pattern_blocks( $blocks );
$item['content'] = serialize_blocks( $blocks );
$fields = $this->get_fields_for_response( $request );
$keys = array(
'name' => 'name',
'title' => 'title',
'content' => 'content',
'description' => 'description',
'viewportWidth' => 'viewport_width',
'inserter' => 'inserter',
'categories' => 'categories',
'keywords' => 'keywords',
'blockTypes' => 'block_types',
'postTypes' => 'post_types',
'templateTypes' => 'template_types',
'source' => 'source',
);
$data = array();
foreach ( $keys as $item_key => $rest_key ) {
if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) {
$data[ $rest_key ] = $item[ $item_key ];
}
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
return rest_ensure_response( $data );
}
User Contributed Notes
You must log in before being able to contribute a note or feedback.