1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Main plugin object to define the plugin |
6
|
|
|
* Follow: https://codex.wordpress.org/Plugin_API for details |
7
|
|
|
* |
8
|
|
|
* @author Nirjhar Lo |
9
|
|
|
* @package wp-plugin-framework |
10
|
|
|
*/ |
11
|
|
|
if ( ! class_exists( 'PLUGIN_BUILD' ) ) { |
12
|
|
|
|
13
|
|
|
final class PLUGIN_BUILD { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var String |
17
|
|
|
*/ |
18
|
|
|
protected $version = '1.3'; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Plugin Instance. |
23
|
|
|
* |
24
|
|
|
* @var PLUGIN_BUILD the PLUGIN Instance |
25
|
|
|
*/ |
26
|
|
|
protected static $_instance; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Text domain to be used throughout the plugin |
31
|
|
|
* |
32
|
|
|
* @var String |
33
|
|
|
*/ |
34
|
|
|
protected static $text_domain = 'textdomain'; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Minimum PHP version allowed for the plugin |
39
|
|
|
* |
40
|
|
|
* @var String |
41
|
|
|
*/ |
42
|
|
|
protected static $php_ver_allowed = '5.3'; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* DB tabble used in plugin |
47
|
|
|
* |
48
|
|
|
* @var String |
49
|
|
|
*/ |
50
|
|
|
protected static $plugin_table = 'plugin_db_table_name'; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Plugin listing page links, along with Deactivate |
55
|
|
|
* |
56
|
|
|
* @var Array |
57
|
|
|
*/ |
58
|
|
|
protected static $plugin_page_links = array( |
59
|
|
|
array( |
60
|
|
|
'slug' => '', |
61
|
|
|
'label' => '' |
62
|
|
|
) ); |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Main Plugin Instance. |
67
|
|
|
* |
68
|
|
|
* @return PLUGIN_BUILD |
69
|
|
|
*/ |
70
|
|
|
public static function instance() { |
71
|
|
|
|
72
|
|
|
if ( is_null( self::$_instance ) ) { |
73
|
|
|
self::$_instance = new self(); |
74
|
|
|
self::$_instance->init(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return self::$_instance; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Install plugin setup |
83
|
|
|
* |
84
|
|
|
* @return Void |
85
|
|
|
*/ |
86
|
|
|
public function installation() { |
87
|
|
|
|
88
|
|
|
if (class_exists('PLUGIN_INSTALL')) { |
89
|
|
|
|
90
|
|
|
$install = new PLUGIN_INSTALL(); |
91
|
|
|
$install->text_domain = self::$text_domain; |
92
|
|
|
$install->php_ver_allowed = self::$php_ver_allowed; |
93
|
|
|
$install->plugin_page_links = self::$plugin_page_links; |
94
|
|
|
$install->execute(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
//If CPT exists, include taht and flush the rewrite rules |
98
|
|
|
if ( class_exists( 'PLUGIN_CPT' ) ) new PLUGIN_CPT(); |
99
|
|
|
flush_rewrite_rules(); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Custom corn class, register it while activation |
105
|
|
|
* |
106
|
|
|
* @return Void |
107
|
|
|
*/ |
108
|
|
|
public function cron_activation() { |
109
|
|
|
|
110
|
|
|
if ( class_exists( 'PLUGIN_CRON' ) ) { |
111
|
|
|
|
112
|
|
|
$cron = new PLUGIN_CRON(); |
113
|
|
|
$schedule = $cron->schedule_task( |
|
|
|
|
114
|
|
|
array( |
115
|
|
|
'timestamp' => current_time('timestamp'), |
|
|
|
|
116
|
|
|
//'schedule' can be 'hourly', 'daily', 'weekly' or anything custom as defined in PLUGIN_CRON |
117
|
|
|
'recurrence' => 'schedule', |
118
|
|
|
// Use custom_corn_hook to hook into any cron process, anywhere in the plugin. |
119
|
|
|
'hook' => 'custom_cron_hook' |
120
|
|
|
) ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Install plugin data |
128
|
|
|
* |
129
|
|
|
* @return Void |
130
|
|
|
*/ |
131
|
|
|
public function db_install() { |
132
|
|
|
|
133
|
|
|
if ( class_exists( 'PLUGIN_DB' ) ) { |
134
|
|
|
|
135
|
|
|
$db = new PLUGIN_DB(); |
136
|
|
|
$db->table = self::$plugin_table; |
137
|
|
|
$db->sql = "ID mediumint(9) NOT NULL AUTO_INCREMENT, |
138
|
|
|
date date NOT NULL, |
139
|
|
|
UNIQUE KEY ID (ID)"; |
140
|
|
|
$db->build(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (get_option( '_plugin_db_exist') == '0' ) { |
|
|
|
|
144
|
|
|
add_action( 'admin_notices', array( $this, 'db_error_msg' ) ); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$options = array( |
148
|
|
|
array( 'option_name', '__value__' ), |
149
|
|
|
); |
150
|
|
|
foreach ( $options as $value ) { |
151
|
|
|
update_option( $value[0], $value[1] ); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Notice of DB |
158
|
|
|
* |
159
|
|
|
* @return Html |
|
|
|
|
160
|
|
|
*/ |
161
|
|
|
public function db_error_msg() { ?> |
162
|
|
|
|
163
|
|
|
<div class="notice notice-error is-dismissible"> |
164
|
|
|
<p><?php _e( 'Database table Not installed correctly.', 'textdomain' ); ?></p> |
|
|
|
|
165
|
|
|
</div> |
166
|
|
|
<?php |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Uninstall plugin data |
172
|
|
|
* |
173
|
|
|
* @return Void |
174
|
|
|
*/ |
175
|
|
|
public function db_uninstall() { |
176
|
|
|
|
177
|
|
|
$table_name = self::$plugin_table; |
178
|
|
|
|
179
|
|
|
global $wpdb; |
180
|
|
|
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}$table_name" ); |
181
|
|
|
|
182
|
|
|
$options = array( |
183
|
|
|
'_plugin_db_exist' |
184
|
|
|
); |
185
|
|
|
foreach ( $options as $value ) { |
186
|
|
|
delete_option( $value ); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* CRON callback |
193
|
|
|
* |
194
|
|
|
* @return Void |
195
|
|
|
*/ |
196
|
|
|
public function do_cron_job_function() { |
197
|
|
|
|
198
|
|
|
//Do cron function |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Run CRON action |
204
|
|
|
* |
205
|
|
|
* @return Void |
206
|
|
|
*/ |
207
|
|
|
public function custom_cron_hook_cb() { |
208
|
|
|
|
209
|
|
|
add_action( 'custom_cron_hook', array( $this, 'do_cron_job_function' ) ); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Uninstall CRON hook |
215
|
|
|
* |
216
|
|
|
* @return Void |
217
|
|
|
*/ |
218
|
|
|
public function cron_uninstall() { |
219
|
|
|
|
220
|
|
|
wp_clear_scheduled_hook( 'custom_cron_hook' ); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Install Custom post types |
226
|
|
|
* |
227
|
|
|
* @return Void |
228
|
|
|
*/ |
229
|
|
|
public function cpt() { |
230
|
|
|
|
231
|
|
|
if ( class_exists( 'PLUGIN_CPT' ) ) new PLUGIN_CPT(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Include scripts |
237
|
|
|
* |
238
|
|
|
* @return Void |
239
|
|
|
*/ |
240
|
|
|
public function scripts() { |
241
|
|
|
|
242
|
|
|
if ( class_exists( 'PLUGIN_SCRIPT' ) ) new PLUGIN_SCRIPT(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Include settings pages |
248
|
|
|
* |
249
|
|
|
* @return Void |
250
|
|
|
*/ |
251
|
|
|
public function settings() { |
252
|
|
|
|
253
|
|
|
if ( class_exists( 'PLUGIN_SETTINGS' ) ) new PLUGIN_SETTINGS(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Include widget classes |
259
|
|
|
* |
260
|
|
|
* @return Void |
261
|
|
|
*/ |
262
|
|
|
public function widgets() { |
263
|
|
|
|
264
|
|
|
if ( class_exists( 'PLUGIN_WIDGET' ) ) new PLUGIN_WIDGET(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
*Include metabox classes |
270
|
|
|
* |
271
|
|
|
* @return Void |
272
|
|
|
*/ |
273
|
|
|
public function metabox() { |
274
|
|
|
|
275
|
|
|
if ( class_exists( 'PLUGIN_METABOX' ) ) new PLUGIN_METABOX(); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Include shortcode classes |
281
|
|
|
* |
282
|
|
|
* @return Void |
283
|
|
|
*/ |
284
|
|
|
public function shortcode() { |
285
|
|
|
|
286
|
|
|
if ( class_exists( 'PLUGIN_SHORTCODE' ) ) new PLUGIN_SHORTCODE(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Instantiate REST API |
292
|
|
|
* |
293
|
|
|
* @return Void |
294
|
|
|
*/ |
295
|
|
|
public function rest_api() { |
296
|
|
|
|
297
|
|
|
if ( class_exists( 'PLUGIN_CUSTOM_ROUTE' ) ) new PLUGIN_CUSTOM_ROUTE(); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Instantiate REST API |
303
|
|
|
* |
304
|
|
|
* @return Void |
305
|
|
|
*/ |
306
|
|
|
public function prevent_unauthorized_rest_access( $result ) { |
307
|
|
|
// If a previous authentication check was applied, |
308
|
|
|
// pass that result along without modification. |
309
|
|
|
if ( true === $result || is_wp_error( $result ) ) { |
|
|
|
|
310
|
|
|
return $result; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
// No authentication has been performed yet. |
314
|
|
|
// Return an error if user is not logged in. |
315
|
|
|
if ( ! is_user_logged_in() ) { |
|
|
|
|
316
|
|
|
return new WP_Error( |
|
|
|
|
317
|
|
|
'rest_not_logged_in', |
318
|
|
|
__( 'You are not currently logged in.' ), |
|
|
|
|
319
|
|
|
array( 'status' => 401 ) |
320
|
|
|
); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return $result; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Add the functionality files |
329
|
|
|
* Available classes: PLUGIN_INSTALL, PLUGIN_DB, PLUGIN_METABOX, PLUGIN_QUERY, PLUGIN_SETTINGS, PLUGIN_SHORTCODE, PLUGIN_WIDGET |
330
|
|
|
* |
331
|
|
|
* @return Void |
332
|
|
|
*/ |
333
|
|
|
public function functionality() { |
334
|
|
|
|
335
|
|
|
require_once( 'src/class-install.php' ); |
336
|
|
|
require_once( 'src/class-db.php' ); |
337
|
|
|
require_once( 'src/class-query.php' ); |
338
|
|
|
require_once( 'src/class-settings.php' ); |
339
|
|
|
require_once( 'src/class-widget.php' ); |
340
|
|
|
require_once( 'src/class-metabox.php' ); |
341
|
|
|
require_once( 'src/class-shortcode.php' ); |
342
|
|
|
require_once( 'src/class-cpt.php' ); |
343
|
|
|
require_once( 'src/class-rest.php' ); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Call the dependency files |
349
|
|
|
* Available classes: PLUGIN_CORN, PLUGIN_API, PLUGIN_TABLE, PLUGIN_AJAX, PLUGIN_UPLOAD, PLUGIN_SCRIPT |
350
|
|
|
* |
351
|
|
|
* @return Void |
352
|
|
|
*/ |
353
|
|
|
public function helpers() { |
354
|
|
|
|
355
|
|
|
require_once( 'lib/class-cron.php' ); |
356
|
|
|
require_once( 'lib/class-api.php' ); |
357
|
|
|
require_once( 'lib/class-table.php' ); |
358
|
|
|
require_once( 'lib/class-ajax.php' ); |
359
|
|
|
require_once( 'lib/class-upload.php' ); |
360
|
|
|
require_once( 'lib/class-script.php' ); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Instantiate the plugin |
366
|
|
|
* |
367
|
|
|
* @return Void |
368
|
|
|
*/ |
369
|
|
|
public function init() { |
370
|
|
|
|
371
|
|
|
$this->helpers(); |
372
|
|
|
$this->functionality(); |
373
|
|
|
|
374
|
|
|
register_activation_hook( PLUGIN_FILE, array( $this, 'db_install' ) ); |
|
|
|
|
375
|
|
|
register_activation_hook( PLUGIN_FILE, array( $this, 'cron_activation' ) ); |
376
|
|
|
|
377
|
|
|
//remove the DB and CORN upon uninstallation |
378
|
|
|
//using $this won't work here. |
379
|
|
|
register_uninstall_hook( PLUGIN_FILE, array( 'PLUGIN_BUILD', 'db_uninstall' ) ); |
|
|
|
|
380
|
|
|
register_uninstall_hook( PLUGIN_FILE, array( 'PLUGIN_BUILD', 'cron_uninstall' ) ); |
381
|
|
|
|
382
|
|
|
add_filter( 'rest_authentication_errors', array( $this, 'prevent_unauthorized_rest_access' ) ); |
|
|
|
|
383
|
|
|
|
384
|
|
|
add_action( 'init', array( $this, 'installation' ) ); |
|
|
|
|
385
|
|
|
add_action( 'init', array( $this, 'custom_cron_hook_cb' ) ); |
386
|
|
|
add_action( 'init', array( $this, 'cpt' ) ); |
387
|
|
|
|
388
|
|
|
$this->scripts(); |
389
|
|
|
$this->widgets(); |
390
|
|
|
$this->metabox(); |
391
|
|
|
$this->shortcode(); |
392
|
|
|
$this->settings(); |
393
|
|
|
|
394
|
|
|
//Alternative method: add_action( 'rest_api_init', array($this, 'rest_api') ); |
395
|
|
|
$this->rest_api(); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
} ?> |
399
|
|
|
|