Skip to content

Commit e45d7ec

Browse files
committed
Add getTableName functions for each table class
1 parent 094e31e commit e45d7ec

7 files changed

Lines changed: 76 additions & 61 deletions

File tree

src/Addons.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
namespace WP2Static;
44

55
class Addons {
6+
public static function getTableName() : string {
7+
return Controller::getTableName( 'addons' );
8+
}
9+
610
public static function createTable() : void {
711
global $wpdb;
812

9-
$table_name = $wpdb->prefix . 'wp2static_addons';
13+
$table_name = self::getTableName();
1014

1115
$charset_collate = $wpdb->get_charset_collate();
1216

@@ -35,7 +39,7 @@ public static function registerAddon(
3539

3640
global $wpdb;
3741

38-
$table_name = $wpdb->prefix . 'wp2static_addons';
42+
$table_name = self::getTableName();
3943

4044
$sql = "INSERT IGNORE INTO {$table_name} (slug,type,name,docs_url,description)" .
4145
' VALUES (%s,%s,%s,%s,%s)';
@@ -54,7 +58,7 @@ public static function getAll( string $type = 'all' ) : array {
5458
global $wpdb;
5559
$addons = [];
5660

57-
$table_name = $wpdb->prefix . 'wp2static_addons';
61+
$table_name = self::getTableName();
5862

5963
$query_string = "SELECT * FROM $table_name";
6064
$query_params = [];
@@ -78,7 +82,7 @@ public static function getAll( string $type = 'all' ) : array {
7882
public static function getType( string $type ) : array {
7983
global $wpdb;
8084

81-
$table_name = $wpdb->prefix . 'wp2static_addons';
85+
$table_name = self::getTableName();
8286

8387
$query = $wpdb->prepare(
8488
"SELECT * FROM $table_name WHERE type = %s AND enabled = 1 ORDER BY slug",
@@ -95,7 +99,7 @@ public static function getType( string $type ) : array {
9599
public static function truncate() : void {
96100
global $wpdb;
97101

98-
$table_name = $wpdb->prefix . 'wp2static_addons';
102+
$table_name = self::getTableName();
99103

100104
$wpdb->query( "TRUNCATE TABLE $table_name" );
101105

src/Controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ public static function wp2staticToggleAddon( string $addon_slug = null ) : void
513513

514514
global $wpdb;
515515

516-
$table_name = $wpdb->prefix . 'wp2static_addons';
516+
$table_name = Addons::getTableName();
517517

518518
// get target addon's current state
519519
$addon =
@@ -584,7 +584,7 @@ public static function wp2staticProcessQueue() : void {
584584
$jobs = JobQueue::getProcessableJobs();
585585

586586
foreach ( $jobs as $job ) {
587-
$lock = $wpdb->prefix . '.wp2static_jobs.' . $job->job_type;
587+
$lock = JobQueue::getTableName() . '.' . $job->job_type;
588588
$query = "SELECT GET_LOCK('$lock', 30) AS lck";
589589
$locked = intval( $wpdb->get_row( $query )->lck );
590590
if ( ! $locked ) {
@@ -658,7 +658,7 @@ public static function wp2staticProcessQueue() : void {
658658
JobQueue::setStatus( $job->id, 'failed' );
659659
// We don't want to crawl and deploy if the detect step fails.
660660
// Skip all waiting jobs when one fails.
661-
$table_name = $wpdb->prefix . 'wp2static_jobs';
661+
$table_name = JobQueue::getTableName();
662662
$wpdb->query(
663663
"UPDATE $table_name
664664
SET status = 'skipped'

src/CoreOptions.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace WP2Static;
44

55
/*
6-
Simple interface to wp2static_core_options DB table
6+
Simple interface to the core options DB table
77
88
99
*/
@@ -14,20 +14,19 @@ class CoreOptions {
1414
*/
1515
private static $cached_option_specs = null;
1616

17-
/**
18-
* @var string
19-
*/
20-
private static $table_name = 'wp2static_core_options';
21-
2217
public static function init() : void {
2318
self::createTable();
2419
self::seedOptions();
2520
}
2621

22+
public static function getTableName() : string {
23+
return Controller::getTableName( 'core_options' );
24+
}
25+
2726
public static function createTable() : void {
2827
global $wpdb;
2928

30-
$table_name = $wpdb->prefix . self::$table_name;
29+
$table_name = self::getTableName();
3130

3231
$charset_collate = $wpdb->get_charset_collate();
3332

@@ -394,7 +393,7 @@ public static function optionSpecs() : array {
394393
public static function seedOptions() : void {
395394
global $wpdb;
396395

397-
$table_name = $wpdb->prefix . self::$table_name;
396+
$table_name = self::getTableName();
398397

399398
$query_string =
400399
"INSERT IGNORE INTO $table_name (name, value, blob_value)
@@ -427,7 +426,7 @@ public static function getValue( string $name ) : string {
427426
return '';
428427
}
429428

430-
$table_name = $wpdb->prefix . self::$table_name;
429+
$table_name = self::getTableName();
431430

432431
$sql = $wpdb->prepare(
433432
"SELECT value FROM $table_name WHERE" . ' name = %s LIMIT 1',
@@ -465,7 +464,7 @@ public static function getValue( string $name ) : string {
465464
public static function getBlobValue( string $name ) : string {
466465
global $wpdb;
467466

468-
$table_name = $wpdb->prefix . self::$table_name;
467+
$table_name = self::getTableName();
469468

470469
$sql = $wpdb->prepare(
471470
"SELECT blob_value FROM $table_name WHERE" . ' name = %s LIMIT 1',
@@ -536,7 +535,7 @@ public static function getDefaultLineDelimitedBlobValue( string $name ) : array
536535
public static function get( string $name ) {
537536
global $wpdb;
538537

539-
$table_name = $wpdb->prefix . self::$table_name;
538+
$table_name = self::getTableName();
540539

541540
$sql = $wpdb->prepare(
542541
"SELECT name, value, blob_value
@@ -583,7 +582,7 @@ public static function get( string $name ) {
583582
public static function getAll() {
584583
global $wpdb;
585584

586-
$table_name = $wpdb->prefix . self::$table_name;
585+
$table_name = self::getTableName();
587586

588587
$sql = "SELECT name, value, blob_value FROM $table_name";
589588

@@ -673,7 +672,7 @@ public static function encrypt_decrypt( string $action, string $string ) : strin
673672
public static function savePosted( string $screen = 'core' ) : void {
674673
global $wpdb;
675674

676-
$table_name = $wpdb->prefix . self::$table_name;
675+
$table_name = self::getTableName();
677676

678677
switch ( $screen ) {
679678
case 'core':
@@ -920,7 +919,7 @@ public static function savePosted( string $screen = 'core' ) : void {
920919
public static function save( string $name, $value ) : void {
921920
global $wpdb;
922921

923-
$table_name = $wpdb->prefix . self::$table_name;
922+
$table_name = self::getTableName();
924923

925924
// TODO: some validation on save types
926925
$wpdb->update(

src/DeployCache.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ class DeployCache {
66

77
const DEFAULT_NAMESPACE = 'default';
88

9+
10+
public static function getTableName() : string {
11+
return Controller::getTableName( 'deploy_cache' );
12+
}
13+
914
public static function createTable() : void {
1015
global $wpdb;
1116

12-
$table_name = $wpdb->prefix . 'wp2static_deploy_cache';
17+
$table_name = self::getTableName();
1318

1419
$charset_collate = $wpdb->get_charset_collate();
1520

@@ -56,7 +61,7 @@ public static function addFile(
5661
) : void {
5762
global $wpdb;
5863

59-
$deploy_cache_table = $wpdb->prefix . 'wp2static_deploy_cache';
64+
$table_name = self::getTableName();
6065

6166
$post_processed_dir = ProcessedSite::getPath();
6267

@@ -74,7 +79,7 @@ public static function addFile(
7479
$file_hash = md5( $file_contents );
7580
}
7681

77-
$sql = "INSERT INTO {$deploy_cache_table} (path_hash,path,file_hash,namespace)" .
82+
$sql = "INSERT INTO {$table_name} (path_hash,path,file_hash,namespace)" .
7883
' VALUES (%s,%s,%s,%s) ON DUPLICATE KEY UPDATE file_hash = %s, namespace = %s';
7984

8085
$sql = $wpdb->prepare(
@@ -119,7 +124,7 @@ public static function fileisCached(
119124
$file_hash = md5( $file_contents );
120125
}
121126

122-
$table_name = $wpdb->prefix . 'wp2static_deploy_cache';
127+
$table_name = self::getTableName();
123128

124129
$sql = $wpdb->prepare(
125130
"SELECT path_hash FROM $table_name WHERE" .
@@ -141,7 +146,7 @@ public static function truncate(
141146

142147
global $wpdb;
143148

144-
$table_name = $wpdb->prefix . 'wp2static_deploy_cache';
149+
$table_name = self::getTableName();
145150

146151
if ( ! $namespace ) {
147152
$sql = "TRUNCATE TABLE $table_name";
@@ -160,7 +165,7 @@ public static function getTotalByNamespace(
160165
) : int {
161166
global $wpdb;
162167

163-
$table_name = $wpdb->prefix . 'wp2static_deploy_cache';
168+
$table_name = self::getTableName();
164169

165170
$sql = "SELECT count(*) FROM $table_name WHERE namespace = %s";
166171
$sql = $wpdb->prepare( $sql, $namespace );
@@ -178,7 +183,7 @@ public static function getTotal() : array {
178183
global $wpdb;
179184
$counts = [];
180185

181-
$table_name = $wpdb->prefix . 'wp2static_deploy_cache';
186+
$table_name = self::getTableName();
182187

183188
$sql = "SELECT namespace, COUNT(*) AS count FROM $table_name GROUP BY namespace";
184189
$rows = $wpdb->get_results( $sql );
@@ -202,7 +207,7 @@ public static function getPaths(
202207
global $wpdb;
203208
$urls = [];
204209

205-
$table_name = $wpdb->prefix . 'wp2static_deploy_cache';
210+
$table_name = self::getTableName();
206211

207212
$sql = "SELECT path FROM $table_name WHERE namespace = %s ORDER BY path";
208213
$sql = $wpdb->prepare( $sql, $namespace );

src/JobQueue.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
class JobQueue {
66

7+
public static function getTableName() : string {
8+
return Controller::getTableName( 'jobs' );
9+
}
10+
711
public static function createTable() : void {
812
global $wpdb;
913

10-
$table_name = $wpdb->prefix . 'wp2static_jobs';
14+
$table_name = self::getTableName();
1115

1216
$charset_collate = $wpdb->get_charset_collate();
1317

@@ -47,7 +51,7 @@ public static function addJob( string $job_type, ?int $post_id = null ) : void {
4751

4852
global $wpdb;
4953

50-
$table_name = $wpdb->prefix . 'wp2static_jobs';
54+
$table_name = self::getTableName();
5155

5256
// Add triggering_post_id column if it doesn't exist already
5357
$triggering_post_id_row = $wpdb->get_row(
@@ -76,7 +80,7 @@ public static function getJobs() : array {
7680
global $wpdb;
7781
$urls = [];
7882

79-
$table_name = $wpdb->prefix . 'wp2static_jobs';
83+
$table_name = self::getTableName();
8084

8185
$rows = $wpdb->get_results( "SELECT * FROM $table_name ORDER BY id DESC" );
8286

@@ -96,7 +100,7 @@ public static function jobsInProgress() : bool {
96100
global $wpdb;
97101
$jobs = [];
98102

99-
$table_name = $wpdb->prefix . 'wp2static_jobs';
103+
$table_name = self::getTableName();
100104

101105
$jobs_in_progress = $wpdb->get_var(
102106
"SELECT COUNT(*) FROM $table_name
@@ -115,7 +119,7 @@ public static function getProcessableJobs() : array {
115119
global $wpdb;
116120
$jobs = [];
117121

118-
$table_name = $wpdb->prefix . 'wp2static_jobs';
122+
$table_name = self::getTableName();
119123

120124
$rows = $wpdb->get_results(
121125
"SELECT * FROM $table_name
@@ -139,7 +143,7 @@ public static function getJobCountByType() : array {
139143
global $wpdb;
140144
$jobs = [];
141145

142-
$table_name = $wpdb->prefix . 'wp2static_jobs';
146+
$table_name = self::getTableName();
143147
$query = "SELECT job_type, count(*) FROM $table_name GROUP BY job_type";
144148

145149
$rows = $wpdb->get_results( $query, 'ARRAY_N' );
@@ -157,7 +161,7 @@ public static function getJobCountByType() : array {
157161
public static function squashQueue() : void {
158162
global $wpdb;
159163

160-
$table_name = $wpdb->prefix . 'wp2static_jobs';
164+
$table_name = self::getTableName();
161165

162166
// TODO: loop for each job_type
163167
$job_types = [
@@ -215,7 +219,7 @@ public static function squashQueue() : void {
215219
public static function setStatus( int $id, string $status ) : void {
216220
global $wpdb;
217221

218-
$table_name = $wpdb->prefix . 'wp2static_jobs';
222+
$table_name = self::getTableName();
219223

220224
$wpdb->update(
221225
$table_name,
@@ -232,7 +236,7 @@ public static function setStatus( int $id, string $status ) : void {
232236
public static function getTotalJobs() : int {
233237
global $wpdb;
234238

235-
$table_name = $wpdb->prefix . 'wp2static_jobs';
239+
$table_name = self::getTableName();
236240

237241
$total_jobs = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
238242

@@ -251,7 +255,7 @@ public static function getWaitingJobs() : int {
251255
public static function getWaitingJobsCount() : int {
252256
global $wpdb;
253257

254-
$table_name = $wpdb->prefix . 'wp2static_jobs';
258+
$table_name = self::getTableName();
255259

256260
$total_jobs = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name WHERE status = 'waiting'" );
257261

@@ -266,7 +270,7 @@ public static function truncate() : void {
266270

267271
global $wpdb;
268272

269-
$table_name = $wpdb->prefix . 'wp2static_jobs';
273+
$table_name = self::getTableName();
270274

271275
$wpdb->query( "TRUNCATE TABLE $table_name" );
272276

@@ -286,13 +290,13 @@ public static function markFailedJobs() : void {
286290
global $wpdb;
287291

288292
$job_types = [ 'detect', 'crawl', 'post_process', 'deploy', 'direct_deploy' ];
289-
$table_name = $wpdb->prefix . 'wp2static_jobs';
293+
$table_name = self::getTableName();
290294

291295
$wpdb->query( 'START TRANSACTION' );
292296

293297
foreach ( $job_types as $type ) {
294298
try {
295-
$lock = "{$wpdb->prefix}.wp2static_jobs.$type";
299+
$lock = self::getTableName() . '.' . $type;
296300
$query = "SELECT IS_FREE_LOCK('$lock') AS free";
297301
$free = intval( $wpdb->get_row( $query )->free );
298302

0 commit comments

Comments
 (0)