| @@ -51,11 +51,11 @@ | ||
| 51 | 51 | * @return Yoast_Plugin_Conflict |
| 52 | 52 | */ |
| 53 | 53 | public static function get_instance( $class_name = '' ) { |
| 54 | 54 | |
| 55 | - if ( \is_null( self::$instance ) ) { | |
| 56 | - if ( ! \is_string( $class_name ) || $class_name === '' ) { | |
| 57 | - $class_name = __CLASS__; | |
| 55 | + if ( self::$instance === null ) { | |
| 56 | + if ( ! is_string( $class_name ) || $class_name === '' ) { | |
| 57 | + $class_name = self::class; | |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | self::$instance = new $class_name(); |
| 61 | 61 | } |
| @@ -66,16 +66,21 @@ | ||
| 66 | 66 | /** |
| 67 | 67 | * Setting instance, all active plugins and search for active plugins. |
| 68 | 68 | * |
| 69 | 69 | * Protected constructor to prevent creating a new instance of the |
| 70 | - * *Singleton* via the `new` operator from outside of this class. | |
| 70 | + * *Singleton* via the `new` operator from outside this class. | |
| 71 | 71 | */ |
| 72 | 72 | protected function __construct() { |
| 73 | 73 | // Set active plugins. |
| 74 | - $this->all_active_plugins = \get_option( 'active_plugins' ); | |
| 74 | + $this->all_active_plugins = get_option( 'active_plugins' ); | |
| 75 | 75 | |
| 76 | - if ( \filter_input( INPUT_GET, 'action' ) === 'deactivate' ) { | |
| 77 | - $this->remove_deactivated_plugin(); | |
| 76 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 77 | + if ( isset( $_GET['action'] ) && is_string( $_GET['action'] ) ) { | |
| 78 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information and only comparing the variable in a condition. | |
| 79 | + $action = wp_unslash( $_GET['action'] ); | |
| 80 | + if ( $action === 'deactivate' ) { | |
| 81 | + $this->remove_deactivated_plugin(); | |
| 82 | + } | |
| 78 | 83 | } |
| 79 | 84 | |
| 80 | 85 | // Search for active plugins. |
| 81 | 86 | $this->search_active_plugins(); |
| @@ -96,17 +101,13 @@ | ||
| 96 | 101 | if ( empty( $this->active_conflicting_plugins ) ) { |
| 97 | 102 | return false; |
| 98 | 103 | } |
| 99 | 104 | |
| 100 | - if ( $sections_checked === null ) { | |
| 101 | - $sections_checked = []; | |
| 102 | - } | |
| 105 | + $sections_checked ??= []; | |
| 103 | 106 | |
| 104 | - if ( ! \in_array( $plugin_section, $sections_checked, true ) ) { | |
| 107 | + if ( ! in_array( $plugin_section, $sections_checked, true ) ) { | |
| 105 | 108 | $sections_checked[] = $plugin_section; |
| 106 | - $has_conflicts = ( ! empty( $this->active_conflicting_plugins[ $plugin_section ] ) ); | |
| 107 | - | |
| 108 | - return $has_conflicts; | |
| 109 | + return ( ! empty( $this->active_conflicting_plugins[ $plugin_section ] ) ); | |
| 109 | 110 | } |
| 110 | 111 | |
| 111 | 112 | return false; |
| 112 | 113 | } |
| @@ -111,46 +112,13 @@ | ||
| 111 | 112 | return false; |
| 112 | 113 | } |
| 113 | 114 | |
| 114 | 115 | /** |
| 115 | - * Getting all the conflicting plugins and return them as a string. | |
| 116 | - * | |
| 117 | - * This method will loop through all conflicting plugins to get the details of each plugin. The plugin name | |
| 118 | - * will be taken from the details to parse a comma separated string, which can be use for by example a notice | |
| 119 | - * | |
| 120 | - * @param string $plugin_section Plugin conflict type (such as Open Graph or sitemap). | |
| 121 | - * | |
| 122 | - * @deprecated 17.7 This method is unused and will be removed in the future | |
| 123 | - * @codeCoverageIgnore | |
| 124 | - * | |
| 125 | - * @return string | |
| 126 | - */ | |
| 127 | - public function get_conflicting_plugins_as_string( $plugin_section ) { | |
| 128 | - if ( ! \function_exists( 'get_plugin_data' ) ) { | |
| 129 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 130 | - } | |
| 131 | - | |
| 132 | - // Getting the active plugins by given section. | |
| 133 | - $plugins = $this->active_conflicting_plugins[ $plugin_section ]; | |
| 134 | - | |
| 135 | - $plugin_names = []; | |
| 136 | - foreach ( $plugins as $plugin ) { | |
| 137 | - $name = $this->get_plugin_name( $plugin ); | |
| 138 | - if ( ! empty( $name ) ) { | |
| 139 | - $plugin_names[] = '<em>' . $name . '</em>'; | |
| 140 | - } | |
| 141 | - } | |
| 142 | - unset( $plugins, $plugin ); | |
| 143 | - | |
| 144 | - if ( ! empty( $plugin_names ) ) { | |
| 145 | - return \implode( ' & ', $plugin_names ); | |
| 146 | - } | |
| 147 | - } | |
| 148 | - | |
| 149 | - /** | |
| 150 | 116 | * Checks for given $plugin_sections for conflicts. |
| 151 | 117 | * |
| 152 | 118 | * @param array $plugin_sections Set of sections. |
| 119 | + * | |
| 120 | + * @return void | |
| 153 | 121 | */ |
| 154 | 122 | public function check_plugin_conflicts( $plugin_sections ) { |
| 155 | 123 | foreach ( $plugin_sections as $plugin_section => $readable_plugin_section ) { |
| 156 | 124 | // Check for conflicting plugins and show error if there are conflicts. |
| @@ -159,11 +127,11 @@ | ||
| 159 | 127 | } |
| 160 | 128 | } |
| 161 | 129 | |
| 162 | 130 | // List of all active sections. |
| 163 | - $sections = \array_keys( $plugin_sections ); | |
| 131 | + $sections = array_keys( $plugin_sections ); | |
| 164 | 132 | // List of all sections. |
| 165 | - $all_plugin_sections = \array_keys( $this->plugins ); | |
| 133 | + $all_plugin_sections = array_keys( $this->plugins ); | |
| 166 | 134 | |
| 167 | 135 | /* |
| 168 | 136 | * Get all sections that are inactive. |
| 169 | 137 | * These plugins need to be cleared. |
| @@ -169,26 +137,26 @@ | ||
| 169 | 137 | * These plugins need to be cleared. |
| 170 | 138 | * |
| 171 | 139 | * This happens when Sitemaps or OpenGraph implementations toggle active/disabled. |
| 172 | 140 | */ |
| 173 | - $inactive_sections = \array_diff( $all_plugin_sections, $sections ); | |
| 141 | + $inactive_sections = array_diff( $all_plugin_sections, $sections ); | |
| 174 | 142 | if ( ! empty( $inactive_sections ) ) { |
| 175 | 143 | foreach ( $inactive_sections as $section ) { |
| 176 | - \array_walk( $this->plugins[ $section ], [ $this, 'clear_error' ] ); | |
| 144 | + array_walk( $this->plugins[ $section ], [ $this, 'clear_error' ] ); | |
| 177 | 145 | } |
| 178 | 146 | } |
| 179 | 147 | |
| 180 | 148 | // For active sections clear errors for inactive plugins. |
| 181 | 149 | foreach ( $sections as $section ) { |
| 182 | - // By default clear errors for all plugins of the section. | |
| 150 | + // By default, clear errors for all plugins of the section. | |
| 183 | 151 | $inactive_plugins = $this->plugins[ $section ]; |
| 184 | 152 | |
| 185 | 153 | // If there are active plugins, filter them from being cleared. |
| 186 | 154 | if ( isset( $this->active_conflicting_plugins[ $section ] ) ) { |
| 187 | - $inactive_plugins = \array_diff( $this->plugins[ $section ], $this->active_conflicting_plugins[ $section ] ); | |
| 155 | + $inactive_plugins = array_diff( $this->plugins[ $section ], $this->active_conflicting_plugins[ $section ] ); | |
| 188 | 156 | } |
| 189 | 157 | |
| 190 | - \array_walk( $inactive_plugins, [ $this, 'clear_error' ] ); | |
| 158 | + array_walk( $inactive_plugins, [ $this, 'clear_error' ] ); | |
| 191 | 159 | } |
| 192 | 160 | } |
| 193 | 161 | |
| 194 | 162 | /** |
| @@ -195,8 +163,10 @@ | ||
| 195 | 163 | * Setting an error on the screen. |
| 196 | 164 | * |
| 197 | 165 | * @param string $plugin_section Type of conflict group (such as Open Graph or sitemap). |
| 198 | 166 | * @param string $readable_plugin_section This is the value for the translation. |
| 167 | + * | |
| 168 | + * @return void | |
| 199 | 169 | */ |
| 200 | 170 | protected function set_error( $plugin_section, $readable_plugin_section ) { |
| 201 | 171 | |
| 202 | 172 | $notification_center = Yoast_Notification_Center::get(); |
| @@ -221,10 +191,10 @@ | ||
| 221 | 191 | $error_message, |
| 222 | 192 | [ |
| 223 | 193 | 'type' => Yoast_Notification::ERROR, |
| 224 | 194 | 'id' => 'wpseo-conflict-' . $identifier, |
| 225 | - ] | |
| 226 | - ) | |
| 195 | + ], | |
| 196 | + ), | |
| 227 | 197 | ); |
| 228 | 198 | } |
| 229 | 199 | } |
| 230 | 200 | |
| @@ -231,8 +201,10 @@ | ||
| 231 | 201 | /** |
| 232 | 202 | * Clear the notification for a plugin. |
| 233 | 203 | * |
| 234 | 204 | * @param string $plugin_file Clear the optional notification for this plugin. |
| 205 | + * | |
| 206 | + * @return void | |
| 235 | 207 | */ |
| 236 | 208 | public function clear_error( $plugin_file ) { |
| 237 | 209 | $identifier = $this->get_notification_identifier( $plugin_file ); |
| 238 | 210 | |
| @@ -243,8 +215,10 @@ | ||
| 243 | 215 | /** |
| 244 | 216 | * Loop through the $this->plugins to check if one of the plugins is active. |
| 245 | 217 | * |
| 246 | 218 | * This method will store the active plugins in $this->active_plugins. |
| 219 | + * | |
| 220 | + * @return void | |
| 247 | 221 | */ |
| 248 | 222 | protected function search_active_plugins() { |
| 249 | 223 | foreach ( $this->plugins as $plugin_section => $plugins ) { |
| 250 | 224 | $this->check_plugins_active( $plugins, $plugin_section ); |
| @@ -255,8 +229,10 @@ | ||
| 255 | 229 | * Loop through plugins and check if each plugin is active. |
| 256 | 230 | * |
| 257 | 231 | * @param array $plugins Set of plugins. |
| 258 | 232 | * @param string $plugin_section Type of conflict group (such as Open Graph or sitemap). |
| 233 | + * | |
| 234 | + * @return void | |
| 259 | 235 | */ |
| 260 | 236 | protected function check_plugins_active( $plugins, $plugin_section ) { |
| 261 | 237 | foreach ( $plugins as $plugin ) { |
| 262 | 238 | if ( $this->check_plugin_is_active( $plugin ) ) { |
| @@ -272,9 +248,9 @@ | ||
| 272 | 248 | * |
| 273 | 249 | * @return bool |
| 274 | 250 | */ |
| 275 | 251 | protected function check_plugin_is_active( $plugin ) { |
| 276 | - return \in_array( $plugin, $this->all_active_plugins, true ); | |
| 252 | + return in_array( $plugin, $this->all_active_plugins, true ); | |
| 277 | 253 | } |
| 278 | 254 | |
| 279 | 255 | /** |
| 280 | 256 | * Add plugin to the list of active plugins. |
| @@ -283,15 +259,17 @@ | ||
| 283 | 259 | * If $plugin itself doesn't exist it will be added. |
| 284 | 260 | * |
| 285 | 261 | * @param string $plugin_section Type of conflict group (such as Open Graph or sitemap). |
| 286 | 262 | * @param string $plugin Plugin basename string. |
| 263 | + * | |
| 264 | + * @return void | |
| 287 | 265 | */ |
| 288 | 266 | protected function add_active_plugin( $plugin_section, $plugin ) { |
| 289 | - if ( ! \array_key_exists( $plugin_section, $this->active_conflicting_plugins ) ) { | |
| 267 | + if ( ! array_key_exists( $plugin_section, $this->active_conflicting_plugins ) ) { | |
| 290 | 268 | $this->active_conflicting_plugins[ $plugin_section ] = []; |
| 291 | 269 | } |
| 292 | 270 | |
| 293 | - if ( ! \in_array( $plugin, $this->active_conflicting_plugins[ $plugin_section ], true ) ) { | |
| 271 | + if ( ! in_array( $plugin, $this->active_conflicting_plugins[ $plugin_section ], true ) ) { | |
| 294 | 272 | $this->active_conflicting_plugins[ $plugin_section ][] = $plugin; |
| 295 | 273 | } |
| 296 | 274 | } |
| 297 | 275 | |
| @@ -305,9 +283,9 @@ | ||
| 305 | 283 | * @return int|string |
| 306 | 284 | */ |
| 307 | 285 | protected function find_plugin_category( $plugin ) { |
| 308 | 286 | foreach ( $this->plugins as $plugin_section => $plugins ) { |
| 309 | - if ( \in_array( $plugin, $plugins, true ) ) { | |
| 287 | + if ( in_array( $plugin, $plugins, true ) ) { | |
| 310 | 288 | return $plugin_section; |
| 311 | 289 | } |
| 312 | 290 | } |
| 313 | 291 | } |
| @@ -319,9 +297,9 @@ | ||
| 319 | 297 | * |
| 320 | 298 | * @return string|bool Plugin name or false when no name is set. |
| 321 | 299 | */ |
| 322 | 300 | protected function get_plugin_name( $plugin ) { |
| 323 | - $plugin_details = \get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); | |
| 301 | + $plugin_details = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); | |
| 324 | 302 | |
| 325 | 303 | if ( $plugin_details['Name'] !== '' ) { |
| 326 | 304 | return $plugin_details['Name']; |
| 327 | 305 | } |
| @@ -330,13 +308,21 @@ | ||
| 330 | 308 | } |
| 331 | 309 | |
| 332 | 310 | /** |
| 333 | 311 | * When being in the deactivation process the currently deactivated plugin has to be removed. |
| 312 | + * | |
| 313 | + * @return void | |
| 334 | 314 | */ |
| 335 | 315 | private function remove_deactivated_plugin() { |
| 336 | - $deactivated_plugin = \filter_input( INPUT_GET, 'plugin' ); | |
| 337 | - $key_to_remove = \array_search( $deactivated_plugin, $this->all_active_plugins, true ); | |
| 316 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: On the deactivation screen the nonce is already checked by WordPress itself. | |
| 317 | + if ( ! isset( $_GET['plugin'] ) || ! is_string( $_GET['plugin'] ) ) { | |
| 318 | + return; | |
| 319 | + } | |
| 338 | 320 | |
| 321 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: On the deactivation screen the nonce is already checked by WordPress itself. | |
| 322 | + $deactivated_plugin = sanitize_text_field( wp_unslash( $_GET['plugin'] ) ); | |
| 323 | + $key_to_remove = array_search( $deactivated_plugin, $this->all_active_plugins, true ); | |
| 324 | + | |
| 339 | 325 | if ( $key_to_remove !== false ) { |
| 340 | 326 | unset( $this->all_active_plugins[ $key_to_remove ] ); |
| 341 | 327 | } |
| 342 | 328 | } |
| @@ -348,7 +334,7 @@ | ||
| 348 | 334 | * |
| 349 | 335 | * @return string |
| 350 | 336 | */ |
| 351 | 337 | private function get_notification_identifier( $plugin_file ) { |
| 352 | - return \md5( $plugin_file ); | |
| 338 | + return md5( $plugin_file ); | |
| 353 | 339 | } |
| 354 | 340 | } |