Skip to content

Commit 45ffb83

Browse files
Refactor db usage (#22)
* refactors database use by implementing specific classes for use cases * phpcs * phpcbf * use wpdb directly instead of saving it as member --------- Co-authored-by: PT-ATA No One <ata-no-one@gdata.de>
1 parent 1482fdc commit 45ffb83

File tree

5 files changed

+77
-50
lines changed

5 files changed

+77
-50
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"vscode": {
99
"extensions": [
1010
"recca0120.vscode-phpunit",
11-
"github.vscode-github-actions"
11+
"github.vscode-github-actions",
12+
"slevesque.vscode-zipexplorer"
1213
]
1314
}
1415
},

Infrastructure/Database/FindingsQuery.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,24 @@
66
use wpdb;
77

88
class FindingsQuery implements IFindingsQuery {
9-
private wpdb $wpdb;
109
private LoggerInterface $logger;
1110

1211
public function __construct(
1312
LoggerInterface $logger,
1413
) {
15-
global $wpdb;
16-
$this->wpdb = $wpdb;
1714
$this->logger = $logger;
1815
}
1916

2017
private function get_table_name(): string {
21-
return $this->wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FINDINGS_TABLE_NAME;
18+
global $wpdb;
19+
20+
return $wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FINDINGS_TABLE_NAME;
2221
}
2322

2423
public function create(): void {
25-
$charset_collate = $this->wpdb->get_charset_collate();
24+
global $wpdb;
25+
26+
$charset_collate = $wpdb->get_charset_collate();
2627
$sql = 'CREATE TABLE ' . $this->get_table_name() . ' (
2728
file_path VARCHAR(512) NOT NULL,
2829
UNIQUE KEY file_path (file_path)
@@ -34,21 +35,25 @@ public function create(): void {
3435
}
3536

3637
public function remove(): void {
38+
global $wpdb;
39+
3740
if (! $this->table_exists()) {
3841
return;
3942
}
40-
$this->wpdb->query(
41-
$this->wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name())
43+
$wpdb->query(
44+
$wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name())
4245
);
4346
\wp_cache_set($this->get_table_name(), 'false', 'GdataAntivirus');
4447
}
4548

4649
public function table_exists(): bool {
50+
global $wpdb;
51+
4752
$tables_exists = \wp_cache_get($this->get_table_name(), 'GdataAntivirus');
4853
$this->logger->debug('Exists in cache: ' . ($tables_exists ? 'true' : 'false'));
4954
if (false === $tables_exists) {
50-
$exists = $this->wpdb->get_var(
51-
$this->wpdb->prepare('SHOW TABLES LIKE %s', $this->get_table_name())
55+
$exists = $wpdb->get_var(
56+
$wpdb->prepare('SHOW TABLES LIKE %s', $this->get_table_name())
5257
) === $this->get_table_name();
5358
$this->logger->debug('Exists in database: ' . ($exists ? 'true' : 'false'));
5459
\wp_cache_set($this->get_table_name(), \wp_json_encode($exists), 'GdataAntivirus');
@@ -61,12 +66,14 @@ public function table_exists(): bool {
6166
}
6267

6368
public function add( string $file ): void {
69+
global $wpdb;
70+
6471
if (! $this->table_exists()) {
6572
return;
6673
}
6774

6875
try {
69-
$this->wpdb->insert(
76+
$wpdb->insert(
7077
$this->get_table_name(),
7178
array( 'file_path' => $file )
7279
);
@@ -76,32 +83,38 @@ public function add( string $file ): void {
7683
}
7784

7885
public function delete( string $file ): void {
86+
global $wpdb;
87+
7988
if (! $this->table_exists()) {
8089
return;
8190
}
82-
$this->wpdb->delete(
91+
$wpdb->delete(
8392
$this->get_table_name(),
8493
array( 'file_path' => $file )
8594
);
8695
}
8796

8897
public function get_all(): array {
98+
global $wpdb;
99+
89100
if (! $this->table_exists()) {
90101
return array();
91102
}
92-
return $this->wpdb->get_results(
93-
$this->wpdb->prepare('SELECT file_path FROM %i', $this->get_table_name()),
103+
return $wpdb->get_results(
104+
$wpdb->prepare('SELECT file_path FROM %i', $this->get_table_name()),
94105
ARRAY_A
95106
);
96107
}
97108

98109
public function count(): int {
110+
global $wpdb;
111+
99112
$this->logger->debug('FindingsMenuPage::get_findings_count');
100113
if (! $this->table_exists()) {
101114
return 0;
102115
}
103-
return (int) $this->wpdb->get_var(
104-
$this->wpdb->prepare('SELECT COUNT(*) FROM %i', $this->get_table_name())
116+
return (int) $wpdb->get_var(
117+
$wpdb->prepare('SELECT COUNT(*) FROM %i', $this->get_table_name())
105118
);
106119
}
107120

Infrastructure/Database/ScansQuery.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,20 @@
22

33
namespace Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database;
44

5-
use Psr\Log\LoggerInterface;
6-
use wpdb;
7-
85
class ScansQuery implements IScansQuery {
9-
private wpdb $wpdb;
10-
private LoggerInterface $logger;
11-
12-
public function __construct(
13-
LoggerInterface $logger,
14-
) {
15-
global $wpdb;
16-
$this->wpdb = $wpdb;
17-
$this->logger = $logger;
6+
public function __construct() {
187
}
198

209
private function get_table_name(): string {
21-
return $this->wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_OPERATIONS_TABLE_NAME;
10+
global $wpdb;
11+
12+
return $wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_OPERATIONS_TABLE_NAME;
2213
}
2314

2415
public function create(): void {
25-
$charset_collate = $this->wpdb->get_charset_collate();
16+
global $wpdb;
17+
18+
$charset_collate = $wpdb->get_charset_collate();
2619
$sql = 'CREATE TABLE ' . $this->get_table_name() . ' (
2720
scheduled_scans TINYINT NOT NULL DEFAULT 0,
2821
finished_scans TINYINT NOT NULL DEFAULT 0
@@ -31,56 +24,72 @@ public function create(): void {
3124
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
3225
dbDelta($sql);
3326

34-
$this->wpdb->query(
35-
$this->wpdb->prepare('INSERT INTO %i (scheduled_scans, finished_scans) VALUES (0, 0)', $this->get_table_name())
27+
$wpdb->query(
28+
$wpdb->prepare('INSERT INTO %i (scheduled_scans, finished_scans) VALUES (0, 0)', $this->get_table_name())
3629
);
3730
}
3831

3932
public function remove(): void {
40-
$this->wpdb->query(
41-
$this->wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name())
33+
global $wpdb;
34+
35+
$wpdb->query(
36+
$wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name())
4237
);
4338
}
4439

4540
public function write_lock(): void {
46-
$this->wpdb->query(
47-
$this->wpdb->prepare('LOCK TABLES %i WRITE', $this->get_table_name())
41+
global $wpdb;
42+
43+
$wpdb->query(
44+
$wpdb->prepare('LOCK TABLES %i WRITE', $this->get_table_name())
4845
);
4946
}
5047

5148
public function write_unlock(): void {
52-
$this->wpdb->query(
53-
$this->wpdb->prepare('UNLOCK TABLES %i WRITE', $this->get_table_name())
49+
global $wpdb;
50+
51+
$wpdb->query(
52+
$wpdb->prepare('UNLOCK TABLES %i WRITE', $this->get_table_name())
5453
);
5554
}
5655

5756
public function scheduled_count(): int {
58-
return $this->wpdb->get_var(
59-
$this->wpdb->prepare('SELECT scheduled_scans FROM %i', $this->get_table_name())
57+
global $wpdb;
58+
59+
return $wpdb->get_var(
60+
$wpdb->prepare('SELECT scheduled_scans FROM %i', $this->get_table_name())
6061
);
6162
}
6263

6364
public function increase_scheduled(): void {
64-
$this->wpdb->query(
65-
$this->wpdb->prepare('UPDATE %i SET scheduled_scans = scheduled_scans + 1', $this->get_table_name())
65+
global $wpdb;
66+
67+
$wpdb->query(
68+
$wpdb->prepare('UPDATE %i SET scheduled_scans = scheduled_scans + 1', $this->get_table_name())
6669
);
6770
}
6871

6972
public function finished_count(): int {
70-
return $this->wpdb->get_var(
71-
$this->wpdb->prepare('SELECT finished_scans FROM %i', $this->get_table_name())
73+
global $wpdb;
74+
75+
return $wpdb->get_var(
76+
$wpdb->prepare('SELECT finished_scans FROM %i', $this->get_table_name())
7277
);
7378
}
7479

7580
public function increase_finished(): void {
76-
$this->wpdb->query(
77-
$this->wpdb->prepare('UPDATE %i SET finished_scans = finished_scans + 1', $this->get_table_name())
81+
global $wpdb;
82+
83+
$wpdb->query(
84+
$wpdb->prepare('UPDATE %i SET finished_scans = finished_scans + 1', $this->get_table_name())
7885
);
7986
}
8087

8188
public function reset(): void {
82-
$this->wpdb->query(
83-
$this->wpdb->prepare('UPDATE %i SET scheduled_scans = 0, finished_scans = 0', $this->get_table_name())
89+
global $wpdb;
90+
91+
$wpdb->query(
92+
$wpdb->prepare('UPDATE %i SET scheduled_scans = 0, finished_scans = 0', $this->get_table_name())
8493
);
8594
}
8695
}

Vaas/ScanClient.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ public function scan_single_upload( $file ) {
160160
$is_plugin_uplad = false;
161161

162162
$action = \sanitize_key($_GET['action'] ?? $_POST['action'] ?? '');
163-
$nonce = \sanitize_key($_POST['nonce'] ?? $_POST['_wpnonce']);
163+
if (isset($_POST['_wpnonce'])) {
164+
$nonce = \sanitize_key($_POST['nonce'] ?? $_POST['_wpnonce']);
165+
} else {
166+
$nonce = \sanitize_key($_GET['nonce'] ?? '');
167+
}
164168
if ($action === 'upload-plugin') {
165169
if (wp_verify_nonce($nonce, $action) === false) {
166170
return $file;

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)