1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
3
|
|
|
|
4
|
|
|
//Main plugin object to define the plugin |
5
|
|
|
if ( ! class_exists( 'PLUGIN_BUILD' ) ) { |
6
|
|
|
|
7
|
|
|
final class PLUGIN_BUILD { |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
public function installation() { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* Plugin installation |
16
|
|
|
* |
17
|
|
|
if (class_exists('PLUGIN_INSTALL')) { |
18
|
|
|
|
19
|
|
|
$install = new PLUGIN_INSTALL(); |
20
|
|
|
$install->textDomin = 'textdomain'; |
21
|
|
|
$install->phpVerAllowed = ''; |
22
|
|
|
$install->pluginPageLinks = array( |
23
|
|
|
array( |
24
|
|
|
'slug' => '', |
25
|
|
|
'label' => '' |
26
|
|
|
), |
27
|
|
|
); |
28
|
|
|
$install->execute(); |
29
|
|
|
} |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
|
36
|
|
|
//Custom corn class, register it while activation |
37
|
|
|
public function cron_activation() { |
38
|
|
|
|
39
|
|
|
if ( class_exists( 'PLUGIN_CRON' ) ) { |
40
|
|
|
$cron = new PLUGIN_CRON(); |
41
|
|
|
$schedule = $cron->schedule_task( |
|
|
|
|
42
|
|
|
array( |
43
|
|
|
'timestamp' => current_time('timestamp'), |
|
|
|
|
44
|
|
|
//'schedule' can be 'hourly', 'daily', 'weekly' or anything custom as defined in PLUGIN_CRON |
45
|
|
|
'recurrence' => 'schedule', |
46
|
|
|
// Use custom_corn_hook to hook into any cron process, anywhere in the plugin. |
47
|
|
|
'hook' => 'custom_cron_hook' |
48
|
|
|
) ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
public function db_install() { |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* |
58
|
|
|
* Install database by defining your SQL |
59
|
|
|
* |
60
|
|
|
if ( class_exists( 'PLUGIN_DB' ) ) { |
61
|
|
|
$db = new PLUGIN_DB(); |
62
|
|
|
$db->table = 'plugin_db_table_name'; |
63
|
|
|
$db->sql = "ID mediumint(9) NOT NULL AUTO_INCREMENT, |
64
|
|
|
date date NOT NULL, |
65
|
|
|
UNIQUE KEY ID (ID)"; |
66
|
|
|
$db->build(); |
67
|
|
|
} |
68
|
|
|
* |
69
|
|
|
* |
70
|
|
|
* Optionally check if the DB table is installed correctly |
71
|
|
|
* |
72
|
|
|
if (get_option('_plugin_db_exist') == '0') { |
73
|
|
|
add_action( 'admin_notices', array( $this, 'db_error_msg' ) ); |
74
|
|
|
} |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* Install DB options |
81
|
|
|
* |
82
|
|
|
$options = array( |
83
|
|
|
array( 'option_name', '__value__' ), |
84
|
|
|
); |
85
|
|
|
foreach ($options as $value) { |
86
|
|
|
update_option( $value[0], $value[1] ); |
87
|
|
|
} |
88
|
|
|
* |
89
|
|
|
*/ |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
|
|
//Notice of DB |
95
|
|
|
public function db_error_msg() { ?> |
96
|
|
|
|
97
|
|
|
<div class="notice notice-error is-dismissible"> |
98
|
|
|
<p><?php _e( 'Database table Not installed correctly.', 'textdomain' ); ?></p> |
|
|
|
|
99
|
|
|
</div> |
100
|
|
|
<?php |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
|
105
|
|
|
public function db_uninstall() { |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* Important table name declarition |
110
|
|
|
* |
111
|
|
|
$tableName = 'plugin_db_table_name'; |
112
|
|
|
|
113
|
|
|
global $wpdb; |
114
|
|
|
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}$tableName" ); |
115
|
|
|
$options = array( |
116
|
|
|
'_plugin_db_exist' |
117
|
|
|
); |
118
|
|
|
foreach ($options as $value) { |
119
|
|
|
delete_option($value); |
120
|
|
|
} |
121
|
|
|
* |
122
|
|
|
*/ |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
public function do_cron_job_function() { |
127
|
|
|
|
128
|
|
|
//Do cron function |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
public function custom_cron_hook_cb() { |
133
|
|
|
|
134
|
|
|
add_action('custom_cron_hook', array( $this, 'do_cron_job_function')); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
public function cron_uninstall() { |
139
|
|
|
|
140
|
|
|
wp_clear_scheduled_hook('custom_cron_hook'); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
//Include scripts |
145
|
|
|
public function scripts() { |
146
|
|
|
|
147
|
|
|
if ( class_exists( 'PLUGIN_SCRIPT' ) ) new PLUGIN_SCRIPT(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
|
152
|
|
|
//Include settings pages |
153
|
|
|
public function settings() { |
154
|
|
|
|
155
|
|
|
if ( class_exists( 'PLUGIN_SETTINGS' ) ) new PLUGIN_SETTINGS(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
//Include widget classes |
160
|
|
|
public function widgets() { |
161
|
|
|
|
162
|
|
|
if ( class_exists( 'PLUGIN_WIDGET' ) ) new PLUGIN_WIDGET(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
|
167
|
|
|
//Include metabox classes |
168
|
|
|
public function metabox() { |
169
|
|
|
|
170
|
|
|
if ( class_exists( 'PLUGIN_METABOX' ) ) new PLUGIN_METABOX(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
|
175
|
|
|
//Include shortcode classes |
176
|
|
|
public function shortcode() { |
177
|
|
|
|
178
|
|
|
if ( class_exists( 'PLUGIN_SHORTCODE' ) ) new PLUGIN_SHORTCODE(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
|
183
|
|
|
//Add functionality files |
184
|
|
|
public function functionality() { |
185
|
|
|
|
186
|
|
|
require_once ('src/install.php'); |
187
|
|
|
require_once ('src/db.php'); |
188
|
|
|
require_once ('src/query.php'); |
189
|
|
|
require_once ('src/settings.php'); |
190
|
|
|
require_once ('src/widget.php'); |
191
|
|
|
require_once ('src/metabox.php'); |
192
|
|
|
require_once ('src/shortcode.php'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
|
197
|
|
|
//Call the dependency files |
198
|
|
|
public function helpers() { |
199
|
|
|
|
200
|
|
|
require_once ('lib/cron.php'); |
201
|
|
|
require_once ('lib/api.php'); |
202
|
|
|
require_once ('lib/table.php'); |
203
|
|
|
require_once ('lib/ajax.php'); |
204
|
|
|
require_once ('lib/upload.php'); |
205
|
|
|
require_once ('lib/script.php'); |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Available classes: |
209
|
|
|
* |
210
|
|
|
* PLUGIN_CORN, PLUGIN_API, PLUGIN_TABLE, PLUGIN_AJAX, PLUGIN_UPLOAD, PLUGIN_SCRIPT |
211
|
|
|
*/ |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
|
216
|
|
|
public function __construct() { |
217
|
|
|
|
218
|
|
|
$this->helpers(); |
219
|
|
|
$this->functionality(); |
220
|
|
|
|
221
|
|
|
register_activation_hook( PLUGIN_FILE, array( $this, 'db_install' ) ); |
|
|
|
|
222
|
|
|
register_activation_hook( PLUGIN_FILE, array($this, 'cron_activation' )); |
223
|
|
|
|
224
|
|
|
//remove the DB upon uninstallation |
225
|
|
|
register_uninstall_hook( PLUGIN_FILE, array( 'PLUGIN_BUILD', 'db_uninstall' ) ); //$this won't work here. |
|
|
|
|
226
|
|
|
register_uninstall_hook( PLUGIN_FILE, array( 'PLUGIN_BUILD', 'cron_uninstall' ) ); |
227
|
|
|
|
228
|
|
|
add_action('init', array($this, 'installation')); |
|
|
|
|
229
|
|
|
add_action('init', array($this, 'custom_cron_hook_cb')); |
230
|
|
|
|
231
|
|
|
$this->scripts(); |
232
|
|
|
|
233
|
|
|
$this->widgets(); |
234
|
|
|
$this->metabox(); |
235
|
|
|
$this->shortcode(); |
236
|
|
|
$this->settings(); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} ?> |
240
|
|
|
|