Created
February 16, 2016 15:49
-
-
Save helgatheviking/2d9e1208a96978b2154b to your computer and use it in GitHub Desktop.
Advanced debugging of all functions attached to a specific filter
This file contains 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
/** | |
* Even fancier debug info | |
* @props @Danijel http://stackoverflow.com/a/26680808/383847 | |
* @since 1.7.7 | |
*/ | |
function list_hooks( $hook = '' ) { | |
global $wp_filter; | |
$hooks = isset( $wp_filter[$hook] ) ? $wp_filter[$hook] : array(); | |
$hooks = call_user_func_array( 'array_merge', $hooks ); | |
foreach( $hooks as &$item ) { | |
// function name as string or static class method eg. 'Foo::Bar' | |
if ( is_string( $item['function'] ) ) { | |
try { | |
$ref = strpos( $item['function'], '::' ) ? new ReflectionClass( strstr( $item['function'], '::', true ) ) : new ReflectionFunction( $item['function'] ); | |
$item['file'] = $ref->getFileName(); | |
$item['line'] = get_class( $ref ) == 'ReflectionFunction' | |
? $ref->getStartLine() | |
: $ref->getMethod( substr( $item['function'], strpos( $item['function'], '::' ) + 2 ) )->getStartLine(); | |
} catch( Exception $e ){ | |
$item['error'] = $e->getMessage(); | |
} | |
// array( object, method ), array( string object, method ), array( string object, string 'parent::method' ) | |
} elseif ( is_array( $item['function'] ) ) { | |
try { | |
$ref = new ReflectionClass( $item['function'][0] ); | |
$item['file'] = $ref->getFileName(); | |
$item['line'] = strpos( $item['function'][1], '::' ) | |
? $ref->getParentClass()->getMethod( substr( $item['function'][1], strpos( $item['function'][1], '::' ) + 2 ) )->getStartLine() | |
: $ref->getMethod( $item['function'][1] )->getStartLine(); | |
} catch( Exception $e ){ | |
$item['error'] = $e->getMessage(); | |
} | |
// closures | |
} elseif ( is_callable( $item['function'] ) ) { | |
try { | |
$ref = new ReflectionFunction( $item['function'] ); | |
$item['function'] = get_class( $item['function'] ); | |
$item['file'] = $ref->getFileName(); | |
$item['line'] = $ref->getStartLine(); | |
} catch( Exception $e ){ | |
$item['error'] = $e->getMessage(); | |
} | |
} | |
} | |
return $hooks; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment