Skip to content

Commit

Permalink
Add a mu-plugin loader that loads a specific version of a plugin if r…
Browse files Browse the repository at this point in the history
…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
dd32 and ryelle authored Feb 1, 2024
1 parent 87d99d0 commit 9e39b9a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
88 changes: 88 additions & 0 deletions mu-plugins/plugin-tweaks/incompatible-plugins.php
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();
1 change: 1 addition & 0 deletions mu-plugins/plugin-tweaks/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@

require_once __DIR__ . '/wporg-internal-notes.php';
require_once __DIR__ . '/stream.php';
require_once __DIR__ . '/incompatible-plugins.php';

0 comments on commit 9e39b9a

Please sign in to comment.