Skip to content

Instantly share code, notes, and snippets.

@ruscon
Last active March 10, 2024 12:24
Show Gist options
  • Save ruscon/2cf45a34e68e83d9f86408b2d2dde665 to your computer and use it in GitHub Desktop.
Save ruscon/2cf45a34e68e83d9f86408b2d2dde665 to your computer and use it in GitHub Desktop.
wordpress debug hooks
<?php
// function.php
// using: ?showDebugTrace=1&showDebugTime=1&minExecutionTime=0.2
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);
$minExecutionTime = isset($_GET['minExecutionTime']) ? (float)$_GET['minExecutionTime'] : 0.01;
/*
* This is the main array we are using to hold the list of actions
*/
static $actions = [];
static $lastActionTime = null;
/*
* 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 )) {
$time = microtime(true);
$executionTime = 0;
if ($lastActionTime) {
$executionTime = number_format($time - $lastActionTime, 10);
$executionTime = $executionTime > 0 ? $executionTime : 0;
$lastActionTime = $time;
}
else {
$lastActionTime = microtime(true);
}
// var_dump($executionTime, $lastActionTime);
//var_dump($executionTime, $minExecutionTime);die;
if ($executionTime > $minExecutionTime) {
// var_dump($executionTime > $minExecutionTime);die;
$actions[] = [
'action' => $thisAction,
'time' => $time,
'execution_time' => $executionTime,
'arguments' => print_r($thisArguments,true)
];
}
}
/*
* Shutdown is the last action, process the list.
*/
if ($thisAction==='shutdown') {
// $times = array();
// foreach ($actions as $key => $row) {
// $times[$key] = $row['execution_time'];
// }
// array_multisort($times, SORT_DESC, $actions);
calevans_format_debug_output($actions,$showArgs,$showTime);
}
return;
}
function calevans_format_debug_output($actions=[],$showArgs=false, $showTime=false)
{
array_multisort(array_column($actions, 'execution_time'), SORT_DESC, $actions);
/*
* 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']);
$timeParts2 = explode('.',$thisAction['execution_time']);
// echo '(' . $timeParts[0] . '.' . str_pad($timeParts[1],4,'0') . ') ';
echo '(' . $timeParts2[0] . '.' . str_pad($timeParts2[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 );
@webworld-home
Copy link

using: ?showDebugTrace=1&showDebugTime=1&minExecutionTime=0.2

@ruscon
Copy link
Author

ruscon commented Jun 7, 2017

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment