Whippet::wps_filter_all PHP Method

wps_filter_all() public method

Emits the details of hooks and filters as they are called, if required
public wps_filter_all ( $hook )
    public function wps_filter_all($hook)
    {
        global $wp_filter;
        //
        // Check whether this hook should be displayed
        //
        $display = false;
        foreach ($this->options['show-hooks'] as $show) {
            if (preg_match("/^{$show}\$/", $hook)) {
                $display = true;
                break;
            }
        }
        if (!$display) {
            return;
        }
        //
        // If this hook has no callbacks, just bail
        //
        if (empty($wp_filter[$hook]) || !count($wp_filter[$hook])) {
            return;
        }
        //
        // Find the callbacks
        //
        $callback_messages = array();
        $all_wp_core = true;
        $hooks = $wp_filter[$hook];
        // TODO: this erroneously reorders hooks with the same priority
        ksort($hooks, SORT_NUMERIC);
        foreach ($hooks as $priority => $callbacks) {
            foreach ($callbacks as $callback) {
                if (is_array($callback['function'])) {
                    $function = $callback['function'][1];
                } else {
                    $function = $callback['function'];
                }
                // Skip Whippet callbacks
                if (preg_match('/^wps_/', $function)) {
                    continue;
                }
                $callback_message = "\t{$priority}: " . Colours::fg('cyan') . $function . Colours::off();
                $callback_data = $this->cb_cache->lookup($function);
                if (!$callback_data) {
                    // Find the function
                    $file = exec("grep -rn 'function {$function}' {$this->options['wp-root']}/*");
                    if (empty($file) && isset($this->options['wp-content'])) {
                        $file = exec("grep -rn 'function {$function}' {$this->options['wp-content']}/*");
                    }
                    // If we got it, add an entry to the cache
                    if (!empty($file) && preg_match('/^([^:]+):(\\d+):/', $file, $matches)) {
                        $this->cb_cache->add($function, $matches[1], $matches[2]);
                        $callback_data = $this->cb_cache->lookup($function);
                    }
                }
                if ($callback_data) {
                    // Is this a callback outside the WP core?
                    if (preg_match('/wp-content/', $callback_data['file'])) {
                        $all_wp_core = false;
                    }
                    // Make paths relative
                    $callback_data['file'] = str_replace($this->options['wp-root'], '', $callback_data['file']);
                    if (!empty($this->options['wp-content'])) {
                        $callback_data['file'] = str_replace($this->options['wp-content'], '', $callback_data['file']);
                    }
                    $callback_message .= " in " . Colours::fg("brown") . str_replace($this->options['wp-root'] . "/wp-content/", '', $callback_data['file']) . Colours::off() . " at line {$callback_data['line']}";
                } else {
                    $callback_message .= " (couldn't find this function's definition)";
                }
                $callback_messages[] = $callback_message;
            }
        }
        //
        // If we're not showing WP core hooks, and all these callbacks are from the core, bail
        // If there are no callbacks (probably because a whippet callback was skipped), bail
        //
        if (!isset($this->options['show-wp-hooks']) && $all_wp_core) {
            return;
        }
        if (!count($callback_messages)) {
            return;
        }
        //
        // Find the caller
        //
        $type = '';
        $caller = '';
        $backtrace = debug_backtrace();
        foreach ($backtrace as $i => $value) {
            if ($value['function'] == 'apply_filters' || $value['function'] == 'do_action' || $value['function'] == 'apply_filters_ref_array' || $value['function'] == 'do_action_ref_array') {
                $caller = $backtrace[$i + 1];
                if ($value['function'] == 'apply_filters') {
                    $type = "Filter";
                } else {
                    $type = "Action";
                }
                break;
            }
        }
        //
        // Put together the message
        //
        $message = Colours::fg('bold_cyan') . "Hook triggered: " . Colours::off() . "{$type} " . Colours::fg('cyan') . "{$hook}" . Colours::off() . " called from function " . Colours::fg('cyan') . "{$caller['function']}";
        if (!empty($caller['file'])) {
            $message .= Colours::off() . " in " . Colours::fg('brown') . str_replace($this->options['wp-root'], '', $caller['file']);
        }
        if (!empty($caller['line'])) {
            $message .= Colours::off() . " at line {$caller['line']}";
        }
        $this->message("{$message}" . Colours::off());
        foreach ($callback_messages as $callback_message) {
            $this->message($callback_message);
        }
    }