Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Backports from lib/compat/wordpress-6.0/blocks.php.
  • Loading branch information
hellofromtonya committed Mar 31, 2022
commit 3163816045f479aba3bd891488ef1f711c4d0015
40 changes: 32 additions & 8 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1126,15 +1126,39 @@ function build_query_vars_from_query_block( $block, $page ) {
$query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset;
$query['posts_per_page'] = $per_page;
}
if ( ! empty( $block->context['query']['categoryIds'] ) ) {
$term_ids = array_map( 'intval', $block->context['query']['categoryIds'] );
$term_ids = array_filter( $term_ids );
$query['category__in'] = $term_ids;
// Migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility.
if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't see why this outer if is needed, the same checks are being done inside it.

Copy link

@ntsekouras ntsekouras Apr 1, 2022

Choose a reason for hiding this comment

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

Just to 'encapsulate' (let's say) the 'migration' code. Also, both old attributes merge into a single one and without the extra if we would have more checks about new value and merge, etc..

I think there is no harm to leave this as is.

$tax_query = array();
if ( ! empty( $block->context['query']['categoryIds'] ) ) {
$tax_query[] = array(
'taxonomy' => 'category',
'terms' => $block->context['query']['categoryIds'],
'include_children' => false,
);
}
if ( ! empty( $block->context['query']['tagIds'] ) ) {
$tax_query[] = array(
'taxonomy' => 'post_tag',
'terms' => $block->context['query']['tagIds'],
'include_children' => false,
);
}
$query['tax_query'] = $tax_query;
}
if ( ! empty( $block->context['query']['tagIds'] ) ) {
$term_ids = array_map( 'intval', $block->context['query']['tagIds'] );
$term_ids = array_filter( $term_ids );
$query['tag__in'] = $term_ids;
if ( ! empty( $block->context['query']['taxQuery'] ) ) {
$query['tax_query'] = array();
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like it will stomp on any categories and tags in the shim above if the query block has both a legacy value and taxQuery defined.

Copy link

@ntsekouras ntsekouras Apr 1, 2022

Choose a reason for hiding this comment

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

I'm not sure if it will be possible to have the old and new attributes, as there is the client parsing that migrates the block. So in the case of deprecated blocks, we just fill the new attribute from the old ones.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if it will be possible to have the old and new attributes, as there is the client parsing that migrates the block.

Thanks, I gather that will come in as part of a package.

Copy link
Contributor

@adamziel adamziel Apr 7, 2022

Choose a reason for hiding this comment

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

Using an else if instead of an if would make this explicit for the future readers of this code. I'm thinking:

else if ( ! empty( $block->context['query']['taxQuery'] ) ) {

Not a blocker for the backport, though.

foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {
if ( ! empty( $terms ) ) {
$term_ids = array_map( 'intval', $terms );
$term_ids = array_filter( $term_ids );

$query['tax_query'][] = array(
'taxonomy' => $taxonomy,
'terms' => $terms,
'include_children' => false,
);
}
}
}
if (
isset( $block->context['query']['order'] ) &&
Expand Down