-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Terms Query: Show nested terms toggle when inheriting #72510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f1e1a7
Show nested terms toggle when inheriting
mikachan ae169b1
Update help text
mikachan 0b47a8a
Update help text based on feedback
mikachan 3752755
Update logic to fetch correct terms
mikachan 1f382a2
Merge branch 'trunk' into update/terms-inherit-nested
mikachan 546598a
Remove new editor logic
mikachan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
88 changes: 88 additions & 0 deletions
88
packages/block-library/src/term-template/use-current-term.js
This file contains hidden or 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,88 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
|
|
||
| /** | ||
| * Hook to get the current term from template context. | ||
| * This is used when blocks need to inherit from the current taxonomy template. | ||
| * | ||
| * @param {boolean} inherit Whether to inherit from current context. | ||
| * @return {Object|null} The current term object or null. | ||
| */ | ||
| export function useCurrentTerm( inherit ) { | ||
| return useSelect( | ||
| ( select ) => { | ||
| if ( ! inherit ) { | ||
| return null; | ||
| } | ||
|
|
||
| // Access core/editor by string to avoid @wordpress/editor dependency. | ||
| // eslint-disable-next-line @wordpress/data-no-store-string-literals | ||
| const { | ||
| getCurrentPostId, | ||
| getCurrentPostType, | ||
| getCurrentTemplateId, | ||
| } = select( 'core/editor' ); | ||
| const currentPostType = getCurrentPostType(); | ||
| const templateId = | ||
| getCurrentTemplateId() || | ||
| ( currentPostType === 'wp_template' | ||
| ? getCurrentPostId() | ||
| : null ); | ||
| const templateSlug = templateId | ||
| ? select( coreStore ).getEditedEntityRecord( | ||
| 'postType', | ||
| 'wp_template', | ||
| templateId | ||
| )?.slug | ||
| : null; | ||
|
|
||
| if ( ! templateSlug ) { | ||
| return null; | ||
| } | ||
|
|
||
| const taxonomyMatches = templateSlug.match( | ||
| /^(category|tag|taxonomy-([^-]+))$|^(((category|tag)|taxonomy-([^-]+))-(.+))$/ | ||
| ); | ||
|
|
||
| if ( ! taxonomyMatches ) { | ||
| return null; | ||
| } | ||
|
|
||
| let currentTaxonomy; | ||
| let termSlug; | ||
|
|
||
| // If it's for all taxonomies of a type (e.g., category, tag). | ||
| if ( taxonomyMatches[ 1 ] ) { | ||
| currentTaxonomy = taxonomyMatches[ 2 ] | ||
| ? taxonomyMatches[ 2 ] | ||
| : taxonomyMatches[ 1 ]; | ||
| } | ||
| // If it's for a specific term (e.g., category-news, tag-featured). | ||
| else if ( taxonomyMatches[ 3 ] ) { | ||
| currentTaxonomy = taxonomyMatches[ 6 ] | ||
| ? taxonomyMatches[ 6 ] | ||
| : taxonomyMatches[ 4 ]; | ||
| termSlug = taxonomyMatches[ 7 ]; | ||
| } | ||
|
|
||
| if ( ! currentTaxonomy || ! termSlug ) { | ||
| return null; | ||
| } | ||
|
|
||
| currentTaxonomy = | ||
| currentTaxonomy === 'tag' ? 'post_tag' : currentTaxonomy; | ||
|
|
||
| const { getEntityRecords } = select( coreStore ); | ||
| const termRecords = getEntityRecords( 'taxonomy', currentTaxonomy, { | ||
| slug: termSlug, | ||
| per_page: 1, | ||
| } ); | ||
|
|
||
| return termRecords && termRecords[ 0 ] ? termRecords[ 0 ] : null; | ||
| }, | ||
| [ inherit ] | ||
| ); | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
This
useCurrentTermis very similar to theuseArchiveLabel,useTermDescription, anduseTermNamehooks, so I think we should consolidate this functionality as much as possible in a follow-up.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.
This functionality had some heavy discussion in #72300 which ultimately didn't get merged. There are issues if taxonomy slugs contain hyphens, because the template slug does not include additional separators. Historically WordPress applies a template if it matches certain criteria but doing the reverse is tricky.
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.
I'd echo @cr0ybot and I've actually created an issue to improve the existing functionality.
Let's don't worry about the editor preview for now and fix the main issue (which I'd consider a bug that we should also back port and not an enhancement).
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.
Thanks both, sounds like something that would be best in its own PR. I've removed the changes to the editor logic in 546598a.