Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion helpers/QueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
);

if ( $wpdb->last_error ) {
throw new \Exception( $wpdb->last_error );

Check failure on line 47 in helpers/QueryHelper.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$wpdb'.
}

return $insert ? $wpdb->insert_id : 0;
Expand Down Expand Up @@ -147,7 +147,7 @@
$wpdb->query( "DELETE FROM {$table} WHERE id IN ( $ids )");

if ( $wpdb->last_error ) {
throw new \Exception( $wpdb->last_error );

Check failure on line 150 in helpers/QueryHelper.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$wpdb'.
}

return true;
Expand Down Expand Up @@ -696,7 +696,10 @@

// 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 ) ) {
Expand Down Expand Up @@ -1178,7 +1181,7 @@

// Throw exception if error occurred.
if ( $wpdb->last_error ) {
throw new \Exception( $wpdb->last_error );

Check failure on line 1184 in helpers/QueryHelper.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$wpdb'.
}

// Prepare response array.
Expand Down Expand Up @@ -1218,7 +1221,7 @@

// If error occurred then throw new exception.
if ( $wpdb->last_error ) {
throw new \Exception( $wpdb->last_error );

Check failure on line 1224 in helpers/QueryHelper.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$wpdb'.
}

return (int) $count;
Expand Down Expand Up @@ -1259,7 +1262,7 @@

// If error occurred then throw new exception.
if ( $wpdb->last_error ) {
throw new \Exception( $wpdb->last_error );

Check failure on line 1265 in helpers/QueryHelper.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$wpdb'.
}

return (int) $total_count;
Expand Down Expand Up @@ -1293,7 +1296,7 @@

// If error occurred then throw new exception.
if ( $wpdb->last_error ) {
throw new \Exception( $wpdb->last_error );

Check failure on line 1299 in helpers/QueryHelper.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$wpdb'.
}

$query = "SELECT SQL_CALC_FOUND_ROWS *
Expand All @@ -1308,7 +1311,7 @@

// If error occurred then throw new exception.
if ( $wpdb->last_error ) {
throw new \Exception( $wpdb->last_error );

Check failure on line 1314 in helpers/QueryHelper.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$wpdb'.
}

// Prepare response array.
Expand Down Expand Up @@ -1503,7 +1506,7 @@

// If error occurred then throw new exception.
if ( $wpdb->last_error ) {
throw new \Exception( $wpdb->last_error );

Check failure on line 1509 in helpers/QueryHelper.php

View workflow job for this annotation

GitHub Actions / WPCS

All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$wpdb'.
}

return $result;
Expand Down
5 changes: 4 additions & 1 deletion models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
$this->table_name = $this->db->prefix . $this->table_name;
}

}

Check failure on line 67 in models/BaseModel.php

View workflow job for this annotation

GitHub Actions / WPCS

Function closing brace must go on the next line following the body; found 1 blank lines before brace

/**
* Check model is associated with a db table.
Expand Down Expand Up @@ -240,7 +240,10 @@

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 . ')';
}
}
}

Expand Down Expand Up @@ -278,7 +281,7 @@

return (object) $pagination;

}

Check failure on line 284 in models/BaseModel.php

View workflow job for this annotation

GitHub Actions / WPCS

Function closing brace must go on the next line following the body; found 1 blank lines before brace

/**
* Count record.
Expand Down
86 changes: 86 additions & 0 deletions tests/phpunit/QueryHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
Loading