From f200ff6f969a14b436617fa427f297ad35e5fc40 Mon Sep 17 00:00:00 2001 From: Marcus Quinn <6428977+marcusquinn@users.noreply.github.com> Date: Wed, 9 Apr 2025 23:46:06 +0100 Subject: [PATCH] Fix timeout issue during plugin activation (v1.2.2) --- CHANGELOG.md | 9 +++++++++ README.md | 9 +++++++-- plugin-reference-cleaner.php | 37 +++++++++++++++++++++++------------- readme.txt | 9 +++++++-- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3de7f95..b87afa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ All notable changes to this project will be documented in this file. +## [1.2.2] - 2023-10-05 +### Fixed +- Timeout issue during plugin activation +- Potential infinite recursion in admin notices handling + +### Improved +- Hook management to prevent performance issues +- Optimized by only loading on plugins page + ## [1.2.1] - 2025-04-07 ### Improved - Fixed typos in documentation diff --git a/README.md b/README.md index 89b1f1b..32bfaa9 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Plugin Reference Cleaner Author: Marcus Quinn -Author URI: https://wpallstars.com -Version: 1.2.1 +Author URI: https://www.wpallstars.com +Version: 1.2.2 License: GPL-2.0+ ## Description @@ -47,6 +47,11 @@ If you don't have this notification perpetually showing on your /wp-admin/plugin ## Changelog +### 1.2.2 +* Fixed timeout issue during plugin activation +* Improved hook management to prevent potential infinite recursion +* Optimized performance by only loading on plugins page + ### 1.2.1 * Fixed typos in documentation * Improved text clarity diff --git a/plugin-reference-cleaner.php b/plugin-reference-cleaner.php index 4e8cebc..06670e1 100644 --- a/plugin-reference-cleaner.php +++ b/plugin-reference-cleaner.php @@ -2,9 +2,9 @@ /* * Plugin Name: Plugin Reference Cleaner * Description: Adds a "Remove Reference" button to plugin deactivation error notices, allowing users to clean up invalid plugin entries. - * Version: 1.2.1 + * Version: 1.2.2 * Author: Marcus Quinn - * Author URI: https://wpallstars.com + * Author URI: https://www.wpallstars.com * License: GPL-2.0+ */ @@ -15,22 +15,21 @@ if (!defined('ABSPATH')) { 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 + // Only hook into admin actions when on the plugins page + add_action('current_screen', function($screen) { + if (isset($screen->id) && ($screen->id === 'plugins' || $screen->id === 'plugins-network')) { + // 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); + } + }); + // Handle the AJAX request to remove the plugin reference add_action('wp_ajax_remove_plugin_reference', array($this, 'remove_plugin_reference')); } // Inject "Remove Reference" button only if a relevant notice exists public function inject_remove_button() { - global $pagenow; - - // Only run on plugins.php or network admin plugins page - if (!in_array($pagenow, array('plugins.php', 'plugins.php'))) { - return; - } - // Check if a "Plugin file does not exist" notice exists $notices = $this->get_admin_notices(); $has_error_notice = false; @@ -47,7 +46,7 @@ class Plugin_Reference_Cleaner { } } } - + // Only proceed if a relevant notice was found if (!$has_error_notice || empty($plugin_files)) { return; @@ -117,11 +116,23 @@ class Plugin_Reference_Cleaner { // Helper function to capture admin notices private function get_admin_notices() { + // Static flag to prevent infinite recursion + static $is_capturing = false; + + // If already capturing, return empty to break potential loops + if ($is_capturing) { + return array(); + } + + $is_capturing = true; + ob_start(); do_action('admin_notices'); do_action('network_admin_notices'); $output = ob_get_clean(); + $is_capturing = false; + if (empty($output)) { return array(); } diff --git a/readme.txt b/readme.txt index e44074a..d1ea76f 100644 --- a/readme.txt +++ b/readme.txt @@ -1,7 +1,7 @@ === Plugin Reference Cleaner === Author: Marcus Quinn -Author URI: https://wpallstars.com -Version: 1.2.1 +Author URI: https://www.wpallstars.com +Version: 1.2.2 License: GPL-2.0+ == Description == @@ -47,6 +47,11 @@ If you don't have this notification perpetually showing on your /wp-admin/plugin == Changelog == += 1.2.2 = +* Fixed timeout issue during plugin activation +* Improved hook management to prevent potential infinite recursion +* Optimized performance by only loading on plugins page + = 1.2.1 = * Fixed typos in documentation * Improved text clarity