* @license GPL-2.0-or-later */ defined( 'ABSPATH' ) || exit; /** * Class Studiou_WCMQ_Utils_Log */ class Studiou_WCMQ_Utils_Log { const NOTICE_TRANSIENT_PREFIX = 'studiou_wcmq_notices_'; const NOTICE_TTL = 300; /** * Register hooks. */ public static function init() { add_action( 'admin_notices', array( __CLASS__, 'render_notices' ) ); } /** * Write to the PHP error log when the debug setting is on. * * @param mixed $message Message or structure. */ public static function log( $message ) { if ( ! Studiou_WCMQ_Settings::debug() ) { return; } if ( is_array( $message ) || is_object( $message ) ) { $message = print_r( $message, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r } error_log( 'STUDIOU WC MAIL: ' . $message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log } /** * Queue a dismissible admin notice for the current user. * * Survives the redirect that follows form posts. Plain text only — it is * escaped with esc_html at render time. * * @param string $message Plain text. * @param string $type info|success|warning|error. */ public static function message( $message, $type = 'info' ) { $user_id = get_current_user_id(); if ( ! $user_id ) { return; } if ( ! in_array( $type, array( 'info', 'success', 'warning', 'error' ), true ) ) { $type = 'info'; } $key = self::NOTICE_TRANSIENT_PREFIX . $user_id; $notices = get_transient( $key ); if ( ! is_array( $notices ) ) { $notices = array(); } $notices[] = array( 'message' => (string) $message, 'type' => $type, ); set_transient( $key, $notices, self::NOTICE_TTL ); } /** * Render and clear queued notices. */ public static function render_notices() { $user_id = get_current_user_id(); if ( ! $user_id ) { return; } $key = self::NOTICE_TRANSIENT_PREFIX . $user_id; $notices = get_transient( $key ); if ( ! is_array( $notices ) || empty( $notices ) ) { return; } delete_transient( $key ); foreach ( $notices as $notice ) { $type = isset( $notice['type'] ) ? $notice['type'] : 'info'; $message = isset( $notice['message'] ) ? $notice['message'] : ''; printf( '
%2$s