From 20f70b662694ae9abb967c579603c8da7000d85f Mon Sep 17 00:00:00 2001 From: "Michael P. Pfeiffer" Date: Fri, 4 Jun 2021 16:47:30 +0200 Subject: [PATCH] Block Widgets: add block transforms for legacy widgets with a block equivalent (https://github.com/woocommerce/woocommerce-blocks/pull/4292) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Block Widgets: add transforms for legacy widgets with a block equivalent * Update All Reviews block transform to more closely resemble the widget Co-authored-by: Albert Juhé Lluveras * Convert integers to boolean Co-authored-by: Albert Juhé Lluveras --- .../js/blocks/product-categories/index.js | 22 +++++++++++++++++- .../assets/js/blocks/product-search/index.js | 21 ++++++++++++++++- .../js/blocks/reviews/all-reviews/index.js | 23 ++++++++++++++++++- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/plugins/woocommerce-blocks/assets/js/blocks/product-categories/index.js b/plugins/woocommerce-blocks/assets/js/blocks/product-categories/index.js index 865a3658c76..87add60886b 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/product-categories/index.js +++ b/plugins/woocommerce-blocks/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/plugins/woocommerce-blocks/assets/js/blocks/product-search/index.js b/plugins/woocommerce-blocks/assets/js/blocks/product-search/index.js index 7bb7a132e59..656e85220e1 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/product-search/index.js +++ b/plugins/woocommerce-blocks/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/plugins/woocommerce-blocks/assets/js/blocks/reviews/all-reviews/index.js b/plugins/woocommerce-blocks/assets/js/blocks/reviews/all-reviews/index.js index 2700ce0ffc3..270ab01eca4 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/reviews/all-reviews/index.js +++ b/plugins/woocommerce-blocks/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,27 @@ 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, + imageType: 'product', + showLoadMore: false, + showOrderby: false, + showReviewDate: false, + showReviewContent: false, + } ), + }, + ], + }, + /** * Renders and manages the block. *