-
Notifications
You must be signed in to change notification settings - Fork 0
/
bedrock-autoloader.php
243 lines (206 loc) · 6.06 KB
/
bedrock-autoloader.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
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
<?php
/**
* Plugin Name: Bedrock Autoloader
* Plugin URI: https://github.com/masonitedoors/bedrock-autoloader/
* Description: An autoloader that enables standard plugins to be required just like must-use plugins. The autoloaded plugins are included during mu-plugin loading. An asterisk (*) next to the name of the plugin designates the plugins that have been autoloaded.
* Version: 1.0.0
* Author: Roots
* Author URI: https://roots.io/
* License: MIT License
*/
namespace Roots\Bedrock;
if (!is_blog_installed()) {
return;
}
/**
* Class Autoloader
* @package Roots\Bedrock
* @author Roots
* @link https://roots.io/
*/
class Autoloader
{
/**
* Singleton instance.
*
* @var static
*/
private static $instance;
/**
* Store Autoloader cache and site option.
*
* @var array
*/
private $cache;
/**
* Autoloaded plugins.
*
* @var array
*/
private $autoPlugins;
/**
* Autoloaded mu-plugins.
*
* @var array
*/
private $muPlugins;
/**
* Number of plugins.
*
* @var int
*/
private $count;
/**
* Newly activated plugins.
*
* @var array
*/
private $activated;
/**
* Relative path to the mu-plugins directory.
*
* @var string
*/
private $relativePath;
/**
* Create an instance of Autoloader.
*
* @return void
*/
public function __construct()
{
if (isset(self::$instance)) {
return;
}
self::$instance = $this;
$this->relativePath = '/../' . basename(__DIR__);
if (is_admin()) {
add_filter('show_advanced_plugins', [$this, 'showInAdmin'], 0, 2);
}
$this->loadPlugins();
}
/**
* Run some checks then autoload our plugins.
*
* @return void
*/
public function loadPlugins()
{
$this->checkCache();
$this->validatePlugins();
$this->countPlugins();
array_map(static function () {
include_once WPMU_PLUGIN_DIR . '/' . func_get_args()[0];
}, array_keys($this->cache['plugins']));
$this->pluginHooks();
}
/**
* Filter show_advanced_plugins to display the autoloaded plugins.
*
* @param bool $show Whether to show the advanced plugins for the specified plugin type.
* @param string $type The plugin type, i.e., `mustuse` or `dropins`
* @return bool We return `false` to prevent WordPress from overriding our work
*/
public function showInAdmin($show, $type)
{
$screen = get_current_screen();
$current = is_multisite() ? 'plugins-network' : 'plugins';
if ($screen->base !== $current || $type !== 'mustuse' || !current_user_can('activate_plugins')) {
return $show;
}
$this->updateCache();
$this->autoPlugins = array_map(function ($auto_plugin) {
$auto_plugin['Name'] .= ' *';
return $auto_plugin;
}, $this->autoPlugins);
$GLOBALS['plugins']['mustuse'] = array_unique(array_merge($this->autoPlugins, $this->muPlugins), SORT_REGULAR);
return false;
}
/**
* This sets the cache or calls for an update
*
* @return void
*/
private function checkCache()
{
$cache = get_site_option('bedrock_autoloader');
if ($cache === false || (isset($cache['plugins'], $cache['count']) && count($cache['plugins']) !== $cache['count'])) {
$this->updateCache();
return;
}
$this->cache = $cache;
}
/**
* Update mu-plugin cache.
*
* Get the plugins and mu-plugins from the mu-plugin path and remove duplicates.
* Check cache against current plugins for newly activated plugins.
* After that, we can update the cache.
*
* @return void
*/
private function updateCache()
{
require_once ABSPATH . 'wp-admin/includes/plugin.php';
$this->autoPlugins = get_plugins($this->relativePath);
$this->muPlugins = get_mu_plugins();
$plugins = array_diff_key($this->autoPlugins, $this->muPlugins);
$rebuild = !is_array($this->cache['plugins']);
$this->activated = $rebuild ? $plugins : array_diff_key($plugins, $this->cache['plugins']);
$this->cache = ['plugins' => $plugins, 'count' => $this->countPlugins()];
update_site_option('bedrock_autoloader', $this->cache);
}
/**
* Activate plugin hooks.
*
* This accounts for the plugin hooks that would run if the plugins were
* loaded as usual. Plugins are removed by deletion, so there's no way
* to deactivate or uninstall.
*
* @return void
*/
private function pluginHooks()
{
if (!is_array($this->activated)) {
return;
}
foreach ($this->activated as $plugin_file => $plugin_info) {
do_action('activate_' . $plugin_file);
}
}
/**
* Check that the plugin file exists, if it doesn't update the cache.
*
* @return void
*/
private function validatePlugins()
{
foreach ($this->cache['plugins'] as $plugin_file => $plugin_info) {
if (!file_exists(WPMU_PLUGIN_DIR . '/' . $plugin_file)) {
$this->updateCache();
break;
}
}
}
/**
* Count the number of autoloaded plugins.
*
* Count our plugins (but only once) by counting the top level folders in the
* mu-plugins dir. If it's more or less than last time, update the cache.
*
* @return int Number of autoloaded plugins.
*/
private function countPlugins()
{
if (isset($this->count)) {
return $this->count;
}
$count = count(glob(WPMU_PLUGIN_DIR . '/*/', GLOB_ONLYDIR | GLOB_NOSORT));
if (!isset($this->cache['count']) || $count !== $this->cache['count']) {
$this->count = $count;
$this->updateCache();
}
return $this->count;
}
}
new Autoloader();