1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plugin Name: Bedrock Autoloader |
4
|
|
|
* Plugin URI: https://github.com/roots/bedrock/ |
5
|
|
|
* Description: An autoloader that enables standard plugins to be required just like must-use plugins. The autoloaded |
6
|
|
|
* plugins are included during mu-plugin loading. An asterisk (*) next to the name of the plugin designates the plugins |
7
|
|
|
* that have been autoloaded. Version: 1.0.0 Author: Roots Author URI: https://roots.io/ License: MIT License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Roots\Bedrock; |
11
|
|
|
|
12
|
|
|
if (!is_blog_installed()) { |
13
|
|
|
return; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Autoloader |
18
|
|
|
* @package Roots\Bedrock |
19
|
|
|
* @author Roots |
20
|
|
|
* @link https://roots.io/ |
21
|
|
|
*/ |
22
|
|
|
class Autoloader |
23
|
|
|
{ |
24
|
|
|
/** @var array Store Autoloader cache and site option */ |
25
|
|
|
private static $cache; |
26
|
|
|
|
27
|
|
|
/** @var array Autoloaded plugins */ |
28
|
|
|
private static $auto_plugins; |
29
|
|
|
|
30
|
|
|
/** @var array Autoloaded mu-plugins */ |
31
|
|
|
private static $mu_plugins; |
32
|
|
|
|
33
|
|
|
/** @var int Number of plugins */ |
34
|
|
|
private static $count; |
35
|
|
|
|
36
|
|
|
/** @var array Newly activated plugins */ |
37
|
|
|
private static $activated; |
38
|
|
|
|
39
|
|
|
/** @var string Relative path to the mu-plugins dir */ |
40
|
|
|
private static $relative_path; |
41
|
|
|
|
42
|
|
|
/** @var static Singleton instance */ |
43
|
|
|
private static $_single; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create singleton, populate vars, and set WordPress hooks |
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
if (isset(self::$_single)) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
self::$_single = $this; |
55
|
|
|
self::$relative_path = '/../' . basename(__DIR__); |
56
|
|
|
|
57
|
|
|
if (is_admin()) { |
58
|
|
|
add_filter('show_advanced_plugins', [$this, 'showInAdmin'], 0, 2); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->loadPlugins(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Run some checks then autoload our plugins. |
66
|
|
|
*/ |
67
|
|
|
public function loadPlugins() |
68
|
|
|
{ |
69
|
|
|
$this->checkCache(); |
70
|
|
|
$this->validatePlugins(); |
71
|
|
|
$this->countPlugins(); |
72
|
|
|
|
73
|
|
|
array_map(static function () { |
74
|
|
|
include_once(WPMU_PLUGIN_DIR . '/' . func_get_args()[0]); |
75
|
|
|
}, array_keys(self::$cache['plugins'])); |
76
|
|
|
|
77
|
|
|
$this->pluginHooks(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Filter show_advanced_plugins to display the autoloaded plugins. |
82
|
|
|
* @param $bool bool Whether to show the advanced plugins for the specified plugin type. |
83
|
|
|
* @param $type string The plugin type, i.e., `mustuse` or `dropins` |
84
|
|
|
* @return bool We return `false` to prevent WordPress from overriding our work |
85
|
|
|
* {@internal We add the plugin details ourselves, so we return false to disable the filter.} |
86
|
|
|
*/ |
87
|
|
|
public function showInAdmin($show, $type) |
88
|
|
|
{ |
89
|
|
|
$screen = get_current_screen(); |
90
|
|
|
$current = is_multisite() ? 'plugins-network' : 'plugins'; |
91
|
|
|
|
92
|
|
|
if ($screen->{'base'} != $current || $type != 'mustuse' || !current_user_can('activate_plugins')) { |
93
|
|
|
return $show; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->updateCache(); |
97
|
|
|
|
98
|
|
|
self::$auto_plugins = array_map(function ($auto_plugin) { |
99
|
|
|
$auto_plugin['Name'] .= ' *'; |
100
|
|
|
|
101
|
|
|
return $auto_plugin; |
102
|
|
|
}, self::$auto_plugins); |
103
|
|
|
|
104
|
|
|
$GLOBALS['plugins']['mustuse'] = array_unique(array_merge(self::$auto_plugins, self::$mu_plugins), SORT_REGULAR); |
105
|
|
|
|
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* This sets the cache or calls for an update |
111
|
|
|
*/ |
112
|
|
|
private function checkCache() |
113
|
|
|
{ |
114
|
|
|
$cache = get_site_option('bedrock_autoloader'); |
115
|
|
|
|
116
|
|
|
if ($cache === false) { |
117
|
|
|
$this->updateCache(); |
118
|
|
|
|
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
self::$cache = $cache; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the plugins and mu-plugins from the mu-plugin path and remove duplicates. |
127
|
|
|
* Check cache against current plugins for newly activated plugins. |
128
|
|
|
* After that, we can update the cache. |
129
|
|
|
*/ |
130
|
|
|
private function updateCache() |
131
|
|
|
{ |
132
|
|
|
require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
133
|
|
|
|
134
|
|
|
self::$auto_plugins = get_plugins(self::$relative_path); |
135
|
|
|
self::$mu_plugins = get_mu_plugins(); |
136
|
|
|
$plugins = array_diff_key(self::$auto_plugins, self::$mu_plugins); |
137
|
|
|
$rebuild = !is_array(self::$cache['plugins']); |
138
|
|
|
self::$activated = ($rebuild) ? $plugins : array_diff_key($plugins, self::$cache['plugins']); |
139
|
|
|
self::$cache = array('plugins' => $plugins, 'count' => $this->countPlugins()); |
140
|
|
|
|
141
|
|
|
update_site_option('bedrock_autoloader', self::$cache); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* This accounts for the plugin hooks that would run if the plugins were |
146
|
|
|
* loaded as usual. Plugins are removed by deletion, so there's no way |
147
|
|
|
* to deactivate or uninstall. |
148
|
|
|
*/ |
149
|
|
|
private function pluginHooks() |
150
|
|
|
{ |
151
|
|
|
if (!is_array(self::$activated)) { |
152
|
|
|
return; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
foreach (self::$activated as $plugin_file => $plugin_info) { |
156
|
|
|
do_action('activate_' . $plugin_file); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Check that the plugin file exists, if it doesn't update the cache. |
162
|
|
|
*/ |
163
|
|
|
private function validatePlugins() |
164
|
|
|
{ |
165
|
|
|
foreach (self::$cache['plugins'] as $plugin_file => $plugin_info) { |
166
|
|
|
if (!file_exists(WPMU_PLUGIN_DIR . '/' . $plugin_file)) { |
167
|
|
|
$this->updateCache(); |
168
|
|
|
break; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Count the number of autoloaded plugins. |
175
|
|
|
* |
176
|
|
|
* Count our plugins (but only once) by counting the top level folders in the |
177
|
|
|
* mu-plugins dir. If it's more or less than last time, update the cache. |
178
|
|
|
* |
179
|
|
|
* @return int Number of autoloaded plugins. |
180
|
|
|
*/ |
181
|
|
|
private function countPlugins() |
182
|
|
|
{ |
183
|
|
|
if (isset(self::$count)) { |
184
|
|
|
return self::$count; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$count = count(glob(WPMU_PLUGIN_DIR . '/*/', GLOB_ONLYDIR | GLOB_NOSORT)); |
188
|
|
|
|
189
|
|
|
if (!isset(self::$cache['count']) || $count != self::$cache['count']) { |
190
|
|
|
self::$count = $count; |
191
|
|
|
$this->updateCache(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return self::$count; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
new Autoloader(); |