Skip to content

Commit e3d991a

Browse files
jujoko7CFstklcode
authored andcommitted
add new statifymeta table
1 parent e789310 commit e3d991a

6 files changed

Lines changed: 136 additions & 100 deletions

inc/class-statify-install.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ private static function _apply() {
8282
);
8383
}
8484

85-
// Create the actual tables.
86-
Statify_Table::init();
87-
Statify_Table::create();
85+
// Initialize the database schema.
86+
Statify_Schema::init();
8887
}
8988
}

inc/class-statify-schema.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Statify: Statify_Schema class
4+
*
5+
* This file contains class for the plugin's DB scheme handling.
6+
*
7+
* @package Statify
8+
* @since 2.0.0
9+
*/
10+
11+
// Quit if accessed outside WP context.
12+
defined( 'ABSPATH' ) || exit;
13+
14+
/**
15+
* Statify DB Schema
16+
*
17+
* @since 2.0.0
18+
*/
19+
class Statify_Schema {
20+
/**
21+
* Database tables
22+
*
23+
* @var string[]
24+
*/
25+
public static $tables = array(
26+
'statify',
27+
'statifymeta',
28+
);
29+
30+
/**
31+
* Needed statify db version for current plugin version
32+
*
33+
* @var string
34+
*/
35+
public static $db_version = '2.0.0';
36+
37+
/**
38+
* Definition of the custom tables.
39+
*
40+
* @since 2.0.0
41+
* @version 2.0.0
42+
*/
43+
public static function init() {
44+
// Global.
45+
global $wpdb;
46+
47+
foreach ( static::$tables as $table ) {
48+
$wpdb->tables[] = $table;
49+
$wpdb->$table = $wpdb->get_blog_prefix() . $table;
50+
}
51+
52+
self::maybe_create_tables();
53+
}
54+
55+
/**
56+
* Create the tables.
57+
*
58+
* @since 2.0.0
59+
* @version 2.0.0
60+
*/
61+
public static function maybe_create_tables() {
62+
$current_db_version = get_option( 'statify_db_version', '1.8.4' );
63+
if ( $current_db_version === self::$db_version ) {
64+
return;
65+
}
66+
67+
// Global.
68+
global $wpdb, $charset_collate;
69+
70+
/**
71+
* Use same index length like the WordPress core
72+
*
73+
* @see wp_get_db_schema()
74+
*/
75+
$max_index_length = 191;
76+
77+
// Include.
78+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
79+
80+
// Create statify table.
81+
dbDelta(
82+
"CREATE TABLE {$wpdb->statify} (
83+
id bigint(20) unsigned NOT NULL auto_increment,
84+
created date NOT NULL default '0000-00-00',
85+
referrer varchar(255) NOT NULL default '',
86+
target varchar(255) NOT NULL default '',
87+
hits integer NOT NULL default 1,
88+
PRIMARY KEY (id),
89+
KEY referrer (referrer),
90+
KEY target (target),
91+
KEY created (created)
92+
) {$charset_collate};"
93+
);
94+
95+
// Create statifymeta table.
96+
dbDelta(
97+
"CREATE TABLE {$wpdb->statifymeta} (
98+
meta_id bigint(20) unsigned NOT NULL auto_increment,
99+
statify_id bigint(20) unsigned NOT NULL default 0,
100+
meta_key varchar(255) default NULL,
101+
meta_value longtext,
102+
PRIMARY KEY (meta_id),
103+
KEY statify_id (statify_id),
104+
KEY meta_key (meta_key({$max_index_length}))
105+
) {$charset_collate};"
106+
);
107+
108+
update_option( 'statify_db_version', self::$db_version );
109+
}
110+
111+
/**
112+
* Remove the custom tables.
113+
*
114+
* @since 2.0.0
115+
* @version 2.0.0
116+
*/
117+
public static function drop_tables() {
118+
global $wpdb;
119+
120+
// Remove.
121+
foreach ( static::$tables as $table ) {
122+
$wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->$table}`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
123+
}
124+
125+
delete_option( 'statify_db_version' );
126+
}
127+
}

inc/class-statify-table.php

Lines changed: 0 additions & 90 deletions
This file was deleted.

inc/class-statify-uninstall.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ private static function _apply() {
7070
// Delete options.
7171
delete_option( 'statify' );
7272

73-
// Init table.
74-
Statify_Table::init();
73+
// Initialize the database schema.
74+
Statify_Schema::init();
7575

76-
// Delete table.
77-
Statify_Table::drop();
76+
// Delete tables.
77+
Statify_Schema::drop_tables();
7878
}
7979
}

inc/class-statify.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public static function init() {
4444
return;
4545
}
4646

47-
// Table init.
48-
Statify_Table::init();
47+
// Initialize the database schema.
48+
Statify_Schema::init();
4949

5050
// Plugin options.
5151
self::$_options = wp_parse_args(

statify.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function statify_autoload( $class ) {
7373
'Statify_Uninstall',
7474
'Statify_Deactivate',
7575
'Statify_Settings',
76-
'Statify_Table',
76+
'Statify_Schema',
7777
'Statify_XMLRPC',
7878
'Statify_Cron',
7979
);

0 commit comments

Comments
 (0)