Last active
January 2, 2016 00:19
-
-
Save thisislawatts/8222352 to your computer and use it in GitHub Desktop.
Shows hooked functions for actions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Debug Bar Actions and Filters Addon | |
* Plugin URI: http://wordpress.org/extend/plugins/debug-bar-actions-and-filters-addon/ | |
* Description: This plugin add two more tabs in the Debug Bar to display hooks(Actions and Filters) attached to the current request. Actions tab displays the actions hooked to current request. Filters tab displays the filter tags along with the functions attached to it with priority. | |
* Version: 1.4.1 | |
* Author: Subharanjan | |
* Author Email: [email protected] | |
* Author URI: http://www.subharanjan.in/ | |
* License: GPLv2 | |
* | |
* @author subharanjan | |
* @package debug-bar-actions-and-filters-addon | |
* @version 1.4.1 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
/** | |
* Function to hook with debug_bar_panels filter. | |
* | |
* @param array $panels list of all the panels in debug bar. | |
* | |
* @return array $panels modified panels list | |
*/ | |
if ( !function_exists( 'debug_bar_action_and_filters_addon_panel' ) ) { | |
function debug_bar_action_and_filters_addon_panel( $panels ) { | |
require_once( plugin_dir_path( __FILE__ ) . 'class-debug-bar-action-and-filters-addon.php' ); | |
$wp_actions = new Debug_Bar_Actions_Addon_Panel(); | |
$wp_actions->set_tab( 'Action Hooks', 'debug_bar_action_and_filters_addon_display_actions' ); | |
$panels[] = $wp_actions; | |
$wp_filters = new Debug_Bar_Filters_Addon_Panel(); | |
$wp_filters->set_tab( 'Filter Hooks', 'debug_bar_action_and_filters_addon_display_filters' ); | |
$panels[] = $wp_filters; | |
return $panels; | |
} | |
} | |
add_filter( 'debug_bar_panels', 'debug_bar_action_and_filters_addon_panel' ); | |
/** | |
* Function to display the Actions attached to current request. | |
* | |
* @return string $output display output for the actions panel | |
*/ | |
function debug_bar_action_and_filters_addon_display_actions() { | |
global $wp_actions; | |
$output = ''; | |
$output .= '<div class="hooks_listing_container">' . "\n"; | |
$output .= '<h3>List of Action Hooks</h3><br />' . "\n"; | |
$output .= "<ol>\n"; | |
foreach ( $wp_actions as $action_key => $action_val ) { | |
$output .= '<li>' . $action_key . debug_bar_action_and_filters_addon_display_hooked_functions( $action_key ) . "</li>\n"; | |
} | |
$output .= '<li><strong>Total Count: </strong>' . count( $wp_actions ) . "</li>\n"; | |
$output .= "</ol>\n"; | |
$output .= "</div>\n"; | |
return $output; | |
} | |
/** | |
* | |
* @return string $output display list of hooks | |
*/ | |
function debug_bar_action_and_filters_addon_display_hooked_functions( $tag = false ) { | |
global $wp_filter; | |
if ( !$tag ) return; | |
$hook = isset( $wp_filter[$tag] ) ? $wp_filter[$tag] : false; | |
if ( !is_array( $hook ) ) { | |
return; | |
} | |
$output = '<ol>'; | |
ksort($hook); | |
foreach ( $hook as $priority => $functions ) { | |
$output .= '<li> Priority: ' . $priority; | |
if ( is_array( $functions ) ) { | |
$output .= '<ul>'; | |
foreach ( $functions as $single_function ) { | |
$output .= debug_bar_action_and_filters_addon_print_function( $single_function ); | |
} | |
$output .= '</ul>'; | |
} | |
$output .= '</li>'; | |
} | |
$output .= '</ol>'; | |
return $output; | |
} | |
/** | |
* Function to to check for closures | |
* | |
* @param mixed $arg function name | |
* | |
* @return boolean $closurecheck return whether or not a closure | |
*/ | |
function dbafa_is_closure( $arg ) { | |
if( version_compare( PHP_VERSION, '5.3', '<' ) ) { | |
return false; | |
} | |
include_once( plugin_dir_path( __FILE__ ) . 'php5.3-closure-test.php' ); | |
return debug_bar_action_and_filters_addon_is_closure( $arg ); | |
} | |
/** | |
* Function to display the Filters applied to current request. | |
* | |
* @return string $output display output for the filters panel | |
*/ | |
function debug_bar_action_and_filters_addon_display_filters() { | |
global $wp_filter, $wp_actions; | |
$output = ''; | |
$output .= '<div class="hooks_listing_container">' . "\n"; | |
$output .= '<h3>List of Filter Hooks</h3><br />' . "\n"; | |
$output .= "<ol>\n"; | |
$actions = array_keys( $wp_actions ); | |
foreach ( $wp_filter as $filter_key => $filter_val ) { | |
if ( !in_array( $filter_key, $actions ) ) | |
$output .= '<li>' . $filter_key . debug_bar_action_and_filters_addon_display_hooked_functions( $filter_key ) . "</li>\n"; | |
} | |
$output .= "</ol>\n"; | |
$output .= "</div>\n"; | |
return $output; | |
} | |
/** | |
* Helper function to convert any number of function configurations into a string | |
* | |
* @param mixed $function [description] | |
* @return string $output | |
*/ | |
function debug_bar_action_and_filters_addon_print_function( $function ) { | |
$output = ''; | |
if ( ( !is_string( $function['function'] ) && !is_object( $function['function'] ) ) && ( !is_array( $function['function'] ) || ( is_array( $function['function'] ) && ( !is_string( $function['function'][0] ) && !is_object( $function['function'][0] ) ) ) ) ) { | |
// Type 1 - not a callback | |
continue; | |
} | |
elseif ( dbafa_is_closure( $function['function'] ) ) { | |
// Type 2 - closure | |
$output .= '<li>[<em>closure</em>]</li>'; | |
} | |
elseif ( ( is_array( $function['function'] ) || is_object( $function['function'] ) ) && dbafa_is_closure( $function['function'][0] ) ) { | |
// Type 3 - closure within an array | |
$output .= '<li>[<em>closure</em>]</li>'; | |
} | |
elseif ( is_string( $function['function'] ) && strpos( $function['function'], '::' ) === false ) { | |
// Type 4 - simple string function (includes lambda's) | |
$output .= '<li>' . sanitize_text_field( $function['function'] ) . '</li>'; | |
} | |
elseif ( is_string( $function['function'] ) && strpos( $function['function'], '::' ) !== false ) { | |
// Type 5 - static class method calls - string | |
$output .= '<li>[<em>class</em>] ' . str_replace( '::', ' :: ', sanitize_text_field( $function['function'] ) ) . '</li>'; | |
} | |
elseif ( is_array( $function['function'] ) && ( is_string( $function['function'][0] ) && is_string( $function['function'][1] ) ) ) { | |
// Type 6 - static class method calls - array | |
$output .= '<li>[<em>class</em>] ' . sanitize_text_field( $function['function'][0] ) . ' :: ' . sanitize_text_field( $function['function'][1] ) . '</li>'; | |
} | |
elseif ( is_array( $function['function'] ) && ( is_object( $function['function'][0] ) && is_string( $function['function'][1] ) ) ) { | |
// Type 7 - object method calls | |
$output .= '<li>[<em>object</em>] ' . get_class( $function['function'][0] ) . ' -> ' . sanitize_text_field( $function['function'][1] ) . '</li>'; | |
} | |
else { | |
// Type 8 - undetermined | |
$output .= '<li><pre>' . var_export( $function, true ) . '</pre></li>'; | |
} | |
return $output; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.hooks_listing_container h2 { | |
width:100%; | |
} | |
.hooks_listing_container h3 { | |
float: none; | |
clear: both; | |
font-family: georgia,times,serif; | |
font-size: 22px; | |
margin: 15px 10px 0 0!important; | |
} | |
.hooks_listing_container ul { | |
list-style-type: circle; | |
padding-left: 20px !important; | |
} | |
.hooks_listing_container ol { | |
list-style-type: square; | |
padding-left: 20px !important; | |
} | |
.hooks_listing_container ul li em { | |
color: #999999; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment