PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / services / importing / conflicting-plugins-service.php

conflicting-plugins-service.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at src/services/importing/conflicting-plugins-service.php

104 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @return void
30 */
31 public function deactivate_conflicting_plugins( $plugins = false ) {
32 // If no plugins are specified, deactivate any known conflicting plugins that are active.
33 if ( ! $plugins ) {
34 $plugins = $this->detect_conflicting_plugins();
35 }
36
37 // In case of a single plugin, wrap it in an array.
38 if ( \is_string( $plugins ) ) {
39 $plugins = [ $plugins ];
40 }
41
42 if ( ! \is_array( $plugins ) ) {
43 return;
44 }
45
46 // Deactivate all specified plugins across the network, while retaining their deactivation hook.
47 \deactivate_plugins( $plugins );
48 }
49
50 /**
51 * Loop through the list of known conflicting plugins to check if one of the plugins is active.
52 *
53 * @param array $all_active_plugins All plugins loaded by WordPress.
54 *
55 * @return array The array of activated conflicting plugins.
56 */
57 protected function get_active_conflicting_plugins( $all_active_plugins ) {
58 $active_conflicting_plugins = [];
59
60 foreach ( Conflicting_Plugins::all_plugins() as $plugin ) {
61 if ( \in_array( $plugin, $all_active_plugins, true ) ) {
62 $active_conflicting_plugins[] = $plugin;
63 }
64 }
65
66 return $active_conflicting_plugins;
67 }
68
69 /**
70 * Get a list of all plugins active in the current WordPress instance.
71 *
72 * @return array|false The names of all active plugins.
73 */
74 protected function get_active_plugins() {
75 // Request a list of active plugins from WordPress.
76 $all_active_plugins = \get_option( 'active_plugins' );
77
78 return $this->ignore_deactivating_plugin( $all_active_plugins );
79 }
80
81 /**
82 * While deactivating a plugin, we should ignore the plugin currently being deactivated.
83 *
84 * @param array $all_active_plugins All plugins currently loaded by WordPress.
85 *
86 * @return array The remaining active plugins.
87 */
88 protected function ignore_deactivating_plugin( $all_active_plugins ) {
89 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are strictly comparing only.
90 if ( isset( $_GET['action'] ) && isset( $_GET['plugin'] ) && \is_string( $_GET['action'] ) && \is_string( $_GET['plugin'] ) && \wp_unslash( $_GET['action'] ) === 'deactivate' ) {
91 $deactivated_plugin = \sanitize_text_field( \wp_unslash( $_GET['plugin'] ) );
92
93 \check_admin_referer( 'deactivate-plugin_' . $deactivated_plugin );
94
95 $key_to_remove = \array_search( $deactivated_plugin, $all_active_plugins, true );
96 if ( $key_to_remove !== false ) {
97 unset( $all_active_plugins[ $key_to_remove ] );
98 }
99 }
100
101 return $all_active_plugins;
102 }
103 }
104