| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Message_Manager |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module; |
| 11 |
|
| 12 |
use PacketeryLatte\Engine; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Message_Manager |
| 16 |
* |
| 17 |
* @package Packetery |
| 18 |
*/ |
| 19 |
class MessageManager { |
| 20 |
|
| 21 |
public const TYPE_ERROR = 'error'; |
| 22 |
public const TYPE_SUCCESS = 'success'; |
| 23 |
public const TYPE_INFO = 'info'; |
| 24 |
|
| 25 |
public const RENDERER_WORDPRESS = 'wordpress'; |
| 26 |
public const RENDERER_PACKETERY = 'packetery'; |
| 27 |
|
| 28 |
private const EXPIRATION = 120; |
| 29 |
|
| 30 |
/** |
| 31 |
* PacketeryLatte engine. |
| 32 |
* |
| 33 |
* @var Engine |
| 34 |
*/ |
| 35 |
private $latteEngine; |
| 36 |
|
| 37 |
/** |
| 38 |
* Messages to be displayed. |
| 39 |
* |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
private $messages = []; |
| 43 |
|
| 44 |
/** |
| 45 |
* Message_Manager constructor. |
| 46 |
* |
| 47 |
* @param Engine $latteEngine PacketeryLatte engine. |
| 48 |
*/ |
| 49 |
public function __construct( Engine $latteEngine ) { |
| 50 |
$this->latteEngine = $latteEngine; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Inits manager on plugin load. |
| 55 |
*/ |
| 56 |
public function init(): void { |
| 57 |
$messages = get_transient( $this->getTransientName() ); |
| 58 |
if ( ! $messages ) { |
| 59 |
$messages = []; |
| 60 |
} |
| 61 |
|
| 62 |
$this->messages = $messages; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Gets transient name. |
| 67 |
*/ |
| 68 |
private function getTransientName(): string { |
| 69 |
return 'packetery_message_manager_messages_' . wp_get_session_token(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Flashes messages to end user. |
| 74 |
* |
| 75 |
* @param string $message Text. |
| 76 |
* @param string $type Type of message. |
| 77 |
* @param string $renderer Renderer of message. |
| 78 |
* @param string $context Message context. |
| 79 |
*/ |
| 80 |
public function flash_message( string $message, string $type = self::TYPE_SUCCESS, string $renderer = self::RENDERER_WORDPRESS, string $context = '' ): void { |
| 81 |
$message = [ |
| 82 |
'type' => $type, |
| 83 |
'message' => $message, |
| 84 |
'renderer' => $renderer, |
| 85 |
'context' => $context, |
| 86 |
]; |
| 87 |
|
| 88 |
$this->flashMessageArray( $message ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Flash message. |
| 93 |
* |
| 94 |
* @param Message $message Message. |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function flashMessageObject( Message $message ): void { |
| 99 |
$this->flashMessageArray( $message->toArray() ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Adds message. |
| 104 |
* |
| 105 |
* @param array $message Message. |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
private function flashMessageArray( array $message ): void { |
| 110 |
$this->messages[] = $message; |
| 111 |
|
| 112 |
set_transient( $this->getTransientName(), $this->messages, self::EXPIRATION ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Renders messages. |
| 117 |
* |
| 118 |
* @param string $renderer Message renderer. |
| 119 |
* @param string $context Message context. |
| 120 |
*/ |
| 121 |
public function render( string $renderer, string $context = '' ): void { |
| 122 |
// @codingStandardsIgnoreStart |
| 123 |
echo $this->renderToString( $renderer, $context ); |
| 124 |
// @codingStandardsIgnoreEnd |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Renders messages. |
| 129 |
* |
| 130 |
* @param string $renderer Message renderer. |
| 131 |
* @param string $context Message context. |
| 132 |
*/ |
| 133 |
public function renderToString( string $renderer, string $context ): string { |
| 134 |
$output = ''; |
| 135 |
|
| 136 |
foreach ( $this->messages as $key => $message ) { |
| 137 |
if ( $message['renderer'] !== $renderer ) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
if ( $message['context'] !== $context ) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
|
| 145 |
$output .= $this->latteEngine->renderToString( |
| 146 |
PACKETERY_PLUGIN_DIR . '/template/admin-notice.latte', |
| 147 |
[ |
| 148 |
'message' => $message, |
| 149 |
] |
| 150 |
); |
| 151 |
|
| 152 |
unset( $this->messages[ $key ] ); |
| 153 |
} |
| 154 |
|
| 155 |
set_transient( $this->getTransientName(), $this->messages, self::EXPIRATION ); |
| 156 |
return $output; |
| 157 |
} |
| 158 |
} |
| 159 |
|