Automattic\WooCommerce\Blocks

BlockTemplatesController::get_block_template_fallback()publicWC 1.0

This function is used on the pre_get_block_template to return the fallback template from the db in case the template is eligible for it.

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

Хуков нет.

Возвращает

Объект|null.

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

$BlockTemplatesController = new BlockTemplatesController();
$BlockTemplatesController->get_block_template_fallback( $template, $id, $template_type );
$template(\WP_Block_Template|null) (обязательный)
Block template object to short-circuit the default query, or null to allow WP to run its normal queries.
$id(строка) (обязательный)
Template unique identifier (example: theme_slug//template_slug).
$template_type(строка) (обязательный)
wp_template or wp_template_part.

Код BlockTemplatesController::get_block_template_fallback() WC 7.3.0

public function get_block_template_fallback( $template, $id, $template_type ) {
	$template_name_parts  = explode( '//', $id );
	list( $theme, $slug ) = $template_name_parts;

	if ( ! BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $slug ) ) {
		return null;
	}

	$wp_query_args  = array(
		'post_name__in'  => array( 'archive-product' ),
		'post_type'      => $template_type,
		'post_status'    => array( 'auto-draft', 'draft', 'publish', 'trash' ),
		'posts_per_page' => 1,
		'no_found_rows'  => true,
		'tax_query'      => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
			array(
				'taxonomy' => 'wp_theme',
				'field'    => 'name',
				'terms'    => $theme,
			),
		),
	);
	$template_query = new \WP_Query( $wp_query_args );
	$posts          = $template_query->posts;

	if ( count( $posts ) > 0 ) {
		$template = _build_block_template_result_from_post( $posts[0] );

		if ( ! is_wp_error( $template ) ) {
			$template->id          = $theme . '//' . $slug;
			$template->slug        = $slug;
			$template->title       = BlockTemplateUtils::get_block_template_title( $slug );
			$template->description = BlockTemplateUtils::get_block_template_description( $slug );

			return $template;
		}
	}

	return $template;
}