From 14b1434c65a34798e8baca1eb9d6a67ae18f19ac Mon Sep 17 00:00:00 2001 From: Marcus Quinn Date: Mon, 7 Apr 2025 05:17:37 +0100 Subject: [PATCH] Improved plugin security and compatibility --- plugin-reference-cleaner.php | 91 +++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 23 deletions(-) diff --git a/plugin-reference-cleaner.php b/plugin-reference-cleaner.php index 718e644..51b55ee 100644 --- a/plugin-reference-cleaner.php +++ b/plugin-reference-cleaner.php @@ -8,10 +8,16 @@ * License: GPL-2.0+ */ +// Exit if accessed directly +if (!defined('ABSPATH')) { + exit; +} + class Plugin_Reference_Cleaner { public function __construct() { // Hook into admin notices to modify plugin error messages add_action('admin_notices', array($this, 'inject_remove_button'), 100); + add_action('network_admin_notices', array($this, 'inject_remove_button'), 100); // Ensure notices in network admin // Handle the AJAX request to remove the plugin reference add_action('wp_ajax_remove_plugin_reference', array($this, 'remove_plugin_reference')); } @@ -21,7 +27,7 @@ class Plugin_Reference_Cleaner { global $pagenow; // Only run on plugins.php or network admin plugins page - if (!in_array($pagenow, array('plugins.php', 'network/plugins.php'))) { + if (!in_array($pagenow, array('plugins.php', 'plugins.php'))) { return; } @@ -30,18 +36,20 @@ class Plugin_Reference_Cleaner { $has_error_notice = false; $plugin_files = array(); - foreach ($notices as $notice) { - if (strpos($notice, 'has been deactivated due to an error: Plugin file does not exist') !== false) { - // Extract plugin file from notice - if (preg_match('/The plugin ([^ ]+)/', $notice, $match)) { - $plugin_files[] = $match[1]; - $has_error_notice = true; + if (!empty($notices)) { + foreach ($notices as $notice) { + if (strpos($notice, 'has been deactivated due to an error: Plugin file does not exist') !== false) { + // Extract plugin file from notice + if (preg_match('/The plugin ([^ ]+)/', $notice, $match)) { + $plugin_files[] = $match[1]; + $has_error_notice = true; + } } } } // Only proceed if a relevant notice was found - if (!$has_error_notice) { + if (!$has_error_notice || empty($plugin_files)) { return; } @@ -49,8 +57,13 @@ class Plugin_Reference_Cleaner { ?>