diff --git a/assets/query-monitor.css b/assets/query-monitor.css index 1af641606..6dfc5072d 100644 --- a/assets/query-monitor.css +++ b/assets/query-monitor.css @@ -539,7 +539,8 @@ body.admin-color-light #wp-admin-bar-query-monitor:not(.qm-all-clear):not(:hover font-size: 14px !important; margin: 20px !important; } -#query-monitor-main .qm-concerns table { +#query-monitor-main .qm-concerns table, +#query-monitor-main .qm-discovered table { border-top: 1px solid #e0e0e0 !important; margin-bottom: 20px !important; } diff --git a/collectors/hooks.php b/collectors/hooks.php index 99f37a649..e07cb317d 100644 --- a/collectors/hooks.php +++ b/collectors/hooks.php @@ -14,6 +14,52 @@ public function name() { return __( 'Hooks & Actions', 'query-monitor' ); } + public function __construct() { + parent::__construct(); + add_action( 'qm/listen/start', array( $this, 'action_function_listen_start' ), 10, 1 ); + add_action( 'qm/listen/stop', array( $this, 'action_function_listen_stop' ), 10, 1 ); + } + + public function action_function_listen_start( $label ) { + if ( !array_key_exists( 'discovered_hooks', $this->data ) ) + $this->data['discovered_hooks'] = array(); + + if ( array_key_exists( $label, $this->data['discovered_hooks'] ) ) + return; + + $this->data['discovered_hooks'][$label] = array(); + + add_action( 'all', array( $this, 'action_function_listen_all' ), 0, 1 ); + } + + public function action_function_listen_all( $var ) { + if ( in_array( current_action(), array( + 'qm/listen/start', + 'qm/listen/stop', + ) ) ) + return; + + end( $this->data['discovered_hooks'] ); + $label = key( $this->data['discovered_hooks'] ); + $last = end( $this->data['discovered_hooks'][$label] ); + + if ( current_action() === $last['action'] ) { + $i = key( $this->data['discovered_hooks'][$label] ); + $this->data['discovered_hooks'][$label][$i]['count']++; + } else { + $this->data['discovered_hooks'][$label][] = array( + 'action' => current_action(), + 'count' => 1, + ); + } + + return $var; + } + + public function action_function_listen_stop( $label ) { + remove_action( 'all', array( $this, 'action_function_listen_all' ), 0, 1 ); + } + public function process() { global $wp_actions, $wp_filter; diff --git a/output/html/hooks.php b/output/html/hooks.php index 9bda68c06..749dc5c4f 100644 --- a/output/html/hooks.php +++ b/output/html/hooks.php @@ -11,9 +11,24 @@ class QM_Output_Html_Hooks extends QM_Output_Html { public function __construct( QM_Collector $collector ) { parent::__construct( $collector ); + add_filter( 'qm/output/panel_menus', array( $this, 'panel_menus' ) ); add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 80 ); } + public function panel_menus( $panel_menu ) { + $data = $this->collector->get_data(); + + if ( empty( $data['discovered_hooks'] ) ) + return; + + $panel_menu[ 'qm-' . $this->id ]['children'][ 'qm-' . $this->id . '-discovered_hooks' ] = array( + 'href' => esc_attr( '#' . $this->collector->id() . '-discovered_hooks' ), + 'title' => '└ ' . __( 'Discovered Hooks', 'query-monitor' ), + ); + + return $panel_menu; + } + public function output() { $data = $this->collector->get_data(); @@ -178,6 +193,58 @@ public static function output_hook_table( array $hooks ) { } + protected function after_tabular_output() { + echo ''; + echo ''; + + $this->output_discovered(); + } + + function output_discovered() { + printf( + '
', + esc_attr( $this->current_id . '-discovered_hooks' ) + ); + + echo '
'; + + printf( + '', + esc_attr( $this->current_id . '-discovered_hooks' ), + esc_html__( 'Discovered Hooks', 'query-monitor' ) + ); + + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + + echo ''; + + $data = $this->collector->get_data(); + + foreach ( $data['discovered_hooks'] as $label => $hooks ) { + foreach ( $hooks as $i => $hook ) { + echo ''; + + if ( 0 === $i ) + echo ''; + + echo ''; + echo ''; + echo ''; + } + } + + echo ''; + echo '

%2$s

' . esc_html__( 'Label', 'query-monitor' ) . '' . esc_html__( 'Hook', 'query-monitor' ) . '' . esc_html__( 'Successive Uses', 'query-monitor' ) . '
' . esc_html( $label ) . '' . esc_html( $hook['action'] ) . '' . esc_html( $hook['count'] ) . '
'; + + echo '
'; + } + } function register_qm_output_html_hooks( array $output, QM_Collectors $collectors ) {