135 lines
4.1 KiB
PHP
135 lines
4.1 KiB
PHP
<?php
|
|
defined('ABSPATH') or die;
|
|
|
|
/**
|
|
* Centralized logging and admin notices.
|
|
*/
|
|
class Logger {
|
|
static $source = "plugin";
|
|
static $clear_text = "Dismiss";
|
|
static $namespace = "logger";
|
|
|
|
// Prevents hooks from being registered multiple times
|
|
private static $initialized = false;
|
|
|
|
const MAX_ERRORS = 5;
|
|
|
|
public static function init($source, $clear_text, $namespace) {
|
|
self::$source = $source;
|
|
self::$clear_text = $clear_text;
|
|
self::$namespace = $namespace;
|
|
|
|
if ( ! self::$initialized ) {
|
|
add_action( 'admin_notices', [ self::class, 'render_admin_notice' ] );
|
|
add_action( 'admin_init', [ self::class, 'handle_admin_notice_dismiss' ] );
|
|
self::$initialized = true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Log an error and optionally raise an admin notice.
|
|
*/
|
|
public static function log($message, array $context = [], $raise_notice = true) {
|
|
if ( $raise_notice ) {
|
|
$errors = get_option( self::$namespace . '_errors', [] );
|
|
if ( ! is_array( $errors ) ) {
|
|
$errors = [];
|
|
}
|
|
|
|
$errors[] = [
|
|
'message' => $message,
|
|
'context' => $context,
|
|
'time' => wp_date( 'Y-m-d H:i:s' )
|
|
];
|
|
|
|
if ( count( $errors ) > self::MAX_ERRORS ) {
|
|
$errors = array_slice( $errors, -self::MAX_ERRORS );
|
|
}
|
|
|
|
update_option( self::$namespace . '_errors', $errors, false );
|
|
}
|
|
|
|
$context_str = '';
|
|
if ( ! empty($context) ) {
|
|
$context_str = ' | ' . wp_json_encode($context);
|
|
}
|
|
error_log('['. self::$namespace .'] ' . $message . $context_str);
|
|
}
|
|
|
|
/**
|
|
* Show latest errors in the admin UI.
|
|
*/
|
|
public static function render_admin_notice() {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
return;
|
|
}
|
|
|
|
$errors = get_option( self::$namespace . '_errors' );
|
|
|
|
if ( empty( $errors ) || ! is_array( $errors ) ) {
|
|
return;
|
|
}
|
|
|
|
$nonce_action = self::$namespace . '_clear_error';
|
|
$nonce_name = self::$namespace . '_error_nonce';
|
|
|
|
$dismiss_url = wp_nonce_url(
|
|
add_query_arg( $nonce_action, '1' ),
|
|
$nonce_action,
|
|
$nonce_name
|
|
);
|
|
|
|
echo '<div class="notice notice-error">';
|
|
printf( '<p><strong>%s:</strong></p>', esc_html( self::$source ) );
|
|
echo '<ul style="list-style-type: disc; margin-left: 20px;">';
|
|
|
|
foreach ( $errors as $error ) {
|
|
$message = esc_html( $error['message'] ?? 'Unknown error' );
|
|
$time = ! empty( $error['time'] ) ? esc_html( $error['time'] ) : '';
|
|
$context = '';
|
|
|
|
if ( ! empty( $error['context'] ) ) {
|
|
$context = '<pre style="white-space:pre-wrap;word-break:break-word;margin-top:4px;background:rgba(0,0,0,0.05);padding:8px;">' .
|
|
esc_html( wp_json_encode( $error['context'], JSON_PARTIAL_OUTPUT_ON_ERROR ) ) .
|
|
'</pre>';
|
|
}
|
|
|
|
printf(
|
|
'<li>%s%s%s</li>',
|
|
$message,
|
|
$time ? ' <em>(' . $time . ')</em>' : '',
|
|
$context
|
|
);
|
|
}
|
|
|
|
echo '</ul>';
|
|
printf(
|
|
'<p><a href="%s" class="button">%s</a></p>',
|
|
esc_url( $dismiss_url ),
|
|
esc_html( self::$clear_text )
|
|
);
|
|
echo '</div>';
|
|
}
|
|
|
|
/**
|
|
* Clear error notice when user dismisses via link.
|
|
*/
|
|
public static function handle_admin_notice_dismiss() {
|
|
$nonce_action = self::$namespace . '_clear_error';
|
|
$nonce_name = self::$namespace . '_error_nonce';
|
|
|
|
if ( ! isset( $_GET[$nonce_action], $_GET[$nonce_name] ) ) {
|
|
return;
|
|
}
|
|
|
|
if ( ! wp_verify_nonce( $_GET[$nonce_name], $nonce_action ) ) {
|
|
return;
|
|
}
|
|
|
|
// Delete the entire array of errors
|
|
delete_option( self::$namespace . '_errors' );
|
|
|
|
wp_safe_redirect( remove_query_arg( [ $nonce_action, $nonce_name ] ) );
|
|
exit;
|
|
}
|
|
} |