From b49e8885e87d0c8e5f24937a0b11ad4820e39df2 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 10 May 2022 16:07:10 +0000 Subject: [PATCH] Query: Restore late `compact()` call for the `posts_clauses_request` filter. This addresses a backward compatibility break where `posts_join_request` and other filters were applied, but their results were subsequently discarded and earlier values were used instead. Follow-up to [52974], [53175]. Props 5um17, johnbillion, peterwilsoncc, hellofromTonya, SergeyBiryukov. Merges [53370] and [53375] to the 6.0 branch. Fixes #55699. git-svn-id: https://develop.svn.wordpress.org/branches/6.0@53379 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-query.php | 6 ++-- tests/phpunit/tests/query.php | 50 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index ef9e0668a1..e7eda7bab7 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -2754,7 +2754,7 @@ class WP_Query { } } - $clauses = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); + $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); /* * Apply post-paging filters on where and join. Only plugins that @@ -2856,7 +2856,7 @@ class WP_Query { * } * @param WP_Query $query The WP_Query instance (passed by reference). */ - $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $clauses ), &$this ) ); + $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; @@ -2990,7 +2990,7 @@ class WP_Query { * } * @param WP_Query $query The WP_Query instance (passed by reference). */ - $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( $clauses, &$this ) ); + $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index c0ce7357a4..9a8a9b3ffd 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -719,4 +719,54 @@ class Tests_Query extends WP_UnitTestCase { $this->assertInstanceOf( 'WP_User', get_queried_object() ); $this->assertSame( get_queried_object_id(), $user_id ); } + + /** + * Tests that the `posts_clauses` filter receives an array of clauses + * with the other `posts_*` filters applied, e.g. `posts_join_paged`. + * + * @ticket 55699 + * @covers WP_Query::get_posts + */ + public function test_posts_clauses_filter_should_receive_filtered_clauses() { + add_filter( + 'posts_join_paged', + static function() { + return '/* posts_join_paged */'; + } + ); + + $filter = new MockAction(); + add_filter( 'posts_clauses', array( $filter, 'filter' ), 10, 2 ); + $this->go_to( '/' ); + $filter_args = $filter->get_args(); + $posts_clauses = $filter_args[0][0]; + + $this->assertArrayHasKey( 'join', $posts_clauses ); + $this->assertSame( '/* posts_join_paged */', $posts_clauses['join'] ); + } + + /** + * Tests that the `posts_clauses_request` filter receives an array of clauses + * with the other `posts_*_request` filters applied, e.g. `posts_join_request`. + * + * @ticket 55699 + * @covers WP_Query::get_posts + */ + public function test_posts_clauses_request_filter_should_receive_filtered_clauses() { + add_filter( + 'posts_join_request', + static function() { + return '/* posts_join_request */'; + } + ); + + $filter = new MockAction(); + add_filter( 'posts_clauses_request', array( $filter, 'filter' ), 10, 2 ); + $this->go_to( '/' ); + $filter_args = $filter->get_args(); + $posts_clauses_request = $filter_args[0][0]; + + $this->assertArrayHasKey( 'join', $posts_clauses_request ); + $this->assertSame( '/* posts_join_request */', $posts_clauses_request['join'] ); + } }