WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
функция не описана

WP_REST_Widgets_Controller::prepare_item_for_response() public WP 5.8.0

Prepares the widget for the REST response.

{} Это метод класса: WP_REST_Widgets_Controller{}

Хуки из метода
Возвращает

WP_REST_Response|WP_Error. Response object on success, or WP_Error object on failure.

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

$WP_REST_Widgets_Controller = new WP_REST_Widgets_Controller();
$WP_REST_Widgets_Controller->prepare_item_for_response( $item, $request );
$item(массив) (обязательный)
An array containing a widget_id and sidebar_id.
$request(WP_REST_Request) (обязательный)
Request object.

Заметки

  • Global. WP_Widget_Factory. $wp_widget_factory
  • Global. Массив. $wp_registered_widgets The registered widgets.

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

С версии 5.8.0 Введена.

Код WP_REST_Widgets_Controller::prepare_item_for_response() WP 5.8.1

public function prepare_item_for_response( $item, $request ) {
	global $wp_widget_factory, $wp_registered_widgets;

	$widget_id  = $item['widget_id'];
	$sidebar_id = $item['sidebar_id'];

	if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {
		return new WP_Error(
			'rest_invalid_widget',
			__( 'The requested widget is invalid.' ),
			array( 'status' => 500 )
		);
	}

	$widget    = $wp_registered_widgets[ $widget_id ];
	$parsed_id = wp_parse_widget_id( $widget_id );
	$fields    = $this->get_fields_for_response( $request );

	$prepared = array(
		'id'            => $widget_id,
		'id_base'       => $parsed_id['id_base'],
		'sidebar'       => $sidebar_id,
		'rendered'      => '',
		'rendered_form' => null,
		'instance'      => null,
	);

	if (
		rest_is_field_included( 'rendered', $fields ) &&
		'wp_inactive_widgets' !== $sidebar_id
	) {
		$prepared['rendered'] = trim( wp_render_widget( $widget_id, $sidebar_id ) );
	}

	if ( rest_is_field_included( 'rendered_form', $fields ) ) {
		$rendered_form = wp_render_widget_control( $widget_id );
		if ( ! is_null( $rendered_form ) ) {
			$prepared['rendered_form'] = trim( $rendered_form );
		}
	}

	if ( rest_is_field_included( 'instance', $fields ) ) {
		$widget_object = $wp_widget_factory->get_widget_object( $parsed_id['id_base'] );
		if ( $widget_object && isset( $parsed_id['number'] ) ) {
			$all_instances                   = $widget_object->get_settings();
			$instance                        = $all_instances[ $parsed_id['number'] ];
			$serialized_instance             = serialize( $instance );
			$prepared['instance']['encoded'] = base64_encode( $serialized_instance );
			$prepared['instance']['hash']    = wp_hash( $serialized_instance );

			if ( ! empty( $widget_object->widget_options['show_instance_in_rest'] ) ) {
				// Use new stdClass so that JSON result is {} and not [].
				$prepared['instance']['raw'] = empty( $instance ) ? new stdClass : $instance;
			}
		}
	}

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

	$response = rest_ensure_response( $prepared );

	$response->add_links( $this->prepare_links( $prepared ) );

	/**
	 * Filters the REST API response for a widget.
	 *
	 * @since 5.8.0
	 *
	 * @param WP_REST_Response $response The response object.
	 * @param array            $widget   The registered widget data.
	 * @param WP_REST_Request  $request  Request used to generate the response.
	 */
	return apply_filters( 'rest_prepare_widget', $response, $widget, $request );
}