WordPress at Your Fingertips
function is not described

WP_REST_Widget_Types_Controller::prepare_item_for_response() public WP 5.8.0

Prepares a widget type object for serialization.

{} It's a method of the class: WP_REST_Widget_Types_Controller{}

Hooks from the method

Return

WP_REST_Response. Widget type data.

Usage

$WP_REST_Widget_Types_Controller = new WP_REST_Widget_Types_Controller();
$WP_REST_Widget_Types_Controller->prepare_item_for_response( $widget_type, $request );
$widget_type(array) (required)
Widget type data.
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 5.8.0 Introduced.

Code of WP_REST_Widget_Types_Controller::prepare_item_for_response() WP 5.8.1

public function prepare_item_for_response( $widget_type, $request ) {
	$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 );

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