Generate dormant hook references

There is no script or plugin that I know of to do what you want. As you have stated, there are scripts (even global variables) which you can use to print filters and actions currently being used.

As for dormant filters and actions, I have written two very basic functions (with some help here and there) which finds all apply_filters and do_action instances in a file and then prints it out

BASICS

  • We will use the RecursiveDirectoryIterator,RecursiveIteratorIterator and RegexIterator PHP classes to get all the PHP files within a directory. As example, on my localhost, I have used E:\xammp\htdocs\wordpress\wp-includes

  • We will then loop through the files, and search and return (preg_match_all) all instances of apply_filters and do_action. I have set it up to match nested instances of parenthesis and also to match possible whitespaces between apply_filters/do_action and the first parenthesis

We will simple then create an array with all filters and actions and then loop through the array and output the file name and filters and actions. We will skip files without filters/actions

IMPORTANT NOTES

  • This functions are very expensive. Run them only on a local test installation.

  • Modify the functions as needed. You can decide to write the output to a file, create a special backend page for that, the options are unlimited

OPTION 1

The first options function is very simple, we will return the contents of a file as a string using file_get_contents, search for the apply_filters/do_action instances and simply output the filename and filter/action names

I have commented the code for easy following

function get_all_filters_and_actions( $path="" )
{
    //Check if we have a path, if not, return false
    if ( !$path ) 
        return false;

    // Validate and sanitize path
    $path = filter_var( $path, FILTER_SANITIZE_URL );
    /**
     * If valiadtion fails, return false
     *
     * You can add an error message of something here to tell
     * the user that the URL validation failed
     */
    if ( !$path ) 
        return false;

    // Get each php file from the directory or URL  
    $dir   = new RecursiveDirectoryIterator( $path );
    $flat  = new RecursiveIteratorIterator( $dir );
    $files = new RegexIterator( $flat, '/\.php$/i' );

    if ( $files ) {

        $output="";
        foreach($files as $name=>$file) {
            /**
             * Match and return all instances of apply_filters(**) or do_action(**)
             * The regex will match the following
             * - Any depth of nesting of parentheses, so apply_filters( 'filter_name', parameter( 1,2 ) ) will be matched
             * - Whitespaces that might exist between apply_filters or do_action and the first parentheses
             */
            // Use file_get_contents to get contents of the php file
            $get_file_content =  file_get_contents( $file );
            // Use htmlspecialchars() to avoid HTML in filters from rendering in page
            $save_content = htmlspecialchars( $get_file_content );
            preg_match_all( '/(apply_filters|do_action)\s*(\([^()]*(?:(?-1)[^()]*)*+\))/', $save_content, $matches );

            // Build an array to hold the file name as key and apply_filters/do_action values as value
            if ( $matches[0] )
                $array[$name] = $matches[0];
        }
        foreach ( $array as $file_name=>$value ) {

            $output .= '<ul>';
                $output .= '<strong>File Path: ' . $file_name .'</strong></br>';
                $output .= 'The following filters and/or actions are available';
                foreach ( $value as $k=>$v ) {
                    $output .= '<li>' . $v . '</li>';
                }
            $output .= '</ul>';
        }
        return $output;
    }

    return false;
}

You can use at follow on a template, frontend or backend

echo get_all_filters_and_actions( 'E:\xammp\htdocs\wordpress\wp-includes' );

This will print

enter image description here

OPTION 2

This option is a bit more expensive to run. This function returns the line number where the filter/action can be found.

Here we use file to explode the file into an array, then we search and return the filter/action and the line number

function get_all_filters_and_actions2( $path="" )
{
    //Check if we have a path, if not, return false
    if ( !$path ) 
        return false;

    // Validate and sanitize path
    $path = filter_var( $path, FILTER_SANITIZE_URL );
    /**
     * If valiadtion fails, return false
     *
     * You can add an error message of something here to tell
     * the user that the URL validation failed
     */
    if ( !$path ) 
        return false;

    // Get each php file from the directory or URL  
    $dir   = new RecursiveDirectoryIterator( $path );
    $flat  = new RecursiveIteratorIterator( $dir );
    $files = new RegexIterator( $flat, '/\.php$/i' );

    if ( $files ) {

        $output="";
        $array  = [];
        foreach($files as $name=>$file) {
            /**
             * Match and return all instances of apply_filters(**) or do_action(**)
             * The regex will match the following
             * - Any depth of nesting of parentheses, so apply_filters( 'filter_name', parameter( 1,2 ) ) will be matched
             * - Whitespaces that might exist between apply_filters or do_action and the first parentheses
             */
            // Use file_get_contents to get contents of the php file
            $get_file_contents =  file( $file );
            foreach ( $get_file_contents as  $key=>$get_file_content ) {
                preg_match_all( '/(apply_filters|do_action)\s*(\([^()]*(?:(?-1)[^()]*)*+\))/', $get_file_content, $matches );

                if ( $matches[0] )
                    $array[$name][$key+1] = $matches[0];
            }
        }

        if ( $array ) {
            foreach ( $array as $file_name=>$values ) {
                $output .= '<ul>';
                    $output .= '<strong>File Path: ' . $file_name .'</strong></br>';
                    $output .= 'The following filters and/or actions are available';

                    foreach ( $values as $line_number=>$string ) {
                        $whitespaces="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                        $output .= '<li>Line reference ' . $line_number . $whitespaces . $string[0] . '</li>';
                    }
                $output .= '</ul>';
            }
        }
        return $output;

    }

    return false;
}

You can use at follow on a template, frontend or backend

echo get_all_filters_and_actions2( 'E:\xammp\htdocs\wordpress\wp-includes' );

This will print

enter image description here

EDIT

This is basically as much as I can do without the scripts timing out or running out of memory. With the code in option 2, it is as easy as going to the said file and said line in the source code and then get all the valid parameter values of the filter/action, also, importantly, get the function and further context in which the filter/action is used

Leave a Comment