| @@ -8,9 +8,10 @@ | ||
| 8 | 8 | declare( strict_types=1 ); |
| 9 | 9 | |
| 10 | 10 | namespace Packetery\Module; |
| 11 | 11 | |
| 12 | -use PacketeryLatte\Engine; | |
| 12 | +use Packetery\Latte\Engine; | |
| 13 | +use Packetery\Module\Framework\WpAdapter; | |
| 13 | 14 | |
| 14 | 15 | /** |
| 15 | 16 | * Class Message_Manager |
| 16 | 17 | * |
| @@ -34,21 +35,25 @@ | ||
| 34 | 35 | */ |
| 35 | 36 | private $latteEngine; |
| 36 | 37 | |
| 37 | 38 | /** |
| 39 | + * @var WpAdapter | |
| 40 | + */ | |
| 41 | + private $wpAdapter; | |
| 42 | + | |
| 43 | + /** | |
| 38 | 44 | * Messages to be displayed. |
| 39 | 45 | * |
| 40 | - * @var array | |
| 46 | + * @var array<array<string, string>> | |
| 41 | 47 | */ |
| 42 | 48 | private $messages = []; |
| 43 | 49 | |
| 44 | - /** | |
| 45 | - * Message_Manager constructor. | |
| 46 | - * | |
| 47 | - * @param Engine $latteEngine PacketeryLatte engine. | |
| 48 | - */ | |
| 49 | - public function __construct( Engine $latteEngine ) { | |
| 50 | + public function __construct( | |
| 51 | + Engine $latteEngine, | |
| 52 | + WpAdapter $wpAdapter | |
| 53 | + ) { | |
| 50 | 54 | $this->latteEngine = $latteEngine; |
| 55 | + $this->wpAdapter = $wpAdapter; | |
| 51 | 56 | } |
| 52 | 57 | |
| 53 | 58 | /** |
| 54 | 59 | * Inits manager on plugin load. |
| @@ -53,10 +58,10 @@ | ||
| 53 | 58 | /** |
| 54 | 59 | * Inits manager on plugin load. |
| 55 | 60 | */ |
| 56 | 61 | public function init(): void { |
| 57 | - $messages = get_transient( $this->getTransientName() ); | |
| 58 | - if ( ! $messages ) { | |
| 62 | + $messages = $this->wpAdapter->getTransient( $this->getTransientName() ); | |
| 63 | + if ( $messages === false ) { | |
| 59 | 64 | $messages = []; |
| 60 | 65 | } |
| 61 | 66 | |
| 62 | 67 | $this->messages = $messages; |
| @@ -65,9 +70,9 @@ | ||
| 65 | 70 | /** |
| 66 | 71 | * Gets transient name. |
| 67 | 72 | */ |
| 68 | 73 | private function getTransientName(): string { |
| 69 | - return 'packetery_message_manager_messages_' . wp_get_session_token(); | |
| 74 | + return Transients::MESSAGE_MANAGER_MESSAGES_PREFIX . $this->wpAdapter->getSessionToken(); | |
| 70 | 75 | } |
| 71 | 76 | |
| 72 | 77 | /** |
| 73 | 78 | * Flashes messages to end user. |
| @@ -84,12 +89,23 @@ | ||
| 84 | 89 | 'renderer' => $renderer, |
| 85 | 90 | 'context' => $context, |
| 86 | 91 | ]; |
| 87 | 92 | |
| 88 | - $this->addMessage( $message ); | |
| 93 | + $this->flashMessageArray( $message ); | |
| 89 | 94 | } |
| 90 | 95 | |
| 91 | 96 | /** |
| 97 | + * Flash message. | |
| 98 | + * | |
| 99 | + * @param Message $message Message. | |
| 100 | + * | |
| 101 | + * @return void | |
| 102 | + */ | |
| 103 | + public function flashMessageObject( Message $message ): void { | |
| 104 | + $this->flashMessageArray( $message->toArray() ); | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 92 | 108 | * Adds message. |
| 93 | 109 | * |
| 94 | 110 | * @param array $message Message. |
| 95 | 111 | * |
| @@ -94,12 +110,12 @@ | ||
| 94 | 110 | * @param array $message Message. |
| 95 | 111 | * |
| 96 | 112 | * @return void |
| 97 | 113 | */ |
| 98 | - private function addMessage( array $message ): void { | |
| 114 | + private function flashMessageArray( array $message ): void { | |
| 99 | 115 | $this->messages[] = $message; |
| 100 | 116 | |
| 101 | - set_transient( $this->getTransientName(), $this->messages, self::EXPIRATION ); | |
| 117 | + $this->wpAdapter->setTransient( $this->getTransientName(), $this->messages, self::EXPIRATION ); | |
| 102 | 118 | } |
| 103 | 119 | |
| 104 | 120 | /** |
| 105 | 121 | * Renders messages. |
| @@ -106,9 +122,9 @@ | ||
| 106 | 122 | * |
| 107 | 123 | * @param string $renderer Message renderer. |
| 108 | 124 | * @param string $context Message context. |
| 109 | 125 | */ |
| 110 | - public function render( string $renderer = self::RENDERER_WORDPRESS, string $context = '' ): void { | |
| 126 | + public function render( string $renderer, string $context = '' ): void { | |
| 111 | 127 | // @codingStandardsIgnoreStart |
| 112 | 128 | echo $this->renderToString( $renderer, $context ); |
| 113 | 129 | // @codingStandardsIgnoreEnd |
| 114 | 130 | } |
| @@ -118,9 +134,9 @@ | ||
| 118 | 134 | * |
| 119 | 135 | * @param string $renderer Message renderer. |
| 120 | 136 | * @param string $context Message context. |
| 121 | 137 | */ |
| 122 | - public function renderToString( string $renderer = self::RENDERER_WORDPRESS, string $context = '' ): string { | |
| 138 | + public function renderToString( string $renderer, string $context ): string { | |
| 123 | 139 | $output = ''; |
| 124 | 140 | |
| 125 | 141 | foreach ( $this->messages as $key => $message ) { |
| 126 | 142 | if ( $message['renderer'] !== $renderer ) { |
| @@ -140,8 +156,9 @@ | ||
| 140 | 156 | |
| 141 | 157 | unset( $this->messages[ $key ] ); |
| 142 | 158 | } |
| 143 | 159 | |
| 144 | - set_transient( $this->getTransientName(), $this->messages, self::EXPIRATION ); | |
| 160 | + $this->wpAdapter->setTransient( $this->getTransientName(), $this->messages, self::EXPIRATION ); | |
| 161 | + | |
| 145 | 162 | return $output; |
| 146 | 163 | } |
| 147 | 164 | } |