-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a mu-plugin loader that loads a specific version of a plugin if r…
…equired. (#561) * Add a mu-plugin loader that loads a specific version of a plugin if required. * Run the check on both locally-active plugins and network-activated plugins Gutenberg shows up in both sometimes, so we should check and remove from both. * Add Pattern Creator as incompatible with GB * Don't filter the active plugins on the plugins management page, to avoid the filtered array being saved and becoming un-filtered. --------- Co-authored-by: Kelly Dwan <[email protected]>
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace WordPressdotorg\MU_Plugins\Plugin_Tweaks\IncompatiblePlugins; | ||
|
||
defined( 'WPINC' ) || die(); | ||
|
||
// Don't run this on the plugins.php page, as we don't want to operate on filtered options. | ||
if ( defined( 'WP_ADMIN' ) && WP_ADMIN && str_contains( $_SERVER['REQUEST_URI'] ?? '', '/plugins.php' ) ) { | ||
return; | ||
} | ||
|
||
/** | ||
* Plugin config. | ||
* | ||
* Each item in the array contains a plugin to check (`check`), and a plugin | ||
* that should be deactivated (`from`) in favor of another plugin (`to`). | ||
*/ | ||
const PLUGINS = [ | ||
[ | ||
// Blocks Everywhere: Uses private/unstable APIs, | ||
// which are blocked after GB 16.8. | ||
'check' => 'blocks-everywhere/blocks-everywhere.php', | ||
'from' => 'gutenberg/gutenberg.php', | ||
'to' => 'gutenberg-16.8/gutenberg.php', | ||
], | ||
[ | ||
// Pattern Creator: Uses private/unstable APIs, | ||
// which are blocked after GB 16.8. | ||
'check' => 'pattern-creator/pattern-creator.php', | ||
'from' => 'gutenberg/gutenberg.php', | ||
'to' => 'gutenberg-16.8/gutenberg.php', | ||
], | ||
]; | ||
|
||
/** | ||
* Check the above list of plugins, and filter the appropriate option. | ||
* | ||
* This needs to be done on plugin inclusion, as network-wide plugins are included immediately after mu-plugins. | ||
*/ | ||
function filter_the_filters() { | ||
$active_plugins = (array) get_option( 'active_plugins', [] ); | ||
$active_sitewide_plugins = is_multisite() ? get_site_option( 'active_sitewide_plugins', [] ) : []; | ||
|
||
foreach ( PLUGINS as $incompatible_plugin ) { | ||
$check = $incompatible_plugin['check']; | ||
$from = $incompatible_plugin['from']; | ||
$to = $incompatible_plugin['to']; | ||
|
||
// Check to see if the incompatible plugin is active first. | ||
// Not using the functions that do this, as they're only loaded in wp-admin. | ||
if ( | ||
! in_array( $check, $active_plugins, true ) && | ||
! isset( $active_sitewide_plugins[ $check ] ) | ||
) { | ||
continue; | ||
} | ||
|
||
if ( in_array( $from, $active_plugins, true ) ) { | ||
add_filter( | ||
'option_active_plugins', | ||
function( $plugins ) use ( $from, $to ) { | ||
// Splice to retain load order, if it's important. | ||
array_splice( | ||
$plugins, | ||
array_search( $from, $plugins, true ), | ||
1, | ||
$to | ||
); | ||
return $plugins; | ||
} | ||
); | ||
} | ||
|
||
if ( isset( $active_sitewide_plugins[ $from ] ) ) { | ||
add_filter( | ||
'site_option_active_sitewide_plugins', | ||
function( $plugins ) use ( $from, $to ) { | ||
$plugins[ $to ] = $plugins[ $from ]; | ||
unset( $plugins[ $from ] ); | ||
|
||
return $plugins; | ||
} | ||
); | ||
} | ||
} | ||
} | ||
|
||
filter_the_filters(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters