Created
January 16, 2023 16:10
-
-
Save chrisbergr/81e67b30f699dc6f86e38a4271b2923b to your computer and use it in GitHub Desktop.
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
<?php | |
/* ... */ | |
function list_hooks( $hook = '' ) { | |
global $wp_filter; | |
if ( isset( $wp_filter[$hook]->callbacks ) ) { | |
array_walk( $wp_filter[$hook]->callbacks, function( $callbacks, $priority ) use ( &$hooks ) { | |
foreach ( $callbacks as $id => $callback ) | |
$hooks[] = array_merge( [ 'id' => $id, 'priority' => $priority ], $callback ); | |
}); | |
} else { | |
return []; | |
} | |
foreach( $hooks as &$item ) { | |
if ( !is_callable( $item['function'] ) ) continue; | |
if ( is_string( $item['function'] ) ) { | |
$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(); | |
} elseif ( is_array( $item['function'] ) ) { | |
$ref = new ReflectionClass( $item['function'][0] ); | |
$item['function'] = array( | |
is_object( $item['function'][0] ) ? get_class( $item['function'][0] ) : $item['function'][0], | |
$item['function'][1] | |
); | |
$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(); | |
} elseif ( is_callable( $item['function'] ) ) { | |
$ref = new ReflectionFunction( $item['function'] ); | |
$item['function'] = get_class( $item['function'] ); | |
$item['file'] = $ref->getFileName(); | |
$item['line'] = $ref->getStartLine(); | |
} | |
} | |
return $hooks; | |
} | |
print_r( list_hooks( 'wp_login' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment