-
Notifications
You must be signed in to change notification settings - Fork 0
/
MuPlugin.php
79 lines (72 loc) · 1.9 KB
/
MuPlugin.php
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
<?php
namespace Charm;
use Charm\Helper\File;
/**
* Class MuPlugin
*
* @author Ryan Sechrest
* @package Charm
*/
class MuPlugin
{
/**
* Autoload and initialize mu-plugin
*
* Assuming $name = 'ProjectName', this method will attempt to autoload all
* mu-plugin files within a 'projectname' directory, and then attempt to call the
* init() method within a class of the same name and namespace, e.g.
* 'ProjectName\ProjectName::init()'
*/
public static function run(string $name): void
{
$mu_plugin = strtolower($name);
static::autoload($mu_plugin);
static::init($name);
}
/**
* Autoload all mu-plugin files
*
* @param string $mu_plugin
*/
public static function autoload(string $mu_plugin = ''): void
{
if ($mu_plugin !== '') {
$file_paths = MuPlugin::get_file_paths($mu_plugin);
File::require_once($file_paths);
return;
}
$traces = debug_backtrace();
if (!isset($traces[0]['file'])) {
return;
}
$path = str_replace('.php', '', $traces[0]['file']);
$file_paths = File::get_file_paths($path);
File::require_once($file_paths);
}
/**
* Get path of all files within mu-plugin path
*
* @param string $mu_plugin
* @return array
*/
public static function get_file_paths(string $mu_plugin): array
{
return File::get_file_paths(WPMU_PLUGIN_DIR . '/' . $mu_plugin);
}
/**
* Initialize plugin after WordPress loads
*
* @param string $class_name
*/
public static function init(string $class_name)
{
$class = $class_name . '\\' . $class_name;
if (!class_exists($class)) {
return;
}
if (!method_exists($class, 'init')) {
return;
}
call_user_func([$class, 'init']);
}
}