Created
February 15, 2014 14:42
-
-
Save JDGrimes/9020183 to your computer and use it in GitHub Desktop.
WordPress plugin providing some functions to debug actions and filters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Debug Hooks | |
* Author: J.D. Grimes & Some other guy on the net | |
* Version: $ver$ | |
* Description: Adds some functions you can use to debug actions and filters. | |
*/ | |
/** | |
* List hooks. | |
* | |
* @since $ver$ | |
* | |
* @param string $filter The hook to list. If omitted, all hooks will be listed. | |
*/ | |
function list_hooks( $filter = false ) { | |
global $wp_filter; | |
$hooks = $wp_filter; | |
ksort( $hooks ); | |
foreach( $hooks as $tag => $hook ) { | |
if ( false === $filter || $tag === $filter ) { | |
dump_hook( $tag, $hook ); | |
} | |
} | |
} | |
/** | |
* Log hooks. | |
* | |
* @since $ver$ | |
* | |
* @param string $filter The hook to log. If omitted, all hooks will be logged. | |
*/ | |
function log_hooks( $filter = false ) { | |
global $wp_filter; | |
$hooks = $wp_filter; | |
ksort( $hooks ); | |
foreach( $hooks as $tag => $hook ) { | |
if ( false === $filter || $tag === $filter ) { | |
log_hook( $tag, $hook ); | |
} | |
} | |
} | |
/** | |
* List hook details live. | |
* | |
* @since $ver$ | |
* | |
* @param string $hook The hook to list. If false, all hooks will be listed (default). | |
*/ | |
function list_live_hooks( $hook = false ) { | |
if ( false === $hook ) | |
$hook = 'all'; | |
add_action( $hook, 'list_hook_details', -1 ); | |
} | |
/** | |
* Log hook details live. | |
* | |
* @since $ver$ | |
* | |
* @param string $hook The hook to log. If false, all hooks will be logged (default). | |
*/ | |
function log_live_hooks( $hook = false ) { | |
if ( false === $hook ) | |
$hook = 'all'; | |
add_action( $hook, 'log_hook_details', -1 ); | |
} | |
/** | |
* List the details about a hook. | |
* | |
* @since $ver$ | |
* | |
* @param mixed $input The input passed to this hook. Passed through and ignored. | |
*/ | |
function list_hook_details( $input = null ) { | |
global $wp_filter; | |
$tag = current_filter(); | |
if ( isset( $wp_filter[ $tag ] ) ) | |
dump_hook( $tag, $wp_filter[ $tag ] ); | |
return $input; | |
} | |
/** | |
* Log the details about a hook. | |
* | |
* @since $ver$ | |
* | |
* @param mixed $input The input passed to this hook. Passed through and ignored. | |
*/ | |
function log_hook_details( $input = null ) { | |
global $wp_filter; | |
$tag = current_filter(); | |
if ( isset( $wp_filter[ $tag ] ) ) | |
log_hook( $tag, $wp_filter[ $tag ] ); | |
return $input; | |
} | |
/** | |
* Get the details about a hook. | |
* | |
* @since $ver$ | |
*/ | |
function get_hook_details( $tag, $hook ) { | |
ksort( $hook ); | |
$details = "Tag: {$tag}" . PHP_EOL; | |
foreach( $hook as $priority => $functions ) { | |
$did_priority = false; | |
foreach( $functions as $function ) { | |
if ( $function['function'] != 'list_hook_details' ) { | |
if ( ! $did_priority ) { | |
$details .= "Priority: {$priority}" . PHP_EOL; | |
$did_priority = true; | |
} | |
$details .= "\t"; | |
if ( is_string( $function['function'] ) ) { | |
$details .= $function['function']; | |
} elseif ( $function['function'] instanceof Closure ) { | |
$details .= 'Closure'; | |
} elseif ( is_string( $function['function'][0] ) ) { | |
$details .= $function['function'][0] . ' -> ' . $function['function'][1]; | |
} elseif ( is_object( $function['function'][0] ) ) { | |
$details .= "(object) " . get_class( $function['function'][0] ) . ' -> ' . $function['function'][1]; | |
} else { | |
$details .= print_r( $function, true ); | |
} | |
$details .= ' (' . $function['accepted_args'] . ')' . PHP_EOL; | |
} | |
} | |
} | |
return $details; | |
} | |
/** | |
* Log the details about a hook. | |
* | |
* @since $ver$ | |
*/ | |
function log_hook( $tag, $hook ) { | |
error_log( get_hook_details( $tag, $hook ) ); | |
} | |
/** | |
* Output a dump of information about a hook. | |
* | |
* @since $ver$ | |
* | |
* @param unknown $tag | |
* @param uknonwn $hook | |
*/ | |
function dump_hook( $tag, $hook ) { | |
echo '<pre>' . get_hook_details( $tag, $hook ) . '</pre>'; | |
} | |
/** | |
* Add a function to an action only once. | |
* | |
* @since $ver$ | |
* | |
* @param string $action The name of the action hook. | |
* @param string $function The name of the function to hook to the action. | |
* @param int $priority The priority this function should have in the hook stack. | |
* @param int $args The number of arguments to pass to the function. | |
*/ | |
function add_action_once( $action, $function, $priority, $args = 1 ) { | |
add_action( $action, $function, $prioriy, $args ); | |
add_action( | |
$action | |
, function() use ( $action, $function, $priority, $args ) { | |
remove_action( $action, $function, $priority, $args ); | |
remove_action( $action, __FUNCTION__, $priority, $args ); | |
} | |
, $priority + 1 | |
); | |
} | |
/** | |
* Add a function to a filter only once. | |
* | |
* @since $ver$ | |
* | |
* @param string $filter The name of the action hook. | |
* @param string $function The name of the function to hook to the filter. | |
* @param int $priority The priority this function should have in the hook stack. | |
* @param int $args The number of arguments to pass to the function. | |
*/ | |
function add_filter_once( $filter, $function, $priority = 10, $args = 1 ) { | |
add_filter( $filter, $function, $priority, $args ); | |
add_filter( | |
$filter | |
, function( $filtered ) use ( $filter, $function, $priority, $args ) { | |
remove_filter( $filter, $function, $priority, $args ); | |
remove_filter( $filter, __FUNCTION__, $priority, $args ); | |
return $filtered; | |
} | |
, $priority + 1 | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
typo on line 215 for $priority