| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
|
| 5 |
use Forge12\Shared\Logger; |
| 6 |
use Forge12\Shared\LoggerInterface; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Messages |
| 14 |
* |
| 15 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 16 |
*/ |
| 17 |
class Messages { |
| 18 |
/** |
| 19 |
* @var Messages|null |
| 20 |
*/ |
| 21 |
private static $instance; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
private $messages = []; |
| 27 |
private LoggerInterface $logger; |
| 28 |
|
| 29 |
/** |
| 30 |
* @return Messages|null |
| 31 |
*/ |
| 32 |
public static function getInstance() { |
| 33 |
if ( null === self::$instance ) { |
| 34 |
self::$instance = new Messages( Logger::getInstance() ); |
| 35 |
} |
| 36 |
|
| 37 |
return self::$instance; |
| 38 |
} |
| 39 |
|
| 40 |
private function __clone() { |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
public function __wakeup() { |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
private function __construct( LoggerInterface $logger ) { |
| 49 |
$this->logger = $logger; |
| 50 |
} |
| 51 |
|
| 52 |
public function get_logger() { |
| 53 |
return $this->logger; |
| 54 |
} |
| 55 |
|
| 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 |
] ); |
| 66 |
|
| 67 |
if ( empty( $this->messages ) ) { |
| 68 |
$this->get_logger()->debug( 'No messages found to combine.', [ |
| 69 |
'plugin' => 'double-opt-in', |
| 70 |
] ); |
| 71 |
return ''; |
| 72 |
} |
| 73 |
|
| 74 |
$combined_messages = implode( "\n", $this->messages ); |
| 75 |
|
| 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 |
} |
| 167 |
} |