forked from chasecmiller/Wordpress-Plugin-Debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_debug.php
More file actions
183 lines (160 loc) · 5.04 KB
/
plugin_debug.php
File metadata and controls
183 lines (160 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
Plugin Name: Plugin Debugging Tool
Description: Attempt to automatically de-activate plugins that are causing errors. This must be installed in the mu-plugins directory.
Author: Chase C. Miller
Version: 1.0
Author URI: http://crumbls.com
*/
namespace Crumbls\Debug\Plugins;
error_reporting(E_ALL);
ini_set('display_errors', '1');
class Plugin
{
protected static $errHandler = false;
protected static $errLog = [];
public function __construct()
{
}
public static function getInstance()
{
static $instance;
$class = get_called_class();
if (!$instance instanceof $class) {
$instance = new $class;
add_action('admin_notices', [get_called_class(), 'adminNotice']);
add_action('admin_enqueue_scripts', [get_called_class(), 'adminEnqueue'], 10, 1);
self::$errHandler = set_error_handler([get_called_class(), 'errorHandler']);
set_exception_handler([get_called_class(), 'errorHandler']);
error_reporting(E_ALL);
add_filter('option_active_plugins', [get_called_class(), 'filterActivePlugins'], PHP_INT_MAX, 1);
add_action('shutdown', [get_called_class(), 'wordpressShutdown'], PHP_INT_MAX);
register_shutdown_function([get_called_class(), 'systemShutdown']);
}
return $instance;
}
/**
* Tied to filter "option_active_plugins"
* @param array $plugins
* @return array
*/
public static function filterActivePlugins($plugins = [])
{
global $wp_object_cache;
if (!$dis = get_option('disabled_plugins', [])) {
return $plugins;
}
/**
* Re-enable plugins when activate is clicked again.
* This ugly and doesn't verify the user can do it, but it works for now.
*/
if (
is_admin()
&&
array_key_exists('plugin', $_REQUEST)
&&
array_key_exists('action', $_REQUEST)
&&
is_string($_REQUEST['action'])
&&
$_REQUEST['action'] == 'activate'
&&
strpos(basename($_SERVER['REQUEST_URI']), 'plugins.php') === 0
) {
$dis = array_filter($dis, function ($e) {
return strtolower($e[2]) != strtolower($_REQUEST['plugin']);
});
update_option('disabled_plugins', $dis, true);
}
// Return plugins, with disabled not activated.
$plugins = array_diff($plugins, array_column($dis, 2));
return $plugins;
}
/**
* Handle Shutdowns
* @return bool
*/
public static function wordpressShutdown()
{
// The following plugins gave a horrible error.
if (!self::$errLog) {
return true;
}
$option = get_option('disabled_plugins', []);
$option = array_column($option, null, 'f');
foreach (self::$errLog as $err) {
if (array_key_exists($err[2], $option)) {
continue;
}
$option[$err[2]] = $err;
}
update_option('disabled_plugins', $option, true);
return true;
}
/**
* Tied to register_shutdown_function
* Not currently used.
*/
public static function systemShutdown()
{
// echo __METHOD__;
}
/**
* Error handling.
*/
public static function errorHandler($n = null, $s = null, $f = null, $ln = null)
{
if (is_object($n) && get_class($n) == 'Exception') {
$s = $n->getMessage();
$f = $n->getFile();
$ln = $n->getLine();
$n = $n->getCode();
}
if (
strpos($f, WP_CONTENT_DIR) !== 0
||
strpos($f, WP_CONTENT_DIR . '/plugins/') !== 0
) {
// Send to default error handler.
$func = self::$errHandler;
print_r($func);
echo $n;
return call_user_func($func, $n, $s, $f, $ln);
}
$f = substr($f, strlen(WP_CONTENT_DIR . '/plugins/'));
self::$errLog[] = [$n, $s, $f, $ln];
return true;
}
/**
* Admin Notices
*/
public static function adminNotice()
{
if (!stripos(__FILE__, '/mu-plugins/') || true) {
?>
<div class="notice notice-warning">
<p><?php _e('Plugin Debugging Tool must be moved to mu-plugins to work.', __NAMESPACE__); ?></p>
</div>
<?php
}
}
/**
* Enqueue special CSS on our plugin page to let users know what plugins are busted.
* @param null $hook
*/
public static function adminEnqueue($hook = null)
{
if ($hook != 'plugins.php') {
return;
}
if (!$option = get_option('disabled_plugins', [])) {
return;
}
echo '<style>';
foreach ($option as $e) {
printf('.plugins tr[data-plugin="%s"] { background-color: rgba(255,0,0,0.5) !important; }', $e[2]);
}
echo '</style>';
}
}
Plugin::getInstance();