| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- /**
- * error_log + admin-notice utility.
- *
- * Adapted from studiou-wc-ord-print-statuses, with one deliberate divergence:
- * log() is gated on this plugin's own `debug` setting rather than WP_DEBUG,
- * because the spec calls for a debug toggle on the settings page.
- *
- * The persistent, admin-visible log lives in the DB (Studiou_WCMQ_DB::log()).
- * This class is only for the PHP error log and for transient admin notices.
- *
- * @package studiou-wc-mail-queue
- * @copyright 2026 QUADARAX
- * @author Dalibor Votruba <dvotruba@quadarax.com>
- * @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(
- '<div class="notice notice-%1$s is-dismissible"><p>%2$s</p></div>',
- esc_attr( $type ),
- esc_html( $message )
- );
- }
- }
- }
|