2023-02-27 14:34:18 +00:00
/ * *
* External dependencies
* /
import {
createBlock ,
createBlocksFromInnerBlocksTemplate ,
type BlockInstance ,
} from '@wordpress/blocks' ;
import { isWpVersion } from '@woocommerce/settings' ;
2023-03-30 13:57:33 +00:00
import { isExperimentalBuild } from '@woocommerce/block-settings' ;
2023-02-27 14:34:18 +00:00
import { __ , sprintf } from '@wordpress/i18n' ;
/ * *
* Internal dependencies
* /
import {
INNER_BLOCKS_TEMPLATE as productsInnerBlocksTemplate ,
QUERY_DEFAULT_ATTRIBUTES as productsQueryDefaultAttributes ,
} from '../product-query/constants' ;
import { VARIATION_NAME as productsVariationName } from '../product-query/variations/product-query' ;
import { createArchiveTitleBlock , createRowBlock } from './utils' ;
2023-05-10 11:02:33 +00:00
import { OnClickCallbackParameter , type InheritedAttributes } from './types' ;
2023-02-27 14:34:18 +00:00
2023-04-03 14:29:08 +00:00
const createProductsBlock = ( inheritedAttributes : InheritedAttributes ) = >
createBlock (
2023-02-27 14:34:18 +00:00
'core/query' ,
{
. . . productsQueryDefaultAttributes ,
. . . inheritedAttributes ,
namespace : productsVariationName ,
query : {
. . . productsQueryDefaultAttributes . query ,
inherit : true ,
} ,
} ,
2023-04-03 14:29:08 +00:00
createBlocksFromInnerBlocksTemplate ( productsInnerBlocksTemplate )
2023-02-27 14:34:18 +00:00
) ;
const getBlockifiedTemplate = (
inheritedAttributes : InheritedAttributes ,
withTermDescription = false
2023-04-03 14:29:08 +00:00
) = >
[
2023-02-27 14:34:18 +00:00
createBlock ( 'woocommerce/breadcrumbs' , inheritedAttributes ) ,
createArchiveTitleBlock ( 'archive-title' , inheritedAttributes ) ,
withTermDescription
? createBlock ( 'core/term-description' , inheritedAttributes )
: null ,
createBlock ( 'woocommerce/store-notices' , inheritedAttributes ) ,
createRowBlock (
[
createBlock ( 'woocommerce/product-results-count' ) ,
createBlock ( 'woocommerce/catalog-sorting' ) ,
] ,
inheritedAttributes
) ,
2023-04-03 14:29:08 +00:00
createProductsBlock ( inheritedAttributes ) ,
2023-02-27 14:34:18 +00:00
] . filter ( Boolean ) as BlockInstance [ ] ;
const getBlockifiedTemplateWithTermDescription = (
inheritedAttributes : InheritedAttributes
) = > getBlockifiedTemplate ( inheritedAttributes , true ) ;
const isConversionPossible = ( ) = > {
// Blockification is possible for the WP version 6.1 and above,
// which are the versions the Products block supports.
2023-03-30 13:57:33 +00:00
return isExperimentalBuild ( ) && isWpVersion ( '6.1' , '>=' ) ;
2023-02-27 14:34:18 +00:00
} ;
const getDescriptionAllowingConversion = ( templateTitle : string ) = >
sprintf (
/* translators: %s is the template title */
__ (
2023-05-10 11:02:33 +00:00
'Transform this template into multiple blocks so you can add, remove, reorder, and customize your %s template.' ,
2023-02-27 14:34:18 +00:00
'woo-gutenberg-products-block'
) ,
templateTitle
) ;
const getDescriptionDisallowingConversion = ( templateTitle : string ) = >
sprintf (
/* translators: %s is the template title */
__ (
'This block serves as a placeholder for your %s. It will display the actual product image, title, price in your store. You can move this placeholder around and add more blocks around to customize the template.' ,
'woo-gutenberg-products-block'
) ,
templateTitle
) ;
const getDescription = ( templateTitle : string , canConvert : boolean ) = > {
if ( canConvert ) {
return getDescriptionAllowingConversion ( templateTitle ) ;
}
return getDescriptionDisallowingConversion ( templateTitle ) ;
} ;
const getButtonLabel = ( ) = >
2023-05-10 11:02:33 +00:00
__ ( 'Transform into blocks' , 'woo-gutenberg-products-block' ) ;
const onClickCallback = ( {
clientId ,
attributes ,
getBlocks ,
replaceBlock ,
selectBlock ,
} : OnClickCallbackParameter ) = > {
replaceBlock ( clientId , getBlockifiedTemplate ( attributes ) ) ;
const blocks = getBlocks ( ) ;
const groupBlock = blocks . find (
( block ) = >
block . name === 'core/group' &&
block . innerBlocks . some (
( innerBlock ) = >
innerBlock . name === 'woocommerce/store-notices'
)
) ;
if ( groupBlock ) {
selectBlock ( groupBlock . clientId ) ;
}
} ;
const onClickCallbackWithTermDescription = ( {
clientId ,
attributes ,
getBlocks ,
replaceBlock ,
selectBlock ,
} : OnClickCallbackParameter ) = > {
replaceBlock ( clientId , getBlockifiedTemplate ( attributes , true ) ) ;
const blocks = getBlocks ( ) ;
const groupBlock = blocks . find (
( block ) = >
block . name === 'core/group' &&
block . innerBlocks . some (
( innerBlock ) = >
innerBlock . name === 'woocommerce/store-notices'
)
) ;
if ( groupBlock ) {
selectBlock ( groupBlock . clientId ) ;
}
} ;
2023-02-27 14:34:18 +00:00
export const blockifiedProductCatalogConfig = {
getBlockifiedTemplate ,
isConversionPossible ,
getDescription ,
getButtonLabel ,
2023-05-10 11:02:33 +00:00
onClickCallback ,
2023-02-27 14:34:18 +00:00
} ;
export const blockifiedProductTaxonomyConfig = {
getBlockifiedTemplate : getBlockifiedTemplateWithTermDescription ,
2023-05-10 11:02:33 +00:00
onClickCallback : onClickCallbackWithTermDescription ,
2023-02-27 14:34:18 +00:00
isConversionPossible ,
getDescription ,
getButtonLabel ,
} ;