Skip to content

Commit 9ba053a

Browse files
committed
Fix EE_DB Commands
Fix EE_Site_Command Signed-off-by: Kirtan Gajjar <[email protected]>
1 parent 9d0a33d commit 9ba053a

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

php/class-ee-db.php

+30-27
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct() {
2323
$this->offset = '';
2424
$this->where = [
2525
'query_string' => null,
26-
'binding' => null
26+
'bindings' => null,
2727
];
2828
}
2929

@@ -33,8 +33,7 @@ public function __construct() {
3333
private static function init_db() {
3434
try {
3535
self::$pdo = new PDO( 'sqlite:' . DB );
36-
}
37-
catch ( PDOException $exception ) {
36+
} catch ( PDOException $exception ) {
3837
EE::error( $exception->getMessage() );
3938
}
4039

@@ -105,8 +104,7 @@ private static function create_required_tables() {
105104

106105
try {
107106
self::$pdo->exec( $query );
108-
}
109-
catch ( PDOException $exception ) {
107+
} catch ( PDOException $exception ) {
110108
EE::error( 'Encountered Error while creating table: ' . $exception->getMessage() );
111109
}
112110
}
@@ -121,8 +119,8 @@ private static function create_required_tables() {
121119
* @return bool Success.
122120
*/
123121
public static function site_in_db( $site_name ) {
124-
125-
$site = self::table( 'sites' )
122+
$db = new EE_DB();
123+
$site = $db->table( 'sites' )
126124
->select( 'id' )
127125
->where( 'sitename', $site_name )
128126
->first();
@@ -141,7 +139,7 @@ public static function site_in_db( $site_name ) {
141139
* @throws Exception
142140
*/
143141
public function first() {
144-
$pdo_statement = $this->common_retrival_function();
142+
$pdo_statement = $this->common_retrieval_function();
145143

146144
if ( ! $pdo_statement ) {
147145
return false;
@@ -157,7 +155,7 @@ public function first() {
157155
* @return bool|PDOStatement
158156
* @throws Exception
159157
*/
160-
private function common_retrival_function() {
158+
private function common_retrieval_function() {
161159
if ( null === $this->tables ) {
162160
throw new Exception( 'Select: No table specified' );
163161
}
@@ -172,8 +170,8 @@ private function common_retrival_function() {
172170

173171
$bindings = $this->where['bindings'] ?? [];
174172

175-
foreach ( $bindings as $key => $value ) {
176-
$pdo_statement->bindValue( $key + 1, $value );
173+
foreach ( $bindings as $key => $binding ) {
174+
$pdo_statement->bindValue( $key + 1, $binding );
177175
}
178176

179177
$result = $pdo_statement->execute();
@@ -270,7 +268,8 @@ public function table( ...$args ) {
270268
*/
271269
public static function site_enabled( $site_name ) {
272270

273-
$site = self::table( 'sites' )
271+
$db = new EE_DB();
272+
$site = $db->table( 'sites' )
274273
->select( 'id', 'is_enabled' )
275274
->where( 'sitename', $site_name )
276275
->first();
@@ -292,10 +291,11 @@ public static function site_enabled( $site_name ) {
292291
* @return string type of site.
293292
*/
294293
public static function get_site_command( $site_name ) {
295-
$site = self::table( 'sites' )
294+
$db = new EE_DB();
295+
$site = $db->table( 'sites' )
296296
->select( 'site_command' )
297297
->where( 'sitename', $site_name )
298-
->get();
298+
->first();
299299

300300
return $site['site_command'];
301301
}
@@ -307,7 +307,7 @@ public static function get_site_command( $site_name ) {
307307
* @throws Exception
308308
*/
309309
public function get() {
310-
$pdo_statement = $this->common_retrival_function();
310+
$pdo_statement = $this->common_retrieval_function();
311311

312312
if ( ! $pdo_statement ) {
313313
return false;
@@ -321,7 +321,8 @@ public function get() {
321321
*/
322322
public static function get_migrations() {
323323

324-
$migrations = self::table( 'migrations' )
324+
$db = new EE_DB();
325+
$migrations = $db->table( 'migrations' )
325326
->select( 'migration' )
326327
->get();
327328

@@ -372,7 +373,7 @@ public function insert( $data ) {
372373
throw new Exception( 'Insert: No table specified' );
373374
}
374375

375-
if ( count( $this->tables ) > 1 ) {
376+
if ( strpos( $this->tables, ',' ) !== false ) {
376377
throw new Exception( 'Insert: Multiple table specified' );
377378
}
378379

@@ -412,7 +413,7 @@ public function update( $values ) {
412413
throw new Exception( 'Update: No where clause specified' );
413414
}
414415

415-
if ( count( $this->tables ) > 1 ) {
416+
if ( strpos( $this->tables, ',' ) !== false ) {
416417
throw new Exception( 'Update: Multiple table specified' );
417418
}
418419

@@ -421,8 +422,8 @@ public function update( $values ) {
421422
}
422423

423424
$set_keys = array_keys( $values );
424-
$set_values = array_values( $values );
425-
$where_values = $this->where['binding'];
425+
$set_bindings = array_values( $values );
426+
$where_bindings = $this->where['bindings'];
426427

427428
$set_clause = implode( $set_keys, ' = ?, ' ) . ' = ?';
428429

@@ -431,17 +432,17 @@ public function update( $values ) {
431432

432433
$counter = 0; //We need counter here as we need to bind values of both SET and WHERE clauses
433434

434-
foreach ( $set_values as $value ) {
435-
$pdo_statement->bindValue( ++ $counter, $value );
435+
foreach ( $set_bindings as $binding ) {
436+
$pdo_statement->bindValue( ++ $counter, $binding );
436437
}
437438

438-
foreach ( $where_values as $value ) {
439-
$pdo_statement->bindValue( ++ $counter, $value );
439+
foreach ( $where_bindings as $binding ) {
440+
$pdo_statement->bindValue( ++ $counter, $binding );
440441
}
441442

442443
$result = $pdo_statement->execute();
443444

444-
if ( ! $query_exec ) {
445+
if ( ! $result ) {
445446
EE::debug( self::$pdo->errorInfo() );
446447

447448
return false;
@@ -466,7 +467,7 @@ public function delete() {
466467
throw new Exception( 'Delete: No where clause specified' );
467468
}
468469

469-
if ( count( $this->tables ) > 1 ) {
470+
if ( strpos( $this->tables, ',' ) !== false ) {
470471
throw new Exception( 'Delete: Multiple table specified' );
471472
}
472473

@@ -475,9 +476,11 @@ public function delete() {
475476
$pdo_statement = self::$pdo->query( $query );
476477

477478
foreach ( $this->where['bindings'] as $key => $binding ) {
478-
$pdo_statement->bindValue( $key + 1, $value );
479+
$pdo_statement->bindValue( $key + 1, $binding );
479480
}
480481

482+
$result = $pdo_statement->execute();
483+
481484
if ( ! $result ) {
482485
EE::debug( self::$pdo->errorInfo() );
483486

php/class-ee-docker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public static function docker_compose_up( $dir, $services = [] ) {
194194
return false;
195195
}
196196

197-
/**
197+
/**
198198
* Function to destroy the containers.
199199
*
200200
* @param String $dir Path to docker-compose.yml.

php/class-ee-site.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function down( $args, $assoc_args ) {
241241
$this->populate_site_info( $args );
242242
EE::log( sprintf( 'Disabling site %s.', $this->site['name'] ) );
243243
if ( EE::docker()::docker_compose_down( $this->site['root'] ) ) {
244-
EE::db()::update( [ 'is_enabled' => '0' ], [ 'sitename' => $this->site['name'] ] );
244+
EE::db()->table( 'sites' )->where( 'sitename', $this->site['name'] )->update( [ 'is_enabled' => '0' ] );
245245
EE::success( sprintf( 'Site %s disabled.', $this->site['name'] ) );
246246
} else {
247247
EE::error( sprintf( 'There was error in disabling %s. Please check logs.', $this->site['name'] ) );
@@ -414,9 +414,9 @@ private function populate_site_info( $args ) {
414414

415415
$this->site['name'] = EE\Utils\remove_trailing_slash( $args[0] );
416416

417-
if ( EE::db()::site_in_db( $this->site['name'] ) ) {
417+
if ( EE::db()->site_in_db( $this->site['name'] ) ) {
418418

419-
$db_select = EE::db()::select( [], [ 'sitename' => $this->site['name'] ], 'sites', 1 );
419+
$db_select = EE::db()->table( 'sites' )->where( 'sitename', $this->site['name'] )->first();
420420

421421
$this->site['type'] = $db_select['site_type'];
422422
$this->site['root'] = $db_select['site_path'];

phpcs.xml.dist

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar" />
3434
<exclude name="WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase" />
3535
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase" />
36-
<exclude name="WordPress.DB.*" />
36+
<exclude name="WordPress.DB.RestrictedFunctions" />
37+
<exclude name="WordPress.DB.RestrictedClasses" />
3738
</rule>
3839
<rule ref="WordPress.Files.FileName">
3940
<properties>

0 commit comments

Comments
 (0)