Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Pieces

WordPress_Global_State_Adapter::set_global_statepublicYoast 1.0

Set WordPress global state

Helper method to set $post and $wp_query globals based on the given indexable, and prime the memoizer's current_page slot with the indexable's context so external schema generators (e.g. WPSEO_WooCommerce_Schema) read the correct per-indexable values. This is critical to ensure that schema pieces relying on global state function correctly.

Метод класса: WordPress_Global_State_Adapter{}

Хуков нет.

Возвращает

null. Ничего (null).

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

$WordPress_Global_State_Adapter = new WordPress_Global_State_Adapter();
$WordPress_Global_State_Adapter->set_global_state( $indexable, $context ): void;
$indexable(Indexable) (обязательный)
The indexable to set the global state for.
$context(Meta_Tags_Context) (обязательный)
The indexable's context, installed as the current_page slot.

Код WordPress_Global_State_Adapter::set_global_state() Yoast 28.3

public function set_global_state( Indexable $indexable, Meta_Tags_Context $context ): void {
	global $post, $wp_query;
	$this->previous_post              = $post;
	$this->previous_queried_object    = ( $wp_query->queried_object ?? null );
	$this->previous_queried_object_id = ( $wp_query->queried_object_id ?? null );

	$this->previous_query_flags = [
		'is_single'   => ( $wp_query->is_single ?? false ),
		'is_page'     => ( $wp_query->is_page ?? false ),
		'is_singular' => ( $wp_query->is_singular ?? false ),
	];

	// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
	$post                        = \get_post( $indexable->object_id );
	$wp_query->queried_object    = \get_post( $indexable->object_id );
	$wp_query->queried_object_id = $indexable->object_id;

	$wp_query->is_single   = false;
	$wp_query->is_page     = false;
	$wp_query->is_singular = true;

	if ( $indexable->object_sub_type === 'page' ) {
		$wp_query->is_page = true;
	}
	else {
		$wp_query->is_single = true;

	}

	\setup_postdata( $post );

	// Make for_current_page() resolve to the indexable being processed, so external schema
	// generators (e.g. WPSEO_WooCommerce_Schema) read the correct per-indexable canonical /
	// main_schema_id. reset_global_state() clears this slot at the end of the iteration.
	$this->memoizer->set_for_current_page( $context );
}