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