diff --git a/docs/testing/releases/651.md b/docs/testing/releases/651.md new file mode 100644 index 00000000000..8768c688b15 --- /dev/null +++ b/docs/testing/releases/651.md @@ -0,0 +1,87 @@ +## Testing notes and ZIP for release 6.6.0 + +Zip file for testing: [woocommerce-gutenberg-products-block.zip](https://github.com/woocommerce/woocommerce-gutenberg-products-block/files/7762015/woocommerce-gutenberg-products-block.zip) + +## Feature plugin and package inclusion in WooCommerce + +### Fix saving WooCommerce templates in WP 5.9 beta 3 ([5408](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5408)) + +1. With WP 5.9 beta 3 and Gutenberg disabled, go to Appearance > Editor. +2. Edit one of the WooCommerce templates and try to save it. +3. Verify the template is saved without errors. +4. Repeat the process with Gutenberg enabled and verify it works as well. + +### Fixed error reporting block templates do not exist after editing WooCommerce templates on WP 5.8. ([5425](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5425)) + +Test the following under these scenarios: + +- WordPress 5.8 and the Gutenberg plugin +- WordPress 5.9 and the Gutenberg plugin +- WordPress 5.9 without the Gutenberg plugin + +1. Open the FSE editor +2. From the sidebar on the left click on Templates. +3. Click on one of these templates Product Category Page, Product Archive Page, Product Archive Page or Single Product Page. +4. Edit the template and save it. +5. Go back (not refresh the page, because you will reproduce a known bug (#5426). +6. Reopen the same template and check if the changes are still there. +7. Check if the template works correctly on the frontend side too. + +### Added By template column value is user friendly for modified WooCommerce block templates. ([5420](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5420)) + +1. Modify one of the WooCommerce block templates via the Site Editor. Save it. +2. Load the templates screen and ensure the Added By column value says WooCommerce and not woocommerce +3. Ensure the modified version of the template loads in the Site Editor and on the frontend. + +### Avoid WooCommerce template names being editable. ([5385](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5385)) + +1. With WC 6.0 beta 3 and a block theme installed, go to Appearance > Editor. +2. Go to the Templates page and edit one of the WooCommerce templates (ie: Single Product Page). +3. Save and refresh the page. +4. Verify the template name is not editable. + +### Use the themes product archive block template for product category & product tag pages if the theme does not have more specific templates for those. ([5380](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5380)) + +Add the following block markup to the following templates so you know which one is getting rendered: + +1. woo-blocks/archive-product.html +2. woo-blocks/taxonomy-product_tag.html +3. woo-blocks/taxonomy-product_cat.html + +Template: + +``` + +

Woo Blocks: [template-filename].html

+ +``` + +1. Install Gutenberg plugin and Blockbase theme to activate Site Editor and FSE. +2. Go to a product category page and check that woo-blocks/taxonomy-product_cat.html is the template being used. +3. Duplicate woo-blocks/archive-product.html into blockbase/block-templates/ and change the markup you added above to say "Blockbase" instead of "Woo Blocks" so we can differentiate. +4. Reload the product category page, product tag page, and main shop page/product archive page and check that the archive-product.html from Blockbase is being loaded. +5. Go into Site Editor > All Templates and check that "Product Archive Page", "Product Category Page" and "Product Tag Page" are all in the list and it states they're being loaded from the Blockbase theme +6. Customise the Product Category Page template and save your changes +7. Reload the product category page on the frontend and check the customised version of this template is being rendered. + +### Fixed issue with variable products add to cart error, and gallery not updating to show correct product variant image. ([5346](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5346)) + +1. Install a Block Theme such as [TT1](https://en-gb.wordpress.org/themes/tt1-blocks/) and the [Gutenberg plugin](https://wordpress.org/plugins/gutenberg/). +2. Load the product page of a variable product. +3. Change some of the options (such as colour) on the variable product and check that the image in the product gallery updates to show the correct image for that variant. +4. Clicking "Add to cart" should successfully add the product to the cart. + +### Fix error when reverting WooCommerce templates. ([5342](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5342)) + +1. With WC 6.0 or later, Gutenberg and a block theme installed, go to Appearance > Editor. +2. Go to the Templates page and edit one of the WooCommerce templates (ie: Single Product Page). +3. In the frontend, verify the changes you just saved are applied. +4. Go back to the Templates page and press on _Clear Customizations_ of the template you just edited. +5. Verify there is no error and the changes have been reverted in the frontend. +6. Repeat the steps above with WP 5.9 beta without Gutenberg enabled. + +### Fix: WooCommerce block templates loading for WP 5.9 without Gutenberg plugin. ([5335](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5335)) + +1. Make sure you have WP 5.9 or above. +2. Install a Block Theme such as [TT1](https://en-gb.wordpress.org/themes/tt1-blocks/). +3. Make sure you can load the list of Block Templates in the Appearance > Site Editor diff --git a/docs/testing/releases/README.md b/docs/testing/releases/README.md index 4b5adde93d7..c7063f3195c 100644 --- a/docs/testing/releases/README.md +++ b/docs/testing/releases/README.md @@ -53,3 +53,4 @@ Every release includes specific testing instructions for new features and bug fi - [6.3.3](./633.md) - [6.4.0](./640.md) - [6.5.0](./650.md) +- [6.5.1](./651.md) diff --git a/package.json b/package.json index a75583a4120..a4822deb23f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@woocommerce/block-library", "title": "WooCommerce Blocks", "author": "Automattic", - "version": "6.5.0", + "version": "6.5.1", "description": "WooCommerce blocks for the Gutenberg editor.", "homepage": "https://github.com/woocommerce/woocommerce-gutenberg-products-block/", "keywords": [ diff --git a/readme.txt b/readme.txt index b20271b9ba5..163d3434744 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Tags: gutenberg, woocommerce, woo commerce, products, blocks, woocommerce blocks Requires at least: 5.8 Tested up to: 5.8 Requires PHP: 7.0 -Stable tag: 6.5.0 +Stable tag: 6.5.1 License: GPLv3 License URI: https://www.gnu.org/licenses/gpl-3.0.html @@ -85,6 +85,18 @@ Release and roadmap notes available on the [WooCommerce Developers Blog](https:/ == Changelog == += 6.5.1 - 2021-12-22 = + +#### Bug Fixes + +- Fixed error reporting block templates do not exist after editing WooCommerce templates on WP 5.8. ([5425](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5425)) +- Added By template column value is user friendly for modified WooCommerce block templates. ([5420](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5420)) +- Avoid WooCommerce template names being editable. ([5385](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5385)) +- Use the themes product archive block template for product category & product tag pages if the theme does not have more specific templates for those. ([5380](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5380)) +- Fixed issue with variable products add to cart error, and gallery not updating to show correct product variant image. ([5346](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5346)) +- Fix error when reverting WooCommerce templates. ([5342](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5342)) +- Fix: WooCommerce block templates loading for WP 5.9 without Gutenberg plugin. ([5335](https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/5335)) + = 6.5.0 - 2021-12-06 = #### Enhancements diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 6b4fc990b02..d5a9c0e4963 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -55,7 +55,7 @@ public function __construct() { */ protected function init() { add_action( 'template_redirect', array( $this, 'render_block_template' ) ); - add_filter( 'pre_get_block_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 ); + add_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 ); add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 ); add_filter( 'default_wp_template_part_areas', array( $this, 'add_template_part_areas' ) ); add_filter( 'wp_insert_post', array( $this, 'add_mini_cart_content_to_template_part' ), 10, 3 ); @@ -74,7 +74,13 @@ protected function init() { * @return mixed|\WP_Block_Template|\WP_Error */ public function maybe_return_blocks_template( $template, $id, $template_type ) { - if ( ! function_exists( 'gutenberg_get_block_template' ) ) { + // 'get_block_template' was introduced in WP 5.9. We need to support + // 'gutenberg_get_block_template' for previous versions of WP with + // Gutenberg enabled. + if ( + ! function_exists( 'gutenberg_get_block_template' ) && + ! function_exists( 'get_block_template' ) + ) { return $template; } $template_name_parts = explode( '//', $id ); @@ -84,24 +90,29 @@ public function maybe_return_blocks_template( $template, $id, $template_type ) { list( , $slug ) = $template_name_parts; // Remove the filter at this point because if we don't then this function will infinite loop. - remove_filter( 'pre_get_block_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 ); + remove_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 ); // Check if the theme has a saved version of this template before falling back to the woo one. Please note how // the slug has not been modified at this point, we're still using the default one passed to this hook. - $maybe_template = gutenberg_get_block_template( $id, $template_type ); + $maybe_template = function_exists( 'gutenberg_get_block_template' ) ? + gutenberg_get_block_template( $id, $template_type ) : + get_block_template( $id, $template_type ); + if ( null !== $maybe_template ) { - add_filter( 'pre_get_block_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 ); + add_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 ); return $maybe_template; } // Theme-based template didn't exist, try switching the theme to woocommerce and try again. This function has // been unhooked so won't run again. - add_filter( 'get_block_template', array( $this, 'get_single_block_template' ), 10, 3 ); - $maybe_template = gutenberg_get_block_template( 'woocommerce//' . $slug, $template_type ); + add_filter( 'get_block_file_template', array( $this, 'get_single_block_template' ), 10, 3 ); + $maybe_template = function_exists( 'gutenberg_get_block_template' ) ? + gutenberg_get_block_template( 'woocommerce//' . $slug, $template_type ) : + get_block_template( 'woocommerce//' . $slug, $template_type ); // Re-hook this function, it was only unhooked to stop recursion. - add_filter( 'pre_get_block_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 ); - remove_filter( 'get_block_template', array( $this, 'get_single_block_template' ), 10, 3 ); + add_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 ); + remove_filter( 'get_block_file_template', array( $this, 'get_single_block_template' ), 10, 3 ); if ( null !== $maybe_template ) { return $maybe_template; } @@ -155,7 +166,7 @@ public function get_single_block_template( $template, $id, $template_type ) { * @return array */ public function add_block_templates( $query_result, $query, $template_type ) { - if ( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() ) { + if ( ! BlockTemplateUtils::supports_block_templates() ) { return $query_result; } @@ -338,21 +349,25 @@ function ( $template ) use ( $template_slug ) { 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 ( 'taxonomy-product_cat' === $template_slug && ! BlockTemplateUtils::theme_has_template( 'taxonomy-product_cat' ) && BlockTemplateUtils::theme_has_template( 'archive-product' ) ) { + $template_file = get_stylesheet_directory() . '/' . self::TEMPLATES_DIR_NAME . '/archive-product.html'; + $templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug, true ); + continue; + } + + // If the theme has an archive-product.html template, but not a taxonomy-product_tag.html template let's use the themes archive-product.html template. + if ( 'taxonomy-product_tag' === $template_slug && ! BlockTemplateUtils::theme_has_template( 'taxonomy-product_tag' ) && BlockTemplateUtils::theme_has_template( 'archive-product' ) ) { + $template_file = get_stylesheet_directory() . '/' . self::TEMPLATES_DIR_NAME . '/archive-product.html'; + $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. - $new_template_item = array( - 'slug' => $template_slug, - 'id' => 'woocommerce//' . $template_slug, - 'path' => $template_file, - 'type' => $template_type, - 'theme' => 'woocommerce', - 'source' => 'plugin', - 'title' => BlockTemplateUtils::convert_slug_to_title( $template_slug ), - 'description' => '', - 'post_types' => array(), // Don't appear in any Edit Post template selector dropdown. - ); - $templates[] = (object) $new_template_item; + $templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug ); } + return $templates; } @@ -370,7 +385,6 @@ public function get_block_templates( $slugs = array(), $template_type = 'wp_temp return array_merge( $templates_from_db, $templates_from_woo ); } - /** * Gets the directory where templates of a specific template type can be found. * @@ -408,7 +422,7 @@ public function block_template_is_available( $template_name, $template_type = 'w * Renders the default block template from Woo Blocks if no theme templates exist. */ public function render_block_template() { - if ( is_embed() || ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() ) { + if ( is_embed() || ! BlockTemplateUtils::supports_block_templates() ) { return; } diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index ae3d0efa309..4742ad8219a 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -34,6 +34,14 @@ protected function render( $attributes, $content ) { return; } + // We need to load the scripts here because when using block templates wp_head() gets run after the block template. + // As a result we are trying to enqueue required scripts before we have even registered them. + // See here for more information: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5328#issuecomment-989013447. + if ( class_exists( 'WC_Frontend_Scripts' ) ) { + $frontend_scripts = new \WC_Frontend_Scripts(); + $frontend_scripts::load_scripts(); + } + $archive_templates = array( 'archive-product', 'taxonomy-product_cat', 'taxonomy-product_tag' ); if ( 'single-product' === $attributes['template'] ) { diff --git a/src/Package.php b/src/Package.php index 8c0baed1b93..3dda2341801 100644 --- a/src/Package.php +++ b/src/Package.php @@ -106,7 +106,7 @@ public static function container( $reset = false ) { NewPackage::class, function ( $container ) { // leave for automated version bumping. - $version = '6.5.0'; + $version = '6.5.1'; return new NewPackage( $version, dirname( __DIR__ ), diff --git a/src/Utils/BlockTemplateUtils.php b/src/Utils/BlockTemplateUtils.php index 6e592a98269..13de440cf87 100644 --- a/src/Utils/BlockTemplateUtils.php +++ b/src/Utils/BlockTemplateUtils.php @@ -98,7 +98,7 @@ public static function gutenberg_build_template_result_from_post( $post ) { $template = new \WP_Block_Template(); $template->wp_id = $post->ID; $template->id = $theme . '//' . $post->post_name; - $template->theme = $theme; + $template->theme = 'woocommerce' === $theme ? 'WooCommerce' : $theme; $template->content = $post->post_content; $template->slug = $post->post_name; $template->source = 'custom'; @@ -107,8 +107,9 @@ public static function gutenberg_build_template_result_from_post( $post ) { $template->title = $post->post_title; $template->status = $post->post_status; $template->has_theme_file = $has_theme_file; - $template->is_custom = true; + $template->is_custom = false; $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. + if ( 'wp_template_part' === $post->post_type ) { $type_terms = get_the_terms( $post, 'wp_template_part_area' ); if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { @@ -129,19 +130,26 @@ public static function gutenberg_build_template_result_from_post( $post ) { */ public static function gutenberg_build_template_result_from_file( $template_file, $template_type ) { $template_file = (object) $template_file; + + // If the theme has an archive-products.html template but does not have product taxonomy templates + // then we will load in the archive-product.html template from the theme to use for product taxonomies on the frontend. + $template_is_from_theme = 'theme' === $template_file->source; + $theme_name = wp_get_theme()->get( 'TextDomain' ); + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents - $template_content = file_get_contents( $template_file->path ); - $template = new \WP_Block_Template(); - $template->id = 'woocommerce//' . $template_file->slug; - $template->theme = 'WooCommerce'; - $template->content = self::gutenberg_inject_theme_attribute_in_content( $template_content ); - $template->source = 'plugin'; + $template_content = file_get_contents( $template_file->path ); + $template = new \WP_Block_Template(); + $template->id = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : 'woocommerce//' . $template_file->slug; + $template->theme = $template_is_from_theme ? $theme_name : 'WooCommerce'; + $template->content = self::gutenberg_inject_theme_attribute_in_content( $template_content ); + // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909. + $template->source = $template_file->source ? $template_file->source : 'plugin'; $template->slug = $template_file->slug; $template->type = $template_type; $template->title = ! empty( $template_file->title ) ? $template_file->title : self::convert_slug_to_title( $template_file->slug ); $template->status = 'publish'; $template->has_theme_file = true; - $template->origin = 'plugin'; + $template->origin = $template_file->source; $template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are. $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. if ( 'wp_template_part' === $template_type ) { @@ -154,6 +162,35 @@ public static function gutenberg_build_template_result_from_file( $template_file return $template; } + /** + * Build a new template object so that we can make Woo Blocks default templates available in the current theme should they not have any. + * + * @param string $template_file Block template file path. + * @param string $template_type wp_template or wp_template_part. + * @param string $template_slug Block template slug e.g. single-product. + * @param bool $template_is_from_theme If the block template file is being loaded from the current theme instead of Woo Blocks. + * + * @return object Block template object. + */ + public static function create_new_block_template_object( $template_file, $template_type, $template_slug, $template_is_from_theme = false ) { + $theme_name = wp_get_theme()->get( 'TextDomain' ); + + $new_template_item = array( + 'slug' => $template_slug, + 'id' => $template_is_from_theme ? $theme_name . '//' . $template_slug : 'woocommerce//' . $template_slug, + 'path' => $template_file, + 'type' => $template_type, + 'theme' => $template_is_from_theme ? $theme_name : 'woocommerce', + // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909. + 'source' => $template_is_from_theme ? 'theme' : 'plugin', + 'title' => self::convert_slug_to_title( $template_slug ), + 'description' => '', + 'post_types' => array(), // Don't appear in any Edit Post template selector dropdown. + ); + + return (object) $new_template_item; + } + /** * Finds all nested template part file paths in a theme's directory. * @@ -230,4 +267,20 @@ public static function theme_has_template_part( $template_name ) { return is_readable( get_template_directory() . '/block-template-parts/' . $template_name . '.html' ) || is_readable( get_stylesheet_directory() . '/block-template-parts/' . $template_name . '.html' ); } + + /** + * Checks to see if they are using a compatible version of WP, or if not they have a compatible version of the Gutenberg plugin installed. + * + * @return boolean + */ + public static function supports_block_templates() { + if ( + ( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) && + ( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() ) + ) { + return false; + } + + return true; + } } diff --git a/woocommerce-gutenberg-products-block.php b/woocommerce-gutenberg-products-block.php index 2293312ea84..2c416ab2298 100644 --- a/woocommerce-gutenberg-products-block.php +++ b/woocommerce-gutenberg-products-block.php @@ -3,7 +3,7 @@ * Plugin Name: WooCommerce Blocks * Plugin URI: https://github.com/woocommerce/woocommerce-gutenberg-products-block * Description: WooCommerce blocks for the Gutenberg editor. - * Version: 6.5.0 + * Version: 6.5.1 * Author: Automattic * Author URI: https://woocommerce.com * Text Domain: woo-gutenberg-products-block