-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Update Terms Query block to support nesting & inheritance #71916
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
Closed
cr0ybot
wants to merge
20
commits into
WordPress:trunk
from
cr0ybot:update/terms-query-block-inheritance
Closed
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c18d62c
nesting & inheritance features split out from #71596
cr0ybot 3b7be0e
rename build_query_vars_from_terms_query_block function
cr0ybot 344e866
guard against redeclaration
cr0ybot 569845c
move getQueryContextFromTemplate to shared location, provide template…
cr0ybot 56c09e7
simplify inheritance to be driven only by terms query block, strip ta…
cr0ybot 4eaccdd
move and update getQueryContextFromTemplate tests
cr0ybot 58d1346
apply classes similar to post-template block for taxonomy
cr0ybot b0cc2e8
move experimental terms query features to load conditionally
cr0ybot d70fa73
fix phpcs warning
cr0ybot 5879905
remove backport changelog until block is no longer experimental
cr0ybot 00b3ec1
cleanup
cr0ybot 9e38dd5
explicitly specify hierarchical = false in term queries
cr0ybot 3369fd0
taxonomy should be inherited only if there is a termId in context to …
cr0ybot 1cb5726
fix hierarchical terms displayed for post context
cr0ybot bfa9df9
fix nested terms inherited from a post, but breaks nested terms that …
cr0ybot cc4f0fd
fix nested term query context from post or term archive
cr0ybot 94f3390
fix getting taxonomy/term from template slug with special handling fr…
cr0ybot 1f54b24
fix taxonomy selector while inherit is true, breaks auto-inherit from…
cr0ybot 6f23abe
const variable assignment
cr0ybot fe582e7
remove explicit default context
cr0ybot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| <?php | ||
| /** | ||
| * Terms Query block helpers. | ||
| * | ||
| * @since 6.9.0 | ||
| * @package gutenberg | ||
| * @subpackage Blocks | ||
| */ | ||
|
|
||
| if ( ! function_exists( 'build_query_vars_from_terms_query_block' ) ) { | ||
| /** | ||
| * Helper function that constructs a WP_Term_Query args array from a Terms Query | ||
| * block's attributes. | ||
| * | ||
| * @param WP_Block $block Block instance. | ||
| * @param int $page Current query's page. | ||
| * | ||
| * @return array Returns the constructed WP_Term_Query arguments. | ||
| */ | ||
| function build_query_vars_from_terms_query_block( $block, $page = 1 ) { | ||
| $query = array( | ||
| 'taxonomy' => 'category', | ||
| 'number' => 10, | ||
| 'order' => 'asc', | ||
| 'orderby' => 'name', | ||
| 'hide_empty' => true, | ||
| 'parent' => 0, | ||
| ); | ||
|
|
||
| if ( isset( $block->context['termQuery'] ) ) { | ||
| if ( ! empty( $block->context['termQuery']['taxonomy'] ) ) { | ||
| $taxonomy_param = $block->context['termQuery']['taxonomy']; | ||
| if ( is_taxonomy_viewable( $taxonomy_param ) ) { | ||
| $query['taxonomy'] = $taxonomy_param; | ||
| } | ||
| } | ||
| if ( isset( $block->context['termQuery']['exclude'] ) ) { | ||
| $excluded_term_ids = array_map( 'intval', $block->context['termQuery']['exclude'] ); | ||
| $excluded_term_ids = array_filter( $excluded_term_ids ); | ||
| $query['exclude'] = $excluded_term_ids; | ||
| } | ||
| if ( | ||
| isset( $block->context['termQuery']['perPage'] ) | ||
| && is_numeric( $block->context['termQuery']['perPage'] ) | ||
| ) { | ||
| $per_page = absint( $block->context['termQuery']['perPage'] ); | ||
| $offset = 0; | ||
|
|
||
| if ( | ||
| isset( $block->context['termQuery']['offset'] ) | ||
| && is_numeric( $block->context['termQuery']['offset'] ) | ||
| ) { | ||
| $offset = absint( $block->context['termQuery']['offset'] ); | ||
| } | ||
|
|
||
| $query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset; | ||
| $query['number'] = $per_page; | ||
| } | ||
| if ( | ||
| isset( $block->context['termQuery']['order'] ) | ||
| && in_array( | ||
| strtoupper( $block->context['termQuery']['order'] ), | ||
| array( 'ASC', 'DESC' ), | ||
| true | ||
| ) | ||
| ) { | ||
| $query['order'] = strtoupper( $block->context['termQuery']['order'] ); | ||
| } | ||
| if ( isset( $block->context['termQuery']['orderby'] ) ) { | ||
| $query['orderby'] = $block->context['termQuery']['orderby']; | ||
| } | ||
| if ( | ||
| isset( $block->context['termQuery']['hideEmpty'] ) | ||
| && ! $block->context['termQuery']['hideEmpty'] | ||
| ) { | ||
| $query['hide_empty'] = false; | ||
| } | ||
| if ( | ||
| is_taxonomy_hierarchical( $query['taxonomy'] ) | ||
| && isset( $block->context['termQuery']['parent'] ) | ||
| ) { | ||
| $query['parent'] = $block->context['termQuery']['parent'] || 0; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Filters the arguments which will be passed to `WP_Term_Query` for the Terms Query Block. | ||
| * | ||
| * Anything to this filter should be compatible with the `WP_Term_Query` API to form | ||
| * the query context which will be passed down to the Terms Query Block's children. | ||
| * This can help, for example, to include additional settings or meta queries not | ||
| * directly supported by the core Terms Query Block, and extend its capabilities. | ||
| * | ||
| * Please note that this will only influence the query that will be rendered on the | ||
| * front-end. The editor preview is not affected by this filter. Also, worth noting | ||
| * that the editor preview uses the REST API, so, ideally, one should aim to provide | ||
| * attributes which are also compatible with the REST API, in order to be able to | ||
| * implement identical queries on both sides. | ||
| * | ||
| * @since 6.9.0 | ||
| * | ||
| * @param array $query Array containing parameters for `WP_Term_Query` as parsed by the block context. | ||
| * @param WP_Block $block Block instance. | ||
| * @param int $page Current query's page. | ||
| */ | ||
| return apply_filters( 'terms_query_block_query_vars', $query, $block, $page ); | ||
| } | ||
| } |
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.
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.
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.
Furthermore, these two PHP files should have an additional check for the experimental settings:
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.
It also looks like these were improperly located within the
WP_REST_Controllercheck if I'm reading the commentThese files only need to be loaded if within a rest server instancecorrectly, so I've moved them both outside. @t-hamano Let me know if I've done that incorrectly.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.
Thank you, all files and code seem to be placed correctly.
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.
I'm pretty sure that as a result, this PR could be marked as
No Core Sync Required? There is already a core backport PR open for the original Terms Query block PR #70720 but since it's been marked as experimental I assume that PR will stay open until the experimental flag is removed.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.
Yes, it's fine to prepare a core backport PR beforehand, but it should not be committed if the feature is still experimental. For now, this backport changelog file should also be deleted.
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.
@t-hamano Thanks, I've removed that too.