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 7.1.0

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

	foreach ( $template_files as $template_file ) {
		// Skip the template if it's blockified, and we should only use classic ones.
		// Until the blockified Product Grid Block is implemented, we need to always skip the blockified templates.
		// phpcs:ignore Squiz.PHP.CommentedOutCode
		if ( // $this->package->is_experimental_build() &&
			// ! 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 the theme has an archive-product.html template, but not a taxonomy-product_cat.html template let's use the themes archive-product.html template.
		if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $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 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;
}