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

WP_REST_Sidebars_Controller::prepare_item_for_response() public WP 5.8.0

Prepares a single sidebar output for response.

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

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

WP_REST_Response. Prepared response object.

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

$WP_REST_Sidebars_Controller = new WP_REST_Sidebars_Controller();
$WP_REST_Sidebars_Controller->prepare_item_for_response( $raw_sidebar, $request );
$raw_sidebar(массив) (обязательный)
Sidebar instance.
$request(WP_REST_Request) (обязательный)
Full details about the request.

Заметки

  • Global. Массив. $wp_registered_sidebars The registered sidebars.
  • Global. Массив. $wp_registered_widgets The registered widgets.

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

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

Код WP_REST_Sidebars_Controller::prepare_item_for_response() WP 5.8.1

public function prepare_item_for_response( $raw_sidebar, $request ) {
	global $wp_registered_sidebars, $wp_registered_widgets;

	$id      = $raw_sidebar['id'];
	$sidebar = array( 'id' => $id );

	if ( isset( $wp_registered_sidebars[ $id ] ) ) {
		$registered_sidebar = $wp_registered_sidebars[ $id ];

		$sidebar['status']        = 'active';
		$sidebar['name']          = isset( $registered_sidebar['name'] ) ? $registered_sidebar['name'] : '';
		$sidebar['description']   = isset( $registered_sidebar['description'] ) ? wp_sidebar_description( $id ) : '';
		$sidebar['class']         = isset( $registered_sidebar['class'] ) ? $registered_sidebar['class'] : '';
		$sidebar['before_widget'] = isset( $registered_sidebar['before_widget'] ) ? $registered_sidebar['before_widget'] : '';
		$sidebar['after_widget']  = isset( $registered_sidebar['after_widget'] ) ? $registered_sidebar['after_widget'] : '';
		$sidebar['before_title']  = isset( $registered_sidebar['before_title'] ) ? $registered_sidebar['before_title'] : '';
		$sidebar['after_title']   = isset( $registered_sidebar['after_title'] ) ? $registered_sidebar['after_title'] : '';
	} else {
		$sidebar['status']      = 'inactive';
		$sidebar['name']        = $raw_sidebar['name'];
		$sidebar['description'] = '';
		$sidebar['class']       = '';
	}

	$fields = $this->get_fields_for_response( $request );
	if ( rest_is_field_included( 'widgets', $fields ) ) {
		$sidebars = wp_get_sidebars_widgets();
		$widgets  = array_filter(
			isset( $sidebars[ $sidebar['id'] ] ) ? $sidebars[ $sidebar['id'] ] : array(),
			static function ( $widget_id ) use ( $wp_registered_widgets ) {
				return isset( $wp_registered_widgets[ $widget_id ] );
			}
		);

		$sidebar['widgets'] = array_values( $widgets );
	}

	$schema = $this->get_item_schema();
	$data   = array();
	foreach ( $schema['properties'] as $property_id => $property ) {
		if ( isset( $sidebar[ $property_id ] ) && true === rest_validate_value_from_schema( $sidebar[ $property_id ], $property ) ) {
			$data[ $property_id ] = $sidebar[ $property_id ];
		} elseif ( isset( $property['default'] ) ) {
			$data[ $property_id ] = $property['default'];
		}
	}

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

	/**
	 * Filters the REST API response for a sidebar.
	 *
	 * @since 5.8.0
	 *
	 * @param WP_REST_Response $response    The response object.
	 * @param array            $raw_sidebar The raw sidebar data.
	 * @param WP_REST_Request  $request     The request object.
	 */
	return apply_filters( 'rest_prepare_sidebar', $response, $raw_sidebar, $request );
}