Skip to content

Commit

Permalink
init implementation of johnbillion#405 (third option)
Browse files Browse the repository at this point in the history
  • Loading branch information
crstauf committed Feb 19, 2019
1 parent 28e5e22 commit 96f1452
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
3 changes: 2 additions & 1 deletion assets/query-monitor.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
46 changes: 46 additions & 0 deletions collectors/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
67 changes: 67 additions & 0 deletions output/html/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -178,6 +193,58 @@ public static function output_hook_table( array $hooks ) {

}

protected function after_tabular_output() {
echo '</table>';
echo '</div>';

$this->output_discovered();
}

function output_discovered() {
printf(
'<div class="qm qm-discovered" id="%1$s" role="group" aria-labelledby="%1$s" tabindex="-1">',
esc_attr( $this->current_id . '-discovered_hooks' )
);

echo '<div><table>';

printf(
'<caption><h2 id="%1$s-caption">%2$s</h2></caption>',
esc_attr( $this->current_id . '-discovered_hooks' ),
esc_html__( 'Discovered Hooks', 'query-monitor' )
);

echo '<thead>';
echo '<tr>';
echo '<th scope="col">' . esc_html__( 'Label', 'query-monitor' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Hook', 'query-monitor' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Successive Uses', 'query-monitor' ) . '</th>';
echo '</tr>';
echo '</thead>';

echo '<tbody>';

$data = $this->collector->get_data();

foreach ( $data['discovered_hooks'] as $label => $hooks ) {
foreach ( $hooks as $i => $hook ) {
echo '<tr>';

if ( 0 === $i )
echo '<th scope="row" rowspan="' . esc_attr( count( $hooks ) ) . '" class="qm-nowrap"><span class="qm-sticky">' . esc_html( $label ) . '</span></th>';

echo '<td><code>' . esc_html( $hook['action'] ) . '</code></td>';
echo '<td class="qm-num">' . esc_html( $hook['count'] ) . '</td>';
echo '</tr>';
}
}

echo '</tbody>';
echo '</table></div>';

echo '</div>';
}

}

function register_qm_output_html_hooks( array $output, QM_Collectors $collectors ) {
Expand Down

0 comments on commit 96f1452

Please sign in to comment.