Last active
February 12, 2025 13:56
-
-
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. | |
* ( 'upgrade_hooks' AND/OR 'filesystem_hooks' ) | |
* eg, `define( '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: 1.0.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; | |
/** | |
* Class Error_Logging. | |
*/ | |
class Error_Logging { | |
/** @var array */ | |
private $hooks; | |
/** | |
* Constructor. | |
* | |
* @param array $hooks Array of hooks to process. | |
*/ | |
public function __construct( $hooks ) { | |
$this->hooks = $hooks; | |
} | |
/** | |
* Sets hooks to log data. | |
* | |
* @return void | |
*/ | |
public function init() { | |
foreach ( $this->hooks as $hook => $args ) { | |
add_filter( $hook, array( $this, 'log_filter' ), $args['priority'], $args['accepted_args'] ); | |
} | |
} | |
/** | |
* Log hook data to debug.log. | |
* | |
* @param array ...$args Hook args. | |
* | |
* @return mixed | |
*/ | |
public static function log_filter( ...$args ) { | |
$hook = current_filter(); | |
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 self::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 | |
*/ | |
private static 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, | |
), | |
), | |
); | |
/* | |
* 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. | |
* ( 'upgrade_hooks' AND/OR 'filesystem_hooks' ) | |
* eg, `define( 'ERROR_LOGGING_HOOKS', [ 'filesystem_hooks' ] );` | |
*/ | |
$hooks = $hook_sets['wp_error']; | |
if ( defined( 'ERROR_LOGGING_HOOKS' ) ) { | |
foreach ( ERROR_LOGGING_HOOKS as $set ) { | |
if ( isset( $hook_sets[ $set ] ) ) { | |
foreach ( $hook_sets[ $set ] as $hook => $args ) { | |
$hooks[ $hook ] = $args; | |
} | |
} | |
} | |
} | |
( new Error_Logging( $hooks ) )->init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment