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
35 changes: 23 additions & 12 deletions src/features/class-query-block-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ public function boot(): void {
add_filter( 'render_block_context', [ $this, 'filter_query_context' ], 10, 3 );
}

/**
* Checks if post level deduplication is enabled.
*
* @return bool
*/
function get_post_level_deduplication(): bool {
$post_id = null;
$post_level_deduplication = false;
$main_query = $this->main_query->query_object();
if ( true === $main_query->is_singular() || true === $main_query->is_posts_page ) {
$post_id = $main_query->get_queried_object_id();
$post_level_deduplication = (bool) get_post_meta( $post_id, 'wp_curate_deduplication', true );
}
/**
* Filter to determine if post level deduplication is enabled.
*
* @param bool $post_level_deduplication Whether post level deduplication is enabled.
* @param int|null $post_id The ID of the post being checked.
*/
return (bool) apply_filters( 'wp_curate_post_level_deduplication', $post_level_deduplication, $post_id );
}

/**
* Filters the context provided to a query block to determine the definitive list of backfilled posts.
*
Expand All @@ -85,21 +107,10 @@ public function filter_query_context( $context, $parsed_block, $parent_block ):
// Handles the decision to exclude (deduplicate) posts or not, based on a given input.
$variable_post_queries = new Variable_Post_Queries(
input: function () use ( $parsed_block ) {
$main_query = $this->main_query->query_object();

if ( isset( $parsed_block['attrs']['deduplication'] ) && 'never' === $parsed_block['attrs']['deduplication'] ) {
return false;
}

if ( true === $main_query->is_singular() || true === $main_query->is_posts_page ) {
$post_level_deduplication = get_post_meta( $main_query->get_queried_object_id(), 'wp_curate_deduplication', true );

if ( true === (bool) $post_level_deduplication ) {
return true;
}
}

return false;
return $this->get_post_level_deduplication();
},
// Exclude posts that have already been used in this request.
test: new Comparison( [ 'compared' => true ] ),
Expand Down
9 changes: 8 additions & 1 deletion src/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ function main(): void {
$stop_queries_var = 'wp_curate_stop_queries';
$main_query = new Global_Post_Query( 'wp_query' );

/**
* Filter the Seed object for the history of post IDs.
*
* @param Pinned_In_Post_Content $seed The seed for the history of post IDs.
*/
$seed = apply_filters( 'wp_curate_history_seed', new Pinned_In_Post_Content( $main_query ) );

// phpcs:disable Squiz.Commenting.BlockComment.NoEmptyLineBefore
$plugin = new Group(
/*
Expand All @@ -36,7 +43,7 @@ function main(): void {
new Features\Query_Block_Context(
post_queries: new Default_Post_Queries(),
history: new History(
seed: new Pinned_In_Post_Content( $main_query ),
seed: $seed,
),
main_query: $main_query,
default_per_page: (int) get_option( 'posts_per_page', 10 ), // @phpstan-ignore-line
Expand Down
Loading