Last active
October 10, 2025 19:51
-
-
Save afragen/a09b52047bf1d7f11b622941678824cc to your computer and use it in GitHub Desktop.
Error logging during Git Updater webhook
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 | |
| /** | |
| * Error logging. | |
| * | |
| * To log different sets of hooks the following constant | |
| * ERROR_LOGGING_HOOKS will need to be set. | |
| * | |
| * ERROR_LOGGING_HOOKS accepts an array of any combination of the following elements. | |
| * ( 'wp_error', 'upgrade_hooks' AND/OR 'filesystem_hooks' ) | |
| * eg, `define(__NAMESPACE__ . '\ERROR_LOGGING_HOOKS', [ 'filesystem_hooks' ] );` | |
| * | |
| * @package Fragen\Error_Logging | |
| * | |
| * Plugin Name: Error Logging | |
| * Plugin URI: https://gist.github.com/afragen/a09b52047bf1d7f11b622941678824cc | |
| * Description: Error logging to debug.log of WP_Error and during specific sets of filter hooks. | |
| * Version: 2.1.0 | |
| * Author: Andy Fragen | |
| * License: MIT | |
| * Requires at least: 5.9 | |
| * Requires PHP: 7.4 | |
| * Gist Plugin URI: https://gist.github.com/afragen/a09b52047bf1d7f11b622941678824cc | |
| */ | |
| namespace Fragen\Error_Logging; | |
| use ReflectionClass; | |
| /* | |
| * To log different sets of hooks the following constant | |
| * ERROR_LOGGING_HOOKS will need to be set. | |
| * | |
| * ERROR_LOGGING_HOOKS accepts an array of any combination of the following elements. | |
| * ( 'wp_error', 'upgrade_hooks' AND/OR 'filesystem_hooks' ) | |
| * eg, `define(__NAMESPACE__ . '\ERROR_LOGGING_HOOKS', [ 'filesystem_hooks' ] );` | |
| */ | |
| define(__NAMESPACE__ . '\ERROR_LOGGING_HOOKS', [ 'wp_error', 'upgrade_hooks', 'filesystem_hooks' ] ); | |
| //define(__NAMESPACE__ . '\ERROR_LOGGING_HOOKS', ['wp_error']); | |
| /** | |
| * Sets hooks to log data. | |
| * | |
| * @return void | |
| */ | |
| function init($hook_sets) { | |
| $hooks = array(); | |
| if (defined(__NAMESPACE__ . '\ERROR_LOGGING_HOOKS')) { | |
| foreach (ERROR_LOGGING_HOOKS as $set) { | |
| if (isset($hook_sets[$set])) { | |
| foreach ($hook_sets[$set] as $hook => $args) { | |
| $hooks[$hook] = $args; | |
| } | |
| } | |
| } | |
| } | |
| foreach ($hooks as $hook => $args) { | |
| add_filter($hook, __NAMESPACE__ . '\log_filter', $args['priority'], $args['accepted_args']); | |
| } | |
| } | |
| /** | |
| * Log hook data to debug.log. | |
| * | |
| * @param array ...$args Hook args. | |
| * | |
| * @return mixed | |
| */ | |
| function log_filter(...$args) { | |
| $hook = current_filter(); | |
| // Exit for QM/FAIR. | |
| if ($args[0] === 'http_request_not_executed' && str_contains($args[1], 'filter on pre_http_request')) { | |
| return; | |
| } | |
| error_log(var_export($hook, true)); | |
| foreach ($args as $arg) { | |
| $arg = is_object($arg) ? serialize($arg) : $arg; | |
| error_log(var_export($arg, true)); | |
| } | |
| error_log("\r\n"); | |
| return is_action($hook) ? null : $args[0]; | |
| } | |
| /** | |
| * Return value of 'doing_action' for hook. | |
| * | |
| * @global array $wp_filters Array of called hooks. | |
| * @param string $filter Name of hook. | |
| * | |
| * @return bool | |
| */ | |
| function is_action($hook) { | |
| global $wp_filter; | |
| $object = $wp_filter[$hook]; | |
| $reflection = new ReflectionClass($object); | |
| $property = $reflection->getProperty('doing_action'); | |
| $property->setAccessible(true); | |
| return $property->getValue($object); | |
| } | |
| $hook_sets = array( | |
| 'wp_error' => array( | |
| 'wp_error_added' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 4, | |
| ), | |
| ), | |
| 'filesystem_hooks' => array( | |
| 'filesystem_method' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 4, | |
| ), | |
| ), | |
| 'upgrade_hooks' => array( | |
| 'upgrader_package_options' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 1, | |
| ), | |
| 'upgrader_pre_download' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 4, | |
| ), | |
| 'upgrader_pre_install' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 2, | |
| ), | |
| 'upgrader_source_selection' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 4, | |
| ), | |
| 'upgrader_clear_destination' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 4, | |
| ), | |
| 'upgrader_post_install' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 3, | |
| ), | |
| 'upgrader_install_package_result' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 3, | |
| ), | |
| 'unzip_file_use_ziparchive' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 1, | |
| ), | |
| 'pre_unzip_file' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 5, | |
| ), | |
| 'unzip_file' => array( | |
| 'priority' => 10, | |
| 'accepted_args' => 1, | |
| ), | |
| ), | |
| ); | |
| init($hook_sets); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment