Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.61 KB | None | 0 0
  1.         function hooks($hook = '') {
  2.             global $wp_filter;
  3.  
  4.             if ( isset( $wp_filter[$hook]->callbacks ) ) {
  5.                 array_walk( $wp_filter[$hook]->callbacks, function( $callbacks, $priority ) use ( &$hooks ) {
  6.                     foreach ( $callbacks as $id => $callback )
  7.                         $hooks[] = array_merge( [ 'id' => $id, 'priority' => $priority ], $callback );
  8.                 });
  9.             } else {
  10.                 return [];
  11.             }
  12.  
  13.             foreach( $hooks as &$item ) {
  14.                 // skip if callback does not exist
  15.                 if ( !is_callable( $item['function'] ) ) continue;
  16.  
  17.                 // function name as string or static class method eg. 'Foo::Bar'
  18.                 if ( is_string( $item['function'] ) ) {
  19.                     $ref = strpos( $item['function'], '::' ) ? new ReflectionClass( strstr( $item['function'], '::', true ) ) : new ReflectionFunction( $item['function'] );
  20.                     $item['file'] = $ref->getFileName();
  21.                     $item['line'] = get_class( $ref ) == 'ReflectionFunction'
  22.                         ? $ref->getStartLine()
  23.                         : $ref->getMethod( substr( $item['function'], strpos( $item['function'], '::' ) + 2 ) )->getStartLine();
  24.  
  25.                 // array( object, method ), array( string object, method ), array( string object, string 'parent::method' )
  26.                 } elseif ( is_array( $item['function'] ) ) {
  27.  
  28.                     $ref = new ReflectionClass( $item['function'][0] );
  29.  
  30.                     // $item['function'][0] is a reference to existing object
  31.                     $item['function'] = array(
  32.                         is_object( $item['function'][0] ) ? get_class( $item['function'][0] ) : $item['function'][0],
  33.                         $item['function'][1]
  34.                     );
  35.                     $item['file'] = $ref->getFileName();
  36.                     $item['line'] = strpos( $item['function'][1], '::' )
  37.                         ? $ref->getParentClass()->getMethod( substr( $item['function'][1], strpos( $item['function'][1], '::' ) + 2 ) )->getStartLine()
  38.                         : $ref->getMethod( $item['function'][1] )->getStartLine();
  39.  
  40.                 // closures
  41.                 } elseif ( is_callable( $item['function'] ) ) {
  42.                     $ref = new ReflectionFunction( $item['function'] );
  43.                     $item['function'] = get_class( $item['function'] );
  44.                     $item['file'] = $ref->getFileName();
  45.                     $item['line'] = $ref->getStartLine();
  46.  
  47.                 }
  48.             }
  49.  
  50.             return $hooks;
  51.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement