-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwp-action-trace.php
107 lines (86 loc) · 3.11 KB
/
wp-action-trace.php
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
<?php
/*
Plugin Name: Wp-action-trace
Version: 1.0
Description: A simple plugin to show you exactly what actions are being called when you run WordPress.
Author: Cal Evans
Author URI: http://blog.calevans.com
Plugin URI: https://www.getpantheon.com/blog/tracing-wordpress-actions
*/
function calevans_action_trace()
{
/*
* Even though this plugin should never EVER be used in production, this is
* a safety net. You have to actually set the showTrace=1 flag in the query
* string for it to operate. If you don't it will still slow down your
* site, but it won't do anything.
*/
if (!isset($_GET['showDebugTrace']) || (bool)$_GET['showDebugTrace']!==true) {
return;
}
/*
* There are 2 other flags you can set to control what is output
*/
$showArgs = (isset($_GET['showDebugArgs'])?(bool)$_GET['showDebugArgs']:false);
$showTime = (isset($_GET['showDebugTime'])?(bool)$_GET['showDebugTime']:false);
/*
* This is the main array we are using to hold the list of actions
*/
static $actions = [];
/*
* Some actions are not going to be of interet to you. Add them into this
* array to exclude them. Remove the two default if you want to see them.
*/
$excludeActions = ['gettext','gettext_with_context'];
$thisAction = current_filter();
$thisArguments = func_get_args();
if (!in_array( $thisAction, $excludeActions )) {
$actions[] = ['action' => $thisAction,
'time' => microtime(true),
'arguments' => print_r($thisArguments,true)];
}
/*
* Shutdown is the last action, process the list.
*/
if ($thisAction==='shutdown') {
calevans_format_debug_output($actions,$showArgs,$showTime);
}
return;
}
function calevans_format_debug_output($actions=[],$showArgs=false, $showTime=false)
{
/*
* Let's do a little formatting here.
* The class "debug" is so you can control the look and feel
*/
echo '<pre class="debug">';
foreach($actions as $thisAction) {
echo "Action Name : ";
/*
* if you want the timings, let's make sure everything is padded out properly.
*/
if ($showTime) {
$timeParts = explode('.',$thisAction['time']);
echo '(' . $timeParts[0] . '.' . str_pad($timeParts[1],4,'0') . ') ';
}
echo $thisAction['action'] . PHP_EOL;
/*
* If you've requested the arguments, let's display them.
*/
if ($showArgs && count($thisAction['arguments'])>0) {
echo "Args:" . PHP_EOL . print_r($thisAction['arguments'],true);
echo PHP_EOL;
}
}
echo '</pre>';
return;
}
/*
* Hook it into WordPress.
* all = add this to every action.
* calevans_action_trace = the name of the function above to call
* 99999 = the priority. This is the lowest priority action in the list.
* 99 = the number of parameters that this method can accept. Honestly, if you have a action approaching this many parameter, you really are doing sometheing wrong.
*
*/
add_action( 'all', 'calevans_action_trace', 99999, 99 );