2023-02-14 13:21:48 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-04-06 18:40:43 +00:00
|
|
|
import {
|
|
|
|
BlockAttributes,
|
|
|
|
BlockConfiguration,
|
|
|
|
BlockVariation,
|
|
|
|
getBlockType,
|
|
|
|
registerBlockType,
|
|
|
|
registerBlockVariation,
|
|
|
|
unregisterBlockType,
|
|
|
|
unregisterBlockVariation,
|
|
|
|
} from '@wordpress/blocks';
|
2023-02-14 13:21:48 +00:00
|
|
|
import { subscribe, select } from '@wordpress/data';
|
|
|
|
|
2023-04-19 07:08:40 +00:00
|
|
|
// Creating a local cache to prevent multiple registration tries.
|
|
|
|
const blocksRegistered = new Set();
|
|
|
|
|
|
|
|
function parseTemplateId( templateId: string | undefined ) {
|
|
|
|
return templateId?.split( '//' )[ 1 ];
|
|
|
|
}
|
|
|
|
|
2023-02-14 13:21:48 +00:00
|
|
|
export const registerBlockSingleProductTemplate = ( {
|
|
|
|
blockName,
|
2023-04-06 18:40:43 +00:00
|
|
|
blockMetadata,
|
|
|
|
blockSettings,
|
|
|
|
isVariationBlock = false,
|
|
|
|
variationName,
|
2023-02-14 13:21:48 +00:00
|
|
|
}: {
|
|
|
|
blockName: string;
|
2023-04-06 18:40:43 +00:00
|
|
|
blockMetadata: Partial< BlockConfiguration >;
|
|
|
|
blockSettings: Partial< BlockConfiguration >;
|
|
|
|
isVariationBlock?: boolean;
|
|
|
|
variationName?: string;
|
2023-02-14 13:21:48 +00:00
|
|
|
} ) => {
|
2023-04-06 18:40:43 +00:00
|
|
|
let currentTemplateId: string | undefined = '';
|
2023-02-14 13:21:48 +00:00
|
|
|
|
|
|
|
subscribe( () => {
|
|
|
|
const previousTemplateId = currentTemplateId;
|
|
|
|
const store = select( 'core/edit-site' );
|
2023-04-19 07:08:40 +00:00
|
|
|
currentTemplateId = parseTemplateId( store?.getEditedPostId() );
|
2023-04-06 18:40:43 +00:00
|
|
|
const hasChangedTemplate = previousTemplateId !== currentTemplateId;
|
|
|
|
const hasTemplateId = Boolean( currentTemplateId );
|
2023-02-14 13:21:48 +00:00
|
|
|
|
2023-04-06 18:40:43 +00:00
|
|
|
if ( ! hasChangedTemplate || ! hasTemplateId || ! blockName ) {
|
2023-02-14 13:21:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-06 18:40:43 +00:00
|
|
|
let isBlockRegistered = Boolean( getBlockType( blockName ) );
|
2023-02-14 13:21:48 +00:00
|
|
|
|
2023-04-06 18:40:43 +00:00
|
|
|
/**
|
|
|
|
* We need to unregister the block each time the user visits or leaves the Single Product template.
|
|
|
|
*
|
|
|
|
* The Single Product template is the only template where the `ancestor` property is not needed because it provides the context
|
|
|
|
* for the product blocks. We need to unregister and re-register the block to remove or add the `ancestor` property depending on which
|
|
|
|
* location (template, post, page, etc.) the user is in.
|
|
|
|
*
|
|
|
|
*/
|
2023-02-14 13:21:48 +00:00
|
|
|
if (
|
2023-04-06 18:40:43 +00:00
|
|
|
isBlockRegistered &&
|
|
|
|
( currentTemplateId?.includes( 'single-product' ) ||
|
|
|
|
previousTemplateId?.includes( 'single-product' ) )
|
2023-02-14 13:21:48 +00:00
|
|
|
) {
|
2023-04-06 18:40:43 +00:00
|
|
|
if ( isVariationBlock && variationName ) {
|
|
|
|
unregisterBlockVariation( blockName, variationName );
|
|
|
|
} else {
|
|
|
|
unregisterBlockType( blockName );
|
|
|
|
}
|
|
|
|
isBlockRegistered = false;
|
2023-02-14 13:21:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 18:40:43 +00:00
|
|
|
if ( ! isBlockRegistered ) {
|
|
|
|
if ( isVariationBlock ) {
|
|
|
|
registerBlockVariation( blockName, {
|
|
|
|
...blockSettings,
|
|
|
|
// @ts-expect-error: `ancestor` key is typed in WordPress core
|
|
|
|
ancestor: ! currentTemplateId?.includes( 'single-product' )
|
|
|
|
? blockSettings?.ancestor
|
|
|
|
: undefined,
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
// @ts-expect-error: `registerBlockType` is typed in WordPress core
|
|
|
|
registerBlockType( blockMetadata, {
|
|
|
|
...blockSettings,
|
|
|
|
ancestor: ! currentTemplateId?.includes( 'single-product' )
|
|
|
|
? blockSettings?.ancestor
|
|
|
|
: undefined,
|
|
|
|
} );
|
|
|
|
}
|
2023-02-14 13:21:48 +00:00
|
|
|
}
|
|
|
|
}, 'core/edit-site' );
|
2023-04-06 18:40:43 +00:00
|
|
|
|
|
|
|
subscribe( () => {
|
2023-04-19 07:08:40 +00:00
|
|
|
const isBlockRegistered = Boolean( variationName )
|
|
|
|
? blocksRegistered.has( variationName )
|
|
|
|
: blocksRegistered.has( blockName );
|
|
|
|
// This subscribe callback could be invoked with the core/blocks store
|
|
|
|
// which would cause infinite registration loops because of the `registerBlockType` call.
|
|
|
|
// This local cache helps prevent that.
|
|
|
|
if ( ! isBlockRegistered ) {
|
2023-04-06 18:40:43 +00:00
|
|
|
if ( isVariationBlock ) {
|
2023-04-19 07:08:40 +00:00
|
|
|
blocksRegistered.add( variationName );
|
2023-04-06 18:40:43 +00:00
|
|
|
registerBlockVariation(
|
|
|
|
blockName,
|
|
|
|
blockSettings as BlockVariation< BlockAttributes >
|
|
|
|
);
|
|
|
|
} else {
|
2023-04-19 07:08:40 +00:00
|
|
|
blocksRegistered.add( blockName );
|
2023-04-06 18:40:43 +00:00
|
|
|
// @ts-expect-error: `registerBlockType` is typed in WordPress core
|
|
|
|
registerBlockType( blockMetadata, blockSettings );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 'core/edit-post' );
|
2023-02-14 13:21:48 +00:00
|
|
|
};
|