-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patterns: Fix pattern categories on import (#58926)
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org> Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org> Co-authored-by: glendaviesnz <glendaviesnz@git.wordpress.org> Co-authored-by: bph <bph@git.wordpress.org> Co-authored-by: hanneslsm <hanneslsm@git.wordpress.org> Co-authored-by: colorful-tones <colorful-tones@git.wordpress.org> Co-authored-by: annezazu <annezazu@git.wordpress.org>
- Loading branch information
1 parent
f790549
commit 8057df3
Showing
4 changed files
with
125 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { CATEGORY_SLUG } from './components/category-selector'; | ||
|
||
/** | ||
* Helper hook that creates a Map with the core and user patterns categories | ||
* and removes any duplicates. It's used when we need to create new user | ||
* categories when creating or importing patterns. | ||
* This hook also provides a function to find or create a pattern category. | ||
* | ||
* @return {Object} The merged categories map and the callback function to find or create a category. | ||
*/ | ||
export function useAddPatternCategory() { | ||
const { saveEntityRecord, invalidateResolution } = useDispatch( coreStore ); | ||
const { corePatternCategories, userPatternCategories } = useSelect( | ||
( select ) => { | ||
const { getUserPatternCategories, getBlockPatternCategories } = | ||
select( coreStore ); | ||
|
||
return { | ||
corePatternCategories: getBlockPatternCategories(), | ||
userPatternCategories: getUserPatternCategories(), | ||
}; | ||
}, | ||
[] | ||
); | ||
const categoryMap = useMemo( () => { | ||
// Merge the user and core pattern categories and remove any duplicates. | ||
const uniqueCategories = new Map(); | ||
userPatternCategories.forEach( ( category ) => { | ||
uniqueCategories.set( category.label.toLowerCase(), { | ||
label: category.label, | ||
name: category.name, | ||
id: category.id, | ||
} ); | ||
} ); | ||
|
||
corePatternCategories.forEach( ( category ) => { | ||
if ( | ||
! uniqueCategories.has( category.label.toLowerCase() ) && | ||
// There are two core categories with `Post` label so explicitly remove the one with | ||
// the `query` slug to avoid any confusion. | ||
category.name !== 'query' | ||
) { | ||
uniqueCategories.set( category.label.toLowerCase(), { | ||
label: category.label, | ||
name: category.name, | ||
} ); | ||
} | ||
} ); | ||
return uniqueCategories; | ||
}, [ userPatternCategories, corePatternCategories ] ); | ||
|
||
async function findOrCreateTerm( term ) { | ||
try { | ||
const existingTerm = categoryMap.get( term.toLowerCase() ); | ||
if ( existingTerm?.id ) { | ||
return existingTerm.id; | ||
} | ||
// If we have an existing core category we need to match the new user category to the | ||
// correct slug rather than autogenerating it to prevent duplicates, eg. the core `Headers` | ||
// category uses the singular `header` as the slug. | ||
const termData = existingTerm | ||
? { name: existingTerm.label, slug: existingTerm.name } | ||
: { name: term }; | ||
const newTerm = await saveEntityRecord( | ||
'taxonomy', | ||
CATEGORY_SLUG, | ||
termData, | ||
{ throwOnError: true } | ||
); | ||
invalidateResolution( 'getUserPatternCategories' ); | ||
return newTerm.id; | ||
} catch ( error ) { | ||
if ( error.code !== 'term_exists' ) { | ||
throw error; | ||
} | ||
return error.data.term_id; | ||
} | ||
} | ||
|
||
return { categoryMap, findOrCreateTerm }; | ||
} |
8057df3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in 8057df3.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7893233566
📝 Reported issues:
/test/e2e/specs/editor/blocks/navigation-list-view.spec.js
/test/e2e/specs/editor/blocks/navigation-list-view.spec.js
/test/e2e/specs/editor/blocks/navigation.spec.js