Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer

Content_Renderer::set_template_globalsprivateWC 1.0

Set template globals

Supports synthetic posts (ID === 0, no DB record): the globals are populated from the post object itself without running a query.

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

Хуков нет.

Возвращает

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

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

// private - только в коде основоного (родительского) класса
$result = $this->set_template_globals( $email_post, $template );
$email_post(WP_Post) (обязательный)
Post object.
$template(WP_Block_Template) (обязательный)
Block template.

Код Content_Renderer::set_template_globals() WC 11.1.0

private function set_template_globals( WP_Post $email_post, WP_Block_Template $template ) {
	global $_wp_current_template_content, $_wp_current_template_id, $wp_query, $post;

	// Backup current values of globals.
	// Because overriding the globals can affect rendering of the page itself, we need to backup the current values.
	$this->backup_template_content = $_wp_current_template_content;
	$this->backup_template_id      = $_wp_current_template_id;
	$this->backup_query            = $wp_query;
	$this->backup_post             = $post;

	$_wp_current_template_id      = $template->id;
	$_wp_current_template_content = $template->content;
	if ( $email_post->ID > 0 ) {
		$wp_query = new \WP_Query( array( 'p' => $email_post->ID ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to set the query for correct rendering the blocks.
	} else {
		// Synthetic post (e.g. file-template rendering): querying `p => 0` would
		// run a real "latest posts" query, so populate an empty query manually.
		$wp_query              = new \WP_Query(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to set the query for correct rendering the blocks.
		$wp_query->post        = $email_post;
		$wp_query->posts       = array( $email_post );
		$wp_query->post_count  = 1;
		$wp_query->found_posts = 1;
	}
	$post = $email_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to set the post for correct rendering the blocks.
}