Automattic\WooCommerce\Blocks

BlockTemplatesController::get_block_templates_from_woocommerce()publicWC 1.0

Gets the templates from the WooCommerce blocks directory, skipping those for which a template already exists in the theme directory.

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

Хуков нет.

Возвращает

Массив. Templates from the WooCommerce blocks plugin directory.

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

$BlockTemplatesController = new BlockTemplatesController();
$BlockTemplatesController->get_block_templates_from_woocommerce( $slugs, $already_found_templates, $template_type );
$slugs(string[]) (обязательный)
An array of slugs to filter templates by. Templates whose slug does not match will not be returned.
$already_found_templates(массив) (обязательный)
Templates that have already been found, these are customised templates that are loaded from the database.
$template_type(строка)
wp_template or wp_template_part.
По умолчанию: 'wp_template'

Код BlockTemplatesController::get_block_templates_from_woocommerce() WC 8.7.0

public function get_block_templates_from_woocommerce( $slugs, $already_found_templates, $template_type = 'wp_template' ) {
	$directory      = BlockTemplateUtils::get_templates_directory( $template_type );
	$template_files = BlockTemplateUtils::get_template_paths( $directory );
	$templates      = array();

	foreach ( $template_files as $template_file ) {
		// Skip the Product Gallery template part, as it is not supposed to be exposed at this point.
		if ( str_contains( $template_file, 'templates/parts/product-gallery.html' ) ) {
			continue;
		}

		// Skip the template if it's blockified, and we should only use classic ones.
		if ( ! BlockTemplateUtils::should_use_blockified_product_grid_templates() && strpos( $template_file, 'blockified' ) !== false ) {
			continue;
		}

		$template_slug = BlockTemplateUtils::generate_template_slug_from_path( $template_file );

		// This template does not have a slug we're looking for. Skip it.
		if ( is_array( $slugs ) && count( $slugs ) > 0 && ! in_array( $template_slug, $slugs, true ) ) {
			continue;
		}

		// If the theme already has a template, or the template is already in the list (i.e. it came from the
		// database) then we should not overwrite it with the one from the filesystem.
		if (
			BlockTemplateUtils::theme_has_template( $template_slug ) ||
			count(
				array_filter(
					$already_found_templates,
					function ( $template ) use ( $template_slug ) {
						$template_obj = (object) $template; //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
						return $template_obj->slug === $template_slug;
					}
				)
			) > 0 ) {
			continue;
		}

		if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_db( $template_slug, $already_found_templates ) ) {
			$template              = clone BlockTemplateUtils::get_fallback_template_from_db( $template_slug, $already_found_templates );
			$template_id           = explode( '//', $template->id );
			$template->id          = $template_id[0] . '//' . $template_slug;
			$template->slug        = $template_slug;
			$template->title       = BlockTemplateUtils::get_block_template_title( $template_slug );
			$template->description = BlockTemplateUtils::get_block_template_description( $template_slug );
			$templates[]           = $template;
			continue;
		}

		// 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_file = BlockTemplateUtils::get_theme_template_path( 'archive-product' );
			$templates[]   = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug, true );
			continue;
		}

		// At this point the template only exists in the Blocks filesystem, if is a taxonomy-product_cat/tag/attribute.html template
		// let's use the archive-product.html template from Blocks.
		if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $template_slug ) ) {
			$template_file = $this->get_template_path_from_woocommerce( 'archive-product' );
			$templates[]   = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug, false );
			continue;
		}

		// At this point the template only exists in the Blocks filesystem and has not been saved in the DB,
		// or superseded by the theme.
		$templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug );
	}

	return $templates;
}