From 7b6b2b63edb0e4e3f2c03810e5eab5ea266833f9 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 12 Mar 2015 19:44:41 +0000 Subject: [PATCH] Initial commit --- README.md | 4 + wp-hooks-search.php | 242 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 README.md create mode 100644 wp-hooks-search.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..f1eaac8 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +WordPress Hooks Search Tool +=========================== + + diff --git a/wp-hooks-search.php b/wp-hooks-search.php new file mode 100644 index 0000000..ed0569c --- /dev/null +++ b/wp-hooks-search.php @@ -0,0 +1,242 @@ + 1, 'html' => '

The directory/file does not exist

' ) ); + die(); + } + + $start_time = microtime(TRUE); + $location = ( ! empty( $_POST['location'] ) ? $_POST['location'] : '.' ); + $search_hook = ( isset( $_POST['hookname'] ) && ! empty( $_POST['hookname'] ) ) ? $_POST['hookname'] : '*'; + + //Get a list for files with a .php extension + $wp_files = ( is_dir( $location ) ? find_all_files( $location ) : array( $location ) ); + $all_hooks = Array(); + $output = ' + + + + + + '; + $all_filters = ""; + $all_actions = ""; + foreach( $wp_files as $wp_file ) + { + $filters = ""; + $actions = ""; + + $code = file_get_contents( $wp_file ); + + //Search for actions and filters in the PHP code + preg_match_all( "/(apply_filters|do_action|do_action_ref_array)\s*\(\s*[\'\"](.+?)\s*[,\)]/", $code, $hooks ); + $hooks[2] = str_replace( "'", "", $hooks[2] ); + $hooks[2] = str_replace( '"', "", $hooks[2] ); + if( !empty( $hooks[2] ) ) + { + foreach ( array_unique( $hooks[2] ) as $key => $hook ) + { + if( ( "*" == $search_hook && ( !in_array( $hook, $all_hooks ) || isset( $_POST['duplicates'] ) ) ) || ( FALSE !== strstr( $hook, $search_hook ) && ( !in_array( $hook, $all_hooks ) || isset( $_POST['duplicates'] ) ) ) ) + { + //Segregate Actions and Filters separately + if ( "apply_filters" == $hooks[1][ $key ] ) + $filters .= "\n\t\t\t
  • " . str_replace( $search_hook, ''.$search_hook.'', $hook ) . "
  • "; + elseif ( "do_action" == $hooks[1][ $key ] || "do_action_ref_array" == $hooks[1][ $key ] ) + $actions .= "\n\t\t\t
  • " . str_replace( $search_hook, ''.$search_hook.'', $hook ) . "
  • "; + } + } + + + if ( ! empty( $filters ) || ! empty( $actions ) ) + { + $output.= "\n\t + + + + + + + + "; + } + $all_filters .= $filters; + $all_actions .= $actions; + $add_hooks[] = $hooks[2]; + $all_hooks = array_2d_to_1d( $add_hooks ); + } + } + $output .= "\n
    FiltersActions
    " . $wp_file . "
      " . $filters . "\n\t\t
    \n\t
      " . $actions . "\n\t\t
    \n\t
    \n"; + $end_time = microtime(TRUE); + + //Display the number of Actions and Filters + $output = '

    List of WordPress hooks in ' .$location . '

    +

    Actions: ' . ( count( explode( '

  • ', $all_actions ) ) - 1 ) . ' +
    Filters: ' . ( count( explode( '
  • ', $all_filters ) ) - 1 ) . ' +
    Scan Time: ' . round( ($end_time-$start_time), 5 ) . ' seconds

    ' . $output; + echo json_encode( array( 'status' => 0, 'html' => $output ) ); + die(); + +endif; + +//Function to get a list of .php files +function find_all_files( $dir ) +{ + $root = scandir( $dir ); + foreach( $root as $value ) + { + if( $value !== '.' && $value !== '..' ) + { + if( is_file( "$dir/$value" ) && preg_match( '/\.php$/', $value ) ) + $result[] = "$dir/$value"; + + if( is_dir( "$dir/$value" ) ) + { + foreach( find_all_files("$dir/$value") as $value ) + { + $result[] = $value; + } + } + } + } + return ( isset( $result ) ? $result : Array() ); +} + +//Convert a 2 dimensional array to a single dimension +function array_2d_to_1d( $input_array ) +{ + $output_array = array(); + + for ($i = 0; $i < count($input_array); $i++) + for ($j = 0; $j < count($input_array[$i]); $j++) + $output_array[] = $input_array[$i][$j]; + + return $output_array; +} +?> + + + +WordPress Hooks Search Tool + + + + + + + +
    +
    +

    WordPress Hooks Search Tool

    +

    This tool scans all the PHP files in a directory you specify and lists out the Actions (from do_action) and Filters (from apply_filters) defined in these files. To search all files and subdirectories leave the Search in field empty. You may also specify a keyword to search for in the hook name.

    +
    +
    + + +

    Eg. wp-content/plugins

    +
    +
    + + +
    +
    + +

    If unchecked only the first occurrence of a hook is displayed.
    Eg. The the_permalink filter is present in both comment-template.php and link-template.php files, unchecking this box displays only in first one.

    +
    + +
    +
    +
    +
    + + + +