From e7beb866ba5e21af225f3302def8a00109b559ef Mon Sep 17 00:00:00 2001 From: Lucio Giannotta Date: Mon, 27 Dec 2021 18:20:42 +0100 Subject: [PATCH 1/7] Align Woo Block template locations with the newest convention While we now support both the old and new conventions for the templates paths, our own repo should be aligned with the latest convention. See: #5455 Fixes: #5343 --- src/BlockTemplatesController.php | 17 ++++------ templates/parts/mini-cart.html | 31 +++++++++++++++++++ templates/templates/archive-product.html | 5 +++ templates/templates/single-product.html | 5 +++ templates/templates/taxonomy-product_cat.html | 5 +++ templates/templates/taxonomy-product_tag.html | 5 +++ 6 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 templates/parts/mini-cart.html create mode 100644 templates/templates/archive-product.html create mode 100644 templates/templates/single-product.html create mode 100644 templates/templates/taxonomy-product_cat.html create mode 100644 templates/templates/taxonomy-product_tag.html diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index be1fa2ea7da..054a9577310 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::TEMPLATES_DIR_NAME; + $this->template_parts_directory = $root_path . BlockTemplateUtils::TEMPLATE_PARTS_DIR_NAME; $this->init(); } } diff --git a/templates/parts/mini-cart.html b/templates/parts/mini-cart.html new file mode 100644 index 00000000000..c2400ba76ed --- /dev/null +++ b/templates/parts/mini-cart.html @@ -0,0 +1,31 @@ + +
+ +
+ + + +
+ + + +
+ +

+ Your cart is currently empty! +

+ + + +
+ + + +
+ +
+ +
+ diff --git a/templates/templates/archive-product.html b/templates/templates/archive-product.html new file mode 100644 index 00000000000..4cf01077d40 --- /dev/null +++ b/templates/templates/archive-product.html @@ -0,0 +1,5 @@ + + +
+ + diff --git a/templates/templates/single-product.html b/templates/templates/single-product.html new file mode 100644 index 00000000000..1e4177d96e3 --- /dev/null +++ b/templates/templates/single-product.html @@ -0,0 +1,5 @@ + + +
+ + diff --git a/templates/templates/taxonomy-product_cat.html b/templates/templates/taxonomy-product_cat.html new file mode 100644 index 00000000000..4a300405a67 --- /dev/null +++ b/templates/templates/taxonomy-product_cat.html @@ -0,0 +1,5 @@ + + +
+ + diff --git a/templates/templates/taxonomy-product_tag.html b/templates/templates/taxonomy-product_tag.html new file mode 100644 index 00000000000..071b616f7d5 --- /dev/null +++ b/templates/templates/taxonomy-product_tag.html @@ -0,0 +1,5 @@ + + +
+ + From 2fae89fd4d8b323c8b5b88c4d4ebd782d09bb09d Mon Sep 17 00:00:00 2001 From: Lucio Giannotta Date: Mon, 27 Dec 2021 18:21:15 +0100 Subject: [PATCH 2/7] Simplify `generate_template_slug_from_path` function --- src/BlockTemplatesController.php | 8 +------- src/Utils/BlockTemplateUtils.php | 11 ++++------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 054a9577310..ae00b9bc663 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -306,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/Utils/BlockTemplateUtils.php b/src/Utils/BlockTemplateUtils.php index 8b6d793a63b..9eb20ef785b 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 ); } /** From 2f4749d02e8265a9371c1dc63cd3dbaedb2d7b3d Mon Sep 17 00:00:00 2001 From: Lucio Giannotta Date: Mon, 27 Dec 2021 18:36:39 +0100 Subject: [PATCH 3/7] Remove old templates --- templates/block-template-parts/mini-cart.html | 31 ------------------- .../block-templates/archive-product.html | 5 --- templates/block-templates/single-product.html | 5 --- .../block-templates/taxonomy-product_cat.html | 5 --- .../block-templates/taxonomy-product_tag.html | 5 --- 5 files changed, 51 deletions(-) delete mode 100644 templates/block-template-parts/mini-cart.html delete mode 100644 templates/block-templates/archive-product.html delete mode 100644 templates/block-templates/single-product.html delete mode 100644 templates/block-templates/taxonomy-product_cat.html delete mode 100644 templates/block-templates/taxonomy-product_tag.html diff --git a/templates/block-template-parts/mini-cart.html b/templates/block-template-parts/mini-cart.html deleted file mode 100644 index c2400ba76ed..00000000000 --- a/templates/block-template-parts/mini-cart.html +++ /dev/null @@ -1,31 +0,0 @@ - -
- -
- - - -
- - - -
- -

- Your cart is currently empty! -

- - - -
- - - -
- -
- -
- diff --git a/templates/block-templates/archive-product.html b/templates/block-templates/archive-product.html deleted file mode 100644 index 4cf01077d40..00000000000 --- a/templates/block-templates/archive-product.html +++ /dev/null @@ -1,5 +0,0 @@ - - -
- - diff --git a/templates/block-templates/single-product.html b/templates/block-templates/single-product.html deleted file mode 100644 index 1e4177d96e3..00000000000 --- a/templates/block-templates/single-product.html +++ /dev/null @@ -1,5 +0,0 @@ - - -
- - diff --git a/templates/block-templates/taxonomy-product_cat.html b/templates/block-templates/taxonomy-product_cat.html deleted file mode 100644 index 4a300405a67..00000000000 --- a/templates/block-templates/taxonomy-product_cat.html +++ /dev/null @@ -1,5 +0,0 @@ - - -
- - diff --git a/templates/block-templates/taxonomy-product_tag.html b/templates/block-templates/taxonomy-product_tag.html deleted file mode 100644 index 071b616f7d5..00000000000 --- a/templates/block-templates/taxonomy-product_tag.html +++ /dev/null @@ -1,5 +0,0 @@ - - -
- - From 8db748461c155946fb0afe0427f40be9f382d817 Mon Sep 17 00:00:00 2001 From: Lucio Giannotta Date: Mon, 27 Dec 2021 19:39:17 +0100 Subject: [PATCH 4/7] Change `BlockTemplatesController` constructor to get correct dir names --- src/BlockTemplatesController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index ae00b9bc663..c8fa68f0285 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -39,8 +39,8 @@ public function __construct() { if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '6.0.0', '>=' ) ) { $root_path = plugin_dir_path( __DIR__ ) . self::TEMPLATES_ROOT_DIR . DIRECTORY_SEPARATOR; - $this->templates_directory = $root_path . BlockTemplateUtils::TEMPLATES_DIR_NAME; - $this->template_parts_directory = $root_path . BlockTemplateUtils::TEMPLATE_PARTS_DIR_NAME; + $this->templates_directory = $root_path . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATES']; + $this->template_parts_directory = $root_path . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATE_PARTS']; $this->init(); } } From 258553f2002d97058f5978c3e72452c382be8461 Mon Sep 17 00:00:00 2001 From: Lucio Giannotta Date: Wed, 29 Dec 2021 15:15:51 +0100 Subject: [PATCH 5/7] Update Mini Cart template path --- src/BlockTypes/MiniCart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlockTypes/MiniCart.php b/src/BlockTypes/MiniCart.php index f0decd8685d..f9518b71fa2 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' ) ); } From 82965b76908fe2e5b95401c5490ba3ae2fa943d5 Mon Sep 17 00:00:00 2001 From: Lucio Giannotta Date: Wed, 29 Dec 2021 15:16:18 +0100 Subject: [PATCH 6/7] Switch parent and child theme order --- src/Utils/BlockTemplateUtils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utils/BlockTemplateUtils.php b/src/Utils/BlockTemplateUtils.php index 9eb20ef785b..e634b171721 100644 --- a/src/Utils/BlockTemplateUtils.php +++ b/src/Utils/BlockTemplateUtils.php @@ -295,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; }, From d5743b45373fd1903c0ce61bea30606b9bdc9069 Mon Sep 17 00:00:00 2001 From: Lucio Giannotta Date: Wed, 29 Dec 2021 16:45:06 +0100 Subject: [PATCH 7/7] Fix path in function comment --- src/BlockTemplatesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index c8fa68f0285..7ea5ca9ce1a 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -56,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,