From 1b8e03bd895cf2c16f9f24fa857347d2168eef88 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 19 Jan 2016 02:54:28 +0000 Subject: [PATCH] Ignore false values of 'search' in `WP_Comment_Query`. Props danielbachhuber. Fixes #35513. git-svn-id: https://develop.svn.wordpress.org/trunk@36345 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-comment-query.php | 3 +- tests/phpunit/tests/comment/query.php | 55 ++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index 1068050d38..eb6f094a5e 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -745,7 +745,8 @@ class WP_Comment_Query { $this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] ); } - if ( '' !== $this->query_vars['search'] ) { + // Falsy search strings are ignored. + if ( strlen( $this->query_vars['search'] ) ) { $search_sql = $this->get_search_sql( $this->query_vars['search'], array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 9178cbcb19..01702596f7 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -1179,6 +1179,61 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertEqualSets( array( $c1, $c2, $c3, $c4, $c5 ), $found ); } + /** + * @ticket 35513 + */ + public function test_search_false_should_be_ignored() { + $q = new WP_Comment_Query(); + $q->query( array( + 'search' => false, + ) ); + $this->assertNotContains( "comment_author LIKE", $q->request ); + } + + /** + * @ticket 35513 + */ + public function test_search_null_should_be_ignored() { + $q = new WP_Comment_Query(); + $q->query( array( + 'search' => null, + ) ); + $this->assertNotContains( "comment_author LIKE", $q->request ); + } + + /** + * @ticket 35513 + */ + public function test_search_empty_string_should_be_ignored() { + $q = new WP_Comment_Query(); + $q->query( array( + 'search' => false, + ) ); + $this->assertNotContains( "comment_author LIKE", $q->request ); + } + + /** + * @ticket 35513 + */ + public function test_search_int_0_should_not_be_ignored() { + $q = new WP_Comment_Query(); + $q->query( array( + 'search' => 0, + ) ); + $this->assertContains( "comment_author LIKE '%0%'", $q->request ); + } + + /** + * @ticket 35513 + */ + public function test_search_string_0_should_not_be_ignored() { + $q = new WP_Comment_Query(); + $q->query( array( + 'search' => '0', + ) ); + $this->assertContains( "comment_author LIKE '%0%'", $q->request ); + } + public function test_orderby_default() { global $wpdb;