-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-plugin.php
More file actions
executable file
·404 lines (341 loc) · 7.73 KB
/
class-plugin.php
File metadata and controls
executable file
·404 lines (341 loc) · 7.73 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
<?php
/**
* Class Plugin
*
* @package wpct-plugin
*/
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
// phpcs:disable WordPress.WP.I18n.TextDomainMismatch
namespace WPCT_PLUGIN;
use Exception;
use ReflectionClass;
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
require_once 'class-singleton.php';
require_once 'class-menu.php';
require_once 'class-settings-form.php';
require_once 'class-settings-store.php';
/**
* Plugin abstract class.
*/
class Plugin extends Singleton {
/**
* Handles plugin's menu class name.
*
* @var string
*/
const MENU = '\WPCT_PLUGIN\Menu';
/**
* Handles plugin's settings store class name.
*
* @var string
*/
const STORE = '\WPCT_PLUGIN\Settings_Store';
/**
* Handles plugin's settings class name.
*
* @var string
*/
const SETTINGS_FORM = '\WPCT_PLUGIN\Settings_Form';
/**
* Handles plugin's headers data.
*
* @var string
*/
private static $data;
/**
* Handles plugin's settings store instance.
*
* @var Settings_Store
*/
private $store;
/**
* Handles plugin's menu instance.
*
* @var Menu
*/
private $menu;
/**
* Handles plugin's settings ui instance.
*
* @var Settings_Form
*/
private $settings_form;
/**
* Plugin initializer.
*/
protected static function init() {
}
/**
* Plugin activation callback.
*/
public static function activate() {
}
/**
* Plugin deactivation callback.
*/
public static function deactivate() {
}
/**
* Public plugin's initializer.
*
* @param mixed[] ...$args Plugin constructor array of arguments.
*
* @return Plugin
*/
final public static function setup( ...$args ) {
return static::get_instance( ...$args );
}
/**
* Checks if some plugin is active, also in the network.
*
* @param string $plugin_name index file of the plugin.
*
* @return bool activation state of the plugin
*/
final public static function is_plugin_active( $plugin_name ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
return is_plugin_active( $plugin_name );
}
/**
* Plugin constructor. Bind plugin to wp init hook and load textdomain.
*
* @param mixed[] ...$args Constructor array of arguments.
*/
protected function construct( ...$args ) {
$store_class = static::STORE;
$this->store = $store_class::get_instance( static::slug() );
if ( static::is_active() ) {
$menu_class = static::MENU;
$this->menu = $menu_class::get_instance(
static::name(),
static::slug(),
$this->store
);
$form_class = static::SETTINGS_FORM;
$this->settings_form = $form_class::get_instance( $this->store );
}
add_action(
'init',
function () {
static::load_textdomain();
static::init();
}
);
add_filter(
'plugin_action_links',
static function ( $links, $file ) {
if ( ! static::is_active() ) {
return $links;
}
if ( static::index() !== $file ) {
return $links;
}
$url = admin_url(
'options-general.php?page=' . static::slug()
);
$label = __( 'Settings', 'wpct-plugin' );
$link = sprintf(
'<a href="%s">%s</a>',
esc_url( $url ),
esc_html( $label )
);
array_push( $links, $link );
return $links;
},
10,
2
);
register_activation_hook(
static::index(),
function () {
static::activate();
}
);
register_deactivation_hook(
static::index(),
function () {
static::deactivate();
}
);
}
/**
* Plugin name getter.
*
* @return string $name plugin name
*/
final public static function name() {
return static::data()['Name'];
}
/**
* Plugin slug getter.
*
* @return string $slug plugin's textdomain alias
*/
final public static function slug() {
return pathinfo( static::index() )['filename'];
}
/**
* Plugin index getter.
*
* @return string plugin's index file
*/
final public static function index() {
$path = static::path();
$name = basename( $path );
return plugin_basename( $path . '/' . $name . '.php' );
}
/**
* Plugin's path getter.
*
* @return string
*
* @throws Exception In case not plugin's main file found.
*/
final public static function path() {
$reflection = new ReflectionClass( static::class );
$filepath = wp_normalize_path( $reflection->getFileName() );
$is_installed = str_starts_with( $filepath, WP_PLUGIN_DIR );
$is_mu_installed = str_starts_with( $filepath, WPMU_PLUGIN_DIR );
if ( $is_installed ) {
$relative_path = plugin_basename( $filepath );
return WP_PLUGIN_DIR . '/' . explode( '/', $relative_path )[0];
} elseif ( $is_mu_installed ) {
$relative_path = plugin_basename( $filepath );
return WPMU_PLUGIN_DIR . '/' . explode( '/', $relative_path )[0];
} else {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
$dirname = dirname( $filepath );
$name = get_plugin_data( $filepath )['Name'] ?? null;
if ( $name ) {
return $dirname;
}
while ( ! $name ) {
foreach ( array_diff( scandir( $dirname ), array( '.', '..' ) ) as $filename ) {
$filepath = $dirname . '/' . $filename;
if ( is_dir( $filepath ) || ! is_readable( $filepath ) ) {
continue;
}
$name = get_plugin_data( $filepath )['Name'] ?? null;
if ( $name ) {
return $dirname;
}
}
if ( dirname( $dirname ) === $dirname ) {
throw new Exception( 'Not a plugin' );
}
$dirname = dirname( $dirname );
}
}
}
/**
* Plugin's public url getter.
*
* @return string
*/
final public static function url() {
return plugin_dir_url( static::index() );
}
/**
* Plugin textdomain getter.
*
* @return string plugin's textdomain
*/
final public static function textdomain() {
return static::data()['TextDomain'];
}
/**
* Plugin version getter.
*
* @return string plugin's version
*/
final public static function version() {
return static::data()['Version'];
}
/**
* Plugin dependencies getter.
*
* @return array plugin's dependencies
*/
final public static function dependencies() {
$dependencies = static::data()['RequiresPlugins'];
if ( empty( $dependencies ) ) {
return array();
}
return array_map(
function ( $plugin ) {
$plugin = trim( $plugin );
return $plugin . '/' . $plugin . '.php';
},
explode( ',', $dependencies )
);
}
/**
* Plugin data getter.
*
* @return array $data plugin data
*/
private static function data() {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
$plugin_dir = static::path() . '/' . basename( static::index() );
return get_plugin_data( $plugin_dir, false, false );
}
/**
* Active state getter.
*
* @return bool $is_active plugin active state
*/
final public static function is_active() {
return static::is_plugin_active( static::index() );
}
/**
* Load plugin textdomain.
*/
private static function load_textdomain() {
$data = static::data();
$domain_path =
isset( $data['DomainPath'] ) && ! empty( $data['DomainPath'] )
? $data['DomainPath']
: '/languages';
load_plugin_textdomain(
static::textdomain(),
false,
dirname( static::index() ) . $domain_path
);
}
/**
* Public getter of the plugin's menu instance.
*
* @return Menu|null
*/
final public static function menu() {
return static::get_instance()->menu;
}
/**
* Public getter of the plugin's store instance.
*
* @return Settings_Store|null
*/
final public static function store() {
$store = static::get_instance()->store;
if ( empty( $store ) ) {
return;
}
return $store;
}
/**
* Public getter of the plugin's store settings.
*
* @param string $name Setting name.
*
* @return Setting|null
*/
final public static function setting( $name ) {
$store = static::get_instance()->store;
if ( empty( $store ) ) {
return;
}
return $store::setting( $name );
}
}