diff --git a/helpers/QueryHelper.php b/helpers/QueryHelper.php index 07319c2346..bdb65632c5 100644 --- a/helpers/QueryHelper.php +++ b/helpers/QueryHelper.php @@ -696,7 +696,10 @@ protected static function prepare_where_search_clause( $where = array(), $search // Handle SEARCH conditions. if ( ! empty( $search ) && is_array( $search ) ) { - $clauses[] = self::prepare_like_clause( $search, $search_operator ); + $like_clause = self::prepare_like_clause( $search, $search_operator ); + if ( ! empty( $like_clause ) ) { + $clauses[] = '(' . $like_clause . ')'; + } } if ( empty( $clauses ) ) { diff --git a/models/BaseModel.php b/models/BaseModel.php index c2ba9de599..f4b79bda0f 100644 --- a/models/BaseModel.php +++ b/models/BaseModel.php @@ -240,7 +240,10 @@ public function paginate( $per_page = 10, $page = 1, $args = array() ) { if ( isset( $args['search'] ) && is_array( $args['search'] ) ) { if ( count( $args['search'] ) ) { - $where_clause .= ( empty( $where_clause ) ? 'WHERE ' : ' AND ' ) . QueryHelper::prepare_like_clause( $args['search'], 'AND' ); + $like_clause = QueryHelper::prepare_like_clause( $args['search'], 'AND' ); + if ( ! empty( $like_clause ) ) { + $where_clause .= ( empty( $where_clause ) ? 'WHERE ' : ' AND ' ) . '(' . $like_clause . ')'; + } } } diff --git a/tests/phpunit/QueryHelperTest.php b/tests/phpunit/QueryHelperTest.php index a882d60c2d..b1ce8faee0 100644 --- a/tests/phpunit/QueryHelperTest.php +++ b/tests/phpunit/QueryHelperTest.php @@ -930,4 +930,90 @@ public function test_prepare_where_clause_injection_in_key() { $this->assertStringContainsString( '1=0', $result ); $this->assertStringNotContainsString( 'DROP', $result ); } + + // ─── prepare_where_search_clause ──────────────────────────────────────── + + /** + * Test that prepare_where_search_clause properly wraps LIKE conditions + * in parentheses to preserve AND operator precedence. + * + * Without parentheses, the OR in LIKE clauses breaks out of AND constraints. + * Example bug: WHERE owner_id = 5 AND name LIKE '%x%' OR email LIKE '%x%' + * becomes: (owner_id = 5 AND name LIKE '%x%') OR email LIKE '%x%' + * allowing rows where owner_id != 5 if email matches. + * + * @since 4.3.1 + * + * @return void + */ + public function test_prepare_where_search_clause_parentheses() { + // Use reflection to access the protected method. + $reflection = new \ReflectionClass( QueryHelper::class ); + $method = $reflection->getMethod( 'prepare_where_search_clause' ); + $method->setAccessible( true ); + + $where = array( 'owner_id' => 5 ); + $search = array( + 'name' => 'john', + 'email' => 'john', + ); + + $result = $method->invoke( null, $where, $search ); + + // The result should contain WHERE, the owner constraint, AND, and the LIKE clause. + $this->assertStringContainsString( 'WHERE', $result ); + $this->assertStringContainsString( '`owner_id` = 5', $result ); + $this->assertStringContainsString( 'AND', $result ); + + // Most importantly: the LIKE clause must be wrapped in parentheses. + // Match pattern: AND (...) where ... contains LIKE and OR. + $this->assertMatchesRegularExpression( '/AND\s+\([^)]*LIKE[^)]*OR[^)]*\)/', $result ); + } + + /** + * Test prepare_where_search_clause with empty search returns only where clause. + * + * @since 4.3.1 + * + * @return void + */ + public function test_prepare_where_search_clause_empty_search() { + $reflection = new \ReflectionClass( QueryHelper::class ); + $method = $reflection->getMethod( 'prepare_where_search_clause' ); + $method->setAccessible( true ); + + $where = array( 'status' => 'active' ); + $search = array(); + + $result = $method->invoke( null, $where, $search ); + + $this->assertStringContainsString( 'WHERE', $result ); + $this->assertStringContainsString( '`status` = \'active\'', $result ); + $this->assertStringNotContainsString( 'LIKE', $result ); + // No parentheses needed when there's no search. + $this->assertStringNotContainsString( 'AND (', $result ); + } + + /** + * Test prepare_where_search_clause with empty where and non-empty search. + * + * @since 4.3.1 + * + * @return void + */ + public function test_prepare_where_search_clause_only_search() { + $reflection = new \ReflectionClass( QueryHelper::class ); + $method = $reflection->getMethod( 'prepare_where_search_clause' ); + $method->setAccessible( true ); + + $where = array(); + $search = array( 'name' => 'test' ); + + $result = $method->invoke( null, $where, $search ); + + $this->assertStringContainsString( 'WHERE', $result ); + $this->assertStringContainsString( 'LIKE', $result ); + // When there's only search (no where), parentheses are optional but harmless. + // Just ensure LIKE is present. + } }