Remove deprecated shortcode save handling from SSR Blocks (https://github.com/woocommerce/woocommerce-blocks/pull/4010)
This commit is contained in:
parent
9bdcbe0b4d
commit
25544f3ce4
|
@ -22,9 +22,3 @@ The [Newest Products block](https://github.com/woocommerce/woocommerce-gutenberg
|
|||
We include settings in the sidebar, called the Inspector in gutenberg. [See an example of this.](https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/5c9d587fcc0b9e652813a42b66eafa5520c7ac88/assets/js/blocks/product-new/block.js#L71)
|
||||
|
||||
Other blocks have the concept of an "edit state", like when you need to pick a product in the Featured Product block, or [pick a category in the Products by Category block.](https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/5c9d587fcc0b9e652813a42b66eafa5520c7ac88/assets/js/blocks/product-category/block.js#L140)
|
||||
|
||||
### Saving
|
||||
|
||||
Usually blocks can be converted to HTML in the save function, so that what's saved into the database is the same HTML that's rendered on the frontend. Our blocks are different since they need to show the latest products and reflect any changes to products.
|
||||
|
||||
The grid blocks are saved as shortcodes using [`getShortcode`,](https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/5c9d587fcc0b9e652813a42b66eafa5520c7ac88/assets/js/utils/get-shortcode.js) but the Featured Product block is considered a "dynamic block", so we use PHP to build that each time the post is loaded on the frontend. The code for that is set up when [the block is registered in PHP](https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/5c9d587fcc0b9e652813a42b66eafa5520c7ac88/assets/php/class-wgpb-block-library.php#L216), and lives in [`WGPB_Block_Featured_Product`.](https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/5c9d587fcc0b9e652813a42b66eafa5520c7ac88/assets/php/class-wgpb-block-featured-product.php)
|
||||
|
|
|
@ -11,7 +11,6 @@ import { Icon, widgets } from '@woocommerce/icons';
|
|||
*/
|
||||
import './editor.scss';
|
||||
import Block from './block';
|
||||
import { deprecatedConvertToShortcode } from '../../utils/deprecations';
|
||||
|
||||
registerBlockType( 'woocommerce/handpicked-products', {
|
||||
title: __( 'Hand-picked Products', 'woo-gutenberg-products-block' ),
|
||||
|
@ -107,45 +106,6 @@ registerBlockType( 'woocommerce/handpicked-products', {
|
|||
},
|
||||
},
|
||||
|
||||
deprecated: [
|
||||
{
|
||||
// Deprecate shortcode save method in favor of dynamic rendering.
|
||||
attributes: {
|
||||
align: {
|
||||
type: 'string',
|
||||
},
|
||||
columns: {
|
||||
type: 'number',
|
||||
default: DEFAULT_COLUMNS,
|
||||
},
|
||||
editMode: {
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
},
|
||||
contentVisibility: {
|
||||
type: 'object',
|
||||
default: {
|
||||
title: true,
|
||||
price: true,
|
||||
rating: true,
|
||||
button: true,
|
||||
},
|
||||
},
|
||||
orderby: {
|
||||
type: 'string',
|
||||
default: 'date',
|
||||
},
|
||||
products: {
|
||||
type: 'array',
|
||||
default: [],
|
||||
},
|
||||
},
|
||||
save: deprecatedConvertToShortcode(
|
||||
'woocommerce/handpicked-products'
|
||||
),
|
||||
},
|
||||
],
|
||||
|
||||
/**
|
||||
* Renders and manages the block.
|
||||
*
|
||||
|
|
|
@ -10,7 +10,6 @@ import { createBlock, registerBlockType } from '@wordpress/blocks';
|
|||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import { deprecatedConvertToShortcode } from '../../utils/deprecations';
|
||||
import sharedAttributes, {
|
||||
sharedAttributeBlockTypes,
|
||||
} from '../../utils/shared-attributes';
|
||||
|
@ -57,16 +56,6 @@ registerBlockType( 'woocommerce/product-best-sellers', {
|
|||
],
|
||||
},
|
||||
|
||||
deprecated: [
|
||||
{
|
||||
// Deprecate shortcode save method in favor of dynamic rendering.
|
||||
attributes: sharedAttributes,
|
||||
save: deprecatedConvertToShortcode(
|
||||
'woocommerce/product-best-sellers'
|
||||
),
|
||||
},
|
||||
],
|
||||
|
||||
/**
|
||||
* Renders and manages the block.
|
||||
*
|
||||
|
|
|
@ -11,7 +11,6 @@ import { Icon, folder } from '@woocommerce/icons';
|
|||
*/
|
||||
import './editor.scss';
|
||||
import Block from './block';
|
||||
import { deprecatedConvertToShortcode } from '../../utils/deprecations';
|
||||
import sharedAttributes, {
|
||||
sharedAttributeBlockTypes,
|
||||
} from '../../utils/shared-attributes';
|
||||
|
@ -77,26 +76,6 @@ registerBlockType( 'woocommerce/product-category', {
|
|||
],
|
||||
},
|
||||
|
||||
deprecated: [
|
||||
{
|
||||
// Deprecate shortcode save method in favor of dynamic rendering.
|
||||
attributes: {
|
||||
...sharedAttributes,
|
||||
editMode: {
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
},
|
||||
orderby: {
|
||||
type: 'string',
|
||||
default: 'date',
|
||||
},
|
||||
},
|
||||
save: deprecatedConvertToShortcode(
|
||||
'woocommerce/product-category'
|
||||
),
|
||||
},
|
||||
],
|
||||
|
||||
/**
|
||||
* Renders and manages the block.
|
||||
*
|
||||
|
|
|
@ -10,7 +10,6 @@ import { Icon, exclamation } from '@woocommerce/icons';
|
|||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import { deprecatedConvertToShortcode } from '../../utils/deprecations';
|
||||
import sharedAttributes, {
|
||||
sharedAttributeBlockTypes,
|
||||
} from '../../utils/shared-attributes';
|
||||
|
@ -53,14 +52,6 @@ registerBlockType( 'woocommerce/product-new', {
|
|||
],
|
||||
},
|
||||
|
||||
deprecated: [
|
||||
{
|
||||
// Deprecate shortcode save method in favor of dynamic rendering.
|
||||
attributes: sharedAttributes,
|
||||
save: deprecatedConvertToShortcode( 'woocommerce/product-new' ),
|
||||
},
|
||||
],
|
||||
|
||||
/**
|
||||
* Renders and manages the block.
|
||||
*
|
||||
|
|
|
@ -10,7 +10,6 @@ import { Icon, tag } from '@woocommerce/icons';
|
|||
*/
|
||||
import Block from './block';
|
||||
import './editor.scss';
|
||||
import { deprecatedConvertToShortcode } from '../../utils/deprecations';
|
||||
import sharedAttributes, {
|
||||
sharedAttributeBlockTypes,
|
||||
} from '../../utils/shared-attributes';
|
||||
|
@ -61,20 +60,6 @@ registerBlockType( 'woocommerce/product-on-sale', {
|
|||
],
|
||||
},
|
||||
|
||||
deprecated: [
|
||||
{
|
||||
// Deprecate shortcode save method in favor of dynamic rendering.
|
||||
attributes: {
|
||||
...sharedAttributes,
|
||||
orderby: {
|
||||
type: 'string',
|
||||
default: 'date',
|
||||
},
|
||||
},
|
||||
save: deprecatedConvertToShortcode( 'woocommerce/product-on-sale' ),
|
||||
},
|
||||
],
|
||||
|
||||
/**
|
||||
* Renders and manages the block.
|
||||
*
|
||||
|
|
|
@ -10,7 +10,6 @@ import { without } from 'lodash';
|
|||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import { deprecatedConvertToShortcode } from '../../utils/deprecations';
|
||||
import sharedAttributes, {
|
||||
sharedAttributeBlockTypes,
|
||||
} from '../../utils/shared-attributes';
|
||||
|
@ -53,14 +52,6 @@ registerBlockType( blockTypeName, {
|
|||
],
|
||||
},
|
||||
|
||||
deprecated: [
|
||||
{
|
||||
// Deprecate shortcode save method in favor of dynamic rendering.
|
||||
attributes: sharedAttributes,
|
||||
save: deprecatedConvertToShortcode( blockTypeName ),
|
||||
},
|
||||
],
|
||||
|
||||
/**
|
||||
* Renders and manages the block.
|
||||
*
|
||||
|
|
|
@ -11,7 +11,6 @@ import { DEFAULT_COLUMNS, DEFAULT_ROWS } from '@woocommerce/block-settings';
|
|||
*/
|
||||
import './editor.scss';
|
||||
import Block from './block';
|
||||
import { deprecatedConvertToShortcode } from '../../utils/deprecations';
|
||||
|
||||
const blockTypeName = 'woocommerce/products-by-attribute';
|
||||
|
||||
|
@ -115,48 +114,6 @@ registerBlockType( blockTypeName, {
|
|||
},
|
||||
},
|
||||
|
||||
deprecated: [
|
||||
{
|
||||
// Deprecate shortcode save method in favor of dynamic rendering.
|
||||
attributes: {
|
||||
attributes: {
|
||||
type: 'array',
|
||||
default: [],
|
||||
},
|
||||
attrOperator: {
|
||||
type: 'string',
|
||||
default: 'any',
|
||||
},
|
||||
columns: {
|
||||
type: 'number',
|
||||
default: DEFAULT_COLUMNS,
|
||||
},
|
||||
editMode: {
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
},
|
||||
contentVisibility: {
|
||||
type: 'object',
|
||||
default: {
|
||||
title: true,
|
||||
price: true,
|
||||
rating: true,
|
||||
button: true,
|
||||
},
|
||||
},
|
||||
orderby: {
|
||||
type: 'string',
|
||||
default: 'date',
|
||||
},
|
||||
rows: {
|
||||
type: 'number',
|
||||
default: DEFAULT_ROWS,
|
||||
},
|
||||
},
|
||||
save: deprecatedConvertToShortcode( blockTypeName ),
|
||||
},
|
||||
],
|
||||
|
||||
/**
|
||||
* Renders and manages the block.
|
||||
*
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import classnames from 'classnames';
|
||||
import { RawHTML } from '@wordpress/element';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import getShortcode from './get-shortcode';
|
||||
|
||||
/**
|
||||
* Return a save function using the blockType to generate the correct shortcode.
|
||||
*
|
||||
* @param {*} blockType Block being rendered.
|
||||
*/
|
||||
export const deprecatedConvertToShortcode = ( blockType ) => {
|
||||
return function ( props ) {
|
||||
const { align, contentVisibility } = props.attributes;
|
||||
const classes = classnames( align ? `align${ align }` : '', {
|
||||
'is-hidden-title': ! contentVisibility.title,
|
||||
'is-hidden-price': ! contentVisibility.price,
|
||||
'is-hidden-rating': ! contentVisibility.rating,
|
||||
'is-hidden-button': ! contentVisibility.button,
|
||||
} );
|
||||
return (
|
||||
<RawHTML className={ classes }>
|
||||
{ getShortcode( props, blockType ) }
|
||||
</RawHTML>
|
||||
);
|
||||
};
|
||||
};
|
|
@ -1,99 +0,0 @@
|
|||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { DEFAULT_COLUMNS, DEFAULT_ROWS } from '@woocommerce/block-settings';
|
||||
|
||||
export default function getShortcode( props, name ) {
|
||||
const blockAttributes = props.attributes;
|
||||
const {
|
||||
attributes,
|
||||
attrOperator,
|
||||
categories,
|
||||
catOperator,
|
||||
orderby,
|
||||
products,
|
||||
} = blockAttributes;
|
||||
const columns = blockAttributes.columns || DEFAULT_COLUMNS;
|
||||
const rows = blockAttributes.rows || DEFAULT_ROWS;
|
||||
|
||||
const shortcodeAtts = new Map();
|
||||
shortcodeAtts.set( 'limit', rows * columns );
|
||||
shortcodeAtts.set( 'columns', columns );
|
||||
|
||||
if ( categories && categories.length ) {
|
||||
shortcodeAtts.set( 'category', categories.join( ',' ) );
|
||||
if ( catOperator && catOperator === 'all' ) {
|
||||
shortcodeAtts.set( 'cat_operator', 'AND' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( attributes && attributes.length ) {
|
||||
shortcodeAtts.set(
|
||||
'terms',
|
||||
attributes.map( ( { id } ) => id ).join( ',' )
|
||||
);
|
||||
shortcodeAtts.set( 'attribute', attributes[ 0 ].attr_slug );
|
||||
if ( attrOperator && attrOperator === 'all' ) {
|
||||
shortcodeAtts.set( 'terms_operator', 'AND' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( orderby ) {
|
||||
if ( orderby === 'price_desc' ) {
|
||||
shortcodeAtts.set( 'orderby', 'price' );
|
||||
shortcodeAtts.set( 'order', 'DESC' );
|
||||
} else if ( orderby === 'price_asc' ) {
|
||||
shortcodeAtts.set( 'orderby', 'price' );
|
||||
shortcodeAtts.set( 'order', 'ASC' );
|
||||
} else if ( orderby === 'date' ) {
|
||||
shortcodeAtts.set( 'orderby', 'date' );
|
||||
shortcodeAtts.set( 'order', 'DESC' );
|
||||
} else {
|
||||
shortcodeAtts.set( 'orderby', orderby );
|
||||
}
|
||||
}
|
||||
|
||||
// Toggle shortcode atts depending on block type.
|
||||
switch ( name ) {
|
||||
case 'woocommerce/product-best-sellers':
|
||||
shortcodeAtts.set( 'best_selling', '1' );
|
||||
break;
|
||||
case 'woocommerce/product-top-rated':
|
||||
shortcodeAtts.set( 'orderby', 'rating' );
|
||||
break;
|
||||
case 'woocommerce/product-on-sale':
|
||||
shortcodeAtts.set( 'on_sale', '1' );
|
||||
break;
|
||||
case 'woocommerce/product-new':
|
||||
shortcodeAtts.set( 'orderby', 'date' );
|
||||
shortcodeAtts.set( 'order', 'DESC' );
|
||||
break;
|
||||
case 'woocommerce/handpicked-products':
|
||||
if ( ! products.length ) {
|
||||
return '';
|
||||
}
|
||||
shortcodeAtts.set( 'ids', products.join( ',' ) );
|
||||
shortcodeAtts.set( 'limit', products.length );
|
||||
break;
|
||||
case 'woocommerce/product-category':
|
||||
if ( ! categories || ! categories.length ) {
|
||||
return '';
|
||||
}
|
||||
break;
|
||||
case 'woocommerce/products-by-attribute':
|
||||
if ( ! attributes || ! attributes.length ) {
|
||||
return '';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Build the shortcode string out of the set shortcode attributes.
|
||||
let shortcode = '[products';
|
||||
for ( const [ key, value ] of shortcodeAtts ) {
|
||||
/* eslint-disable-line */
|
||||
shortcode += ' ' + key + '="' + value + '"';
|
||||
}
|
||||
shortcode += ']';
|
||||
|
||||
return shortcode;
|
||||
}
|
Loading…
Reference in New Issue