| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?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. Tags are HTML-escaped at render
- * time (esc_html). Callers should pre-format
- * any user-controlled content with esc_html
- * themselves if they need to keep markup such
- * as <code>.
- * @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'] : '';
- // Tight escape — notice messages are plain text in every
- // current call site. Switching to esc_html (was wp_kses_post)
- // narrows the trust boundary at the API edge: callers can no
- // longer accidentally smuggle HTML through the notice queue.
- printf(
- '<div class="notice notice-%1$s is-dismissible"><p>%2$s</p></div>',
- esc_attr($type),
- esc_html($message)
- );
- }
- }
- }
|