From bdb880bf06b2b1a4e4deeeaf70bb37b173116ecc Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Mon, 10 Oct 2022 11:48:45 +0200 Subject: [PATCH] Products by Rating: Add block transform for the legacy rating widget (#7338) * Products by Rating: Add legacy rating widget deprecation * Products by Rating: Hide legacy Product Ratings widget --- assets/js/blocks/rating-filter/index.tsx | 25 +++++++++++++++++++++++- src/BlockTypesController.php | 3 ++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/assets/js/blocks/rating-filter/index.tsx b/assets/js/blocks/rating-filter/index.tsx index 386a2f381d0..1f11c03e2ac 100644 --- a/assets/js/blocks/rating-filter/index.tsx +++ b/assets/js/blocks/rating-filter/index.tsx @@ -1,8 +1,9 @@ /** * External dependencies */ +import { __ } from '@wordpress/i18n'; import { isExperimentalBuild } from '@woocommerce/block-settings'; -import { registerBlockType } from '@wordpress/blocks'; +import { createBlock, registerBlockType } from '@wordpress/blocks'; import { Icon, starEmpty } from '@wordpress/icons'; import classNames from 'classnames'; import { useBlockProps } from '@wordpress/block-editor'; @@ -29,6 +30,28 @@ if ( isExperimentalBuild() ) { ...metadata.attributes, ...blockAttributes, }, + 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_rating_filter' && + !! instance?.raw, + transform: ( { instance } ) => + createBlock( 'woocommerce/rating-filter', { + heading: + instance?.raw?.title || + __( + 'Filter by rating', + 'woo-gutenberg-products-block' + ), + headingLevel: 3, + } ), + }, + ], + }, edit, // Save the props to post content. save( { attributes }: { attributes: Attributes } ) { diff --git a/src/BlockTypesController.php b/src/BlockTypesController.php index bfe75ac2d34..09e33812ad1 100644 --- a/src/BlockTypesController.php +++ b/src/BlockTypesController.php @@ -148,7 +148,8 @@ public function hide_legacy_widgets_with_block_equivalent( $widget_types ) { 'woocommerce_product_tag_cloud', 'woocommerce_price_filter', 'woocommerce_layered_nav', - 'woocommerce_layered_nav_filters' + 'woocommerce_layered_nav_filters', + 'woocommerce_rating_filter' ); return $widget_types;