Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3049,6 +3049,63 @@ function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) {
return null;
}

/**
* Builds query vars for terms from a Terms Query block instance.
*
* It's used with the Terms Query inner blocks (Term Template, Terms Query Pagination).
*
* @since 7.0.0
*
* @param WP_Block $block Block instance.
* @return array<string, mixed> Query vars suitable for get_terms() or wp_count_terms().
*/
function build_terms_query_vars_from_block( $block ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function build_terms_query_vars_from_block( $block ) {
function build_terms_query_vars_from_block( WP_Block $block ): array {

$query = $block->context['termQuery'];

$query_vars = array(
'number' => $query['perPage'],
'order' => $query['order'],
'orderby' => $query['orderBy'],
'hide_empty' => $query['hideEmpty'],
);

$inherit_query = isset( $query['inherit'] ) && $query['inherit'] && ( is_tax() || is_category() || is_tag() );

if ( $inherit_query ) {
// Get the current term and taxonomy from the queried object.
$queried_object = get_queried_object();

// For hierarchical taxonomies, show children of the current term.
// For non-hierarchical taxonomies, show all terms (don't set parent).
if ( is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
// If showNested is true, use child_of to include nested terms.
// Otherwise, use parent to show only direct children.
if ( ! empty( $query['showNested'] ) ) {
$query_vars['child_of'] = $queried_object->term_id;
} else {
$query_vars['parent'] = $queried_object->term_id;
}
}
$query_vars['taxonomy'] = $queried_object->taxonomy;
} else {
// If not inheriting set `taxonomy` from the block attribute.
$query_vars['taxonomy'] = $query['taxonomy'];

// If we are including specific terms we ignore `showNested` argument.
if ( ! empty( $query['include'] ) ) {
$query_vars['include'] = array_unique( array_map( 'intval', $query['include'] ) );
$query_vars['orderby'] = 'include';
$query_vars['order'] = 'asc';
} elseif ( is_taxonomy_hierarchical( $query['taxonomy'] ) && empty( $query['showNested'] ) ) {
// We set parent only when inheriting from the taxonomy archive context or not
// showing nested terms, otherwise nested terms are not displayed.
$query_vars['parent'] = 0;
}
}

return $query_vars;
}

/**
* Strips all HTML from the content of footnotes, and sanitizes the ID.
*
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class WP {
* @since 2.0.0
* @var string[]
*/
public $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'pagename', 'page_id', 'error', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'favicon', 'taxonomy', 'term', 'cpage', 'post_type', 'embed' );
public $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'pagename', 'page_id', 'error', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'favicon', 'taxonomy', 'term', 'cpage', 'post_type', 'embed', 'termspage' );

/**
* Private query variables.
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/query/vars.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function testPublicQueryVarsAreAsExpected() {
'cpage',
'post_type',
'embed',
'termspage',

// Dynamically added public query vars:
'post_format',
Expand Down
Loading