| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Services\Importing; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Config\Conflicting_Plugins; |
| 6 |
|
| 7 |
/** |
| 8 |
* Detects plugin conflicts. |
| 9 |
*/ |
| 10 |
class Conflicting_Plugins_Service { |
| 11 |
|
| 12 |
/** |
| 13 |
* Detects the conflicting plugins. |
| 14 |
* |
| 15 |
* @return array A list of all active conflicting plugins. |
| 16 |
*/ |
| 17 |
public function detect_conflicting_plugins() { |
| 18 |
$all_active_plugins = $this->get_active_plugins(); |
| 19 |
|
| 20 |
// Search for active plugins. |
| 21 |
return $this->get_active_conflicting_plugins( $all_active_plugins ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Deactivates the specified plugin(s) if any, or the entire list of known conflicting plugins. |
| 26 |
* |
| 27 |
* @param string|array|false $plugins Optional. The plugin filename, or array of plugin filenames, to deactivate. |
| 28 |
*/ |
| 29 |
public function deactivate_conflicting_plugins( $plugins = false ) { |
| 30 |
// If no plugins are specified, deactivate any known conflicting plugins that are active. |
| 31 |
if ( ! $plugins ) { |
| 32 |
$plugins = $this->detect_conflicting_plugins(); |
| 33 |
} |
| 34 |
|
| 35 |
// In case of a single plugin, wrap it in an array. |
| 36 |
if ( \is_string( $plugins ) ) { |
| 37 |
$plugins = [ $plugins ]; |
| 38 |
} |
| 39 |
|
| 40 |
if ( ! \is_array( $plugins ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Deactivate all specified plugins across the network, while retaining their deactivation hook. |
| 45 |
\deactivate_plugins( $plugins ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Loop through the list of known conflicting plugins to check if one of the plugins is active. |
| 50 |
* |
| 51 |
* @param array $all_active_plugins All plugins loaded by WordPress. |
| 52 |
* |
| 53 |
* @return array The array of activated conflicting plugins. |
| 54 |
*/ |
| 55 |
protected function get_active_conflicting_plugins( $all_active_plugins ) { |
| 56 |
$active_conflicting_plugins = []; |
| 57 |
|
| 58 |
foreach ( Conflicting_Plugins::all_plugins() as $plugin ) { |
| 59 |
if ( \in_array( $plugin, $all_active_plugins, true ) ) { |
| 60 |
$active_conflicting_plugins[] = $plugin; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $active_conflicting_plugins; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get a list of all plugins active in the current WordPress instance. |
| 69 |
* |
| 70 |
* @return false|array The names of all active plugins. |
| 71 |
*/ |
| 72 |
protected function get_active_plugins() { |
| 73 |
// Request a list of active plugins from WordPress. |
| 74 |
$all_active_plugins = \get_option( 'active_plugins' ); |
| 75 |
|
| 76 |
return $this->ignore_deactivating_plugin( $all_active_plugins ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* While deactivating a plugin, we should ignore the plugin currently being deactivated. |
| 81 |
* |
| 82 |
* @param array $all_active_plugins All plugins currently loaded by WordPress. |
| 83 |
* |
| 84 |
* @return array The remaining active plugins. |
| 85 |
*/ |
| 86 |
protected function ignore_deactivating_plugin( $all_active_plugins ) { |
| 87 |
if ( isset( $_GET['action'] ) && isset( $_GET['plugin'] ) && \filter_var( \wp_unslash( $_GET['action'] ) ) === 'deactivate' ) { |
| 88 |
$deactivated_plugin = \filter_var( \wp_unslash( $_GET['plugin'] ) ); |
| 89 |
|
| 90 |
\check_admin_referer( 'deactivate-plugin_' . $deactivated_plugin ); |
| 91 |
|
| 92 |
$key_to_remove = \array_search( $deactivated_plugin, $all_active_plugins, true ); |
| 93 |
if ( $key_to_remove !== false ) { |
| 94 |
unset( $all_active_plugins[ $key_to_remove ] ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return $all_active_plugins; |
| 99 |
} |
| 100 |
} |
| 101 |
|