WP_REST_Block_Types_Controller::prepare_item_for_response()publicWP 5.5.0

Prepares a block type object for serialization.

Метод класса: WP_REST_Block_Types_Controller{}

Хуки из метода

Возвращает

WP_REST_Response. Block type data.

Использование

$WP_REST_Block_Types_Controller = new WP_REST_Block_Types_Controller();
$WP_REST_Block_Types_Controller->prepare_item_for_response( $item, $request );
$item(WP_Block_Type) (обязательный)
Block type data.
$request(WP_REST_Request) (обязательный)
Full details about the request.

Список изменений

С версии 5.5.0 Введена.
С версии 5.9.0 Renamed $block_type to $item to match parent class for PHP 8 named parameter support.
С версии 6.3.0 Added selectors field.
С версии 6.5.0 Added view_script_module_ids field.

Код WP_REST_Block_Types_Controller::prepare_item_for_response() WP 6.6.2

public function prepare_item_for_response( $item, $request ) {
	// Restores the more descriptive, specific name for use within this method.
	$block_type = $item;

	$fields = $this->get_fields_for_response( $request );
	$data   = array();

	if ( rest_is_field_included( 'attributes', $fields ) ) {
		$data['attributes'] = $block_type->get_attributes();
	}

	if ( rest_is_field_included( 'is_dynamic', $fields ) ) {
		$data['is_dynamic'] = $block_type->is_dynamic();
	}

	$schema = $this->get_item_schema();
	// Fields deprecated in WordPress 6.1, but left in the schema for backwards compatibility.
	$deprecated_fields = array(
		'editor_script',
		'script',
		'view_script',
		'editor_style',
		'style',
	);
	$extra_fields      = array_merge(
		array(
			'api_version',
			'name',
			'title',
			'description',
			'icon',
			'category',
			'keywords',
			'parent',
			'ancestor',
			'allowed_blocks',
			'provides_context',
			'uses_context',
			'selectors',
			'supports',
			'styles',
			'textdomain',
			'example',
			'editor_script_handles',
			'script_handles',
			'view_script_handles',
			'view_script_module_ids',
			'editor_style_handles',
			'style_handles',
			'view_style_handles',
			'variations',
			'block_hooks',
		),
		$deprecated_fields
	);
	foreach ( $extra_fields as $extra_field ) {
		if ( rest_is_field_included( $extra_field, $fields ) ) {
			if ( isset( $block_type->$extra_field ) ) {
				$field = $block_type->$extra_field;
				if ( in_array( $extra_field, $deprecated_fields, true ) && is_array( $field ) ) {
					// Since the schema only allows strings or null (but no arrays), we return the first array item.
					$field = ! empty( $field ) ? array_shift( $field ) : '';
				}
			} elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) {
				$field = $schema['properties'][ $extra_field ]['default'];
			} else {
				$field = '';
			}
			$data[ $extra_field ] = rest_sanitize_value_from_schema( $field, $schema['properties'][ $extra_field ] );
		}
	}

	if ( rest_is_field_included( 'styles', $fields ) ) {
		$styles         = $this->style_registry->get_registered_styles_for_block( $block_type->name );
		$styles         = array_values( $styles );
		$data['styles'] = wp_parse_args( $styles, $data['styles'] );
		$data['styles'] = array_filter( $data['styles'] );
	}

	$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
	$data    = $this->add_additional_fields_to_object( $data, $request );
	$data    = $this->filter_response_by_context( $data, $context );

	$response = rest_ensure_response( $data );

	if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
		$response->add_links( $this->prepare_links( $block_type ) );
	}

	/**
	 * Filters a block type returned from the REST API.
	 *
	 * Allows modification of the block type data right before it is returned.
	 *
	 * @since 5.5.0
	 *
	 * @param WP_REST_Response $response   The response object.
	 * @param WP_Block_Type    $block_type The original block type object.
	 * @param WP_REST_Request  $request    Request used to generate the response.
	 */
	return apply_filters( 'rest_prepare_block_type', $response, $block_type, $request );
}