{ __(
'You cannot edit the content of this block. However, you can move it and place other blocks around it.',
'woocommerce'
) }
{ canConvert && blockifyConfig && (
) }
);
};
const registerClassicTemplateBlock = ( {
template,
inserter,
}: {
template?: string | null;
inserter: boolean;
} ) => {
/**
* The 'WooCommerce Legacy Template' block was renamed to 'WooCommerce Classic Template', however, the internal block
* name 'woocommerce/legacy-template' needs to remain the same. Otherwise, it would result in a corrupt block when
* loaded for users who have customized templates using the legacy-template (since the internal block name is
* stored in the database).
*
* See https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5861 for more context
*/
registerBlockType( BLOCK_SLUG, {
title:
template && TEMPLATES[ template ]
? TEMPLATES[ template ].title
: __( 'WooCommerce Classic Template', 'woocommerce' ),
icon: (
),
category: 'woocommerce',
apiVersion: 3,
keywords: [ __( 'WooCommerce', 'woocommerce' ) ],
description:
template && TEMPLATES[ template ]
? TEMPLATES[ template ].description
: __(
'Renders classic WooCommerce PHP templates.',
'woocommerce'
),
supports: {
align: [ 'wide', 'full' ],
html: false,
multiple: false,
reusable: false,
inserter,
},
attributes: {
/**
* Template attribute is used to determine which core PHP template gets rendered.
*/
template: {
type: 'string',
default: 'any',
},
align: {
type: 'string',
default: 'wide',
},
},
edit: ( {
attributes,
clientId,
setAttributes,
}: BlockEditProps< Attributes > ) => {
const newTemplate = template ?? attributes.template;
return (
);
},
save: () => null,
} );
};
// @todo Refactor when there will be possible to show a block according on a template/post with a Gutenberg API. https://github.com/WordPress/gutenberg/pull/41718
let previousEditedTemplate: string | number | null = null;
let isBlockRegistered = false;
let isBlockInInserter = false;
const handleRegisterClassicTemplateBlock = ( {
template,
inserter,
}: {
template: string | null;
inserter: boolean;
} ) => {
if ( isBlockRegistered ) {
unregisterBlockType( BLOCK_SLUG );
}
isBlockInInserter = inserter;
isBlockRegistered = true;
registerClassicTemplateBlock( {
template,
inserter,
} );
};
subscribe( () => {
const editorStore = select( 'core/editor' );
// We use blockCount to know if we are editing a template or in the navigation.
const blockCount = editorStore?.getBlockCount() as number;
const templateSlug = editorStore?.getEditedPostSlug() as string | null;
const editedTemplate = blockCount && blockCount > 0 ? templateSlug : null;
// Skip if we are in the same template, except if the block hasn't been registered yet.
if ( isBlockRegistered && previousEditedTemplate === editedTemplate ) {
return;
}
previousEditedTemplate = editedTemplate;
// Handle the case when we are not editing a template (ie: in the navigation screen).
if ( ! editedTemplate ) {
if ( ! isBlockRegistered ) {
handleRegisterClassicTemplateBlock( {
template: editedTemplate,
inserter: false,
} );
}
return;
}
const templateSupportsClassicTemplateBlock =
hasTemplateSupportForClassicTemplateBlock( editedTemplate, TEMPLATES );
// Handle the case when we are editing a template that doesn't support the Classic Template block (ie: Blog Home).
if ( ! templateSupportsClassicTemplateBlock && isBlockInInserter ) {
handleRegisterClassicTemplateBlock( {
template: editedTemplate,
inserter: false,
} );
return;
}
// Handle the case when we are editing a template that does support the Classic Template block (ie: Product Catalog).
if ( templateSupportsClassicTemplateBlock && ! isBlockInInserter ) {
handleRegisterClassicTemplateBlock( {
template: editedTemplate,
inserter: true,
} );
return;
}
// Handle the case when we are editing a template that does support the Classic Template block but it's currently registered with another title (ie: navigating from the Product Catalog template to the Product Search Results template).
if (
templateSupportsClassicTemplateBlock &&
isClassicTemplateBlockRegisteredWithAnotherTitle(
getBlockType( BLOCK_SLUG ),
editedTemplate
)
) {
handleRegisterClassicTemplateBlock( {
template: editedTemplate,
inserter: true,
} );
}
}, 'core/blocks-editor' );