Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. <?php
  2. /**
  3. * List callbacks registered to a given action or filter.
  4. *
  5. * <hook>
  6. * : The key for the action or filter.
  7. *
  8. * [--format=<format>]
  9. * : List callbacks as a table, JSON, or CSV. Default: table.
  10. *
  11. * EXAMPLES
  12. *
  13. * wp --require=wp-hook-command.php hook wp_enqueue_script
  14. */
  15. $hook_command = function( $args, $assoc_args ) {
  16. global $wp_filter;
  17.  
  18. $assoc_args = array_merge( array(
  19. 'format' => 'table',
  20. ), $assoc_args );
  21.  
  22. $hook = $args[0];
  23. if ( ! isset( $wp_filter[ $hook ] ) ) {
  24. WP_CLI::error( "No callbacks specified for {$hook}." );
  25. }
  26.  
  27. $callbacks_output = array();
  28. foreach( $wp_filter[ $hook ] as $priority => $callbacks ) {
  29. foreach( $callbacks as $callback ) {
  30. if ( is_array( $callback['function'] ) && is_object( $callback['function'][0] ) ) {
  31.  
  32. $class = get_class( $callback['function'][0] );
  33. $method = $callback['function'][1];
  34. $function_name = "$class->$method";
  35.  
  36. $reflection = new ReflectionClass( $class );
  37. $definition = $reflection->getFileName() . ':' . $reflection->getMethod( $method )->getStartLine();
  38.  
  39. } else {
  40.  
  41. $function_name = $callback['function'];
  42. $reflection = new ReflectionFunction( $function_name );
  43. $definition = $reflection->getFileName() . ':' . $reflection->getStartLine();
  44.  
  45. }
  46. $callbacks_output[] = array(
  47. 'function' => $function_name,
  48. 'priority' => $priority,
  49. 'accepted_args' => $callback['accepted_args'],
  50. 'definition' => preg_replace( '#^' . ABSPATH . '#', '', $definition ),
  51. );
  52. }
  53. }
  54. WP_CLI\Utils\format_items( $assoc_args['format'], $callbacks_output, array( 'function', 'priority', 'accepted_args', 'definition' ) );
  55. };
  56. WP_CLI::add_command( 'hook', $hook_command );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement