| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- // Logging + admin-notice utility for the plugin.
- defined('ABSPATH') || exit;
- class UtilsLog {
- const NOTICE_TRANSIENT_PREFIX = 'studiou_wc_ops_notices_';
- const NOTICE_TTL = 300; // 5 minutes — survives slow redirects.
- public static function init() {
- add_action('admin_notices', array(__CLASS__, 'render_notices'));
- }
- public static function log($message) {
- if (defined('WP_DEBUG') && WP_DEBUG) {
- if (is_array($message) || is_object($message)) {
- error_log('[studiou-wc-ord-print-statuses] ' . print_r($message, true));
- } else {
- error_log('[studiou-wc-ord-print-statuses] ' . $message);
- }
- }
- }
- /**
- * Queue an admin notice for the current user.
- * Notices survive the redirect that follows bulk actions and imports.
- *
- * @param string $message Plain text or pre-escaped HTML (will be wp_kses_post-rendered).
- * @param string $type One of: info, success, warning, error.
- */
- public static function message($message, $type = 'info') {
- $user_id = get_current_user_id();
- if (!$user_id) {
- return;
- }
- $allowed = array('info', 'success', 'warning', 'error');
- if (!in_array($type, $allowed, 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);
- }
- 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),
- wp_kses_post($message)
- );
- }
- }
- }
|