From 746f545f9aed5e4555e5cddfc33f84659cf8b352 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 26 Jan 2016 03:11:12 +0000 Subject: [PATCH] Query: Respect 'suppress_filters' when filtering search-related SQL. Props 5um17. Fixes #35594. git-svn-id: https://develop.svn.wordpress.org/trunk@36404 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 41 ++++++++++++++++------------ tests/phpunit/tests/query/search.php | 20 ++++++++++++++ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index dc301afe48..982b92e97a 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2790,15 +2790,17 @@ class WP_Query { $search = $this->parse_search( $q ); } - /** - * Filter the search SQL that is used in the WHERE clause of WP_Query. - * - * @since 3.0.0 - * - * @param string $search Search SQL for WHERE clause. - * @param WP_Query $this The current WP_Query object. - */ - $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) ); + if ( ! $q['suppress_filters'] ) { + /** + * Filter the search SQL that is used in the WHERE clause of WP_Query. + * + * @since 3.0.0 + * + * @param string $search Search SQL for WHERE clause. + * @param WP_Query $this The current WP_Query object. + */ + $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) ); + } // Taxonomies if ( !$this->is_singular ) { @@ -3007,15 +3009,18 @@ class WP_Query { if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) ) $search_orderby = $this->parse_search_order( $q ); - /** - * Filter the ORDER BY used when ordering search results. - * - * @since 3.7.0 - * - * @param string $search_orderby The ORDER BY clause. - * @param WP_Query $this The current WP_Query instance. - */ - $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this ); + if ( ! $q['suppress_filters'] ) { + /** + * Filter the ORDER BY used when ordering search results. + * + * @since 3.7.0 + * + * @param string $search_orderby The ORDER BY clause. + * @param WP_Query $this The current WP_Query instance. + */ + $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this ); + } + if ( $search_orderby ) $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; } diff --git a/tests/phpunit/tests/query/search.php b/tests/phpunit/tests/query/search.php index c21787d090..a870110992 100644 --- a/tests/phpunit/tests/query/search.php +++ b/tests/phpunit/tests/query/search.php @@ -159,4 +159,24 @@ class Tests_Query_Search extends WP_UnitTestCase { $this->assertEqualSets( array( $p2 ), $q->posts ); } + + /** + * @ticket 35594 + */ + public function test_search_should_respect_suppress_filters() { + add_filter( 'posts_search', array( $this, 'filter_posts_search' ) ); + add_filter( 'posts_search_orderby', array( $this, 'filter_posts_search' ) ); + $q = new WP_Query( array( + 's' => 'foo', + 'suppress_filters' => true, + ) ); + remove_filter( 'posts_search', array( $this, 'filter_posts_search' ) ); + remove_filter( 'posts_search_orderby', array( $this, 'filter_posts_search' ) ); + + $this->assertNotContains( 'posts_search', $q->request ); + } + + public function filter_posts_search( $sql ) { + return $sql . ' /* posts_search */'; + } }