Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 9257076

Browse files
frontdevdeAljullu
andauthored
Block Widgets: add block transforms for legacy widgets with a block equivalent (#4292)
* 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 <[email protected]> * Convert integers to boolean Co-authored-by: Albert Juhé Lluveras <[email protected]>
1 parent b6a9cc6 commit 9257076

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

assets/js/blocks/product-categories/index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* External dependencies
33
*/
44
import { __ } from '@wordpress/i18n';
5-
import { registerBlockType } from '@wordpress/blocks';
5+
import { createBlock, registerBlockType } from '@wordpress/blocks';
66
import { Icon, list } from '@woocommerce/icons';
77

88
/**
@@ -83,6 +83,26 @@ registerBlockType( 'woocommerce/product-categories', {
8383
},
8484
},
8585

86+
transforms: {
87+
from: [
88+
{
89+
type: 'block',
90+
blocks: [ 'core/legacy-widget' ],
91+
// We can't transform if raw instance isn't shown in the REST API.
92+
isMatch: ( { idBase, instance } ) =>
93+
idBase === 'woocommerce_product_categories' &&
94+
!! instance?.raw,
95+
transform: ( { instance } ) =>
96+
createBlock( 'woocommerce/product-categories', {
97+
hasCount: !! instance.raw.count,
98+
hasEmpty: ! instance.raw.hide_empty,
99+
isDropdown: !! instance.raw.dropdown,
100+
isHierarchical: !! instance.raw.hierarchical,
101+
} ),
102+
},
103+
],
104+
},
105+
86106
deprecated: [
87107
{
88108
// Deprecate HTML save method in favor of dynamic rendering.

assets/js/blocks/product-search/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* External dependencies
33
*/
44
import { __ } from '@wordpress/i18n';
5-
import { registerBlockType } from '@wordpress/blocks';
5+
import { createBlock, registerBlockType } from '@wordpress/blocks';
66
import { Icon, search } from '@woocommerce/icons';
77
/**
88
* Internal dependencies
@@ -71,6 +71,25 @@ registerBlockType( 'woocommerce/product-search', {
7171
},
7272
},
7373

74+
transforms: {
75+
from: [
76+
{
77+
type: 'block',
78+
blocks: [ 'core/legacy-widget' ],
79+
// We can't transform if raw instance isn't shown in the REST API.
80+
isMatch: ( { idBase, instance } ) =>
81+
idBase === 'woocommerce_product_search' && !! instance?.raw,
82+
transform: ( { instance } ) =>
83+
createBlock( 'woocommerce/product-search', {
84+
label:
85+
instance.raw.title === ''
86+
? __( 'Search', 'woo-gutenberg-products-block' )
87+
: instance.raw.title,
88+
} ),
89+
},
90+
],
91+
},
92+
7493
edit,
7594

7695
/**

assets/js/blocks/reviews/all-reviews/index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* External dependencies
33
*/
44
import { __ } from '@wordpress/i18n';
5-
import { registerBlockType } from '@wordpress/blocks';
5+
import { createBlock, registerBlockType } from '@wordpress/blocks';
66
import { Icon, discussion } from '@woocommerce/icons';
77

88
/**
@@ -51,6 +51,27 @@ registerBlockType( 'woocommerce/all-reviews', {
5151
},
5252
},
5353

54+
transforms: {
55+
from: [
56+
{
57+
type: 'block',
58+
blocks: [ 'core/legacy-widget' ],
59+
// We can't transform if raw instance isn't shown in the REST API.
60+
isMatch: ( { idBase, instance } ) =>
61+
idBase === 'woocommerce_recent_reviews' && !! instance?.raw,
62+
transform: ( { instance } ) =>
63+
createBlock( 'woocommerce/all-reviews', {
64+
reviewsOnPageLoad: instance.raw.number,
65+
imageType: 'product',
66+
showLoadMore: false,
67+
showOrderby: false,
68+
showReviewDate: false,
69+
showReviewContent: false,
70+
} ),
71+
},
72+
],
73+
},
74+
5475
/**
5576
* Renders and manages the block.
5677
*

0 commit comments

Comments
 (0)