WP_REST_Widget_Types_Controller::prepare_item_for_response( array $item, WP_REST_Request $request ): WP_REST_Response

Prepares a widget type object for serialization.

Parameters

$itemarrayrequired
Widget type data.
$requestWP_REST_Requestrequired
Full details about the request.

Return

WP_REST_Response Widget type data.

Source

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

	$fields = $this->get_fields_for_response( $request );
	$data   = array(
		'id' => $widget_type['id'],
	);

	$schema       = $this->get_item_schema();
	$extra_fields = array(
		'name',
		'description',
		'is_multi',
		'classname',
		'widget_class',
		'option_name',
		'customize_selective_refresh',
	);

	foreach ( $extra_fields as $extra_field ) {
		if ( ! rest_is_field_included( $extra_field, $fields ) ) {
			continue;
		}

		if ( isset( $widget_type[ $extra_field ] ) ) {
			$field = $widget_type[ $extra_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 ] );
	}

	$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( $widget_type ) );
	}

	/**
	 * Filters the REST API response for a widget type.
	 *
	 * @since 5.8.0
	 *
	 * @param WP_REST_Response $response    The response object.
	 * @param array            $widget_type The array of widget data.
	 * @param WP_REST_Request  $request     The request object.
	 */
	return apply_filters( 'rest_prepare_widget_type', $response, $widget_type, $request );
}

Hooks

apply_filters( ‘rest_prepare_widget_type’, WP_REST_Response $response, array $widget_type, WP_REST_Request $request )

Filters the REST API response for a widget type.

Changelog

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

User Contributed Notes

You must log in before being able to contribute a note or feedback.