-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePlugin.php
More file actions
692 lines (632 loc) · 17.2 KB
/
BasePlugin.php
File metadata and controls
692 lines (632 loc) · 17.2 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
<?php
/*
* This file is part of WBF Framework: https://github.com/wagaweb/wbf
*
* @author WAGA Team <dev@waga.it>
*/
namespace WBF\components\pluginsframework;
use WBF\components\license\License_Manager;
use WBF\components\notices\Notice_Manager;
use WBF\components\license\License;
use WBF\components\customupdater\Plugin_Update_Checker;
use WBF\components\utils\Utilities;
class BasePlugin {
/**
* A reference to an instance of this class for singleton usage.
*
* @since 1.0.0
*
* @var BasePlugin
*/
private static $instance;
/**
* The loader that's responsible for maintaining and registering all hooks that power
* the plugin.
*
* @since 1.0.0
* @access protected
* @var Loader $loader maintains and registers all hooks for the plugin.
*/
protected $loader;
/**
* The i18n instance
*
* @access protected
* @var I18n
*/
protected $i18n;
/**
* The unique identifier of this plugin.
*
* @since 1.0.0
* @access protected
* @var string $plugin_name The string used to uniquely identify this plugin.
*/
protected $plugin_name;
/**
* The plugin dir
*
* @since 1.0.0
* @access protected
*/
protected $plugin_dir;
/**
* The full path to main plugin file
*
* @since 0.10.0
* @access protected
* @var string
*/
protected $plugin_path;
/**
* The path relative to WP_PLUGIN_DIR
*
* @var string
*/
protected $plugin_relative_dir;
/**
* The path to /src/ if exists
*
* @var string
*/
protected $src_path;
/**
* The namespace to the public class (if provided)
* @var string
*/
protected $public_class_name;
/**
* The namespace to the admin class (if provided)
* @var string
*/
protected $admin_class_name;
/**
* The current version of the plugin.
*
* @since 1.0.0
* @access protected
* @var string $version The current version of the plugin.
*/
protected $version;
/**
* The instance of Plugin_Update_Checker
*
* @since 0.10.0
* @access protected
* @var object
*/
protected $update_instance;
/**
* The instance of License
* @access public
* @var \WBF\includes\License
*/
public $license = false;
/**
* The instance of Notice_Manager.
* @var Notice_Manager
*/
public $notice_manager;
/**
* @var boolean
* @since 0.14.8
*/
var $use_object_cache = false;
/**
* @var bool
*/
protected $debug_mode = false;
/**
* @var bool
*/
protected $script_debug_mode = false;
/**
* BasePlugin constructor.
*
* @param string $plugin_name
* @param string $dir
* @param string $version
*/
public function __construct( $plugin_name, $dir, $version = false ) {
$this->plugin_name = $plugin_name;
$this->plugin_dir = $dir;
$this->plugin_path = $this->plugin_dir.$this->plugin_name.".php";
//Set relative path
$pinfo = pathinfo($dir);
$this->plugin_relative_dir = "/".$pinfo['basename'];
//Set src path
if(is_dir($this->plugin_dir."/src")){
$this->src_path = $this->plugin_dir."src/";
}
//Set paths for Admin and Public
$class_name_parts = explode("\\",get_class($this));
if(!isset($this->public_class_name) && (is_file($this->get_src_dir()."public/class-public.php") || is_file($this->get_src_dir()."public/Public.php"))){
$class_name = $class_name_parts[0].'\pub\Pub';
$this->public_class_name = $class_name;
}elseif(!isset($this->public_class_name) && is_file($this->get_src_dir()."frontend/Frontend.php")){
$class_name = $class_name_parts[0].'\frontend\Frontend';
$this->public_class_name = $class_name;
}elseif(!isset($this->public_class_name) && is_file($this->get_src_dir()."Frontend.php")){ //@since 0.14.8
$class_name = $class_name_parts[0].'\Frontend';
$this->public_class_name = $class_name;
}
if(!isset($this->admin_class_name) && (is_file($this->get_src_dir()."admin/class-admin.php") || is_file($this->get_src_dir()."admin/Admin.php"))){
$class_name = $class_name_parts[0].'\admin\Admin';
$this->admin_class_name = $class_name;
}elseif(!isset($this->admin_class_name) && is_file($this->get_src_dir()."Admin.php")){ //@since 0.14.8
$class_name = $class_name_parts[0].'\Admin';
$this->admin_class_name = $class_name;
}
//Get the version
if(!$version){
$default_headers = array(
'Name' => 'Plugin Name',
'PluginURI' => 'Plugin URI',
'Version' => 'Version',
'Description' => 'Description',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path',
'Network' => 'Network',
// Site Wide Only is deprecated in favor of Network.
'_sitewide' => 'Site Wide Only',
);
$data = \get_file_data($this->get_path(),$default_headers,"plugin");
if ( isset($data['Version']) ) {
$this->version = $data['Version'];
} else {
$this->version = "1.0.0";
}
}else{
$this->version = $version;
}
//Check if debug mode must be activated
if( (defined("WP_DEBUG") && WP_DEBUG) || (defined("WBF_ENV") && WBF_ENV == "dev") ){
$this->debug_mode = true;
}
if( defined("SCRIPT_DEBUG") && SCRIPT_DEBUG ){
$this->script_debug_mode = true;
}
$GLOBALS['wbf_loaded_plugins'][$this->get_plugin_name()] = &$this;
$this->load_dependencies();
$this->set_locale();
$this->set_plugin_action_link();
}
/**
* Automatically adds actions link through filter. Called during object construction.
*/
private function set_plugin_action_link() {
$plugin_file = $this->plugin_name.'/'.$this->plugin_name;
$this->loader->add_action('plugin_action_links_'.$plugin_file.'.php', $this, 'add_plugin_action_links');
}
/**
* Adds a settings link in the plugin page after deactivation link only (i.e. not visible unless you activate the plugin)
*
* @param array $links array of action links (e.g. activation/deactivation)
*
* @hooked 'plugin_action_links_plugin_name/plugin_name.php'
*
* @return array
*/
public function add_plugin_action_links($links){
/*
* Do we want some defaults ??
*/
/*
$default_settings_link = [
[
'name' => 'name',
'link' => 'link'
]
];
$pages = apply_filters('wbf/plugin_framework/base_plugin', $default_settings_links);
*/
$pages = [];
$pages = apply_filters('wbf/plugin_framework/base_plugin/wbf_action_links', $pages);
if (isset($pages) && !empty($pages)) {
foreach ( $pages as $page ) {
$links[] = '<a href="' . esc_url( get_admin_url( null, $page['link'] ) ) . '">' . $page['name'] . '</a>';
}
}
return $links;
}
/**
* Adds settings link in the Wordpress Plugin page
*
* @param array $links
*
* $links_example = [
* [
* 'name' => 'name',
* 'link' => 'link'
* ]
* ];
*
* @return void
*/
public function add_action_links($links = []){
$plugin_file = ltrim($this->plugin_relative_dir,"/").'/'.basename($this->plugin_path);
add_action('plugin_action_links_'.$plugin_file, function($current_links) use($links){
if(isset($links) && is_array($links)){
foreach ( $links as $link ) {
$current_links[sanitize_title_with_dashes($link['name'])] = '<a href="' . esc_url( get_admin_url( null, $link['link'] ) ) . '">' . $link['name'] . '</a>';
}
}
return $current_links;
});
}
/**
* Set the update server for the plugin. You can specify also a License class.
* The License class must extends WBF\includes\License and implements WBF\includes\License_Interface.
*
* @param string|null $endpoint
* @param License|null $license
*
* @return bool|Plugin_Update_Checker
*/
public function set_update_server($endpoint = null,License $license = null){
if(isset($endpoint) && is_string($endpoint) && !empty($endpoint)){
if($license){
$this->register_license($license);
}elseif(is_file($this->plugin_dir."/includes/class-ls.php") && !isset($license)){
//Automatically register a new license
require_once $this->plugin_dir."/includes/class-ls.php";
$classname = preg_replace("/Plugin/","LS",get_class($this));
$license = $classname::getInstance($this->plugin_name);
if($license){
$license->type = "plugin";
$this->register_license($license);
}
}
$this->update_instance = new Plugin_Update_Checker(
$endpoint,
$this->plugin_dir.$this->plugin_name.".php",
$this->plugin_name,
$this->license
);
return $this->update_instance;
}else{
return false;
}
}
/**
* Just a wrapper around License_Manager::register
* @param License $license
*/
public function register_license(License $license){
$this->license = License_Manager::register_plugin_license($license);
}
/**
* Load the required dependencies for the plugin.
*
* Include the following files that make up the plugin:
*
* - Waboot_Galleries_Loader. Orchestrates the hooks of the plugin.
* - Waboot_Galleries_i18n. Defines internationalization functionality.
*
* Create an instance of the loader which will be used to register the hooks
* with WordPress.
*
* @since 1.0.0
* @access private
*/
protected function load_dependencies() {
/**
* The class responsible for orchestrating the actions and filters of the
* core plugin.
*/
//require_once plugin_dir_path( dirname( __FILE__ ) ) . 'class-waboot-plugin-loader.php';
/**
* The class responsible for defining internationalization functionality
* of the plugin.
*/
//require_once plugin_dir_path( dirname( __FILE__ ) ) . 'class-waboot-plugin-i18n.php';
//Load Notice Manager if needed
$wbf_notice_manager = Utilities::get_wbf_notice_manager();
$this->notice_manager = &$wbf_notice_manager;
$this->loader = new Loader($this);
}
/**
* Define the locale for this plugin for internationalization.
*
* Uses the Waboot_Plugin_i18n class in order to set the domain and to register the hook
* with WordPress.
*
* @since 1.0.0
* @access private
*/
private function set_locale() {
$this->i18n = new I18n();
$this->i18n->set_domain( $this->get_plugin_name() );
$this->i18n->set_language_dir( $this->plugin_relative_dir."/languages/" );
$this->loader->add_action( 'plugins_loaded', $this->i18n, 'load_plugin_textdomain' );
}
/**
* Saves the vars specified in $settings into a standard option name
*
* @param array $settings
* @param bool $override
*
* @return bool
*/
public function save_plugin_settings($settings, $override = false){
$setting_option_name = $this->get_plugin_settings_option_name();
if(!is_array($settings)) return false;
if(!$override) {
//Get the current settings
$actual = $this->get_plugin_settings();
//Merge the differences
$settings = wp_parse_args( $settings, $actual );
}
return update_option($setting_option_name,$settings);
}
/**
* Retrieve the plugin settings
*
* @return array
*/
public function get_plugin_settings(){
$setting_option_name = $this->get_plugin_settings_option_name();
$defaults = $this->get_plugin_default_settings();
$settings = get_option($setting_option_name,$defaults);
return $settings;
}
/**
* Retrieve the default settings. This method is meant to be overrided.
*
* @return array
*/
public function get_plugin_default_settings(){
return [];
}
/**
* Return the standard plugin settings option name
*
* @return string
*/
private function get_plugin_settings_option_name(){
return "wbf_".$this->plugin_name."_settings";
}
/**
* Check if cache is enabled
*
* @since 0.14.8
*
* @return bool
*/
public function cache_enabled(){
return $this->use_object_cache;
}
/**
* Get a list of plugin transient
*
* @since 0.14.8
*
* @return array|null|object
*/
function list_transients(){
global $wpdb;
$sql = "SELECT `option_name` AS `name`, `option_value` AS `value`
FROM $wpdb->options
WHERE `option_name` LIKE '%transient_".$this->get_plugin_name()."%'
ORDER BY `option_name`";
$results = $wpdb->get_results( $sql );
if(is_array($results)){
return $results;
}
return [];
}
/**
* Clear plugin object cache
*
* @since 0.14.8
*
* @param null $node_type
* @param $node_id
*/
public function clear_transients($node_type = null, $node_id = null){
global $wpdb;
$transients = $this->list_transients();
foreach($transients as $t){
if(!isset($node_type)){
$wpdb->delete($wpdb->options,[
'option_name' => $t->name
]);
}else{
if(isset($node_id)){
$transient_pattern = "\[".$node_type.":".$node_id."\]";
}else{
$transient_pattern = "\[".$node_type."[:a-zA-Z0-9]*\]";
}
if(preg_match("/".$transient_pattern."/",$t->name)){
$wpdb->delete($wpdb->options,[
'option_name' => $t->name
]);
}
}
}
}
/**
* Get a transient if cache is enabled
*
* @since 0.14.8
*
* @param $transient_name
*
* @return bool
*/
public function maybe_get_transient($transient_name){
if($this->cache_enabled()){
$transient_name = $this->get_plugin_name()."[".$transient_name."]";
return get_transient($transient_name);
}
return false;
}
/**
* Add a transient if cache is enabled.
*
* @usage
* This caching system supports a special type of transient names. Any provided transient name will be prefixed with the name of the plugin and wrapped in square brackets.
*
* So: foobar => pluginName[foobar]
*
* foobar will become a "node". You then can remove the whole node or parts of it.
*
* To create node parts, you have to specify their name after a colon: "foobar:baz".
*
* This is useful to create semantic nodes; by doing "maybe_set_transient("object:12",<value>)", you have just set the cache for the "object" with id 12. You then can remove only this cache or the whole "object".
*
* @since 0.14.8
*
* @param $transient_name
* @param $value
*/
public function maybe_set_transient($transient_name,$value){
if($this->cache_enabled()){
$transient_name = $this->get_plugin_name()."[".$transient_name."]";
set_transient($transient_name,$value);
}
}
/**
* The name of the plugin used to uniquely identify it within the context of
* WordPress and to define internationalization functionality.
*
* @since 1.0.0
* @return string The name of the plugin.
*/
public function get_plugin_name() {
return $this->plugin_name;
}
/**
* Returns an instance of this class. An implementation of the singleton design pattern.
*
* @param $plugin_name
* @param $dir
* @param $version
*
* @return BasePlugin A reference to an instance of this class.
* @since 1.0.0
*/
public static function get_instance( $plugin_name, $dir, $version ) {
if ( null == self::$instance ) {
self::$instance = new self( $plugin_name, $dir, $version );
}
return self::$instance;
}
/**
* Run the loader to execute all of the hooks with WordPress.
*
* @since 1.0.0
*/
public function run() {
$this->loader->run();
}
public function get_uri(){
return get_bloginfo("wpurl")."/wp-content/plugins/".$this->plugin_name."/";
}
public function get_dir(){
return $this->plugin_dir;
}
public function get_src_dir(){
if(isset($this->src_path)){
return $this->src_path;
}else{
return $this->get_dir();
}
}
public function get_path(){
return $this->plugin_path;
}
public function get_relative_dir(){
return $this->plugin_relative_dir;
}
public function get_public_class_name(){
return $this->public_class_name;
}
public function get_admin_class_name(){
return $this->admin_class_name;
}
public function is_debug(){
return $this->debug_mode;
}
public function is_script_debug(){
return $this->script_debug_mode;
}
/**
* The reference to the class that orchestrates the hooks with the plugin.
*
* @since 1.0.0
* @return Loader Orchestrates the hooks of the plugin.
*/
public function get_loader() {
return $this->loader;
}
/**
* Get the instance of i18n related to the plugin
*
* @return I18n
*/
public function get_locale(){
return $this->i18n;
}
/**
* Get the textdomain ralated to the plugin
*
* @return I18n|false
*/
public function get_textdomain(){
$locale = $this->get_locale();
if($locale instanceof I18n){
return $locale->get_domain();
}
return false;
}
/**
* Retrieve the version number of the plugin.
*
* @since 1.0.0
* @return string The version number of the plugin.
*/
public function get_version() {
return $this->version;
}
/**
* Get admin\core\frontend instances for $plugin
*
* @param string $plugin
*
* @return array with 'core', 'admin' and 'public' keys. Each keys is associated with respective classes.
* @throws \Exception
*/
static function get_instances_of($plugin_name){
global $wbf_loaded_plugins;
if(isset($wbf_loaded_plugins[$plugin_name])){
$plugin = $wbf_loaded_plugins[$plugin_name];
if($plugin instanceof BasePlugin || $plugin instanceof TemplatePlugin){
$loader = $plugin->get_loader();
return [
'core' => $plugin,
'public' => isset($loader->public_plugin) ? $loader->public_plugin : false,
'admin' => isset($loader->admin_plugin) ? $loader->admin_plugin : false
];
}else{
throw new \Exception("Trying to get $plugin_name instances: plugin $plugin_name has no instances");
}
}else{
return [];
}
}
/**
* Get loaded plugin instances
*
* @return array
*/
static function get_loaded_plugins(){
global $wbf_loaded_plugins;
if(isset($wbf_loaded_plugins) && is_array($wbf_loaded_plugins)){
return $wbf_loaded_plugins;
}
return [];
}
}