Skip to content

Commit

Permalink
Simplifies the handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya committed Feb 5, 2024
1 parent 63ae03d commit 2389ada
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 111 deletions.
8 changes: 1 addition & 7 deletions src/wp-includes/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,13 +991,7 @@ function wp_get_active_and_valid_plugins() {
// Not already included as a network plugin.
&& ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $plugin, $network_plugins, true ) )
) {
$plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
$plugins[] = $plugin_file;

// Check if the plugin is a for-Core plugin and, if yes, flag it for a later compatibility check.
if ( ! empty( $_wp_maybe_flag_plugin_for_compat_check ) && is_callable( $_wp_maybe_flag_plugin_for_compat_check ) ) {
$_wp_maybe_flag_plugin_for_compat_check( $plugin, $plugin_file, $plugins );
}
$plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
}
}

Expand Down
186 changes: 82 additions & 104 deletions src/wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,116 +409,102 @@
*
* @since 6.5.0
*/
global $_wp_for_core_plugins_compat_version, $_wp_activated_plugins_to_compat_check, $_wp_maybe_flag_plugin_for_compat_check, $_wp_deactivate_incompatible_plugins;
global $_wp_for_core_plugins_compat_version, $_found_incompatible_for_core_plugins, $_is_plugin_compatible_with_wp, $_handle_incompatible_for_core_plugins;

// List of For-Core plugins' compatible version definitions.
$_wp_for_core_plugins_compat_version = array(
'gutenberg/gutenberg.php' => array(
WP_PLUGIN_DIR . '/gutenberg/gutenberg.php' => array(
'name' => 'Gutenberg',
'minimum_compatible_version' => '17.6',
),
);

// Found activated for-Core plugins to do the compatibility check.
$_wp_activated_plugins_to_compat_check = array();
// Found incompatible for-Core plugins.
$_found_incompatible_for_core_plugins = array();

/*
* Flags the plugin for compatibility check if it's in the for-Core list.
/**
* Checks if the given plugin is compatible with this WordPress version.
*
* @since 6.5.0
*
* @param string $plugin The plugin to check.
* @param string $plugin_file The plugin's file.
* @param array $plugins The list of active and valid plugins to be loaded.
* @global $_wp_for_core_plugins_compat_version
* @global $_found_incompatible_for_core_plugins
*/
$_wp_maybe_flag_plugin_for_compat_check = static function ( $plugin, $plugin_file, $plugins ) {
global $_wp_for_core_plugins_compat_version, $_wp_activated_plugins_to_compat_check;
$_is_plugin_compatible_with_wp = static function ( $plugin ) use ( $_wp_for_core_plugins_compat_version ) {
global $_found_incompatible_for_core_plugins;

if ( ! isset( $_wp_for_core_plugins_compat_version[ $plugin ] ) ) {
return;
if ( isset( $_wp_for_core_plugins_compat_version[ $plugin ] ) ) {
return true;
}

// Found a for-Core plugin that hasn't yet been stored for compat check.
if ( isset( $_wp_activated_plugins_to_compat_check[ $plugin ] ) ) {
return false;
}

// Get the Name and Version from the plugin's header.
$plugin_data = get_file_data(
$plugin,
array(
'Name' => 'Plugin Name',
'Version' => 'Version',
),
'plugin'
);

// Whoops, something went wrong. Bail out.
if ( ! ( isset( $plugin_data['Version'] ) && isset( $plugin_data['Name'] ) ) ) {
return true;
}

// Get the last index value, as this is where this plugin is stored in the $plugins array.
end( $plugins );
$plugins_index = key( $plugins );
// Check if compatible. If yes, bail out.
$min_compat_version = $_wp_for_core_plugins_compat_version[ $plugin ]['minimum_compatible_version'];
if ( version_compare( $plugin_data['Version'], $min_compat_version, '>=' ) ) {
return true;
}

$_wp_activated_plugins_to_compat_check[ $plugin ] = array(
'file' => $plugin_file,
'deactivated_version' => '',
'index_plugins_to_load' => $plugins_index,
// Found an incompatible for-Core plugin. Add it to the found global for later batch processing.
$plugin_slug = str_replace( WP_PLUGIN_DIR . '/', '', $plugin );
$_found_incompatible_for_core_plugins[ $plugin_slug ] = array(
'plugin_path' => $plugin,
'plugin_name' => $plugin_data['Name'],
'version_deactivated' => $plugin_data['Version'],
'version_compatible' => $min_compat_version,
);

return false;
};

/**
* On WordPress load, deactivate any incompatible for-Core plugins.
* Handle incompatible for-Core plugins.
*
* Also removes the incompatible plugins from the given list of plugins to load,
* i.e. to prevent these plugins from being loaded into memory by the plugin
* loader loops.
* The handler does the following:
* * Removes the incompatible plugins from the "active_plugins" option.
* * Stores them in "wp_force_deactivation_incompatible_plugins" option, which will be used
* in wp-admin to notify the user.
*
* @since 6.5.0
*
* @param array $plugins_to_load Activate and valid plugins to load. (Passed by reference.)
* @global $_found_incompatible_for_core_plugins
*/
$_wp_deactivate_incompatible_plugins = static function ( &$plugins_to_load ) {
global $_wp_for_core_plugins_compat_version, $_wp_activated_plugins_to_compat_check;

// Bail out if there are no active plugins for compatibility check.
if ( empty( $_wp_activated_plugins_to_compat_check ) || empty( $plugins_to_load ) ) {
return;
}
$_handle_incompatible_for_core_plugins = static function () {
global $_found_incompatible_for_core_plugins;

$active_plugins = (array) get_option( 'active_plugins', array() );
$active_plugins_by_plugin = array_flip( $active_plugins );
$found_incompatibles = array();

/*
* Loop through the plugins to do the compatibility check.
* Deactivate each incompatible plugin.
*/
foreach ( $_wp_activated_plugins_to_compat_check as $plugin => $plugin_info ) {

$index_plugins_to_load = $plugin_info['index_plugins_to_load'];

if ( ! isset( $plugins_to_load[ $index_plugins_to_load ] ) ) {
return;
}

// Get the Name and Version from the plugin's header.
$plugin_data = get_file_data(
$plugins_to_load[ $index_plugins_to_load ],
array(
'Name' => 'Plugin Name',
'Version' => 'Version',
),
'plugin'
);

// Whoops, something went wrong. Bail out.
if ( ! ( isset( $plugin_data['Version'] ) && isset( $plugin_data['Name'] ) ) ) {
return;
}

// Check if compatible. If yes, bail out.
$min_compat_version = $_wp_for_core_plugins_compat_version[ $plugin ]['minimum_compatible_version'];
if ( version_compare( $plugin_data['Version'], $min_compat_version, '>=' ) ) {
return;s
}

// Add the plugin to found incompatibles.
$found_incompatibles[ $plugin ] = array(
'plugin_name' => $plugin_data['Name'],
'version_deactivated' => $plugin_data['Version'],
'version_compatible' => $min_compat_version,
);

// Remove it from the 'active_plugins' option.
unset( $active_plugins[ $active_plugins_by_plugin[ $plugin ] ] );

// Remove it from the plugins to be loaded.
unset( $plugins_to_load[ $index_plugins_to_load ] );
// Remove each of the found incompatible plugins from the "active_plugins" option.
foreach ( $_found_incompatible_for_core_plugins as $plugin_slug => $plugin ) {
unset( $active_plugins[ $active_plugins_by_plugin[ $plugin_slug ] ] );
}

// Clean up global space.
unset(
$GLOBALS['_wp_for_core_plugins_compat_version'],
$GLOBALS['_wp_activated_plugins_to_compat_check'],
$GLOBALS['_is_plugin_compatible_with_wp']
);

if ( empty( $found_incompatibles ) ) {
return;
}
Expand All @@ -527,28 +513,9 @@
update_option( 'active_plugins', $active_plugins );

// Update the list of incompatible plugins to notify user in the admin.
$incompatible_plugins = (array) get_option( 'wp_force_deactivation_incompatible_plugins', array() );
$incompatible_plugins = array_merge( $incompatible_plugins, $found_incompatibles );
update_option( 'wp_force_deactivation_incompatible_plugins', $incompatible_plugins );
};

/*
* Removes the for-Core plugins compatiblity handler.
*
* Unsets each of the global variables, removing them from memory and usage.
*
* @since 6.5.0
*/
$_wp_remove_for_core_compat_handler = static function () {
global $_wp_for_core_plugins_compat_version, $_wp_activated_plugins_to_compat_check, $_wp_maybe_flag_plugin_for_compat_check, $_wp_deactivate_incompatible_plugins;
unset(
$_wp_for_core_plugins_compat_version,
$_wp_activated_plugins_to_compat_check,
$_wp_maybe_flag_plugin_for_compat_check,
$_wp_deactivate_incompatible_plugins
);
update_option( 'wp_force_deactivation_incompatible_plugins', $_found_incompatible_for_core_plugins );
};
// End of the for-Core compatibility handler.
// End of the for-Core plugins compatibility handler.

// Load must-use plugins.
foreach ( wp_get_mu_plugins() as $mu_plugin ) {
Expand Down Expand Up @@ -625,9 +592,18 @@
}

// Load active plugins.
$_active_and_valid_plugins_to_load = wp_get_active_and_valid_plugins();
$_wp_deactivate_incompatible_plugins( $_active_and_valid_plugins_to_load );
foreach ( $_active_and_valid_plugins_to_load as $plugin ) {
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {

/*
* If the plugin is incompatible with this WordPress version,
* skip loading the plugin.
*
* @since 6.5.0
*/
if ( ! $_is_plugin_compatible_with_wp( $plugin ) ) {
continue;
}

wp_register_plugin_realpath( $plugin );

$_wp_plugin_file = $plugin;
Expand All @@ -643,8 +619,10 @@
*/
do_action( 'plugin_loaded', $plugin );
}
unset( $plugin, $_wp_plugin_file, $_active_and_valid_plugins_to_load );
$_wp_remove_for_core_compat_handler();
unset( $plugin, $_wp_plugin_file );
// Handle incompatible for-Core plugins.
$_handle_incompatible_for_core_plugins();
unset( $_handle_incompatible_for_core_plugins );

// Load pluggable functions.
require ABSPATH . WPINC . '/pluggable.php';
Expand Down

0 comments on commit 2389ada

Please sign in to comment.