Skip to content

Commit

Permalink
Patterns: Allow orphaned template parts to appear in general category (
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw authored Jul 27, 2023
1 parent fb5f660 commit 54facfa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,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,
Expand Down
22 changes: 19 additions & 3 deletions packages/edit-site/src/components/page-patterns/use-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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 ) ??
Expand All @@ -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',
Expand Down

0 comments on commit 54facfa

Please sign in to comment.