diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index be1fa2ea7da..7ea5ca9ce1a 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -25,18 +25,11 @@ class BlockTemplatesController { private $template_parts_directory; /** - * Directory name of the block template directory. + * Directory which contains all templates * * @var string */ - const TEMPLATES_DIR_NAME = 'block-templates'; - - /** - * Directory name of the block template parts directory. - * - * @var string - */ - const TEMPLATE_PARTS_DIR_NAME = 'block-template-parts'; + const TEMPLATES_ROOT_DIR = 'templates'; /** * Constructor. @@ -44,8 +37,10 @@ class BlockTemplatesController { public function __construct() { // This feature is gated for WooCommerce versions 6.0.0 and above. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '6.0.0', '>=' ) ) { - $this->templates_directory = plugin_dir_path( __DIR__ ) . 'templates/' . self::TEMPLATES_DIR_NAME; - $this->template_parts_directory = plugin_dir_path( __DIR__ ) . 'templates/' . self::TEMPLATE_PARTS_DIR_NAME; + $root_path = plugin_dir_path( __DIR__ ) . self::TEMPLATES_ROOT_DIR . DIRECTORY_SEPARATOR; + + $this->templates_directory = $root_path . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATES']; + $this->template_parts_directory = $root_path . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATE_PARTS']; $this->init(); } } @@ -61,7 +56,7 @@ protected function init() { /** * This function checks if there's a blocks template (ultimately it resolves either a saved blocks template from the - * database or a template file in `woo-gutenberg-products-block/templates/block-templates/`) + * database or a template file in `woo-gutenberg-products-block/templates/templates/`) * to return to pre_get_posts short-circuiting the query in Gutenberg. * * @param \WP_Block_Template|null $template Return a block template object to short-circuit the default query, @@ -311,14 +306,8 @@ public function get_block_templates_from_woocommerce( $slugs, $already_found_tem $template_files = BlockTemplateUtils::gutenberg_get_template_paths( $directory ); $templates = array(); - if ( 'wp_template_part' === $template_type ) { - $dir_name = self::TEMPLATE_PARTS_DIR_NAME; - } else { - $dir_name = self::TEMPLATES_DIR_NAME; - } - foreach ( $template_files as $template_file ) { - $template_slug = BlockTemplateUtils::generate_template_slug_from_path( $template_file, $dir_name ); + $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 ) ) { diff --git a/src/BlockTypes/MiniCart.php b/src/BlockTypes/MiniCart.php index 9a1b2d78a03..2ee0aff9e25 100644 --- a/src/BlockTypes/MiniCart.php +++ b/src/BlockTypes/MiniCart.php @@ -387,7 +387,7 @@ protected function get_markup( $attributes ) { if ( '' === $template_part_contents ) { $template_part_contents = do_blocks( // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents - file_get_contents( Package::get_path() . 'templates/block-template-parts/mini-cart.html' ) + file_get_contents( Package::get_path() . 'templates/' . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATE_PARTS'] . '/mini-cart.html' ) ); } diff --git a/src/Utils/BlockTemplateUtils.php b/src/Utils/BlockTemplateUtils.php index c856ce17e4c..fc50cd059b9 100644 --- a/src/Utils/BlockTemplateUtils.php +++ b/src/Utils/BlockTemplateUtils.php @@ -254,15 +254,12 @@ public static function convert_slug_to_title( $template_slug ) { * Converts template paths into a slug * * @param string $path The template's path. - * @param string $directory_name The template's directory name. * @return string slug */ - public static function generate_template_slug_from_path( $path, $directory_name = 'block-templates' ) { - return substr( - $path, - strpos( $path, $directory_name . DIRECTORY_SEPARATOR ) + 1 + strlen( $directory_name ), - -5 - ); + public static function generate_template_slug_from_path( $path ) { + $template_extension = '.html'; + + return basename( $path, $template_extension ); } /** @@ -298,8 +295,8 @@ public static function get_theme_template_path( $template_slug, $template_type = function( $carry, $item ) use ( $template_filename ) { $filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename; - $carry[] = get_template_directory() . $filepath; $carry[] = get_stylesheet_directory() . $filepath; + $carry[] = get_template_directory() . $filepath; return $carry; }, diff --git a/templates/block-template-parts/mini-cart.html b/templates/parts/mini-cart.html similarity index 100% rename from templates/block-template-parts/mini-cart.html rename to templates/parts/mini-cart.html diff --git a/templates/block-templates/archive-product.html b/templates/templates/archive-product.html similarity index 100% rename from templates/block-templates/archive-product.html rename to templates/templates/archive-product.html diff --git a/templates/block-templates/single-product.html b/templates/templates/single-product.html similarity index 100% rename from templates/block-templates/single-product.html rename to templates/templates/single-product.html diff --git a/templates/block-templates/taxonomy-product_cat.html b/templates/templates/taxonomy-product_cat.html similarity index 100% rename from templates/block-templates/taxonomy-product_cat.html rename to templates/templates/taxonomy-product_cat.html diff --git a/templates/block-templates/taxonomy-product_tag.html b/templates/templates/taxonomy-product_tag.html similarity index 100% rename from templates/block-templates/taxonomy-product_tag.html rename to templates/templates/taxonomy-product_tag.html