Skip to content

Commit

Permalink
cleanup pge hook list
Browse files Browse the repository at this point in the history
  • Loading branch information
bueltge committed Jan 21, 2017
1 parent 2e1e10c commit eb7ec7a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 61 deletions.
4 changes: 2 additions & 2 deletions inc/class-classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct() {

// Filter classes from this plugin
if ( 1 === (int) $options[ 'filter' ] ) {
add_filter( 'debug_objects_declared_classes', array( $this, 'remove_debug_objects_classes' ) );
add_filter( 'debug_objects_declared_classes', array( $this, 'filter_debug_objects_classes' ) );
}
}

Expand Down Expand Up @@ -79,7 +79,7 @@ public function get_conditional_tab( $tabs ) {
*
* @return array
*/
public function remove_debug_objects_classes( array $classes ) {
public function filter_debug_objects_classes( array $classes ) {

foreach ( $classes as $count => $class ) {
if ( 0 === strpos( $class, 'Debug_Objects' ) ) {
Expand Down
142 changes: 83 additions & 59 deletions inc/class-page_hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ class Debug_Objects_Page_Hooks {

protected static $classobj;

public $filters_storage = array();
public $filter_storage = array();

// define strings for important hooks to easier identify
public $my_important_hooks = array();
/**
* Store options.
*
* @var array
*/
private $options;

/**
* Handler for the action 'init'. Instantiates this class.
Expand Down Expand Up @@ -64,6 +70,8 @@ public function __construct() {
*/
public function get_conditional_tab( $tabs ) {

$this->get_options();

$tabs[] = array(
'tab' => esc_attr__( 'Page Hooks', 'debug_objects' ),
'function' => array( $this, 'get_hooks' )
Expand All @@ -72,37 +80,37 @@ public function get_conditional_tab( $tabs ) {
return $tabs;
}

/**
* Store tag of filter hooks in var.
*
* @param string $tag
*/
public function store_fired_filters( $tag ) {

global $wp_filter;

if ( ! isset( $wp_filter[ $tag ] ) ) {
return NULL;
return;
}

$hooked = (array) $wp_filter[ $tag ];

// Usable since WP 3.9
$fired = '';
if ( function_exists( 'doing_filter' ) ) {
$fired = 'Fired: FALSE, ';
if ( doing_filter( $tag ) ) {
$fired = 'Fired: TRUE, ';
}

// Don't translatable the string, to heavy for the hook 'all'.
$fired = 'FALSE';
if ( doing_filter( $tag ) ) {
$fired = 'TRUE';
}

ksort( $hooked );

foreach ( $hooked as $priority => $function ) {

//prevent buffer overflows of PHP_INT_MAX on array keys
//so reset the array keys
// Prevent buffer overflows of PHP_INT_MAX on array keys.
// So reset the array keys.
$hooked = array_values( $hooked );
$hooked[] = $function;
}

$this->filters_storage[] = array(
$this->filter_storage[] = array(
'tag' => $tag,
'hooked' => $wp_filter[ $tag ],
'fired' => $fired
Expand All @@ -117,21 +125,21 @@ public function get_hooks() {
global $wp_actions;

// Use this hook for remove Action Hook, like custom action hooks
$wp_actions = apply_filters( 'debug_objects_wp_actions', $wp_actions );
$wp_actions = (array) apply_filters( 'debug_objects_wp_actions', $wp_actions );

$callbacks = array();
$hooks = array();
$filter_hooks = '';
$filter_callbacks = '';

// Use this hook for remove Filter Hook from the completely array, like custom filter hooks
$filters_storage = apply_filters( 'debug_objects_wp_filters', $this->filters_storage );
// Use this hook for remove Filter Hook from the completely array, like custom filter hooks.
$filters_storage = (array) apply_filters( 'debug_objects_wp_filters', $this->filter_storage );

foreach ( (array) $filters_storage as $index => $the_ ) {
foreach ( $filters_storage as $index => $the_ ) {

// Use this hook for remove Filter Hook, like custom filter hooks
// Use this hook for remove Filter Hook, like custom filter hooks.
$filter_hook = apply_filters( 'debug_objects_filter_tag', array() );
// Filter the Filter Hooks
// Filter the Filter Hooks.
if ( in_array( $the_[ 'tag' ], $filter_hook, TRUE ) ) {
break;
}
Expand All @@ -147,17 +155,16 @@ public function get_hooks() {

if ( is_array( $functions ) ) {
foreach ( (array) $functions as $function ) {
//pre_print( $function );

if ( is_string( $function[ 'function' ] ) ) {
// as array
$hook_callbacks[] = array(
'name' => $function[ 'function' ],
'args' => $function[ 'accepted_args' ],
'priority' => $priority
'priority' => $id
);
// readable
$filter_callbacks = "{$the_['fired']}Function: {$function['function']}(), Arguments: {$function['accepted_args']}, Priority: {$priority}";
$filter_callbacks = "Fired: <code>{$the_['fired']}</code>, Function: <code>{$function['function']}()</code>, Arguments: <code>{$function['accepted_args']}</code>, Priority: <code>{$id}</code>";
}
}
}
Expand All @@ -168,75 +175,59 @@ public function get_hooks() {
$callbacks[ $the_[ 'tag' ] ][] = $filter_callbacks;
}

// Format important hooks, that you easier identifier this hooks
$this->my_important_hooks = apply_filters(
'debug_objects_important_hooks',
array( 'admin_print_', 'admin_head-', 'admin_footer-', 'add_meta_boxes' )
);

$output = '';
$output .= '<table>';

$output .= '<tr class="nohover">';
$output .= "\t" . '<th>Total Action Hooks: ' . count( $wp_actions ) . '</th>';
//$output .= '<th>Total Filter Hooks: ' . count( $hooks ) . '</th>';
$output .= "\t" . '<th>Total Filter Hooks & Callback: ' . count( $callbacks ) . '</th>';
$output .= '</tr>';

$output .= '<tr class="nohover">';

$output .= "\t" . '<td><table class="tablesorter">';

$count_fired = '';
// Usable since WP 3.9
if ( function_exists( 'did_action' ) ) {
$count_fired = '<th>Count Fired</th>';
}
$output .= "\t" . '<thead><tr><th>Fired in order</th><th>Action Hook</th>' . $count_fired . '</tr></thead>';

$order = 1;
$count_fired = '<th>' . esc_attr__( 'Count Fired', 'debug_objects' ) . '</th>';
$output .= "\t" . '<thead><tr><th>' . esc_attr__( 'Fired in order', 'debug_objects' )
. '</th><th>' . esc_attr__( 'Action Hook', 'debug_objects' ) . '</th>'
. $count_fired . '</tr></thead>';

foreach ( (array) $wp_actions as $key => $val ) {
// Format, if the key is inside the important list of hooks
foreach ( (array) $this->my_important_hooks as $hook ) {
// Filter hooks from this plugin.
$wp_actions = $this->filter_debug_objects_hooks( $wp_actions );

if ( FALSE !== strpos( $key, $hook ) ) {
$key = '<span>' . $key . ' </span>';
}
}

// Usable since WP 3.9
if ( function_exists( 'did_action' ) ) {
$count_fired = (int) did_action( $key );
} else {
$count_fired = (int) $val;
}
$order = 1;
foreach ( $wp_actions as $key => $val ) {

$output .= '<tr><td>' . $order . '.</td><td><code>' . $key . '</code></td><td>' . $count_fired . '</td></tr>';
$count_fired = (int) did_action( $key );
$output .= '<tr><td>' . $order . '.</td><td><code>'
. $key . '</code></td><td>' . $count_fired . '</td></tr>';
$order ++;
}
$output .= '</table>';
$output .= '</td>';

$output .= "\t" . '<td>';
$output .= "\t\t" . '<table class="tablesorter">';
$output .= "\t" . '<thead><tr><th>Fired in order</th><th>Filter Hook & Callback</th></tr></thead>';
$output .= "\t" . '<thead><tr><th>' . esc_attr__( 'Fired in order', 'debug_objects' ) . '</th><th>'
. esc_attr__( 'Filter Hook & Callback', 'debug_objects' ) . '</th></tr></thead>';

// Filter hooks from this plugin.
$callbacks = $this->filter_debug_objects_hooks( $callbacks );

$order = 1;
foreach ( $callbacks as $hook => $values ) {

// remove duplicate items
$values = array_unique( $values );
foreach ( $values as $key => $value ) {
$escape = htmlspecialchars( $value, ENT_QUOTES, 'utf-8', FALSE );

if ( empty( $escape ) ) {
$escape = __( 'Empty' );
if ( empty( $value ) ) {
$value = esc_attr__( 'Empty', 'debug_objects' );
}

$output .= '<tr>';
$output .= "\t" . '<td>' . $order . '.</td><td>' . __( 'Hook:' )
. ' <code>' . $hook . '</code><br> ' . $escape . '</td>';
$output .= "\t" . '<td>' . $order . '.</td><td>' . esc_attr__( 'Hook:', 'debug_objects' )
. ' <code>' . $hook . '</code><br> ' . $value . '</td>';
$output .= '</tr>';
}

Expand All @@ -250,4 +241,37 @@ public function get_hooks() {

echo $output;
}

/**
* Store options of the plugin.
*/
private function get_options() {

$this->options = Debug_Objects_Settings::return_options();
}
/**
* Filter hooks to remove the hooks from this plugin.
*
* @param array
*
* @return array
*/
public function filter_debug_objects_hooks( array $data ) {

if ( ! isset( $this->options[ 'filter' ] ) ) {
return $data;
}

if ( 1 !== (int) $this->options[ 'filter' ] ) {
return $data;
}

foreach ( $data as $hook => $class ) {
if ( 0 === strpos( strtolower( $hook ), 'debug_objects' ) ) {
unset( $data[ $hook ] );
}
}

return $data;
}
} // end class

0 comments on commit eb7ec7a

Please sign in to comment.