diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 1066aa0141915..86a1feefd6dbc 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -250,7 +250,7 @@ function build_template_part_block_instance_variations() { 'area' => $template_part->area, ), 'scope' => array( 'inserter' ), - 'icon' => $icon_by_area[ $template_part->area ], + 'icon' => isset( $icon_by_area[ $template_part->area ] ) ? $icon_by_area[ $template_part->area ] : null, 'example' => array( 'attributes' => array( 'slug' => $template_part->slug, diff --git a/packages/edit-site/src/components/page-patterns/use-patterns.js b/packages/edit-site/src/components/page-patterns/use-patterns.js index 37b4fcce6cfa7..d39d737210119 100644 --- a/packages/edit-site/src/components/page-patterns/use-patterns.js +++ b/packages/edit-site/src/components/page-patterns/use-patterns.js @@ -4,6 +4,7 @@ import { parse } from '@wordpress/blocks'; import { useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; +import { store as editorStore } from '@wordpress/editor'; import { decodeEntities } from '@wordpress/html-entities'; /** @@ -39,14 +40,12 @@ const templatePartToPattern = ( templatePart ) => ( { templatePart, } ); -const templatePartHasCategory = ( item, category ) => - item.templatePart.area === category; - const selectTemplatePartsAsPatterns = ( select, { categoryId, search = '' } = {} ) => { const { getEntityRecords, getIsResolving } = select( coreStore ); + const { __experimentalGetDefaultTemplatePartAreas } = select( editorStore ); const query = { per_page: -1 }; const rawTemplateParts = getEntityRecords( 'postType', TEMPLATE_PARTS, query ) ?? @@ -55,6 +54,23 @@ const selectTemplatePartsAsPatterns = ( templatePartToPattern( templatePart ) ); + // In the case where a custom template part area has been removed we need + // the current list of areas to cross check against so orphaned template + // parts can be treated as uncategorized. + const knownAreas = __experimentalGetDefaultTemplatePartAreas() || []; + const templatePartAreas = knownAreas.map( ( area ) => area.area ); + + const templatePartHasCategory = ( item, category ) => { + if ( category !== 'uncategorized' ) { + return item.templatePart.area === category; + } + + return ( + item.templatePart.area === category || + ! templatePartAreas.includes( item.templatePart.area ) + ); + }; + const isResolving = getIsResolving( 'getEntityRecords', [ 'postType', 'wp_template_part',