utils-log.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * error_log + admin-notice utility.
  4. *
  5. * Adapted from studiou-wc-ord-print-statuses, with one deliberate divergence:
  6. * log() is gated on this plugin's own `debug` setting rather than WP_DEBUG,
  7. * because the spec calls for a debug toggle on the settings page.
  8. *
  9. * The persistent, admin-visible log lives in the DB (Studiou_WCMQ_DB::log()).
  10. * This class is only for the PHP error log and for transient admin notices.
  11. *
  12. * @package studiou-wc-mail-queue
  13. * @copyright 2026 QUADARAX
  14. * @author Dalibor Votruba <dvotruba@quadarax.com>
  15. * @license GPL-2.0-or-later
  16. */
  17. defined( 'ABSPATH' ) || exit;
  18. /**
  19. * Class Studiou_WCMQ_Utils_Log
  20. */
  21. class Studiou_WCMQ_Utils_Log {
  22. const NOTICE_TRANSIENT_PREFIX = 'studiou_wcmq_notices_';
  23. const NOTICE_TTL = 300;
  24. /**
  25. * Register hooks.
  26. */
  27. public static function init() {
  28. add_action( 'admin_notices', array( __CLASS__, 'render_notices' ) );
  29. }
  30. /**
  31. * Write to the PHP error log when the debug setting is on.
  32. *
  33. * @param mixed $message Message or structure.
  34. */
  35. public static function log( $message ) {
  36. if ( ! Studiou_WCMQ_Settings::debug() ) {
  37. return;
  38. }
  39. if ( is_array( $message ) || is_object( $message ) ) {
  40. $message = print_r( $message, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
  41. }
  42. error_log( 'STUDIOU WC MAIL: ' . $message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
  43. }
  44. /**
  45. * Queue a dismissible admin notice for the current user.
  46. *
  47. * Survives the redirect that follows form posts. Plain text only — it is
  48. * escaped with esc_html at render time.
  49. *
  50. * @param string $message Plain text.
  51. * @param string $type info|success|warning|error.
  52. */
  53. public static function message( $message, $type = 'info' ) {
  54. $user_id = get_current_user_id();
  55. if ( ! $user_id ) {
  56. return;
  57. }
  58. if ( ! in_array( $type, array( 'info', 'success', 'warning', 'error' ), true ) ) {
  59. $type = 'info';
  60. }
  61. $key = self::NOTICE_TRANSIENT_PREFIX . $user_id;
  62. $notices = get_transient( $key );
  63. if ( ! is_array( $notices ) ) {
  64. $notices = array();
  65. }
  66. $notices[] = array(
  67. 'message' => (string) $message,
  68. 'type' => $type,
  69. );
  70. set_transient( $key, $notices, self::NOTICE_TTL );
  71. }
  72. /**
  73. * Render and clear queued notices.
  74. */
  75. public static function render_notices() {
  76. $user_id = get_current_user_id();
  77. if ( ! $user_id ) {
  78. return;
  79. }
  80. $key = self::NOTICE_TRANSIENT_PREFIX . $user_id;
  81. $notices = get_transient( $key );
  82. if ( ! is_array( $notices ) || empty( $notices ) ) {
  83. return;
  84. }
  85. delete_transient( $key );
  86. foreach ( $notices as $notice ) {
  87. $type = isset( $notice['type'] ) ? $notice['type'] : 'info';
  88. $message = isset( $notice['message'] ) ? $notice['message'] : '';
  89. printf(
  90. '<div class="notice notice-%1$s is-dismissible"><p>%2$s</p></div>',
  91. esc_attr( $type ),
  92. esc_html( $message )
  93. );
  94. }
  95. }
  96. }