Automattic\WooCommerce\Blocks

BlockTemplatesController::get_block_templates_from_woocommercepublicWC 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 10.6.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;
		}

		// 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;
}