WP_REST_Widget_Types_Controller::prepare_item_for_response()
Prepares a widget type object for serialization.
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( $item, $request );
- $item(array) (required)
- Widget type data.
- $request(WP_REST_Request) (required)
- Full details about the request.
Changelog
Since 5.8.0 | Introduced. |
Since 5.9.0 | Renamed $widget_type to $item to match parent class for PHP 8 named parameter support. |
WP_REST_Widget_Types_Controller::prepare_item_for_response() WP REST Widget Types Controller::prepare item for response code WP 6.7.1
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 ); }