From ff9de80ce2dee14a91c7a16477a86d89287403c0 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:25:26 +1000 Subject: [PATCH] Patterns: Allow orphaned template parts to appear in general category (#52961) --- .../block-library/src/template-part/index.php | 2 +- .../components/page-patterns/use-patterns.js | 22 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 1066aa01419159..86a1feefd6dbc9 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 37b4fcce6cfa7e..d39d7372101195 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',