Skip to content
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

Editor: Add option to ignore sticky posts in Query block #8265

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2489,8 +2489,10 @@ function build_query_vars_from_query_block( $block, $page ) {
*/
$query['post__in'] = ! empty( $sticky ) ? $sticky : array( 0 );
$query['ignore_sticky_posts'] = 1;
} else {
} elseif ( 'exclude' === $block->context['query']['sticky'] ) {
$query['post__not_in'] = array_merge( $query['post__not_in'], $sticky );
} elseif ( 'ignore' === $block->context['query']['sticky'] ) {
$query['ignore_sticky_posts'] = 1;
}
}
if ( ! empty( $block->context['query']['exclude'] ) ) {
Expand Down
143 changes: 143 additions & 0 deletions tests/phpunit/tests/blocks/wpBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,149 @@ public function test_build_query_vars_from_query_block_with_top_level_parent() {
);
}

/**
* Ensure requesting only sticky posts returns only sticky posts.
*
* @ticket 62908
*/
public function test_build_query_vars_from_block_query_only_sticky_posts() {
$this->factory()->post->create_many( 5 );
$sticky_post_id = $this->factory()->post->create(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post_title' => 'Sticky Post',
)
);
stick_post( $sticky_post_id );

$this->registry->register(
'core/example',
array( 'uses_context' => array( 'query' ) )
);

$parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
$parsed_block = $parsed_blocks[0];
$context = array(
'query' => array(
'sticky' => 'only',
),
);
$block = new WP_Block( $parsed_block, $context, $this->registry );
$query_args = build_query_vars_from_query_block( $block, 1 );

$this->assertSame(
array(
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => array(),
'tax_query' => array(),
'post__in' => array( $sticky_post_id ),
'ignore_sticky_posts' => 1,
),
$query_args
);

$query = new WP_Query( $query_args );
$this->assertSame( array( $sticky_post_id ), wp_list_pluck( $query->posts, 'ID' ) );
}

/**
* Ensure excluding sticky posts returns only non-sticky posts.
*
* @ticket 62908
*/
public function test_build_query_vars_from_block_query_exclude_sticky_posts() {
$not_sticky_post_ids = $this->factory()->post->create_many( 5 );
$sticky_post_id = $this->factory()->post->create(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post_title' => 'Sticky Post',
)
);
stick_post( $sticky_post_id );

$this->registry->register(
'core/example',
array( 'uses_context' => array( 'query' ) )
);

$parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
$parsed_block = $parsed_blocks[0];
$context = array(
'query' => array(
'sticky' => 'exclude',
),
);
$block = new WP_Block( $parsed_block, $context, $this->registry );
$query_args = build_query_vars_from_query_block( $block, 1 );

$this->assertSame(
array(
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => array(),
'tax_query' => array(),
'post__not_in' => array( $sticky_post_id ),
),
$query_args
);

$query = new WP_Query( $query_args );
$this->assertNotContains( $sticky_post_id, wp_list_pluck( $query->posts, 'ID' ) );
$this->assertSameSets( $not_sticky_post_ids, wp_list_pluck( $query->posts, 'ID' ) );
}

/**
* Ensure ignoring sticky posts includes both sticky and non-sticky posts.
*
* @ticket 62908
*/
public function test_build_query_vars_from_block_query_ignore_sticky_posts() {
$not_sticky_post_ids = $this->factory()->post->create_many( 5 );
$sticky_post_id = $this->factory()->post->create(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post_title' => 'Sticky Post',
)
);
stick_post( $sticky_post_id );

$this->registry->register(
'core/example',
array( 'uses_context' => array( 'query' ) )
);

$parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
$parsed_block = $parsed_blocks[0];
$context = array(
'query' => array(
'sticky' => 'ignore',
),
);
$block = new WP_Block( $parsed_block, $context, $this->registry );
$query_args = build_query_vars_from_query_block( $block, 1 );

$this->assertSame(
array(
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => array(),
'tax_query' => array(),
'ignore_sticky_posts' => 1,
),
$query_args
);

$query = new WP_Query( $query_args );
$this->assertSameSets( array_merge( $not_sticky_post_ids, array( $sticky_post_id ) ), wp_list_pluck( $query->posts, 'ID' ) );
}

/**
* @ticket 56467
*/
Expand Down
Loading