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 Guteberg
2023-02-27 14:34:18 +00:00
createBlocksFromInnerBlocksTemplate ,
type BlockInstance ,
type InnerBlockTemplate ,
} 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
const createNoResultsParagraph = ( ) = >
createBlock ( 'core/paragraph' , {
content : __ (
'No products were found matching your selection.' ,
2023-12-12 22:12:36 +00:00
'woocommerce'
2023-02-27 14:34:18 +00:00
) ,
} ) ;
const createProductSearch = ( ) = >
createBlock ( 'core/search' , {
buttonPosition : 'button-outside' ,
2023-12-12 22:12:36 +00:00
buttonText : __ ( 'Search' , 'woocommerce' ) ,
2023-02-27 14:34:18 +00:00
buttonUseIcon : false ,
showLabel : false ,
2023-12-12 22:12:36 +00:00
placeholder : __ ( 'Search products…' , 'woocommerce' ) ,
2023-02-27 14:34:18 +00:00
query : { post_type : 'product' } ,
} ) ;
const extendInnerBlocksWithNoResultsContent = (
innerBlocks : InnerBlockTemplate [ ] ,
inheritedAttributes : InheritedAttributes
) = > {
2024-06-12 13:45:09 +00:00
// InnerBlockTemplate is an array block representation so properties
// like name or attributes need to be accessed with array indexes.
const nameArrayIndex = 0 ;
const attributesArrayIndex = 1 ;
2023-02-27 14:34:18 +00:00
const noResultsContent = [
createNoResultsParagraph ( ) ,
createProductSearch ( ) ,
] ;
2024-06-12 13:45:09 +00:00
const noResultsBlockName = 'woocommerce/product-collection-no-results' ;
2023-02-27 14:34:18 +00:00
const noResultsBlockIndex = innerBlocks . findIndex (
2024-06-12 13:45:09 +00:00
( block ) = > block [ nameArrayIndex ] === noResultsBlockName
2023-02-27 14:34:18 +00:00
) ;
const noResultsBlock = innerBlocks [ noResultsBlockIndex ] ;
const attributes = {
2024-06-12 13:45:09 +00:00
. . . ( noResultsBlock [ attributesArrayIndex ] || { } ) ,
2023-02-27 14:34:18 +00:00
. . . inheritedAttributes ,
} ;
const extendedNoResults = [
noResultsBlockName ,
attributes ,
noResultsContent ,
] ;
return [
2024-06-12 13:45:09 +00:00
. . . innerBlocks . slice ( 0 , noResultsBlockIndex ) ,
2023-02-27 14:34:18 +00:00
extendedNoResults ,
2024-06-12 13:45:09 +00:00
. . . innerBlocks . slice ( noResultsBlockIndex + 1 ) ,
2023-02-27 14:34:18 +00:00
] ;
} ;
2024-06-12 13:45:09 +00:00
const createProductCollectionBlock = (
inheritedAttributes : InheritedAttributes
) = > {
const productCollectionInnerBlocksWithNoResults =
2023-02-27 14:34:18 +00:00
extendInnerBlocksWithNoResultsContent (
2024-06-12 13:45:09 +00:00
productCollectionInnerBlocksTemplate ,
2023-02-27 14:34:18 +00:00
inheritedAttributes
) ;
return 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 (
productCollectionInnerBlocksWithNoResults
)
2023-02-27 14:34:18 +00:00
) ;
} ;
2023-04-03 14:29:08 +00:00
const getBlockifiedTemplate = ( inheritedAttributes : InheritedAttributes ) = >
[
2023-02-27 14:34:18 +00:00
createArchiveTitleBlock ( 'search-title' , inheritedAttributes ) ,
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 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-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 ) ;
}
} ;
2023-12-12 23:05:20 +00:00
const getButtonLabel = ( ) = > __ ( 'Transform into blocks' , 'woocommerce' ) ;
2023-02-27 14:34:18 +00:00
2023-06-29 13:41:22 +00:00
const blockifyConfig = {
2023-02-27 14:34:18 +00:00
getButtonLabel ,
2023-05-10 11:02:33 +00:00
onClickCallback ,
2023-06-29 13:41:22 +00:00
getBlockifiedTemplate ,
2023-02-27 14:34:18 +00:00
} ;
2023-06-29 13:41:22 +00:00
export { isConversionPossible , getDescription , blockifyConfig } ;