PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / classes / service / GlobalSystemMessageService.php

GlobalSystemMessageService.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/classes/service/GlobalSystemMessageService.php

156 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Description of GlobalSystemMessageService
5 *
6 * @author Ali2Woo Team
7 */
8
9 namespace AliNext_Lite;;
10
11 class GlobalSystemMessageService
12 {
13 public const MESSAGE_TYPE_SUCCESS ='success';
14 public const MESSAGE_TYPE_ERROR ='error';
15 public const MESSAGE_TYPE_WARNING = 'warning';
16 public const MESSAGE_TYPE_INFO = 'info';
17
18
19 /**
20 * Get all messages: system + critical
21 */
22 public function getAllMessages(): array
23 {
24 $system_message = get_setting(Settings::SETTING_SYSTEM_MESSAGE);
25 $messages = [];
26
27 if (!empty($system_message)) {
28 foreach ($system_message as $key => $message) {
29 if (!empty($message['message'])) {
30 $message_class = 'updated';
31 if ($message['type'] === self::MESSAGE_TYPE_ERROR) {
32 $message_class = 'error';
33 } elseif ($message['type'] === self::MESSAGE_TYPE_WARNING) {
34 $message_class = 'warning';
35 }
36
37 $messages[] =
38 '<div id="a2wl-system-message-' . esc_attr($key) .
39 '" class="a2wl-system-message notice ' . esc_attr($message_class) .
40 ' is-dismissible"><p>' .
41 esc_html($message['message']) .
42 '</p></div>';
43 }
44 }
45 }
46
47 // critical messages
48 $critical_messages = get_setting(Settings::SETTING_CRITICAL_MESSAGES);
49 if (!empty($critical_messages)) {
50 foreach ($critical_messages as $key => $message) {
51 if (!empty($message['text'])) {
52
53 $message_class = 'error';
54 if (!empty($message['type'])) {
55 if ($message['type'] === self::MESSAGE_TYPE_WARNING) {
56 $message_class = 'warning';
57 } elseif ($message['type'] === self::MESSAGE_TYPE_INFO) {
58 $message_class = 'success';
59 }
60 }
61
62 $text = esc_html($message['text']);
63 if (!empty($message['link'])) {
64 $link_text = !empty($message['link_text'])
65 ? esc_html($message['link_text'])
66 : esc_html__('Click here', 'ali2woo');
67
68 $text .= ' <a href="' . esc_url($message['link']) . '">' . $link_text . '</a>';
69 }
70
71 $messages[] =
72 '<div id="a2wl-critical-message-' . esc_attr($key) .
73 '" class="a2wl-critical-message notice notice-' . esc_attr($message_class) .
74 ' is-dismissible" data-code="' . esc_attr($message['code']) . '"><p>' .
75 $text .
76 '</p></div>';
77 }
78 }
79 }
80
81 return $messages;
82 }
83
84 public function deleteCriticalMessage(string $messageCode): void
85 {
86 $messages = get_setting(Settings::SETTING_CRITICAL_MESSAGES, []);
87
88 $messages = array_filter($messages, static function ($message) use ($messageCode) {
89 return $message['code'] !== $messageCode;
90 });
91
92 set_setting(Settings::SETTING_CRITICAL_MESSAGES, array_values($messages));
93 }
94
95
96 public function clearCritical(): void
97 {
98 set_setting(Settings::SETTING_CRITICAL_MESSAGES, []);
99 }
100
101 public function clearSystem(): void
102 {
103 set_setting(Settings::SETTING_SYSTEM_MESSAGE, []);
104 }
105
106 public function clear(): void
107 {
108 set_setting(Settings::SETTING_SYSTEM_MESSAGE, []);
109 set_setting(Settings::SETTING_CRITICAL_MESSAGES, []);
110 }
111
112 public function addErrorMessage(string $message): void
113 {
114 $this->addNewMessage(self::MESSAGE_TYPE_ERROR, $message);
115 }
116
117 public function addSuccessMessage(string $message): void
118 {
119 $this->addNewMessage(self::MESSAGE_TYPE_SUCCESS, $message);
120 }
121
122 public function addWarningMessage(string $message): void
123 {
124 $this->addNewMessage(self::MESSAGE_TYPE_WARNING, $message);
125 }
126
127 public function addMessages(array $messages): void
128 {
129 foreach ($messages as $message) {
130 if (!empty($message['type']) && !empty($message['message'])) {
131 if ($message['type'] === self::MESSAGE_TYPE_SUCCESS) {
132 $this->addNewMessage(self::MESSAGE_TYPE_SUCCESS, $message['message']);
133 } elseif ($message['type'] === self::MESSAGE_TYPE_ERROR) {
134 $this->addNewMessage(self::MESSAGE_TYPE_ERROR, $message['message']);
135 } elseif ($message['type'] === self::MESSAGE_TYPE_WARNING) {
136 $this->addNewMessage(self::MESSAGE_TYPE_WARNING, $message['message']);
137 }
138 }
139 }
140 }
141
142 private function getMessagesData(): array
143 {
144 $allMessages = get_setting(Settings::SETTING_SYSTEM_MESSAGE);
145
146 return !empty($allMessages) ? $allMessages : [];
147 }
148
149 private function addNewMessage(string $type, string $message): void
150 {
151 $allMessages = $this->getMessagesData();
152 $allMessages[] = ['type' => $type, 'message' => $message];
153 set_setting(Settings::SETTING_SYSTEM_MESSAGE, $allMessages);
154 }
155 }
156