Skip to content

Commit

Permalink
Incompatible Plugins: Don't overwrite position 0 plugins when running…
Browse files Browse the repository at this point in the history
… in a switched context (#569)

* Don't overwrite position 0 plugins when running in a switched context

* Always queue the filters, to handle switching when the problematic plugin is site vs network activated.
  • Loading branch information
dd32 authored Feb 2, 2024
1 parent d98738e commit 0b0aa7c
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions mu-plugins/plugin-tweaks/incompatible-plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
* 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.
*
* NOTE: This doesn't support blog switching well at all, on a switched blog the filters will be applied to the wrong blog,
* this isn't as bad as it sounds, as loading plugins for other sites from the context of a different site rarely
* works in the first place. Instead, this code simply double-checks that the `$from` plugin is active.
* The `$check` plugin is not checked for in the switched context, as including two different versions of the same
* plugin on the same request is not going to work, so it still needs to attempt to load the versioned version.
*/
function filter_the_filters() {
$active_plugins = (array) get_option( 'active_plugins', [] );
Expand All @@ -55,33 +61,35 @@ function filter_the_filters() {
continue;
}

if ( in_array( $from, $active_plugins, true ) ) {
add_filter(
'option_active_plugins',
function( $plugins ) use ( $from, $to ) {
add_filter(
'option_active_plugins',
function( $plugins ) use ( $from, $to ) {
$pos = array_search( $from, $plugins, true );
if ( false !== $pos ) {
// Splice to retain load order, if it's important.
array_splice(
$plugins,
array_search( $from, $plugins, true ),
$pos,
1,
$to
);
return $plugins;
}
);
}

if ( isset( $active_sitewide_plugins[ $from ] ) ) {
add_filter(
'site_option_active_sitewide_plugins',
function( $plugins ) use ( $from, $to ) {
return $plugins;
}
);

add_filter(
'site_option_active_sitewide_plugins',
function( $plugins ) use ( $from, $to ) {
if ( isset( $plugins[ $from ] ) ) {
$plugins[ $to ] = $plugins[ $from ];
unset( $plugins[ $from ] );

return $plugins;
}
);
}

return $plugins;
}
);
}
}

Expand Down

0 comments on commit 0b0aa7c

Please sign in to comment.