summaryrefslogtreecommitdiff
path: root/Postman/PostmanMessageHandler.php
diff options
context:
space:
mode:
authoryehudah <yehudah@b8457f37-d9ea-0310-8a92-e5e31aec5664>2019-11-25 08:22:35 +0000
committeryehudah <yehudah@b8457f37-d9ea-0310-8a92-e5e31aec5664>2019-11-25 08:22:35 +0000
commitc61784411988d36d9bbd93cd3a97e773990af342 (patch)
tree924e6e9dea2ba7b1eedb14d0c4b03a38aefdf179 /Postman/PostmanMessageHandler.php
parent907ce8c044159ca8da6ccce3ec5362ac61e7c142 (diff)
downloadPost-SMTP-c61784411988d36d9bbd93cd3a97e773990af342.zip
Adding a folder
Diffstat (limited to 'Postman/PostmanMessageHandler.php')
-rw-r--r--Postman/PostmanMessageHandler.php115
1 files changed, 115 insertions, 0 deletions
diff --git a/Postman/PostmanMessageHandler.php b/Postman/PostmanMessageHandler.php
new file mode 100644
index 0000000..cd9220d
--- /dev/null
+++ b/Postman/PostmanMessageHandler.php
@@ -0,0 +1,115 @@
+<?php
+if ( ! defined( 'ABSPATH' ) ) {
+ exit; // Exit if accessed directly
+}
+if (! class_exists ( 'PostmanMessageHandler' )) {
+
+ require_once ('PostmanSession.php');
+ class PostmanMessageHandler {
+
+ // The Session variables that carry messages
+ const ERROR_CLASS = 'error';
+ const WARNING_CLASS = 'update-nag';
+ const SUCCESS_CLASS = 'updated';
+ private $logger;
+
+ /**
+ *
+ * @param mixed $options
+ */
+ function __construct() {
+ $this->logger = new PostmanLogger ( get_class ( $this ) );
+
+ // we'll let the 'init' functions run first; some of them may end the request
+ add_action ( 'admin_notices', Array (
+ $this,
+ 'displayAllMessages'
+ ) );
+ }
+
+ /**
+ *
+ * @param mixed $message
+ */
+ public function addError($message) {
+ $this->storeMessage ( $message, 'error' );
+ }
+ /**
+ *
+ * @param mixed $message
+ */
+ public function addWarning($message) {
+ $this->storeMessage ( $message, 'warning' );
+ }
+ /**
+ *
+ * @param mixed $message
+ */
+ public function addMessage($message) {
+ $this->storeMessage ( $message, 'notify' );
+ }
+
+ /**
+ * store messages for display later
+ *
+ * @param mixed $message
+ * @param mixed $type
+ */
+ private function storeMessage($message, $type) {
+ $messageArray = array ();
+ $oldMessageArray = PostmanSession::getInstance ()->getMessage ();
+ if ($oldMessageArray) {
+ $messageArray = $oldMessageArray;
+ }
+ $weGotIt = false;
+ foreach ( $messageArray as $storedMessage ) {
+ if ($storedMessage ['message'] === $message) {
+ $weGotIt = true;
+ }
+ }
+ if (! $weGotIt) {
+ $m = array (
+ 'type' => $type,
+ 'message' => $message
+ );
+ array_push ( $messageArray, $m );
+ PostmanSession::getInstance ()->setMessage ( $messageArray );
+ }
+ }
+ /**
+ * Retrieve the messages and show them
+ */
+ public function displayAllMessages() {
+ $messageArray = PostmanSession::getInstance ()->getMessage ();
+ if ($messageArray) {
+ PostmanSession::getInstance ()->unsetMessage ();
+ foreach ( $messageArray as $m ) {
+ $type = $m ['type'];
+ switch ($type) {
+ case 'error' :
+ $className = self::ERROR_CLASS;
+ break;
+ case 'warning' :
+ $className = self::WARNING_CLASS;
+ break;
+ default :
+ $className = self::SUCCESS_CLASS;
+ break;
+ }
+ $message = $m ['message'];
+ $this->printMessage ( $message, $className );
+ }
+ }
+ }
+
+ /**
+ * putput message
+ *
+ * @param mixed $message
+ * @param mixed $className
+ */
+ public function printMessage($message, $className) {
+ printf ( '<div class="%s"><p>%s</p></div>', $className, $message );
+ }
+ }
+}