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.

Method of the class: BlockTemplatesController{}

No Hooks.

Return

Array. Templates from the WooCommerce blocks plugin directory.

Usage

$BlockTemplatesController = new BlockTemplatesController();
$BlockTemplatesController->get_block_templates_from_woocommerce( $slugs, $already_found_templates, $template_type );
$slugs(string[]) (required)
An array of slugs to filter templates by. Templates whose slug does not match will not be returned.
$already_found_templates(array) (required)
Templates that have already been found, these are customised templates that are loaded from the database.
$template_type(string)
wp_template or wp_template_part.
Default: 'wp_template'

BlockTemplatesController::get_block_templates_from_woocommerce() code WC 9.4.2

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

	foreach ( $template_files as $template_file ) {
		// 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_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 template is not present in the theme but its fallback template is,
		// let's use the theme's fallback template.
		if ( BlockTemplateUtils::template_is_eligible_for_fallback_from_theme( $template_slug ) ) {
			$registered_template = BlockTemplateUtils::get_template( $template_slug );
			$template_file       = BlockTemplateUtils::get_theme_template_path( $registered_template->fallback_template );
			$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;
}