├── composer.json ├── LICENSE.md └── src └── Autoloader.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "roots/bedrock-autoloader", 3 | "license": "MIT", 4 | "description": "An autoloader that enables standard plugins to be required just like must-use plugins", 5 | "authors": [ 6 | { 7 | "name": "Nick Fox", 8 | "email": "nick@foxaii.com", 9 | "homepage": "https://github.com/foxaii" 10 | }, 11 | { 12 | "name": "Scott Walkinshaw", 13 | "email": "scott.walkinshaw@gmail.com", 14 | "homepage": "https://github.com/swalkinshaw" 15 | }, 16 | { 17 | "name": "Austin Pray", 18 | "email": "austin@austinpray.com", 19 | "homepage": "https://github.com/austinpray" 20 | } 21 | ], 22 | "keywords": [ 23 | "autoloader", "bedrock", "mu-plugin", "must-use", "plugin", "wordpress" 24 | ], 25 | "support": { 26 | "forum": "https://discourse.roots.io/" 27 | }, 28 | "require": { 29 | "php": ">=7.1" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "Roots\\Bedrock\\": "src/" 34 | } 35 | }, 36 | "require-dev": { 37 | "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0", 38 | "10up/wp_mock": "^0.4.2" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Roots Software LLC 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Autoloader.php: -------------------------------------------------------------------------------- 1 | relativePath = '/../' . basename(WPMU_PLUGIN_DIR); 46 | 47 | if (is_admin()) { 48 | add_filter('show_advanced_plugins', [$this, 'showInAdmin'], 0, 2); 49 | } 50 | 51 | $this->loadPlugins(); 52 | } 53 | 54 | /** 55 | * Run some checks then autoload our plugins. 56 | */ 57 | public function loadPlugins() 58 | { 59 | $this->checkCache(); 60 | $this->validatePlugins(); 61 | $this->countPlugins(); 62 | 63 | array_map(static function () { 64 | include_once WPMU_PLUGIN_DIR . '/' . func_get_args()[0]; 65 | }, array_keys($this->cache['plugins'])); 66 | 67 | add_action('plugins_loaded', [$this, 'pluginHooks'], -9999); 68 | } 69 | 70 | /** 71 | * Filter show_advanced_plugins to display the autoloaded plugins. 72 | * @param $show bool Whether to show the advanced plugins for the specified plugin type. 73 | * @param $type string The plugin type, i.e., `mustuse` or `dropins` 74 | * @return bool We return `false` to prevent WordPress from overriding our work 75 | * {@internal We add the plugin details ourselves, so we return false to disable the filter.} 76 | */ 77 | public function showInAdmin($show, $type) 78 | { 79 | $screen = get_current_screen(); 80 | $current = is_multisite() ? 'plugins-network' : 'plugins'; 81 | 82 | if ($screen->base !== $current || $type !== 'mustuse' || !current_user_can('activate_plugins')) { 83 | return $show; 84 | } 85 | 86 | $this->updateCache(); 87 | 88 | $this->autoPlugins = array_map(function ($auto_plugin) { 89 | $auto_plugin['Name'] .= ' *'; 90 | return $auto_plugin; 91 | }, $this->autoPlugins); 92 | 93 | $GLOBALS['plugins']['mustuse'] = array_unique(array_merge($this->autoPlugins, $this->muPlugins), SORT_REGULAR); 94 | 95 | return false; 96 | } 97 | 98 | /** 99 | * This sets the cache or calls for an update 100 | */ 101 | private function checkCache() 102 | { 103 | $cache = get_site_option('bedrock_autoloader'); 104 | 105 | if ($cache === false || (isset($cache['plugins'], $cache['count']) && count($cache['plugins']) !== $cache['count'])) { 106 | $this->updateCache(); 107 | return; 108 | } 109 | 110 | $this->cache = $cache; 111 | } 112 | 113 | /** 114 | * Get the plugins and mu-plugins from the mu-plugin path and remove duplicates. 115 | * Check cache against current plugins for newly activated plugins. 116 | * After that, we can update the cache. 117 | */ 118 | private function updateCache() 119 | { 120 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; 121 | 122 | $this->autoPlugins = get_plugins($this->relativePath); 123 | $this->muPlugins = get_mu_plugins(); 124 | $plugins = array_diff_key($this->autoPlugins, $this->muPlugins); 125 | $rebuild = !isset($this->cache['plugins']); 126 | $this->activated = $rebuild ? $plugins : array_diff_key($plugins, $this->cache['plugins']); 127 | $this->cache = ['plugins' => $plugins, 'count' => $this->countPlugins()]; 128 | 129 | update_site_option('bedrock_autoloader', $this->cache); 130 | } 131 | 132 | /** 133 | * This accounts for the plugin hooks that would run if the plugins were 134 | * loaded as usual. Plugins are removed by deletion, so there's no way 135 | * to deactivate or uninstall. 136 | */ 137 | public function pluginHooks() 138 | { 139 | if (!is_array($this->activated)) { 140 | return; 141 | } 142 | 143 | foreach ($this->activated as $plugin_file => $plugin_info) { 144 | do_action('activate_' . $plugin_file); 145 | } 146 | } 147 | 148 | /** 149 | * Check that the plugin file exists, if it doesn't update the cache. 150 | */ 151 | private function validatePlugins() 152 | { 153 | foreach ($this->cache['plugins'] as $plugin_file => $plugin_info) { 154 | if (!file_exists(WPMU_PLUGIN_DIR . '/' . $plugin_file)) { 155 | $this->updateCache(); 156 | break; 157 | } 158 | } 159 | } 160 | 161 | /** 162 | * Count the number of autoloaded plugins. 163 | * 164 | * Count our plugins (but only once) by counting the top level folders in the 165 | * mu-plugins dir. If it's more or less than last time, update the cache. 166 | * 167 | * @return int Number of autoloaded plugins. 168 | */ 169 | private function countPlugins() 170 | { 171 | if (isset($this->count)) { 172 | return $this->count; 173 | } 174 | 175 | $count = count(glob(WPMU_PLUGIN_DIR . '/*/', GLOB_ONLYDIR | GLOB_NOSORT)); 176 | 177 | if (!isset($this->cache['count']) || $count !== $this->cache['count']) { 178 | $this->count = $count; 179 | $this->updateCache(); 180 | } 181 | 182 | return $this->count; 183 | } 184 | } 185 | --------------------------------------------------------------------------------