$data) {
if (strpos($key, NOTIFIER_PREFIX) !== false) {
if (is_array($data)) {
$notification_data[$key] = notifier_sanitize_array($data);
} else {
$notification_data[$key] = sanitize_text_field( wp_unslash ($data) );
}
update_post_meta( $post_id, $key, $notification_data[$key]);
}
}
if (!empty($notification_data)) {
self::setup_notifcation($notification_data, $post_id);
}
}
/**
* Update save action messages
*/
public static function update_save_messages ($messages) {
$messages['wa_notification'][1] = 'Notification updated.';
$messages['wa_notification'][6] = 'Notification saved.';
return $messages;
}
/**
* Remove inline edit from Bulk Edit
*/
public static function remove_bulk_actions( $actions ) {
unset( $actions['inline'] );
return $actions;
}
/**
* Remove inline Quick Edit
*/
public static function remove_quick_edit( $actions, $post ) {
if ('wa_notification' == $post->post_type) {
unset($actions['inline hide-if-no-js']);
}
return $actions;
}
/**
* Change text of buttons and links
*/
public static function change_texts( $translation, $text ) {
if ( 'wa_notification' == get_post_type() ) {
if ( 'Update' === $text ) {
return 'Update';
} elseif ( 'Publish' === $text ) {
return 'Save';
}
}
return $translation;
}
/**
* Notifications JS variables
*/
public static function notifications_js_variables ($variables) {
return $variables;
}
/**
* Fetch and return message template data
*/
public static function fetch_message_template_data() {
if (!isset($_POST['template_id'])) {
wp_die();
}
$template_id = intval($_POST['template_id']);
if ('wa_message_template' != get_post_type( $template_id ) ) {
wp_die();
}
$post_id = isset($_POST['post_id']) ? intval( $_POST['post_id'] ) : 0;
$notification_type = isset($_POST['notification_type']) ? sanitize_text_field( wp_unslash($_POST['notification_type']) ) : '';
$trigger = isset($_POST['trigger']) ? sanitize_text_field( wp_unslash($_POST['trigger']) ) : '';
if ('marketing' === $notification_type) {
$trigger = '';
}
$template_meta = array(
'header_type',
'header_text',
'body_text',
'footer_text',
'button_type',
'button_num',
'button_1_type',
'button_1_text',
'button_2_type',
'button_2_text'
);
$template_data = array ();
foreach ($template_meta as $meta) {
$template_data[$meta] = get_post_meta( $template_id, NOTIFIER_PREFIX . $meta, true);
}
$notification_status = get_post_meta ( $post_id, NOTIFIER_PREFIX . 'notification_status', true);
if ($notification_status && in_array($notification_status, array('Sending', 'Sent', 'Scheduled'))) {
$disabled = array (
'disabled' => 'disabled'
);
} else {
$disabled = array ();
}
$response = array (
'status' => 'success',
'data' => $template_data,
'notification_status' => $notification_status,
);
echo json_encode($response);
wp_die();
}
/**
* Setup notification
*/
public static function setup_notifcation($data, $id) {
$type = isset($data['notifier_notification_type']) ? $data['notifier_notification_type'] : 'marketing';
switch ($type) {
case 'marketing':
self::schedule_notification_to_list($data, $id);
break;
case 'transactional':
self::setup_transactional_notification($data, $id);
break;
}
}
/**
* Schedule marketing notification to list
*/
public static function schedule_notification_to_list ($notification_data, $notification_id) {
if ( as_has_scheduled_action( 'notifier_marketing_notification', array($notification_id), 'notifier' ) ) {
as_unschedule_action('notifier_marketing_notification', array($notification_id), 'notifier');
}
update_post_meta ( $notification_id, NOTIFIER_PREFIX . 'notification_status', 'Scheduled');
// Remove from triggers if this notification was earlier a transactional one
$active_triggers = get_option('notifier_active_triggers');
if (false !== $active_triggers) {
foreach ($active_triggers as $trigger => $notif_ids) {
$key = array_search($notification_id, $notif_ids);
if (false !== $key) {
unset($notif_ids[$key]);
}
$active_triggers[$trigger] = $notif_ids;
}
}
update_option('notifier_active_triggers', $active_triggers, true);
$when = isset($notification_data['notifier_notification_when']) ? $notification_data['notifier_notification_when'] : 'now';
if ('now' == $when) {
as_enqueue_async_action( 'notifier_marketing_notification', array($notification_id), 'notifier' );
} else {
$datetime = isset($notification_data['notifier_notification_datetime']) ? $notification_data['notifier_notification_datetime'] : '';
$timestamp = strtotime($datetime);
as_schedule_single_action( $timestamp, 'notifier_marketing_notification', array($notification_id), 'notifier' );
}
}
/**
* Setup transactional notification
*/
public static function setup_transactional_notification ($notification_data, $notification_id) {
update_post_meta ( $notification_id, NOTIFIER_PREFIX . 'notification_status', 'On-going');
$trigger = $notification_data['notifier_notification_trigger'];
$active_triggers = get_option('notifier_active_triggers');
if ( false !== $active_triggers ) {
foreach ($active_triggers as $t => $notif_ids) {
$key = array_search($notification_id, $notif_ids);
if (false !== $key) {
unset($notif_ids[$key]);
}
$active_triggers[$t] = $notif_ids;
}
$active_triggers[$trigger][] = $notification_id;
} else {
$active_triggers = array(
$trigger => array($notification_id)
);
}
update_option('notifier_active_triggers', $active_triggers, true);
}
/**
* Delete notification ID from active triggers
*/
public static function delete_from_active_triggers ($post_id) {
$active_triggers = get_option('notifier_active_triggers');
if (false !== $active_triggers) {
foreach ($active_triggers as $trigger => $notif_ids) {
$key = array_search($post_id, $notif_ids);
if (false !== $key) {
unset($notif_ids[$key]);
}
$active_triggers[$trigger] = $notif_ids;
}
}
update_option('notifier_active_triggers', $active_triggers, true);
}
/**
* Send scheduled broadcast notifications
*/
public static function send_broadcast_notifications ($notification_id) {
update_post_meta ( $notification_id, NOTIFIER_PREFIX . 'notification_status', 'Sending');
$list_slug = get_post_meta($notification_id, 'notifier_notification_list', true);
$template_id = get_post_meta($notification_id, 'notifier_notification_message_template', true);
$list_offset = get_post_meta($notification_id, 'notifier_notification_list_offset', true);
$sent_phone_numbers = get_post_meta($notification_id, 'notifier_notification_sent_phone_numbers', true);
$unsent_phone_numbers = get_post_meta($notification_id, 'notifier_notification_unsent_phone_numbers', true);
$sent_phone_numbers = (isset($sent_phone_numbers)) ? $sent_phone_numbers : array();
$unsent_phone_numbers = (isset($unsent_phone_numbers)) ? $unsent_phone_numbers : array();
$offset = (!$list_offset) ? 0 : (int) $list_offset;
$count = 0;
$limit = get_option( NOTIFIER_PREFIX . 'bulk_message_batch_limit', 50 );
$contact_ids = get_posts( array(
'post_type' => 'wa_contact',
'post_status' => 'publish',
'wa_contact_list' => $list_slug,
'fields' => 'ids',
'offset' => $offset,
'posts_per_page' => $limit
) );
foreach ($contact_ids as $contact_id) {
$count++;
$phone_number = get_post_meta( $contact_id, NOTIFIER_PREFIX . 'wa_number', true);
if ('' == $phone_number) {
continue;
}
$message_sent = Notifier_Message_Templates::send_message_template_to_number($template_id, $notification_id, $phone_number);
if ($message_sent) {
$sent_numbers[] = $phone_number;
} else {
$unsent_numbers[] = $phone_number;
}
}
update_post_meta($notification_id, 'notifier_notification_sent_phone_numbers', $sent_numbers);
update_post_meta($notification_id, 'notifier_notification_unsent_phone_numbers', $unsent_numbers);
$new_offset = $offset + $limit;
update_post_meta($notification_id, 'notifier_notification_list_offset', $new_offset);
if ($count == $limit) {
as_enqueue_async_action( 'notifier_marketing_notification', array($notification_id), 'notifier' );
} else {
update_post_meta ( $notification_id, NOTIFIER_PREFIX . 'notification_status', 'Sent');
}
}
/**
* Send triggered notifications
*/
public static function send_triggered_notification ($notification_id, $context_args) {
$send_to = get_post_meta($notification_id, 'notifier_notification_send_to', true);
$template_id = get_post_meta($notification_id, 'notifier_notification_message_template', true);
$sent_phone_numbers = get_post_meta($notification_id, 'notifier_notification_sent_phone_numbers', true);
$unsent_phone_numbers = get_post_meta($notification_id, 'notifier_notification_unsent_phone_numbers', true);
$sent_phone_numbers = (!$sent_phone_numbers) ? array() : $sent_phone_numbers;
$unsent_phone_numbers = (!$unsent_phone_numbers) ? array() : $unsent_phone_numbers;
$list_ids = array();
$phone_numbers = array();
if (false !== $send_to && is_array($send_to)) {
foreach ($send_to as $recipient) {
switch ($recipient['type']) {
case 'contact':
$phone_number = get_post_meta( $recipient['recipient']['contact'], NOTIFIER_PREFIX . 'wa_number', true);
if ($phone_number) {
$phone_numbers[] = $phone_number;
}
break;
case 'list':
$list_ids[] = $recipient['recipient']['list'];
break;
case 'customer':
$order = wc_get_order($context_args['object_id']);
$phone_number = $order->get_billing_phone();
$country_code = $order->get_billing_country();
$phone_numbers[] = Notifier_Contacts::get_formatted_phone_number($phone_number, $country_code);
break;
}
}
}
foreach ($phone_numbers as $phone_number) {
$message_sent = Notifier_Message_Templates::send_message_template_to_number($template_id, $notification_id, $phone_number, $context_args);
if ($message_sent) {
$sent_numbers[] = $phone_number;
} else {
$unsent_numbers[] = $phone_number;
}
}
update_post_meta($notification_id, 'notifier_notification_sent_phone_numbers', $sent_numbers);
update_post_meta($notification_id, 'notifier_notification_unsent_phone_numbers', $unsent_numbers);
}
/**
* Add body class
*/
public static function admin_body_class ($classes) {
global $post_id, $current_screen;
if ( 'wa_notification' == $current_screen->id ) {
$sent = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_status', true);
if (in_array($sent, array('Sending', 'Sent', 'Scheduled'))) {
$classes = $classes . ' disable-publishing';
}
}
return $classes;
}
/**
* Add columns to list page
*/
public static function add_columns ($columns) {
$columns['notification_type'] = 'Type';
$columns['notification_status'] = 'Status';
$columns['notification_stats'] = 'Stats';
unset($columns['date']);
return $columns;
}
/**
* Add column content
*/
public static function add_column_content ( $column, $post_id ) {
if ( 'notification_type' === $column ) {
$notification_type = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_type', true);
echo ($notification_type) ? '' . esc_html($notification_type) . '' : '-';
}
if ( 'notification_status' === $column ) {
$notification_status = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_status', true);
echo ($notification_status) ? '' . esc_html($notification_status) . '' : '-';
}
if ( 'notification_stats' === $column ) {
$sent_phone_numbers = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_sent_phone_numbers', true);
echo 'Sent: ';
echo ($sent_phone_numbers && is_array($sent_phone_numbers)) ? count($sent_phone_numbers) : '0';
$unsent_phone_numbers = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_unsent_phone_numbers', true);
echo '
Failed: ';
echo ($unsent_phone_numbers && is_array($unsent_phone_numbers)) ? count($unsent_phone_numbers) : '0';
}
}
/**
* Get notification statuses
*/
public static function get_notification_statuses () {
return array('Sending', 'Sent', 'Scheduled', 'On-going');
}
/**
* Fetch Send to fields
*/
public static function fetch_send_to_fields () {
$post_id = isset($_POST['post_id']) ? intval( $_POST['post_id'] ) : 0;
$trigger = isset($_POST['trigger']) ? sanitize_text_field( wp_unslash( $_POST['trigger'] ) ) : '';
$send_to = get_post_meta( $post_id, NOTIFIER_PREFIX . 'notification_send_to', true);
$html = '';
$html .= '
| Type | Recipient |
|---|
Add recipients for this notification. You can click on Add Recipient to add more than one recipient.
Add recipient