Skip to content

Instantly share code, notes, and snippets.

@mfields
Created August 20, 2012 00:06
Show Gist options
  • Save mfields/3398704 to your computer and use it in GitHub Desktop.
Save mfields/3398704 to your computer and use it in GitHub Desktop.
<?php
final class MfieldsDumpHook {
/**
* A list of hooks to dump
*/
private static $hooks = null;
/**
* Create an instance.
*
* Add the hook name to the static list.
* Register the shutdown callback when the
* first instance is created.
*
* @param string $hook_name The name of a hook. Required.
*
* @uses MfieldsDumpHook::$hooks
* @uses MfieldsDumpHook::dump_all()
*/
public function __construct( $hook_name ) {
if ( is_null( self::$hooks ) )
self::$hooks = array();
self::$hooks[] = $hook_name;
if ( ! has_filter( 'shutdown', array( __class__, 'dump_all' ) ) )
add_action( 'shutdown', array( __class__, 'dump_all' ) );
}
/**
* Dump all hooks and and assigned functions.
*
* Ensure that duplicate and empty hook names
* do not get dumped.
*
* @uses MfieldsDumpHook::$hooks
* @uses MfieldsDumpHook::dump()
*/
public static function dump_all() {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
$hooks = array_unique( self::$hooks );
$hooks = array_filter( $hooks );
if ( empty( $hooks ) )
return;
foreach ( $hooks as $hook ) {
self::dump( $hook );
}
}
/**
* Print a table containg the information for a single hook.
*
* @param string $hook Name of an action or filter.
*
* @uses MfieldsDumpHook::css()
*/
public static function dump( $hook ) {
global $wp_filter;
if ( ! isset( $wp_filter[$hook] ) ) {
print '<p style="color:red;">No functions are attached to <code>' . esc_html( $hook ) . '</code>.</p>';
return;
}
self::css();
ksort( $wp_filter[$hook] );
echo '<div id="mfields-hook-dump">';
echo '<table>';
echo '<caption><code>' . esc_html( $hook ) . '</code></caption>';
echo '<thead><tr><th class="priority">Priority</th><th>Hooked Functions</th></tr></thead>';
echo '<tbody>';
foreach ( (array) $wp_filter[$hook] as $priority => $functions ) {
echo "\n" . '<tr>';
echo "\n\t" . '<th class="priority">' . esc_html( $priority ) . '</th>';
echo "\n\t" . '<td>';
echo "\n\t" . '<ul>';
foreach ( $functions as $slug => $function ) {
$function = wp_parse_args( $function, array(
'function' => '',
'accepted_args' => '',
) );
$function_name = '';
if ( is_array( $function['function'] ) ) {
if ( is_string( $function['function'][0] ) )
$function_name = $function['function'][0] . '::' . $function['function'][1];
else if ( is_object( $function['function'][0] ) )
$function_name = get_class( $function['function'][0] ) . '::'. $function['function'][1];
}
else if ( is_string( $function['function'] ) ) {
$function_name = $function['function'];
}
echo "\n\t" . '<li><code>' . esc_html( $function_name . '()' ) . '</code></li>';
}
echo "\n\t" . '</ul>';
echo "\n\t" . '</td>';
echo "\n\n" . '</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
}
/**
* Styles for the table rendered by MfieldsDumpHook::dump()
*
* This style block will be printed inline. This is not
* the best practice. Since this class is not intended
* to be used in production, I have made an exception.
*/
function css() {
echo '
<style>
#mfields-hook-dump {
padding: 0 1em;
font-family: Arial, sans-serif;
font-size: 14px;
max-width: 600px;
min-width: 500px;
margin: 2em auto;
}
#mfields-hook-dump table {
color: #555;
border: 1px solid #bbb !important;
border-collapse: collapse;
box-shadow: 2px 2px 10px rgba( 0, 0, 0, .3 );
}
#mfields-hook-dump code {
font-size: 14px;
}
#mfields-hook-dump code {
color: #333;
background: transparent;
}
#mfields-hook-dump ul {
margin: 0;
padding: 0 0 0 1em;
list-style-type: square;
list-style-position: inside;
}
#mfields-hook-dump table caption {
background: #eee;
border: 1px solid #ccc;
border-bottom: none;
border-top-right-radius: 5px;
border-top-left-radius: 5px;
font-size: 14px;
padding: .4em .2em;
text-align: center;
min-width: 3em;
max-width: 10em;
width: auto;
}
#mfields-hook-dump table caption code {
font-size: 14px;
}
#mfields-hook-dump table th,
#mfields-hook-dump table td {
border: 1px solid #bbb;
padding: .5em;
}
#mfields-hook-dump th.priority {
text-align: center !important;
width: 5em;
}
#mfields-hook-dump thead th {
background: #7a7a7a;
background-image:-moz-linear-gradient( center bottom , #6D6D6D, #808080 );
color: #fff;
padding: .5em 1.5em;
}
#mfields-hook-dump tbody th {
background: #eaf2fa;
color: #7a7a7a;
text-align: center;
vertical-align: top;
}
#mfields-hook-dump tbody td,
#mfields-hook-dump tbody td code {
background: #fff;
color: #555;
}
</style>
';
}
}
@mfields
Copy link
Author

mfields commented Aug 20, 2012

Called like this:

/**
 * Dump all functions assigned to a hook.
 *
 * You may specify any number of hooks to dump by
 * passing their names as parameters.
 *
 * One hook ...... Mfields::dump_hook( 'the_content' );
 * Two hooks ..... Mfields::dump_hook( 'the_content', 'the_excerpt' );
 *
 * If no parameters are passed, an E_USER_NOTICE
 * will be triggered.
 *
 * @uses MfieldsDumpHook
 * @uses Mfields::queue_error()
 */
public static function dump_hook() {
    $args = func_get_args();

    if ( empty( $args ) ) {
        self::queue_error( 'No parameters were passed to ' . __method__ . '()' );
        return;
    }

    foreach( $args as $hook_name ) {
        $obj = new MfieldsDumpHook( $hook_name );
    }
}

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