PluginProbe
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more / 1.1
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more v1.1
2.0.2 trunk 0.2 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 2.0.0 2.0.1
texty / includes / Notifications / Notification.php

Notification.php in Texty – SMS Notification for WordPress, WooCommerce, Dokan and more 1.1, at includes/Notifications/Notification.php

283 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Texty\Notifications;
4
5 abstract class Notification {
6
7 /**
8 * Name of the notification
9 *
10 * @var string
11 */
12 protected $title;
13
14 /**
15 * The notification ID
16 *
17 * @var string
18 */
19 protected $id;
20
21 /**
22 * Type of the notification
23 *
24 * @var string
25 */
26 protected $type = 'role';
27
28 /**
29 * The default message
30 *
31 * @var string
32 */
33 protected $default;
34
35 /**
36 * Default recipients
37 *
38 * @var mixed
39 */
40 protected $default_recipients;
41
42 /**
43 * Notification group
44 *
45 * @var string
46 */
47 protected $group = 'wp';
48
49 /**
50 * Get the title
51 *
52 * @return string
53 */
54 public function get_title() {
55 return $this->title;
56 }
57
58 /**
59 * Get the ID
60 *
61 * @return string
62 */
63 public function get_id() {
64 return $this->id;
65 }
66
67 /**
68 * Type of the message
69 *
70 * @return string
71 */
72 public function get_type() {
73 return $this->type;
74 }
75
76 /**
77 * Type of the message
78 *
79 * @return string
80 */
81 public function get_group() {
82 return $this->group;
83 }
84
85 /**
86 * Return the default message
87 *
88 * @return string
89 */
90 public function get_default_message() {
91 return $this->default;
92 }
93
94 /**
95 * Return the message
96 *
97 * @return string
98 */
99 public function get_message_raw() {
100 $settings = $this->settings();
101
102 if ( isset( $settings['message'] ) ) {
103 return $settings['message'];
104 }
105
106 return $this->get_default_message();
107 }
108
109 /**
110 * Return the message
111 *
112 * @return string
113 */
114 public function get_message() {
115 return $this->get_message_raw();
116 }
117
118 /**
119 * Get replacement keys
120 *
121 * @return array
122 */
123 public function replacement_keys() {
124 return __return_empty_array();
125 }
126
127 /**
128 * Global replacement keys
129 *
130 * @return array
131 */
132 public function global_replacement_keys() {
133 return [
134 'site_name' => 'get_bloginfo',
135 'site_url' => 'home_url',
136 ];
137 }
138
139 /**
140 * Replace messages with global replacement keys
141 *
142 * @param string $message
143 *
144 * @return string
145 */
146 protected function replace_global_keys( $message ) {
147 foreach ( $this->global_replacement_keys() as $search => $function ) {
148 $message = str_replace( '{' . $search . '}', $function(), $message );
149 }
150
151 return $message;
152 }
153
154 /**
155 * Return recipients
156 *
157 * @return array
158 */
159 public function get_recipients_raw() {
160 $settings = $this->settings();
161
162 if ( isset( $settings['recipients'] ) ) {
163 return $settings['recipients'];
164 }
165
166 return $this->default_recipients;
167 }
168
169 /**
170 * Return recipients
171 *
172 * @return array
173 */
174 public function get_recipients() {
175 return $this->get_recipients_raw();
176 }
177
178 /**
179 * Get phone numbers by roles
180 *
181 * @return array
182 */
183 protected function get_numbers_by_roles() {
184 global $wpdb;
185
186 $numbers = [];
187 $roles = $this->get_recipients_raw();
188
189 if ( ! ( is_array( $roles ) && $roles ) ) {
190 return false;
191 }
192
193 foreach ( $roles as $role ) {
194 // phpcs:disable
195 $users = get_users( [
196 'role' => $role,
197 'fields' => 'ID',
198 'meta_query' => [
199 [
200 'key' => 'texty_phone',
201 ],
202 ],
203 ] );
204 // phpcs:enable
205
206 if ( ! $users ) {
207 continue;
208 }
209
210 $results = $wpdb->get_col(
211 sprintf( "SELECT `meta_value` from $wpdb->usermeta WHERE `user_id` IN (%s) AND `meta_key` = 'texty_phone'", implode( ', ', $users ) ) // phpcs:ignore
212 );
213
214 foreach ( $results as $number ) {
215 $numbers[] = $number;
216 }
217 }
218
219 return $numbers;
220 }
221
222 /**
223 * Check if the gateway is enabled
224 *
225 * @return bool
226 */
227 public function enabled() {
228 $settings = $this->settings();
229
230 if ( isset( $settings['enabled'] ) && $settings['enabled'] === true ) {
231 return true;
232 }
233
234 return false;
235 }
236
237 /**
238 * Get the notification settings
239 *
240 * @return array
241 */
242 public function settings() {
243 $settings = texty()->notifications()->settings();
244
245 if ( isset( $settings[ $this->get_id() ] ) ) {
246 return $settings[ $this->get_id() ];
247 }
248
249 return [];
250 }
251
252 /**
253 * Send message to recipients
254 *
255 * @return void
256 */
257 public function send() {
258 if ( ! $this->enabled() ) {
259 return;
260 }
261
262 $recipients = $this->get_recipients();
263
264 if ( ! $recipients ) {
265 return;
266 }
267
268 // Check unique recipients numbers
269 $recipients = array_unique( $recipients );
270
271 $content = $this->get_message();
272 $gateway = texty()->gateways();
273
274 foreach ( $recipients as $number ) {
275 if ( empty( $number ) ) {
276 continue;
277 }
278
279 $gateway->send( $number, $content );
280 }
281 }
282 }
283