Automattic\WooCommerce\Blocks

BlockTemplatesController::get_block_file_template()publicWC 1.0

This function checks if there's a block template file in woocommerce/templates/templates/ to return to pre_get_posts short-circuiting the query in Gutenberg.

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

Хуков нет.

Возвращает

Разное|\WP_Block_Template|\WP_Error.

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

$BlockTemplatesController = new BlockTemplatesController();
$BlockTemplatesController->get_block_file_template( $template, $id, $template_type );
$template(\WP_Block_Template|null) (обязательный)
Return a 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_file_template() WC 8.7.0

public function get_block_file_template( $template, $id, $template_type ) {
	$template_name_parts = explode( '//', $id );

	if ( count( $template_name_parts ) < 2 ) {
		return $template;
	}

	list( $template_id, $template_slug ) = $template_name_parts;

	// If the theme has an archive-product.html template, but not a taxonomy-product_cat/tag/attribute.html template let's use the themes archive-product.html template.
	if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_theme( $template_slug ) ) {
		$template_path   = BlockTemplateUtils::get_theme_template_path( 'archive-product' );
		$template_object = BlockTemplateUtils::create_new_block_template_object( $template_path, $template_type, $template_slug, true );
		return BlockTemplateUtils::build_template_result_from_file( $template_object, $template_type );
	}

	// This is a real edge-case, we are supporting users who have saved templates under the deprecated slug. See its definition for more information.
	// You can likely ignore this code unless you're supporting/debugging early customised templates.
	if ( BlockTemplateUtils::DEPRECATED_PLUGIN_SLUG === strtolower( $template_id ) ) {
		// Because we are using get_block_templates we have to unhook this method to prevent a recursive loop where this filter is applied.
		remove_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
		$template_with_deprecated_id = get_block_template( $id, $template_type );
		// Let's hook this method back now that we have used the function.
		add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );

		if ( null !== $template_with_deprecated_id ) {
			return $template_with_deprecated_id;
		}
	}

	// If we are not dealing with a WooCommerce template let's return early and let it continue through the process.
	if ( BlockTemplateUtils::PLUGIN_SLUG !== $template_id ) {
		return $template;
	}

	// If we don't have a template let Gutenberg do its thing.
	if ( ! $this->block_template_is_available( $template_slug, $template_type ) ) {
		return $template;
	}

	$directory          = BlockTemplateUtils::get_templates_directory( $template_type );
	$template_file_path = $directory . '/' . $template_slug . '.html';
	$template_object    = BlockTemplateUtils::create_new_block_template_object( $template_file_path, $template_type, $template_slug );
	$template_built     = BlockTemplateUtils::build_template_result_from_file( $template_object, $template_type );

	if ( null !== $template_built ) {
		return $template_built;
	}

	// Hand back over to Gutenberg if we can't find a template.
	return $template;
}