From d230dacd02f8ab00abfadd06108b751175fccf4e Mon Sep 17 00:00:00 2001 From: "Michael P. Pfeiffer" Date: Mon, 31 May 2021 11:49:53 +0200 Subject: [PATCH 1/3] Block Widgets: add transforms for legacy widgets with a block equivalent --- assets/js/blocks/product-categories/index.js | 22 ++++++++++++++++++- assets/js/blocks/product-search/index.js | 21 +++++++++++++++++- assets/js/blocks/reviews/all-reviews/index.js | 18 ++++++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/assets/js/blocks/product-categories/index.js b/assets/js/blocks/product-categories/index.js index 865a3658c76..cd8727cf631 100644 --- a/assets/js/blocks/product-categories/index.js +++ b/assets/js/blocks/product-categories/index.js @@ -2,7 +2,7 @@ * External dependencies */ import { __ } from '@wordpress/i18n'; -import { registerBlockType } from '@wordpress/blocks'; +import { createBlock, registerBlockType } from '@wordpress/blocks'; import { Icon, list } from '@woocommerce/icons'; /** @@ -83,6 +83,26 @@ registerBlockType( 'woocommerce/product-categories', { }, }, + transforms: { + from: [ + { + type: 'block', + blocks: [ 'core/legacy-widget' ], + // We can't transform if raw instance isn't shown in the REST API. + isMatch: ( { idBase, instance } ) => + idBase === 'woocommerce_product_categories' && + !! instance?.raw, + transform: ( { instance } ) => + createBlock( 'woocommerce/product-categories', { + hasCount: instance.raw.count, + hasEmpty: ! instance.raw.hide_empty, + isDropdown: instance.raw.dropdown, + isHierarchical: instance.raw.hierarchical, + } ), + }, + ], + }, + deprecated: [ { // Deprecate HTML save method in favor of dynamic rendering. diff --git a/assets/js/blocks/product-search/index.js b/assets/js/blocks/product-search/index.js index 7bb7a132e59..656e85220e1 100644 --- a/assets/js/blocks/product-search/index.js +++ b/assets/js/blocks/product-search/index.js @@ -2,7 +2,7 @@ * External dependencies */ import { __ } from '@wordpress/i18n'; -import { registerBlockType } from '@wordpress/blocks'; +import { createBlock, registerBlockType } from '@wordpress/blocks'; import { Icon, search } from '@woocommerce/icons'; /** * Internal dependencies @@ -71,6 +71,25 @@ registerBlockType( 'woocommerce/product-search', { }, }, + transforms: { + from: [ + { + type: 'block', + blocks: [ 'core/legacy-widget' ], + // We can't transform if raw instance isn't shown in the REST API. + isMatch: ( { idBase, instance } ) => + idBase === 'woocommerce_product_search' && !! instance?.raw, + transform: ( { instance } ) => + createBlock( 'woocommerce/product-search', { + label: + instance.raw.title === '' + ? __( 'Search', 'woo-gutenberg-products-block' ) + : instance.raw.title, + } ), + }, + ], + }, + edit, /** diff --git a/assets/js/blocks/reviews/all-reviews/index.js b/assets/js/blocks/reviews/all-reviews/index.js index 2700ce0ffc3..1008e605c8c 100644 --- a/assets/js/blocks/reviews/all-reviews/index.js +++ b/assets/js/blocks/reviews/all-reviews/index.js @@ -2,7 +2,7 @@ * External dependencies */ import { __ } from '@wordpress/i18n'; -import { registerBlockType } from '@wordpress/blocks'; +import { createBlock, registerBlockType } from '@wordpress/blocks'; import { Icon, discussion } from '@woocommerce/icons'; /** @@ -51,6 +51,22 @@ registerBlockType( 'woocommerce/all-reviews', { }, }, + transforms: { + from: [ + { + type: 'block', + blocks: [ 'core/legacy-widget' ], + // We can't transform if raw instance isn't shown in the REST API. + isMatch: ( { idBase, instance } ) => + idBase === 'woocommerce_recent_reviews' && !! instance?.raw, + transform: ( { instance } ) => + createBlock( 'woocommerce/all-reviews', { + reviewsOnPageLoad: instance.raw.number, + } ), + }, + ], + }, + /** * Renders and manages the block. * From 9000af08dbe3e2e0c3921e181f0741a2d508ab28 Mon Sep 17 00:00:00 2001 From: "Michael P. Pfeiffer" Date: Tue, 1 Jun 2021 12:39:51 +0200 Subject: [PATCH 2/3] Update All Reviews block transform to more closely resemble the widget MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Albert Juhé Lluveras --- assets/js/blocks/reviews/all-reviews/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/assets/js/blocks/reviews/all-reviews/index.js b/assets/js/blocks/reviews/all-reviews/index.js index 1008e605c8c..270ab01eca4 100644 --- a/assets/js/blocks/reviews/all-reviews/index.js +++ b/assets/js/blocks/reviews/all-reviews/index.js @@ -62,6 +62,11 @@ registerBlockType( 'woocommerce/all-reviews', { transform: ( { instance } ) => createBlock( 'woocommerce/all-reviews', { reviewsOnPageLoad: instance.raw.number, + imageType: 'product', + showLoadMore: false, + showOrderby: false, + showReviewDate: false, + showReviewContent: false, } ), }, ], From 5261ba26912825a4eed45588231f42e19af6602b Mon Sep 17 00:00:00 2001 From: "Michael P. Pfeiffer" Date: Wed, 2 Jun 2021 16:41:03 +0200 Subject: [PATCH 3/3] Convert integers to boolean --- assets/js/blocks/product-categories/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/assets/js/blocks/product-categories/index.js b/assets/js/blocks/product-categories/index.js index cd8727cf631..87add60886b 100644 --- a/assets/js/blocks/product-categories/index.js +++ b/assets/js/blocks/product-categories/index.js @@ -94,10 +94,10 @@ registerBlockType( 'woocommerce/product-categories', { !! instance?.raw, transform: ( { instance } ) => createBlock( 'woocommerce/product-categories', { - hasCount: instance.raw.count, + hasCount: !! instance.raw.count, hasEmpty: ! instance.raw.hide_empty, - isDropdown: instance.raw.dropdown, - isHierarchical: instance.raw.hierarchical, + isDropdown: !! instance.raw.dropdown, + isHierarchical: !! instance.raw.hierarchical, } ), }, ],