-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.class.php
98 lines (84 loc) · 1.98 KB
/
setup.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handle plugin setup
*/
class NashaatSetup {
/**
* Current database version
*
* @var string
*/
private $db_version = '1.1';
/**
* Table name
*
* @var string
*/
private $table_name = NASHAAT_DB_TABLE;
/**
* Setup hooks
*/
public function __construct() {
register_activation_hook( NASHAAT_MAIN_FILE, array( $this, 'on_plugin_install' ) );
register_deactivation_hook( NASHAAT_MAIN_FILE, array( $this, 'on_plugin_deactivation' ) );
add_action( 'plugins_loaded', array( $this, 'on_plugin_update' ) );
}
/**
* Run when plugin deactivated.
*
* @return void
*/
public function on_plugin_deactivation() {
wp_clear_scheduled_hook( 'nashaat_cron_purge_after_days' );
}
/**
* Install database using dbDelta on plugin activation
*
* @return void
*/
public function on_plugin_install() {
$this->create_db_table();
}
/**
* Handle plugin update
*
* @return void
*/
public function on_plugin_update() {
$installed_version = get_option( 'nashaat_db_version' );
if ( $installed_version !== $this->db_version ) {
$this->create_db_table();
}
}
/**
* Install or update table using dbDelta
*
* @return void
*/
private function create_db_table() {
$sql = "CREATE TABLE $this->table_name (
id int(11) NOT NULL AUTO_INCREMENT,
date int(11) NOT NULL DEFAULT '0',
level int(4) NOT NULL DEFAULT '0',
user_id int(11) NOT NULL DEFAULT '0',
bookmark smallint(1) NOT NULL DEFAULT 0,
user_data longtext NOT NULL,
ip varchar(100) NOT NULL,
context varchar(225) NOT NULL,
event varchar(225) NOT NULL,
log_info longtext NOT NULL,
PRIMARY KEY (id),
INDEX idx_level (level),
INDEX idx_userid (user_id),
INDEX idx_context (context),
INDEX idx_event (event)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
include_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
update_option( 'nashaat_db_version', $this->db_version );
}
}
new NashaatSetup();