2023-02-27 14:34:18 +00:00
/ * *
* External dependencies
* /
2024-06-12 13:45:09 +00:00
import { isWpVersion } from '@woocommerce/settings' ;
import { __ , sprintf } from '@wordpress/i18n' ;
import {
INNER_BLOCKS_TEMPLATE as productCollectionInnerBlocksTemplate ,
DEFAULT_ATTRIBUTES as productCollectionDefaultAttributes ,
DEFAULT_QUERY as productCollectionDefaultQuery ,
} from '@woocommerce/blocks/product-collection/constants' ;
2023-02-27 14:34:18 +00:00
import {
createBlock ,
2024-06-12 13:45:09 +00:00
// @ts-expect-error Type definitions for this function are missing in Gutenberg
2023-02-27 14:34:18 +00:00
createBlocksFromInnerBlocksTemplate ,
type BlockInstance ,
} from '@wordpress/blocks' ;
/ * *
* Internal dependencies
* /
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
2024-06-12 13:45:09 +00:00
const createProductCollectionBlock = (
inheritedAttributes : InheritedAttributes
) = >
2023-04-03 14:29:08 +00:00
createBlock (
2024-06-12 13:45:09 +00:00
'woocommerce/product-collection' ,
2023-02-27 14:34:18 +00:00
{
2024-06-12 13:45:09 +00:00
. . . productCollectionDefaultAttributes ,
2023-02-27 14:34:18 +00:00
. . . inheritedAttributes ,
query : {
2024-06-12 13:45:09 +00:00
. . . productCollectionDefaultQuery ,
2023-02-27 14:34:18 +00:00
inherit : true ,
} ,
} ,
2024-06-12 13:45:09 +00:00
createBlocksFromInnerBlocksTemplate (
productCollectionInnerBlocksTemplate
)
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
) ,
2024-06-12 13:45:09 +00:00
createProductCollectionBlock ( 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-05-22 12:17:56 +00:00
return 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-12-12 22:12:36 +00:00
'woocommerce'
2023-02-27 14:34:18 +00:00
) ,
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.' ,
2023-12-12 22:12:36 +00:00
'woocommerce'
2023-02-27 14:34:18 +00:00
) ,
templateTitle
) ;
const getDescription = ( templateTitle : string , canConvert : boolean ) = > {
if ( canConvert ) {
return getDescriptionAllowingConversion ( templateTitle ) ;
}
return getDescriptionDisallowingConversion ( templateTitle ) ;
} ;
2023-12-12 23:05:20 +00:00
const getButtonLabel = ( ) = > __ ( 'Transform into blocks' , 'woocommerce' ) ;
2023-05-10 11:02:33 +00:00
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
2023-06-29 13:41:22 +00:00
const productCatalogBlockifyConfig = {
getButtonLabel ,
onClickCallback ,
2023-02-27 14:34:18 +00:00
getBlockifiedTemplate ,
2023-06-29 13:41:22 +00:00
} ;
const productTaxonomyBlockifyConfig = {
getButtonLabel ,
onClickCallback : onClickCallbackWithTermDescription ,
getBlockifiedTemplate : getBlockifiedTemplateWithTermDescription ,
} ;
export const blockifiedProductCatalogConfig = {
2023-02-27 14:34:18 +00:00
isConversionPossible ,
getDescription ,
2023-06-29 13:41:22 +00:00
blockifyConfig : productCatalogBlockifyConfig ,
2023-02-27 14:34:18 +00:00
} ;
export const blockifiedProductTaxonomyConfig = {
isConversionPossible ,
getDescription ,
2023-06-29 13:41:22 +00:00
blockifyConfig : productTaxonomyBlockifyConfig ,
2023-02-27 14:34:18 +00:00
} ;