Skip to content

Commit

Permalink
Merge pull request #437 from nasyrov/patch-1
Browse files Browse the repository at this point in the history
Remove unnecessary static variables from autoloader

+ PSR2 naming
  • Loading branch information
swalkinshaw authored Jun 9, 2019
2 parents 43c2cdc + 57f9f15 commit 544e3c0
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions web/app/mu-plugins/bedrock-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,39 @@
*/
class Autoloader
{
/** @var static Singleton instance */
private static $instance;

/** @var array Store Autoloader cache and site option */
private static $cache;
private $cache;

/** @var array Autoloaded plugins */
private static $auto_plugins;
private $autoPlugins;

/** @var array Autoloaded mu-plugins */
private static $mu_plugins;
private $muPlugins;

/** @var int Number of plugins */
private static $count;
private $count;

/** @var array Newly activated plugins */
private static $activated;
private $activated;

/** @var string Relative path to the mu-plugins dir */
private static $relative_path;

/** @var static Singleton instance */
private static $_single;
private $relativePath;

/**
* Create singleton, populate vars, and set WordPress hooks
*/
public function __construct()
{
if (isset(self::$_single)) {
if (isset(self::$instance)) {
return;
}

self::$_single = $this;
self::$relative_path = '/../' . basename(__DIR__);
self::$instance = $this;

$this->relativePath = '/../' . basename(__DIR__);

if (is_admin()) {
add_filter('show_advanced_plugins', [$this, 'showInAdmin'], 0, 2);
Expand All @@ -74,7 +75,7 @@ public function loadPlugins()

array_map(static function () {
include_once WPMU_PLUGIN_DIR . '/' . func_get_args()[0];
}, array_keys(self::$cache['plugins']));
}, array_keys($this->cache['plugins']));

$this->pluginHooks();
}
Expand All @@ -97,12 +98,12 @@ public function showInAdmin($show, $type)

$this->updateCache();

self::$auto_plugins = array_map(function ($auto_plugin) {
$this->autoPlugins = array_map(function ($auto_plugin) {
$auto_plugin['Name'] .= ' *';
return $auto_plugin;
}, self::$auto_plugins);
}, $this->autoPlugins);

$GLOBALS['plugins']['mustuse'] = array_unique(array_merge(self::$auto_plugins, self::$mu_plugins), SORT_REGULAR);
$GLOBALS['plugins']['mustuse'] = array_unique(array_merge($this->autoPlugins, $this->muPlugins), SORT_REGULAR);

return false;
}
Expand All @@ -119,7 +120,7 @@ private function checkCache()
return;
}

self::$cache = $cache;
$this->cache = $cache;
}

/**
Expand All @@ -131,14 +132,14 @@ private function updateCache()
{
require_once ABSPATH . 'wp-admin/includes/plugin.php';

self::$auto_plugins = get_plugins(self::$relative_path);
self::$mu_plugins = get_mu_plugins();
$plugins = array_diff_key(self::$auto_plugins, self::$mu_plugins);
$rebuild = !is_array(self::$cache['plugins']);
self::$activated = $rebuild ? $plugins : array_diff_key($plugins, self::$cache['plugins']);
self::$cache = ['plugins' => $plugins, 'count' => $this->countPlugins()];
$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', self::$cache);
update_site_option('bedrock_autoloader', $this->cache);
}

/**
Expand All @@ -148,11 +149,11 @@ private function updateCache()
*/
private function pluginHooks()
{
if (!is_array(self::$activated)) {
if (!is_array($this->activated)) {
return;
}

foreach (self::$activated as $plugin_file => $plugin_info) {
foreach ($this->activated as $plugin_file => $plugin_info) {
do_action('activate_' . $plugin_file);
}
}
Expand All @@ -162,7 +163,7 @@ private function pluginHooks()
*/
private function validatePlugins()
{
foreach (self::$cache['plugins'] as $plugin_file => $plugin_info) {
foreach ($this->cache['plugins'] as $plugin_file => $plugin_info) {
if (!file_exists(WPMU_PLUGIN_DIR . '/' . $plugin_file)) {
$this->updateCache();
break;
Expand All @@ -180,18 +181,18 @@ private function validatePlugins()
*/
private function countPlugins()
{
if (isset(self::$count)) {
return self::$count;
if (isset($this->count)) {
return $this->count;
}

$count = count(glob(WPMU_PLUGIN_DIR . '/*/', GLOB_ONLYDIR | GLOB_NOSORT));

if (!isset(self::$cache['count']) || $count !== self::$cache['count']) {
self::$count = $count;
if (!isset($this->cache['count']) || $count !== $this->cache['count']) {
$this->count = $count;
$this->updateCache();
}

return self::$count;
return $this->count;
}
}

Expand Down

0 comments on commit 544e3c0

Please sign in to comment.