| @@ -1,92 +1,167 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 | - if(!defined('ABSPATH')){ | |
| 5 | - exit; | |
| 6 | - } | |
| 7 | - /** | |
| 8 | - * Class Messages | |
| 9 | - * @package forge12\contactform7\CF7DoubleOptIn | |
| 10 | - */ | |
| 11 | - class Messages | |
| 12 | - { | |
| 13 | - /** | |
| 14 | - * @var Messages|null | |
| 15 | - */ | |
| 16 | - private static $instance; | |
| 17 | 4 | |
| 18 | - /** | |
| 19 | - * @var array | |
| 20 | - */ | |
| 21 | - private $messages = []; | |
| 5 | + use Forge12\Shared\Logger; | |
| 6 | + use Forge12\Shared\LoggerInterface; | |
| 22 | 7 | |
| 23 | - /** | |
| 24 | - * @return Messages|null | |
| 25 | - */ | |
| 26 | - public static function getInstance() | |
| 27 | - { | |
| 28 | - if (null === self::$instance) { | |
| 29 | - self::$instance = new Messages(); | |
| 30 | - } | |
| 8 | + if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit; | |
| 10 | + } | |
| 31 | 11 | |
| 32 | - return self::$instance; | |
| 33 | - } | |
| 12 | + /** | |
| 13 | + * Class Messages | |
| 14 | + * | |
| 15 | + * @package forge12\contactform7\CF7DoubleOptIn | |
| 16 | + */ | |
| 17 | + class Messages { | |
| 18 | + /** | |
| 19 | + * @var Messages|null | |
| 20 | + */ | |
| 21 | + private static $instance; | |
| 34 | 22 | |
| 35 | - private function __clone() | |
| 36 | - { | |
| 23 | + /** | |
| 24 | + * @var array | |
| 25 | + */ | |
| 26 | + private $messages = []; | |
| 27 | + private LoggerInterface $logger; | |
| 37 | 28 | |
| 38 | - } | |
| 29 | + /** | |
| 30 | + * @return Messages|null | |
| 31 | + */ | |
| 32 | + public static function getInstance() { | |
| 33 | + if ( null === self::$instance ) { | |
| 34 | + self::$instance = new Messages( Logger::getInstance() ); | |
| 35 | + } | |
| 39 | 36 | |
| 40 | - public function __wakeup() | |
| 41 | - { | |
| 37 | + return self::$instance; | |
| 38 | + } | |
| 42 | 39 | |
| 43 | - } | |
| 40 | + private function __clone() { | |
| 44 | 41 | |
| 45 | - private function __construct() | |
| 46 | - { | |
| 47 | - } | |
| 42 | + } | |
| 48 | 43 | |
| 49 | - /** | |
| 50 | - * getAll function. | |
| 51 | - * | |
| 52 | - * @access public | |
| 53 | - * @return string | |
| 54 | - */ | |
| 55 | - public function getAll() | |
| 56 | - { | |
| 57 | - return implode("\n", $this->messages); | |
| 58 | - } | |
| 44 | + public function __wakeup() { | |
| 59 | 45 | |
| 60 | - /** | |
| 61 | - * add function. | |
| 62 | - * | |
| 63 | - * @access public | |
| 64 | - * @param mixed $message | |
| 65 | - * @param mixed $type | |
| 66 | - * @return void | |
| 67 | - */ | |
| 68 | - public function add($message, $type) | |
| 69 | - { | |
| 70 | - if ($type === 'error') { | |
| 71 | - $type = 'alert-danger'; | |
| 46 | + } | |
| 72 | 47 | |
| 73 | - } elseif ($type === 'success') { | |
| 74 | - $type = 'alert-success'; | |
| 48 | + private function __construct( LoggerInterface $logger ) { | |
| 49 | + $this->logger = $logger; | |
| 50 | + } | |
| 75 | 51 | |
| 76 | - } elseif ($type === 'info') { | |
| 77 | - $type = 'alert-info'; | |
| 52 | + public function get_logger() { | |
| 53 | + return $this->logger; | |
| 54 | + } | |
| 78 | 55 | |
| 79 | - } elseif ($type === 'warning') { | |
| 80 | - $type = 'alert-warning'; | |
| 56 | + /** | |
| 57 | + * getAll function. | |
| 58 | + * | |
| 59 | + * @access public | |
| 60 | + * @return string | |
| 61 | + */ | |
| 62 | + public function getAll() { | |
| 63 | + $this->get_logger()->info( 'Retrieving all messages and combining them into a single string.', [ | |
| 64 | + 'plugin' => 'double-opt-in', | |
| 65 | + ] ); | |
| 81 | 66 | |
| 82 | - } elseif ($type === 'offer') { | |
| 83 | - $type = 'alert-offer'; | |
| 67 | + if ( empty( $this->messages ) ) { | |
| 68 | + $this->get_logger()->debug( 'No messages found to combine.', [ | |
| 69 | + 'plugin' => 'double-opt-in', | |
| 70 | + ] ); | |
| 71 | + return ''; | |
| 72 | + } | |
| 84 | 73 | |
| 85 | - } elseif ($type === 'critical') { | |
| 86 | - $type = 'alert-critical'; | |
| 87 | - } | |
| 74 | + $combined_messages = implode( "\n", $this->messages ); | |
| 88 | 75 | |
| 89 | - $this->messages[] = '<div class="box ' . \esc_attr($type) . '" role="alert">' . esc_html($message) . '</div>'; | |
| 90 | - } | |
| 91 | - } | |
| 76 | + $this->get_logger()->debug( 'Combined ' . count($this->messages) . ' messages.', [ | |
| 77 | + 'plugin' => 'double-opt-in', | |
| 78 | + 'message_count' => count( $this->messages ), | |
| 79 | + ] ); | |
| 80 | + | |
| 81 | + // Clear messages after retrieval to prevent duplicates | |
| 82 | + $this->messages = []; | |
| 83 | + | |
| 84 | + return $combined_messages; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * add function. | |
| 89 | + * | |
| 90 | + * @access public | |
| 91 | + * | |
| 92 | + * @param mixed $message | |
| 93 | + * @param mixed $type | |
| 94 | + * | |
| 95 | + * @return void | |
| 96 | + */ | |
| 97 | + public function add( $message, $type ) { | |
| 98 | + $this->get_logger()->info( 'Adding a new UI message.', [ | |
| 99 | + 'plugin' => 'double-opt-in', | |
| 100 | + 'message' => $message, | |
| 101 | + 'original_type' => $type, | |
| 102 | + ] ); | |
| 103 | + | |
| 104 | + // Convert the given message type to a specific CSS class. | |
| 105 | + switch ($type) { | |
| 106 | + case 'error': | |
| 107 | + $type = 'alert-danger'; | |
| 108 | + $this->get_logger()->error( 'An error message has been added to the UI.', [ | |
| 109 | + 'plugin' => 'double-opt-in', | |
| 110 | + 'message_content' => $message, | |
| 111 | + ] ); | |
| 112 | + break; | |
| 113 | + case 'success': | |
| 114 | + $type = 'alert-success'; | |
| 115 | + $this->get_logger()->info( 'A success message has been added to the UI.', [ | |
| 116 | + 'plugin' => 'double-opt-in', | |
| 117 | + 'message_content' => $message, | |
| 118 | + ] ); | |
| 119 | + break; | |
| 120 | + case 'info': | |
| 121 | + $type = 'alert-info'; | |
| 122 | + $this->get_logger()->info( 'An info message has been added to the UI.', [ | |
| 123 | + 'plugin' => 'double-opt-in', | |
| 124 | + 'message_content' => $message, | |
| 125 | + ] ); | |
| 126 | + break; | |
| 127 | + case 'warning': | |
| 128 | + $type = 'alert-warning'; | |
| 129 | + $this->get_logger()->warning( 'A warning message has been added to the UI.', [ | |
| 130 | + 'plugin' => 'double-opt-in', | |
| 131 | + 'message_content' => $message, | |
| 132 | + ] ); | |
| 133 | + break; | |
| 134 | + case 'offer': | |
| 135 | + $type = 'alert-offer'; | |
| 136 | + $this->get_logger()->notice( 'An offer message has been added to the UI.', [ | |
| 137 | + 'plugin' => 'double-opt-in', | |
| 138 | + 'message_content' => $message, | |
| 139 | + ] ); | |
| 140 | + break; | |
| 141 | + case 'critical': | |
| 142 | + $type = 'alert-critical'; | |
| 143 | + $this->get_logger()->critical( 'A critical message has been added to the UI.', [ | |
| 144 | + 'plugin' => 'double-opt-in', | |
| 145 | + 'message_content' => $message, | |
| 146 | + ] ); | |
| 147 | + break; | |
| 148 | + default: | |
| 149 | + $this->get_logger()->debug( 'An unknown message type was provided. Using default type.', [ | |
| 150 | + 'plugin' => 'double-opt-in', | |
| 151 | + 'unknown_type' => $type, | |
| 152 | + ] ); | |
| 153 | + // Fallback for unknown types | |
| 154 | + $type = 'alert-info'; | |
| 155 | + break; | |
| 156 | + } | |
| 157 | + | |
| 158 | + $this->messages[] = '<div class="box ' . \esc_attr( $type ) . '" role="alert">' . esc_html( $message ) . '</div>'; | |
| 159 | + | |
| 160 | + $this->get_logger()->debug( 'Message added to messages array.', [ | |
| 161 | + 'plugin' => 'double-opt-in', | |
| 162 | + 'final_html' => end($this->messages), | |
| 163 | + 'message_count' => count($this->messages), | |
| 164 | + ] ); | |
| 165 | + } | |
| 166 | + } | |
| 92 | 167 | } |