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';
|
|
|
|
|
|
|
|
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-06 18:40:43 +00:00
|
|
|
currentTemplateId = store?.getEditedPostContext< {
|
|
|
|
templateSlug?: string;
|
|
|
|
} >()?.templateSlug;
|
|
|
|
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( () => {
|
|
|
|
const isBlockRegistered = Boolean( getBlockType( blockName ) );
|
|
|
|
const editPostStoreExists = Boolean( select( 'core/edit-post' ) );
|
|
|
|
|
|
|
|
if ( ! isBlockRegistered && editPostStoreExists ) {
|
|
|
|
if ( isVariationBlock ) {
|
|
|
|
registerBlockVariation(
|
|
|
|
blockName,
|
|
|
|
blockSettings as BlockVariation< BlockAttributes >
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// @ts-expect-error: `registerBlockType` is typed in WordPress core
|
|
|
|
registerBlockType( blockMetadata, blockSettings );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 'core/edit-post' );
|
2023-02-14 13:21:48 +00:00
|
|
|
};
|