Skip to content

Instantly share code, notes, and snippets.

@ve3
Last active December 24, 2023 13:33
Show Gist options
  • Select an option

  • Save ve3/96c50c671e06dde475f6e3c80492f7e5 to your computer and use it in GitHub Desktop.

Select an option

Save ve3/96c50c671e06dde475f6e3c80492f7e5 to your computer and use it in GitHub Desktop.

Revisions

  1. ve3 revised this gist Dec 24, 2023. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions wp-hooks-trace.php
    Original file line number Diff line number Diff line change
    @@ -82,10 +82,10 @@ function rdht_traceHooks()
    $contents .= 'End actions. ================================' . "\n";
    $contents .= "\n";

    global $wp_filter;
    global $wp_filters;
    $contents .= '#FILTERS ====================================' . "\n";
    if (is_iterable($wp_filter)) {
    foreach ($wp_filter as $name => $items) {
    if (is_iterable($wp_filters)) {
    foreach ($wp_filters as $name => $items) {
    $contents .= $name . "\n";
    }
    unset($items, $name);
  2. ve3 revised this gist Dec 24, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions wp-hooks-trace.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,9 @@
    /**
    * Plugin Name: WP hooks trace
    * Description: Enable trace > browse or work > disable trace. And everything will be written into this plugin folder /hooks-trace.txt file.
    * License: MIT
    */
    // You can use this plugin for any purpose without attribution required.


    add_filter('plugin_row_meta', 'rdht_pluginRowMeta', 10, 4);
  3. ve3 created this gist Dec 24, 2023.
    99 changes: 99 additions & 0 deletions wp-hooks-trace.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    <?php
    /**
    * Plugin Name: WP hooks trace
    * Description: Enable trace > browse or work > disable trace. And everything will be written into this plugin folder /hooks-trace.txt file.
    */


    add_filter('plugin_row_meta', 'rdht_pluginRowMeta', 10, 4);
    function rdht_pluginRowMeta(array $plugin_meta, string $plugin_file, array $plugin_data, string $status)
    {
    $thisFileName = plugin_basename(__FILE__);

    if ($plugin_file === $thisFileName) {
    $traceHooksOptions = get_transient('rdht_tracehooks');
    if ($traceHooksOptions === '1') {
    // if marked as trace hooks.
    $plugin_meta[] = '<a href="?rdht_tracehooks=false">Disable trace hooks</a>';
    } else {
    $plugin_meta[] = '<a href="?rdht_tracehooks=true">Enable trace hooks</a>';
    }
    unset($traceHooksOptions);
    }

    unset($thisFileName);
    return $plugin_meta;
    }// rdht_pluginRowMeta


    add_action('init', 'rdht_updateTraceOption');
    function rdht_updateTraceOption(string $plugin)
    {
    if (isset($_REQUEST['rdht_tracehooks']) && $_REQUEST['rdht_tracehooks'] === 'true') {
    set_transient('rdht_tracehooks', '1', time() + (2 * DAY_IN_SECONDS));
    } elseif (isset($_REQUEST['rdht_tracehooks']) && $_REQUEST['rdht_tracehooks'] === 'false') {
    delete_transient('rdht_tracehooks');
    delete_transient('rdht_tracehooks_started');
    }

    if (isset($_REQUEST['rdht_tracehooks'])) {
    $url = remove_query_arg('rdht_tracehooks');
    wp_redirect($url);
    exit();
    }
    }


    add_action('shutdown', 'rdht_traceHooks');
    function rdht_traceHooks()
    {
    $traceHooksOptions = get_transient('rdht_tracehooks');
    if ($traceHooksOptions !== '1') {
    return ;
    }
    unset($traceHooksOptions);

    if (stripos($_SERVER['REQUEST_URI'], 'plugins.php') !== false && stripos($_SERVER['REQUEST_URI'], 'rdht_tracehooks=') !== false) {
    return ;
    }

    $logFile = __DIR__ . DIRECTORY_SEPARATOR . 'hooks-trace.txt';
    $isStarted = get_transient('rdht_tracehooks_started');
    if ($isStarted !== '1') {
    if (is_file($logFile)) {
    unlink($logFile);
    }
    }
    unset($isStarted);

    $contents = '';

    global $wp_actions;
    $contents .= 'page: ' . ($_SERVER['REQUEST_URI'] ?? 'UNKNOWN') . ' method: ' . ($_SERVER['REQUEST_METHOD'] ?? 'GET') . "\n";
    $contents .= '#ACTIONS (hits) =============================' . "\n";
    if (is_iterable($wp_actions)) {
    foreach ($wp_actions as $name => $hits) {
    $contents .= $name . ' (' . $hits . ')' . "\n";
    }
    unset($hits, $name);
    }
    $contents .= 'End actions. ================================' . "\n";
    $contents .= "\n";

    global $wp_filter;
    $contents .= '#FILTERS ====================================' . "\n";
    if (is_iterable($wp_filter)) {
    foreach ($wp_filter as $name => $items) {
    $contents .= $name . "\n";
    }
    unset($items, $name);
    }
    $contents .= 'End filters. ================================' . "\n";
    $contents .= "\n";

    // end of all record (new line);
    $contents .= "\n";
    file_put_contents($logFile, $contents, FILE_APPEND);

    set_transient('rdht_tracehooks_started', '1', time() + (2 * DAY_IN_SECONDS));
    }