| 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 |
$enabled = isset( $settings['enabled'] ) && $settings['enabled'] === true; |
| 230 |
|
| 231 |
/** |
| 232 |
* Filter whether a notification is enabled. |
| 233 |
* |
| 234 |
* @param bool $enabled Whether the notification is enabled |
| 235 |
* @param string $id The notification ID |
| 236 |
* @param Notification $notification The notification instance |
| 237 |
*/ |
| 238 |
return apply_filters( 'texty_notification_enabled', $enabled, $this->get_id(), $this ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Get the notification settings |
| 243 |
* |
| 244 |
* @return array |
| 245 |
*/ |
| 246 |
public function settings() { |
| 247 |
$settings = texty()->notifications()->settings(); |
| 248 |
$value = isset( $settings[ $this->get_id() ] ) ? $settings[ $this->get_id() ] : []; |
| 249 |
|
| 250 |
/** |
| 251 |
* Filter the notification settings. |
| 252 |
* |
| 253 |
* @param array $value The notification settings |
| 254 |
* @param string $id The notification ID |
| 255 |
* @param Notification $notification The notification instance |
| 256 |
*/ |
| 257 |
return apply_filters( 'texty_notification_settings', $value, $this->get_id(), $this ); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Send message to recipients |
| 262 |
* |
| 263 |
* @return bool |
| 264 |
*/ |
| 265 |
public function send(): bool { |
| 266 |
if ( ! $this->enabled() ) { |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Filter the recipients for a notification. |
| 272 |
* |
| 273 |
* @param array $recipients The recipient phone numbers |
| 274 |
* @param Notification $notification The notification instance |
| 275 |
*/ |
| 276 |
$recipients = apply_filters( 'texty_notification_recipients', $this->get_recipients(), $this ); |
| 277 |
|
| 278 |
// Drop nulls / empty strings before deduping — these can leak in |
| 279 |
// from an empty `texty_phone` meta or a third-party filter. |
| 280 |
$recipients = is_array( $recipients ) ? array_values( array_filter( $recipients ) ) : []; |
| 281 |
|
| 282 |
if ( ! $recipients ) { |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
// Check unique recipients numbers |
| 287 |
$recipients = array_unique( $recipients ); |
| 288 |
|
| 289 |
$content = $this->get_message(); |
| 290 |
|
| 291 |
/** |
| 292 |
* Filter the notification message content. |
| 293 |
* |
| 294 |
* @param string $content The message content |
| 295 |
* @param Notification $notification The notification instance |
| 296 |
*/ |
| 297 |
$content = apply_filters( 'texty_notification_message', $content, $this ); |
| 298 |
|
| 299 |
/** |
| 300 |
* Filter the message for a specific notification type. |
| 301 |
* |
| 302 |
* @param string $content The message content |
| 303 |
* @param Notification $notification The notification instance |
| 304 |
*/ |
| 305 |
$content = apply_filters( 'texty_notification_message_' . $this->get_id(), $content, $this ); |
| 306 |
|
| 307 |
/** |
| 308 |
* Fires before the notification send loop. |
| 309 |
* |
| 310 |
* @param Notification $notification The notification instance |
| 311 |
* @param array $recipients The recipient phone numbers |
| 312 |
* @param string $content The message content |
| 313 |
*/ |
| 314 |
do_action( 'texty_before_notification', $this, $recipients, $content ); |
| 315 |
|
| 316 |
$gateway = texty()->gateways(); |
| 317 |
|
| 318 |
// Stash the active notification so the after-send logger can attach |
| 319 |
// notification_id / notification_group to each SmsStat row without |
| 320 |
// threading them through the gateway pipeline. |
| 321 |
texty()->notifications()->set_active( $this ); |
| 322 |
|
| 323 |
try { |
| 324 |
foreach ( $recipients as $number ) { |
| 325 |
if ( empty( $number ) ) { |
| 326 |
continue; |
| 327 |
} |
| 328 |
|
| 329 |
$gateway->send( $number, $content ); |
| 330 |
} |
| 331 |
} finally { |
| 332 |
texty()->notifications()->clear_active(); |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Fires after the notification send loop. |
| 337 |
* |
| 338 |
* @param Notification $notification The notification instance |
| 339 |
* @param array $recipients The recipient phone numbers |
| 340 |
* @param string $content The message content |
| 341 |
*/ |
| 342 |
do_action( 'texty_after_notification', $this, $recipients, $content ); |
| 343 |
|
| 344 |
return true; |
| 345 |
} |
| 346 |
} |
| 347 |
|