Skip to content

Commit

Permalink
Remove maxmemory-policy from redis config, update bedrock-autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
raikasdev committed Jun 20, 2024
1 parent aa9acf6 commit c3536eb
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 107 deletions.
1 change: 0 additions & 1 deletion config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
'prefix' => env( 'DB_NAME' ),
'database' => env( 'REDIS_DATABASE' ) ?: 0,
'maxttl' => 43200, // Max cache half day
'maxmemory-policy' => 'allkeys-lru',
'timeout' => 0.5,
'read_timeout' => 0.5,
'retry_interval' => 10,
Expand Down
279 changes: 173 additions & 106 deletions content/mu-plugins/bedrock-autoloader.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* Plugin Name: Bedrock Autoloader
* Plugin URI: https://github.com/roots/bedrock/
* Plugin URI: https://github.com/roots/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
* Version: 1.0.4
* Author: Roots
* Author URI: https://roots.io/
* License: MIT License
Expand All @@ -12,111 +12,178 @@
namespace Roots\Bedrock;

if ( ! is_blog_installed() ) { return; }

/**
* Class Autoloader
* @package Roots\Bedrock
* @author Roots
* @link https://roots.io/
*/
class Autoloader {
private static $cache; // Stores our plugin cache and site option.
private static $auto_plugins; // Contains the autoloaded plugins (only when needed).
private static $mu_plugins; // Contains the mu plugins (only when needed).
private static $count; // Contains the plugin count.
private static $activated; // Newly activated plugins.
private static $relative_path; // Relative path to the mu-plugins dir.
private static $_single; // Let's make this a singleton.
public function __construct() {
if ( isset( self::$_single ) ) { return; }
self::$_single = $this; // Singleton set.
self::$relative_path = '/../' . basename( __DIR__ ); // Rel path set.
if ( is_admin() ) {
add_filter( 'show_advanced_plugins', array( $this, 'showInAdmin' ), 0, 2 ); // Admin only filter.
}
$this->loadPlugins();
}
/**
* Run some checks then autoload our plugins.
*/
public function loadPlugins() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$this->checkCache();
$this->validatePlugins();
$this->countPlugins();
foreach ( self::$cache['plugins'] as $plugin_file => $plugin_info ) {
include_once WPMU_PLUGIN_DIR . '/' . $plugin_file;
}
$this->pluginHooks();
}
/**
* Filter show_advanced_plugins to display the autoloaded plugins.
*/
public function showInAdmin( $bool, $type ) { // phpcs:ignore
$screen = get_current_screen();
$current = is_multisite() ? 'plugins-network' : 'plugins';
if ( $screen->{'base'} !== $current || $type !== 'mustuse' || ! current_user_can( 'activate_plugins' ) ) {
return $bool;
}
$this->updateCache(); // May as well update the transient cache whilst here.
self::$auto_plugins = array_map(function ( $auto_plugin ) {
$auto_plugin['Name'] .= ' *';
return $auto_plugin;
}, self::$auto_plugins);
$GLOBALS['plugins']['mustuse'] = array_unique( array_merge( self::$auto_plugins, self::$mu_plugins ), SORT_REGULAR );
return false; // Prevent WordPress overriding our work.
}
/**
* This sets the cache or calls for an update
*/
private function checkCache() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$cache = get_site_option( 'bedrock_autoloader' );
if ( $cache === false ) {
return $this->updateCache();
}
self::$cache = $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.
*/
private function updateCache() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
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 = array( 'plugins' => $plugins, 'count' => $this->countPlugins() );
update_site_option( 'bedrock_autoloader', self::$cache );
}
/**
* 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.
*/
private function pluginHooks() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
if ( ! is_array( self::$activated ) ) { return; }
foreach ( self::$activated as $plugin_file => $plugin_info ) {
do_action( 'activate_' . $plugin_file );
}
}
/**
* Check that the plugin file exists, if it doesn't update the cache.
*/
private function validatePlugins() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
foreach ( self::$cache['plugins'] as $plugin_file => $plugin_info ) {
if ( ! file_exists( WPMU_PLUGIN_DIR . '/' . $plugin_file ) ) {
$this->updateCache();
break;
}
}
}
/**
* 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.
*/
private function countPlugins() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
if ( isset( self::$count ) ) { return self::$count; }
/** @var static Singleton instance */
private static $instance;

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

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

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

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

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

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

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

self::$instance = $this;

$this->relative_path = '/../' . basename( WPMU_PLUGIN_DIR );

if ( is_admin() ) {
add_filter( 'show_advanced_plugins', [ $this, 'show_in_admin' ], 0, 2 );
}

$this->load_plugins();
}

/**
* Run some checks then autoload our plugins.
*/
public function load_plugins() {
$this->check_cache();
$this->validate_plugins();
$this->countPlugins();

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

add_action( 'plugins_loaded', [ $this, 'plugin_hooks' ], -9999 );
}

/**
* 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
* {@internal We add the plugin details ourselves, so we return false to disable the filter.}
*/
public function show_in_admin( $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->update_cache();

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

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

return false;
}

/**
* This sets the cache or calls for an update
*/
private function check_cache() {
$cache = get_site_option( 'bedrock_autoloader' );

// is_array check added by dude
if ( $cache === false || ! is_array( $cache ) || ( isset( $cache['plugins'], $cache['count'] ) && count( $cache['plugins'] ) !== $cache['count'] ) ) {
$this->update_cache();
return;
}

$this->cache = $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.
*/
private function update_cache() {
require_once ABSPATH . 'wp-admin/includes/plugin.php';

$this->auto_plugins = get_plugins( $this->relative_path );
$this->mu_plugins = get_mu_plugins();
$plugins = array_diff_key( $this->auto_plugins, $this->mu_plugins );
$rebuild = ! isset( $this->cache['plugins'] );
$this->activated = $rebuild ? $plugins : array_diff_key( $plugins, $this->cache['plugins'] );
$this->cache = [ 'plugins' => $plugins, 'count' => $this->count_plugins() ];

update_site_option( 'bedrock_autoloader', $this->cache );
}

/**
* 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.
*/
public function plugin_hooks() {
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.
*/
private function validate_plugins() {
foreach ( $this->cache['plugins'] as $plugin_file => $plugin_info ) {
if ( ! file_exists( WPMU_PLUGIN_DIR . '/' . $plugin_file ) ) {
$this->update_cache();
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 count_plugins() {
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;
$this->updateCache();
}
return self::$count;
}

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

return $this->count;
}
}

new Autoloader();

0 comments on commit c3536eb

Please sign in to comment.