98 lines
3.1 KiB
PHP
98 lines
3.1 KiB
PHP
<?php
|
|
defined('ABSPATH') or die;
|
|
|
|
/**
|
|
* Centralized logging and admin notices for the MiniCRM integration.
|
|
*/
|
|
class Minicrm_Integration_Pb_Logger {
|
|
|
|
/**
|
|
* Log an error and optionally raise an admin notice.
|
|
*/
|
|
public static function log($message, array $context = [], $raise_notice = true) {
|
|
if ( $raise_notice ) {
|
|
update_option(
|
|
'minicrm_integration_last_error',
|
|
[
|
|
'message' => $message,
|
|
'context' => $context,
|
|
'time' => self::now_utc_plus_one()
|
|
],
|
|
false
|
|
);
|
|
}
|
|
|
|
if ( class_exists( '\WC_Logger' ) ) {
|
|
$logger = wc_get_logger();
|
|
$logger->error($message, array_merge(['source' => 'minicrm-integration'], $context));
|
|
return;
|
|
}
|
|
|
|
$context_str = '';
|
|
if ( ! empty($context) ) {
|
|
$context_str = ' | ' . wp_json_encode($context);
|
|
}
|
|
error_log('[minicrm-integration] ' . $message . $context_str);
|
|
}
|
|
|
|
/**
|
|
* Current time in UTC+1 (Y-m-d H:i:s) using wp_date().
|
|
*/
|
|
private static function now_utc_plus_one() {
|
|
return wp_date( 'Y-m-d H:i:s', time() + HOUR_IN_SECONDS, new DateTimeZone( 'UTC' ) );
|
|
}
|
|
|
|
/**
|
|
* Show latest MiniCRM sync error in the admin UI so someone can act on it.
|
|
*/
|
|
public static function render_admin_notice() {
|
|
if ( ! current_user_can( 'manage_woocommerce' ) ) {
|
|
return;
|
|
}
|
|
|
|
$error = get_option( 'minicrm_integration_last_error' );
|
|
|
|
if ( ! $error || empty( $error['message'] ) ) {
|
|
return;
|
|
}
|
|
|
|
$message = esc_html( $error['message'] );
|
|
$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;">' . esc_html( wp_json_encode( $error['context'], JSON_PARTIAL_OUTPUT_ON_ERROR ) ) . '</pre>';
|
|
}
|
|
|
|
$dismiss_url = wp_nonce_url(
|
|
add_query_arg( 'minicrm_clear_error', '1' ),
|
|
'minicrm_clear_error',
|
|
'minicrm_nonce'
|
|
);
|
|
|
|
printf(
|
|
'<div class="notice notice-error"><p><strong>MiniCRM integráció hiba:</strong> %s%s%s</p><p><a href="%s" class="button">Megjegyezve, elrejtés</a></p></div>',
|
|
$message,
|
|
$time ? ' <em>(' . $time . ')</em>' : '',
|
|
$context,
|
|
esc_url( $dismiss_url )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Clear error notice when user dismisses via link.
|
|
*/
|
|
public static function handle_admin_notice_dismiss() {
|
|
if ( ! isset( $_GET['minicrm_clear_error'], $_GET['minicrm_nonce'] ) ) {
|
|
return;
|
|
}
|
|
|
|
if ( ! wp_verify_nonce( $_GET['minicrm_nonce'], 'minicrm_clear_error' ) ) {
|
|
return;
|
|
}
|
|
|
|
delete_option( 'minicrm_integration_last_error' );
|
|
wp_safe_redirect( remove_query_arg( [ 'minicrm_clear_error', 'minicrm_nonce' ] ) );
|
|
exit;
|
|
}
|
|
}
|