PluginProbe
WANotifier for Forms and Actions / 0.1.1
WANotifier for Forms and Actions v0.1.1
3.1.1 3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 All 67 releases
notifier / includes / classes / class-notifier-notifications.php

class-notifier-notifications.php in WANotifier for Forms and Actions 0.1.1, at includes/classes/class-notifier-notifications.php

543 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Notifications CPT class
4 *
5 * @package Wa_Notifier
6 */
7 class Notifier_Notifications {
8
9 /**
10 * Init.
11 */
12 public static function init() {
13 add_filter( 'init', array( __CLASS__ , 'register_cpt') );
14 add_action( 'admin_menu', array( __CLASS__ , 'setup_admin_page') );
15 add_action( 'add_meta_boxes', array( __CLASS__, 'create_meta_box' ) );
16 add_filter( 'bulk_actions-wa_notification', array( __CLASS__, 'remove_bulk_actions' ) );
17 add_filter( 'post_row_actions', array(__CLASS__, 'remove_quick_edit'), 10, 2);
18 add_filter( 'gettext', array(__CLASS__, 'change_texts'), 10, 2 );
19 add_action( 'wp_ajax_fetch_send_to_fields', array(__CLASS__, 'fetch_send_to_fields' ) );
20 add_action( 'wp_ajax_fetch_message_template_data', array(__CLASS__, 'fetch_message_template_data') );
21 add_filter( 'notifier_js_variables', array(__CLASS__, 'notifications_js_variables'));
22 add_action( 'save_post_wa_notification', array(__CLASS__, 'save_meta'), 10, 2 );
23 add_filter( 'post_updated_messages', array(__CLASS__, 'update_save_messages') );
24 add_action( 'before_delete_post', array(__CLASS__, 'delete_from_active_triggers'), 10 );
25 add_filter( 'manage_wa_notification_posts_columns', array( __CLASS__ , 'add_columns' ) );
26 add_action( 'manage_wa_notification_posts_custom_column', array( __CLASS__ , 'add_column_content' ), 10, 2 );
27
28 add_action( 'notifier_marketing_notification', array(__CLASS__, 'send_broadcast_notifications') );
29 add_filter( 'admin_body_class', array(__CLASS__, 'admin_body_class'));
30 add_action( 'wp_ajax_get_wa_contacts_data', array(__CLASS__, 'get_wa_contacts_data') );
31 }
32
33 /**
34 * Register custom post type
35 */
36 public static function register_cpt () {
37 notifier_register_post_type ('wa_notification', 'Notification', 'Notifications');
38 }
39
40 /**
41 * Add page to admin menu
42 */
43 public static function setup_admin_page () {
44 $api_credentials_validated = get_option( NOTIFIER_PREFIX . 'api_credentials_validated');
45 if (!$api_credentials_validated) {
46 return;
47 }
48
49 add_submenu_page( NOTIFIER_NAME, 'Notification', 'Notifications', 'manage_options', 'edit.php?post_type=wa_notification' );
50 }
51
52 /**
53 * Create meta boxes
54 */
55 public static function create_meta_box () {
56 add_meta_box(
57 NOTIFIER_NAME . '-notification-data',
58 'Notification Settings',
59 'Notifier_Notifications::output',
60 'wa_notification'
61 );
62
63 add_meta_box(
64 NOTIFIER_NAME . '-message-template-preview',
65 'Preview Template',
66 'Notifier_Message_Templates::output_preview',
67 'wa_notification',
68 'side'
69 );
70
71 remove_meta_box( 'submitdiv', 'wa_notification', 'side' );
72 add_meta_box( 'submitdiv', 'Save Notification', 'post_submit_meta_box', 'wa_notification', 'side', 'high' );
73 }
74
75 /**
76 * Output meta box
77 */
78 public static function output () {
79 include_once NOTIFIER_PATH . 'views/admin-notifications-meta-box.php';
80 }
81
82 /**
83 * Save meta
84 */
85 public static function save_meta( $post_id, $post ) {
86 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
87 return;
88 }
89
90 if ( ! current_user_can( 'edit_post', $post_id ) ) {
91 return;
92 }
93
94 $notification_data = array();
95
96 if (isset($_POST[NOTIFIER_PREFIX . 'notification_send_to']['row_num'])) {
97 unset($_POST[NOTIFIER_PREFIX . 'notification_send_to']['row_num']);
98 }
99
100 foreach ($_POST as $key => $data) {
101 if (strpos($key, NOTIFIER_PREFIX) !== false) {
102 if (is_array($data)) {
103 $notification_data[$key] = notifier_sanitize_array($data);
104 } else {
105 $notification_data[$key] = sanitize_text_field( wp_unslash ($data) );
106 }
107 update_post_meta( $post_id, $key, $notification_data[$key]);
108 }
109 }
110
111 if (!empty($notification_data)) {
112 self::setup_notifcation($notification_data, $post_id);
113 }
114 }
115
116 /**
117 * Update save action messages
118 */
119 public static function update_save_messages ($messages) {
120 $messages['wa_notification'][1] = 'Notification updated.';
121 $messages['wa_notification'][6] = 'Notification saved.';
122 return $messages;
123 }
124
125 /**
126 * Remove inline edit from Bulk Edit
127 */
128 public static function remove_bulk_actions( $actions ) {
129 unset( $actions['inline'] );
130 return $actions;
131 }
132
133 /**
134 * Remove inline Quick Edit
135 */
136 public static function remove_quick_edit( $actions, $post ) {
137 if ('wa_notification' == $post->post_type) {
138 unset($actions['inline hide-if-no-js']);
139 }
140 return $actions;
141 }
142
143 /**
144 * Change text of buttons and links
145 */
146 public static function change_texts( $translation, $text ) {
147 if ( 'wa_notification' == get_post_type() ) {
148 if ( 'Update' === $text ) {
149 return 'Update';
150 } elseif ( 'Publish' === $text ) {
151 return 'Save';
152 }
153 }
154 return $translation;
155 }
156
157 /**
158 * Notifications JS variables
159 */
160 public static function notifications_js_variables ($variables) {
161 return $variables;
162 }
163
164 /**
165 * Fetch and return message template data
166 */
167 public static function fetch_message_template_data() {
168 if (!isset($_POST['template_id'])) {
169 wp_die();
170 }
171
172 $template_id = intval($_POST['template_id']);
173
174 if ('wa_message_template' != get_post_type( $template_id ) ) {
175 wp_die();
176 }
177
178 $post_id = isset($_POST['post_id']) ? intval( $_POST['post_id'] ) : 0;
179 $notification_type = isset($_POST['notification_type']) ? sanitize_text_field( wp_unslash($_POST['notification_type']) ) : '';
180 $trigger = isset($_POST['trigger']) ? sanitize_text_field( wp_unslash($_POST['trigger']) ) : '';
181
182 if ('marketing' === $notification_type) {
183 $trigger = '';
184 }
185
186 $template_meta = array(
187 'header_type',
188 'header_text',
189 'body_text',
190 'footer_text',
191 'button_type',
192 'button_num',
193 'button_1_type',
194 'button_1_text',
195 'button_2_type',
196 'button_2_text'
197 );
198
199 $template_data = array ();
200 foreach ($template_meta as $meta) {
201 $template_data[$meta] = get_post_meta( $template_id, NOTIFIER_PREFIX . $meta, true);
202 }
203
204 $notification_status = get_post_meta ( $post_id, NOTIFIER_PREFIX . 'notification_status', true);
205 if ($notification_status && in_array($notification_status, array('Sending', 'Sent', 'Scheduled'))) {
206 $disabled = array (
207 'disabled' => 'disabled'
208 );
209 } else {
210 $disabled = array ();
211 }
212
213 $response = array (
214 'status' => 'success',
215 'data' => $template_data,
216 'notification_status' => $notification_status,
217 );
218
219 echo json_encode($response);
220 wp_die();
221 }
222
223 /**
224 * Setup notification
225 */
226 public static function setup_notifcation($data, $id) {
227 $type = isset($data['notifier_notification_type']) ? $data['notifier_notification_type'] : 'marketing';
228 switch ($type) {
229 case 'marketing':
230 self::schedule_notification_to_list($data, $id);
231 break;
232 case 'transactional':
233 self::setup_transactional_notification($data, $id);
234 break;
235 }
236 }
237
238 /**
239 * Schedule marketing notification to list
240 */
241 public static function schedule_notification_to_list ($notification_data, $notification_id) {
242 if ( as_has_scheduled_action( 'notifier_marketing_notification', array($notification_id), 'notifier' ) ) {
243 as_unschedule_action('notifier_marketing_notification', array($notification_id), 'notifier');
244 }
245
246 update_post_meta ( $notification_id, NOTIFIER_PREFIX . 'notification_status', 'Scheduled');
247
248 // Remove from triggers if this notification was earlier a transactional one
249 $active_triggers = get_option('notifier_active_triggers');
250 if (false !== $active_triggers) {
251 foreach ($active_triggers as $trigger => $notif_ids) {
252 $key = array_search($notification_id, $notif_ids);
253 if (false !== $key) {
254 unset($notif_ids[$key]);
255 }
256 $active_triggers[$trigger] = $notif_ids;
257 }
258 }
259 update_option('notifier_active_triggers', $active_triggers, true);
260
261 $when = isset($notification_data['notifier_notification_when']) ? $notification_data['notifier_notification_when'] : 'now';
262 if ('now' == $when) {
263 as_enqueue_async_action( 'notifier_marketing_notification', array($notification_id), 'notifier' );
264 } else {
265 $datetime = isset($notification_data['notifier_notification_datetime']) ? $notification_data['notifier_notification_datetime'] : '';
266 $timestamp = strtotime($datetime);
267 as_schedule_single_action( $timestamp, 'notifier_marketing_notification', array($notification_id), 'notifier' );
268 }
269 }
270
271 /**
272 * Setup transactional notification
273 */
274 public static function setup_transactional_notification ($notification_data, $notification_id) {
275 update_post_meta ( $notification_id, NOTIFIER_PREFIX . 'notification_status', 'On-going');
276 $trigger = $notification_data['notifier_notification_trigger'];
277 $active_triggers = get_option('notifier_active_triggers');
278 if ( false !== $active_triggers ) {
279 foreach ($active_triggers as $t => $notif_ids) {
280 $key = array_search($notification_id, $notif_ids);
281 if (false !== $key) {
282 unset($notif_ids[$key]);
283 }
284 $active_triggers[$t] = $notif_ids;
285 }
286 $active_triggers[$trigger][] = $notification_id;
287 } else {
288 $active_triggers = array(
289 $trigger => array($notification_id)
290 );
291 }
292 update_option('notifier_active_triggers', $active_triggers, true);
293 }
294
295 /**
296 * Delete notification ID from active triggers
297 */
298 public static function delete_from_active_triggers ($post_id) {
299 $active_triggers = get_option('notifier_active_triggers');
300 if (false !== $active_triggers) {
301 foreach ($active_triggers as $trigger => $notif_ids) {
302 $key = array_search($post_id, $notif_ids);
303 if (false !== $key) {
304 unset($notif_ids[$key]);
305 }
306 $active_triggers[$trigger] = $notif_ids;
307 }
308 }
309 update_option('notifier_active_triggers', $active_triggers, true);
310 }
311
312 /**
313 * Send scheduled broadcast notifications
314 */
315 public static function send_broadcast_notifications ($notification_id) {
316 update_post_meta ( $notification_id, NOTIFIER_PREFIX . 'notification_status', 'Sending');
317
318 $list_slug = get_post_meta($notification_id, 'notifier_notification_list', true);
319 $template_id = get_post_meta($notification_id, 'notifier_notification_message_template', true);
320 $list_offset = get_post_meta($notification_id, 'notifier_notification_list_offset', true);
321 $sent_phone_numbers = get_post_meta($notification_id, 'notifier_notification_sent_phone_numbers', true);
322 $unsent_phone_numbers = get_post_meta($notification_id, 'notifier_notification_unsent_phone_numbers', true);
323
324 $sent_phone_numbers = (isset($sent_phone_numbers)) ? $sent_phone_numbers : array();
325 $unsent_phone_numbers = (isset($unsent_phone_numbers)) ? $unsent_phone_numbers : array();
326
327 $offset = (!$list_offset) ? 0 : (int) $list_offset;
328
329 $count = 0;
330 $limit = get_option( NOTIFIER_PREFIX . 'bulk_message_batch_limit', 50 );
331
332 $contact_ids = get_posts( array(
333 'post_type' => 'wa_contact',
334 'post_status' => 'publish',
335 'wa_contact_list' => $list_slug,
336 'fields' => 'ids',
337 'offset' => $offset,
338 'posts_per_page' => $limit
339 ) );
340
341 foreach ($contact_ids as $contact_id) {
342 $count++;
343
344 $phone_number = get_post_meta( $contact_id, NOTIFIER_PREFIX . 'wa_number', true);
345 if ('' == $phone_number) {
346 continue;
347 }
348
349 $message_sent = Notifier_Message_Templates::send_message_template_to_number($template_id, $notification_id, $phone_number);
350
351 if ($message_sent) {
352 $sent_numbers[] = $phone_number;
353 } else {
354 $unsent_numbers[] = $phone_number;
355 }
356 }
357
358 update_post_meta($notification_id, 'notifier_notification_sent_phone_numbers', $sent_numbers);
359 update_post_meta($notification_id, 'notifier_notification_unsent_phone_numbers', $unsent_numbers);
360
361 $new_offset = $offset + $limit;
362 update_post_meta($notification_id, 'notifier_notification_list_offset', $new_offset);
363 if ($count == $limit) {
364 as_enqueue_async_action( 'notifier_marketing_notification', array($notification_id), 'notifier' );
365 } else {
366 update_post_meta ( $notification_id, NOTIFIER_PREFIX . 'notification_status', 'Sent');
367 }
368 }
369
370 /**
371 * Send triggered notifications
372 */
373 public static function send_triggered_notification ($notification_id, $context_args) {
374 $send_to = get_post_meta($notification_id, 'notifier_notification_send_to', true);
375 $template_id = get_post_meta($notification_id, 'notifier_notification_message_template', true);
376
377 $sent_phone_numbers = get_post_meta($notification_id, 'notifier_notification_sent_phone_numbers', true);
378 $unsent_phone_numbers = get_post_meta($notification_id, 'notifier_notification_unsent_phone_numbers', true);
379
380 $sent_phone_numbers = (!$sent_phone_numbers) ? array() : $sent_phone_numbers;
381 $unsent_phone_numbers = (!$unsent_phone_numbers) ? array() : $unsent_phone_numbers;
382
383 $list_ids = array();
384
385 $phone_numbers = array();
386
387 if (false !== $send_to && is_array($send_to)) {
388 foreach ($send_to as $recipient) {
389 switch ($recipient['type']) {
390 case 'contact':
391 $phone_number = get_post_meta( $recipient['recipient']['contact'], NOTIFIER_PREFIX . 'wa_number', true);
392 if ($phone_number) {
393 $phone_numbers[] = $phone_number;
394 }
395 break;
396
397 case 'list':
398 $list_ids[] = $recipient['recipient']['list'];
399 break;
400
401 case 'customer':
402 $order = wc_get_order($context_args['object_id']);
403 $phone_number = $order->get_billing_phone();
404 $country_code = $order->get_billing_country();
405 $phone_numbers[] = Notifier_Contacts::get_formatted_phone_number($phone_number, $country_code);
406 break;
407 }
408 }
409 }
410
411 foreach ($phone_numbers as $phone_number) {
412 $message_sent = Notifier_Message_Templates::send_message_template_to_number($template_id, $notification_id, $phone_number, $context_args);
413
414 if ($message_sent) {
415 $sent_numbers[] = $phone_number;
416 } else {
417 $unsent_numbers[] = $phone_number;
418 }
419 }
420
421 update_post_meta($notification_id, 'notifier_notification_sent_phone_numbers', $sent_numbers);
422 update_post_meta($notification_id, 'notifier_notification_unsent_phone_numbers', $unsent_numbers);
423 }
424
425 /**
426 * Add body class
427 */
428 public static function admin_body_class ($classes) {
429 global $post_id, $current_screen;
430 if ( 'wa_notification' == $current_screen->id ) {
431 $sent = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_status', true);
432 if (in_array($sent, array('Sending', 'Sent', 'Scheduled'))) {
433 $classes = $classes . ' disable-publishing';
434 }
435 }
436 return $classes;
437 }
438
439 /**
440 * Add columns to list page
441 */
442 public static function add_columns ($columns) {
443 $columns['notification_type'] = 'Type';
444 $columns['notification_status'] = 'Status';
445 $columns['notification_stats'] = 'Stats';
446 unset($columns['date']);
447 return $columns;
448 }
449
450 /**
451 * Add column content
452 */
453 public static function add_column_content ( $column, $post_id ) {
454 if ( 'notification_type' === $column ) {
455 $notification_type = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_type', true);
456 echo ($notification_type) ? '<code>' . esc_html($notification_type) . '</code>' : '-';
457 }
458
459 if ( 'notification_status' === $column ) {
460 $notification_status = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_status', true);
461 echo ($notification_status) ? '<code>' . esc_html($notification_status) . '</code>' : '-';
462 }
463
464 if ( 'notification_stats' === $column ) {
465 $sent_phone_numbers = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_sent_phone_numbers', true);
466 echo '<b>Sent: </b>';
467 echo ($sent_phone_numbers && is_array($sent_phone_numbers)) ? count($sent_phone_numbers) : '0';
468 $unsent_phone_numbers = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_unsent_phone_numbers', true);
469 echo '<br /><b>Failed: </b>';
470 echo ($unsent_phone_numbers && is_array($unsent_phone_numbers)) ? count($unsent_phone_numbers) : '0';
471 }
472 }
473
474 /**
475 * Get notification statuses
476 */
477 public static function get_notification_statuses () {
478 return array('Sending', 'Sent', 'Scheduled', 'On-going');
479 }
480
481 /**
482 * Fetch Send to fields
483 */
484 public static function fetch_send_to_fields () {
485 $post_id = isset($_POST['post_id']) ? intval( $_POST['post_id'] ) : 0;
486 $trigger = isset($_POST['trigger']) ? sanitize_text_field( wp_unslash( $_POST['trigger'] ) ) : '';
487 $send_to = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_send_to', true);
488 $html = '<label>Recipients</label>';
489 $html .= '<table class="fields-repeater">
490 <tbody>
491 <tr>
492 <th>Type</th>
493 <th>Recipient</th>
494 <th></th>
495 </tr>';
496
497 if ($send_to && is_array($send_to)) {
498 $row = 0;
499 foreach ($send_to as $recipient) {
500 $html .= self::get_notification_send_to_fields_row($row, $recipient, $post_id, $trigger, $disabled);
501 $row++;
502 }
503 } else {
504 $html .= self::get_notification_send_to_fields_row(0, null, $post_id, $trigger);
505 }
506 $html .= self::get_notification_send_to_fields_row('row_num', null, $post_id, $trigger);
507 $html .= '</tbody>
508 </table>
509 <div class="d-flex">
510 <p class="description">Add recipients for this notification. You can click on Add Recipient to add more than one recipient.</p>
511 <a href="" class="button add-recipient">Add recipient</a>
512 </div>';
513
514 $response = array (
515 'status' => 'success',
516 'html' => $html
517 );
518
519 echo json_encode($response);
520 wp_die();
521 }
522
523 /**
524 * Generates "Send to" field row
525 */
526 public static function get_notification_send_to_fields_row ($num = 0, $data = array(), $post_id = 0, $trigger = '', $disabled = array()) {
527 ob_start();
528 require NOTIFIER_PATH . 'views/templates/admin-notification-send-to-fields-row.php';
529 $html = ob_get_clean();
530 return $html;
531 }
532
533 /**
534 * Handle AJAX call to get contacts / lists
535 */
536 public static function get_wa_contacts_data() {
537 $contacts = Notifier_Contacts::get_contacts();
538 echo json_encode( $contacts );
539 die;
540 }
541
542 }
543