public
Last active

Instruments Hooks for a Page of WordPress. Outputs during the Shutdown Hook after add get-params: ?instrument=hooks

  • Download Gist
wp-hooks-filter.php
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 108 109 110 111 112
<?php
/*
Plugin Name: Instrument Hooks for WordPress
Plugin URI: http://bueltge.de/
Description: Instruments Hooks for a Page. Outputs during the Shutdown Hook after add get-params <code>?instrument=hooks</code>.
Version: 0.0.1
Author: Frank B&uuml;ltge
Author URI: http://bueltge.de/
*/
 
if ( isset( $_GET['instrument'] ) && $_GET['instrument'] == 'hooks' ) {
 
function instrument_hooks() {
global $wpdb;
$hooks = $wpdb->get_results("SELECT * FROM wp_hook_list ORDER BY first_call");
$html = array();
$html[] = '<style>#instrumented-hook-list table,#instrumented-hook-list th,#instrumented-hook-list td {border:1px solid gray;padding:2px 5px;}</style>
<div align="center" id="instrumented-hook-list">
<table>
<tr>
<th>First Call</th>
<th>Hook Name</th>
<th>Hook Type</th>
<th>Arg Count</th>
<th>Called By</th>
<th>Line #</th>
<th>File Name</th>
</tr>';
foreach($hooks as $hook) {
$html[] = "<tr>
<td>{$hook->first_call}</td>
<td>{$hook->hook_name}</td>
<td>{$hook->hook_type}</td>
<td>{$hook->arg_count}</td>
<td>{$hook->called_by}</td>
<td>{$hook->line_num}</td>
<td>{$hook->file_name}</td>
</tr>";
}
$html[] = '</table></div>';
echo implode("\n", $html);
}
function record_hook_usage($hook){
global $wpdb;
static $in_hook = FALSE;
static $first_call = 1;
static $doc_root;
$callstack = debug_backtrace();
if ( !$in_hook ) {
$in_hook = TRUE;
if ($first_call == 1) {
$doc_root = $_SERVER['DOCUMENT_ROOT'];
$results = $wpdb->get_results("SHOW TABLE STATUS LIKE 'wp_hook_list'");
if ( count($results) == 1 ) {
$wpdb->query("TRUNCATE TABLE wp_hook_list");
} else {
$wpdb->query(
"CREATE TABLE wp_hook_list (
called_by varchar(96) NOT NULL,
hook_name varchar(96) NOT NULL,
hook_type varchar(15) NOT NULL,
first_call int(11) NOT NULL,
arg_count tinyint(4) NOT NULL,
file_name varchar(128) NOT NULL,
line_num smallint NOT NULL,
PRIMARY KEY (first_call,hook_name))"
);
}
}
$args = func_get_args();
$arg_count = count($args) -1;
$hook_type = str_replace( 'do_', '',
str_replace( 'apply_filters',
'filter',
str_replace( '_ref_array', '[]', $callstack[3]['function'] )
)
);
$file_name = addslashes( str_replace( $doc_root, '', $callstack[3]['file'] ) );
$line_num = $callstack[3]['line'];
if ( isset($callstack[4]['function']) )
$called_by = $callstack[4]['function'];
else
$called_by = '';
$wpdb->query("
INSERT wp_hook_list
( first_call, called_by, hook_name, hook_type, arg_count, file_name, line_num )
VALUES ( $first_call, '$called_by()', '$hook', '$hook_type', $arg_count, '$file_name', $line_num)
");
$first_call++;
$in_hook = FALSE;
}
}
add_action('shutdown', 'instrument_hooks');
add_action('all', 'record_hook_usage');
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.