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

Block Widgets: add block transforms for legacy widgets with a block equivalent #4292

Merged
merged 3 commits into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion assets/js/blocks/product-categories/index.js
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 20 additions & 1 deletion assets/js/blocks/product-search/index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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,

/**
Expand Down
23 changes: 22 additions & 1 deletion assets/js/blocks/reviews/all-reviews/index.js
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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,
frontdevde marked this conversation as resolved.
Show resolved Hide resolved
imageType: 'product',
showLoadMore: false,
showOrderby: false,
showReviewDate: false,
showReviewContent: false,
} ),
},
],
},

/**
* Renders and manages the block.
*
Expand Down