2019-01-10 18:16:37 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-02-21 19:00:47 +00:00
|
|
|
import classnames from 'classnames';
|
2019-02-26 05:31:25 +00:00
|
|
|
import { createBlock, registerBlockType } from '@wordpress/blocks';
|
|
|
|
import { without } from 'lodash';
|
2019-01-10 18:16:37 +00:00
|
|
|
import Gridicon from 'gridicons';
|
|
|
|
import { RawHTML } from '@wordpress/element';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Block from './block';
|
|
|
|
import getShortcode from '../../utils/get-shortcode';
|
2019-02-26 05:31:25 +00:00
|
|
|
import sharedAttributes, { sharedAttributeBlockTypes } from '../../utils/shared-attributes';
|
2019-01-10 18:16:37 +00:00
|
|
|
|
|
|
|
registerBlockType( 'woocommerce/product-on-sale', {
|
|
|
|
title: __( 'On Sale Products', 'woo-gutenberg-products-block' ),
|
|
|
|
icon: <Gridicon icon="tag" />,
|
|
|
|
category: 'woocommerce',
|
|
|
|
keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ],
|
|
|
|
description: __(
|
|
|
|
'Display a grid of on sale products.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
supports: {
|
|
|
|
align: [ 'wide', 'full' ],
|
|
|
|
},
|
|
|
|
attributes: {
|
|
|
|
...sharedAttributes,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How to order the products: 'date', 'popularity', 'price_asc', 'price_desc' 'rating', 'title'.
|
|
|
|
*/
|
|
|
|
orderby: {
|
|
|
|
type: 'string',
|
|
|
|
default: 'date',
|
|
|
|
},
|
|
|
|
},
|
2019-02-26 05:31:25 +00:00
|
|
|
transforms: {
|
|
|
|
from: [
|
|
|
|
{
|
|
|
|
type: 'block',
|
|
|
|
blocks: without( sharedAttributeBlockTypes, 'woocommerce/product-on-sale' ),
|
|
|
|
transform: ( attributes ) => createBlock(
|
|
|
|
'woocommerce/product-on-sale',
|
|
|
|
attributes
|
|
|
|
),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-01-10 18:16:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders and manages the block.
|
|
|
|
*/
|
|
|
|
edit( props ) {
|
|
|
|
return <Block { ...props } />;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the block content in the post content. Block content is saved as a products shortcode.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
save( props ) {
|
|
|
|
const {
|
|
|
|
align,
|
2019-02-21 19:00:47 +00:00
|
|
|
contentVisibility,
|
2019-01-10 18:16:37 +00:00
|
|
|
} = props.attributes; /* eslint-disable-line react/prop-types */
|
2019-02-21 19:00:47 +00:00
|
|
|
const classes = classnames(
|
|
|
|
align ? `align${ align }` : '',
|
|
|
|
{
|
|
|
|
'is-hidden-title': ! contentVisibility.title,
|
|
|
|
'is-hidden-price': ! contentVisibility.price,
|
|
|
|
'is-hidden-button': ! contentVisibility.button,
|
|
|
|
}
|
|
|
|
);
|
2019-01-10 18:16:37 +00:00
|
|
|
return (
|
2019-02-21 19:00:47 +00:00
|
|
|
<RawHTML className={ classes }>
|
2019-01-10 18:16:37 +00:00
|
|
|
{ getShortcode( props, 'woocommerce/product-on-sale' ) }
|
|
|
|
</RawHTML>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} );
|