Skip to content

Commit f001ac2

Browse files
committed
Update var name and PHP docs
Unit tests have been updated according to feedback (using assertSame for simple comparisons), and removing filter removes.
1 parent 2cc9d27 commit f001ac2

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

src/wp-includes/link-template.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,8 @@ function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy =
18141814
* Can either be next or previous post.
18151815
*
18161816
* @since 2.5.0
1817-
* @since 6.9.1 Adds deterministic fallback for sort clause if not modified by a filter.
1817+
* @since 6.9.0 Introduce deterministic fallback based in IDs to account for date collisions.
1818+
* @since 6.9.1 Remove deterministic fallback for sites modifying the WHERE clause via a filter. See #64390.
18181819
*
18191820
* @global wpdb $wpdb WordPress database abstraction object.
18201821
*
@@ -1966,8 +1967,9 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
19661967
$join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );
19671968

19681969
// Prepare the where clause for the adjacent post query.
1969-
$where_prepared_with_deterministic_fallback = $wpdb->prepare( "WHERE (p.post_date $comparison_operator %s OR (p.post_date = %s AND p.ID $comparison_operator %d)) AND p.post_type = %s $where", $current_post_date, $current_post_date, $post->ID, $post->post_type ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $comparison_operator is a string literal, either '<' or '>'.
1970-
$where_prepared = $wpdb->prepare( "WHERE p.post_date $comparison_operator %s AND p.post_type = %s $where", $current_post_date, $post->post_type );
1970+
$where_prepared = $wpdb->prepare( "WHERE p.post_date $comparison_operator %s AND p.post_type = %s $where", $current_post_date, $post->post_type ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $comparison_operator is a string literal, either '<' or '>'.
1971+
$deterministic_where_prepared = $wpdb->prepare( "WHERE (p.post_date $comparison_operator %s OR (p.post_date = %s AND p.ID $comparison_operator %d)) AND p.post_type = %s $where", $current_post_date, $current_post_date, $post->ID, $post->post_type ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $comparison_operator is a string literal, either '<' or '>'.
1972+
19711973
/**
19721974
* Filters the WHERE clause in the SQL for an adjacent post query.
19731975
*
@@ -1982,7 +1984,7 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
19821984
* @since 2.5.0
19831985
* @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
19841986
*
1985-
* @param string $where The `WHERE` clause in the SQL.
1987+
* @param string $where_prepared The `WHERE` clause in the SQL.
19861988
* @param bool $in_same_term Whether post should be in the same taxonomy term.
19871989
* @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided.
19881990
* @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
@@ -1992,9 +1994,11 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
19921994

19931995
// Only force deterministic fallback if the where clause has not been modified by a filter.
19941996
if ( $where === $where_prepared ) {
1995-
$where = $where_prepared_with_deterministic_fallback;
1997+
$where = $deterministic_where_prepared;
19961998
}
19971999

2000+
$sort_prepared = "ORDER BY p.post_date $order LIMIT 1";
2001+
19982002
/**
19992003
* Filters the ORDER BY clause in the SQL for an adjacent post query.
20002004
*
@@ -2009,13 +2013,14 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
20092013
* @since 2.5.0
20102014
* @since 4.4.0 Added the `$post` parameter.
20112015
* @since 4.9.0 Added the `$order` parameter.
2016+
* @since 6.9.0 Adds ID sort to ensure deterministic ordering for posts with identical dates.
2017+
* @since 6.9.1 Remove deterministic fallback for sites modifying the SORT clause via a filter. See #64390.
20122018
*
20132019
* @param string $order_by The `ORDER BY` clause in the SQL.
20142020
* @param WP_Post $post WP_Post object.
20152021
* @param string $order Sort order. 'DESC' for previous post, 'ASC' for next.
20162022
*/
2017-
$sort_prepared = "ORDER BY p.post_date $order LIMIT 1";
2018-
$sort = apply_filters( "get_{$adjacent}_post_sort", $sort_prepared, $post, $order );
2023+
$sort = apply_filters( "get_{$adjacent}_post_sort", $sort_prepared, $post, $order );
20192024

20202025
// Only force deterministic sort if the sort clause has not been modified by a filter.
20212026
if ( $sort === $sort_prepared ) {

tests/phpunit/tests/link/getAdjacentPost.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,7 @@ static function ( $where ) {
657657
// Next post should be the 4th post (higher ID, same date) - deterministic.
658658
$next = get_adjacent_post( false, '', false );
659659
$this->assertInstanceOf( 'WP_Post', $next );
660-
$this->assertEquals( $post_ids[3], $next->ID );
661-
662-
remove_all_filters( 'get_next_post_where' );
660+
$this->assertSame( $post_ids[3], $next->ID, 'Next post should be the 4th post (higher ID, same date).' );
663661
}
664662

665663
/**
@@ -710,8 +708,6 @@ static function ( $where ) use ( &$filter_received, &$filter_returned ) {
710708
$this->assertStringNotContainsString( 'AND p.ID', $filter_received, 'Filter should receive WHERE clause without deterministic ID fallback.' );
711709
// Verify the filter's modification is preserved (proves deterministic logic wasn't applied on top).
712710
$this->assertStringContainsString( 'AND 1=1', $filter_returned, 'Filter modification should be preserved.' );
713-
714-
remove_all_filters( 'get_next_post_where' );
715711
}
716712

717713
/**
@@ -749,9 +745,7 @@ static function ( $sort ) {
749745
// Next post should be the 4th post (higher ID, same date) - deterministic.
750746
$next = get_adjacent_post( false, '', false );
751747
$this->assertInstanceOf( 'WP_Post', $next );
752-
$this->assertEquals( $post_ids[3], $next->ID );
753-
754-
remove_all_filters( 'get_next_post_sort' );
748+
$this->assertSame( $post_ids[3], $next->ID, 'Next post should be the 4th post (higher ID, same date).' );
755749
}
756750

757751
/**
@@ -801,8 +795,6 @@ static function ( $sort, $post, $order ) use ( &$filter_received, &$filter_retur
801795
// Verify the filter's modification is preserved (proves deterministic logic wasn't applied on top).
802796
$this->assertStringNotContainsString( 'p.ID', $filter_returned, 'Filter modification should not include ID when filter removes it.' );
803797
$this->assertStringContainsString( 'ORDER BY p.post_date', $filter_returned, 'Filter modification should be preserved.' );
804-
805-
remove_all_filters( 'get_next_post_sort' );
806798
}
807799

808800
/**
@@ -846,10 +838,7 @@ static function ( $sort ) {
846838
// Previous post should be the 2nd post (lower ID, same date) - deterministic.
847839
$previous = get_adjacent_post( false, '', true );
848840
$this->assertInstanceOf( 'WP_Post', $previous );
849-
$this->assertEquals( $post_ids[1], $previous->ID );
850-
851-
remove_all_filters( 'get_previous_post_where' );
852-
remove_all_filters( 'get_previous_post_sort' );
841+
$this->assertSame( $post_ids[1], $previous->ID, 'Previous post should be the 2nd post (lower ID, same date).' );
853842
}
854843

855844
/**

0 commit comments

Comments
 (0)