register_hooks(); } public function register_hooks() { add_action('add_meta_boxes', [$this, 'add_alertwise_site_not_connected_metabox']); if (!Options::has_credentials() || PlanValidator::isPlanInValid()) return; add_action('add_meta_boxes', [$this, 'add_aw_settings_metabox']); add_action('save_post', [$this, 'save_aw_post_metabox_data']); } public function add_aw_settings_metabox() { add_meta_box( 'alertwise_notification_box', 'Alertwise Push Notification', [$this, 'render_notification_metabox'], ['post', 'page'], 'side', 'high' ); } public function render_notification_metabox($post) { $settings = Options::get_post_page_settings(); $auto_send = get_post_meta($post->ID, '_alertwise_auto_send', true); if ($auto_send === '') { $auto_send = '0'; if ($post->post_type === 'post' && !empty($settings['post_notifications'])) { $auto_send = '1'; } elseif ($post->post_type === 'page' && !empty($settings['page_notifications'])) { $auto_send = '1'; } } $data = [ 'post' => $post, 'auto_send' => $auto_send, 'notif_title' => get_post_meta($post->ID, '_alertwise_custom_title', true), 'notif_msg' => get_post_meta($post->ID, '_alertwise_custom_msg', true), 'already_sent' => get_post_meta($post->ID, '_alertwise_sent', true), 'selected_group_id' => get_post_meta($post->ID, '_alertwise_group_id', true), 'action1_title' => get_post_meta($post->ID, '_alertwise_action1_title', true), 'action1_url' => get_post_meta($post->ID, '_alertwise_action1_url', true), 'action1_icon' => get_post_meta($post->ID, '_alertwise_action1_icon', true), 'action2_title' => get_post_meta($post->ID, '_alertwise_action2_title', true), 'action2_url' => get_post_meta($post->ID, '_alertwise_action2_url', true), 'action2_icon' => get_post_meta($post->ID, '_alertwise_action2_icon', true), 'show_action2' => get_post_meta($post->ID, '_alertwise_show_action2', true), ]; Alertwise::output_view('post-editor-metabox.php', $data); } public function save_aw_post_metabox_data($post_id) { if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || wp_is_post_revision($post_id)) return; if (isset($_POST['alertwise_meta_box_nonce'])) { if (!wp_verify_nonce($_POST['alertwise_meta_box_nonce'], 'alertwise_meta_box')) return; $selected_group_id = sanitize_text_field($_POST['alertwise_group_dropdown'] ?? ''); update_post_meta($post_id, '_alertwise_group_id', $selected_group_id); update_post_meta($post_id, '_alertwise_auto_send', isset($_POST['alertwise_auto_send']) ? '1' : '0'); update_post_meta($post_id, '_alertwise_custom_title', sanitize_text_field(wp_unslash($_POST['alertwise_custom_title'] ?? ''))); update_post_meta($post_id, '_alertwise_custom_msg', sanitize_textarea_field(wp_unslash($_POST['alertwise_custom_msg'] ?? ''))); update_post_meta($post_id, '_alertwise_action1_title', sanitize_text_field(wp_unslash($_POST['alertwise_action1_title'] ?? ''))); update_post_meta($post_id, '_alertwise_action1_url', esc_url_raw(wp_unslash($_POST['alertwise_action1_url'] ?? ''))); update_post_meta($post_id, '_alertwise_action1_icon', esc_url_raw(wp_unslash($_POST['alertwise_action1_icon'] ?? ''))); $show_action2 = isset($_POST['alertwise_show_action2']) ? '1' : '0'; update_post_meta($post_id, '_alertwise_show_action2', $show_action2); if ($show_action2 === '1') { update_post_meta($post_id, '_alertwise_action2_title', sanitize_text_field(wp_unslash($_POST['alertwise_action2_title'] ?? ''))); update_post_meta($post_id, '_alertwise_action2_url', esc_url_raw(wp_unslash($_POST['alertwise_action2_url'] ?? ''))); update_post_meta($post_id, '_alertwise_action2_icon', esc_url_raw(wp_unslash($_POST['alertwise_action2_icon'] ?? ''))); } else { delete_post_meta($post_id, '_alertwise_action2_title'); delete_post_meta($post_id, '_alertwise_action2_url'); delete_post_meta($post_id, '_alertwise_action2_icon'); } $force_resend = !empty($_POST['alertwise_force_resend']) && $_POST['alertwise_force_resend'] === '1'; $post = get_post($post_id); if ($force_resend) { if ($post && $post->post_status === 'future') { update_post_meta($post_id, '_alertwise_resend_on_future_publish', '1'); } else { $this->auto_send_notification($post_id, true, $selected_group_id); } } else { delete_post_meta($post_id, '_alertwise_resend_on_future_publish'); } } else { $post = get_post($post_id); $resend_on_future_publish = get_post_meta($post_id, '_alertwise_resend_on_future_publish', true); if ($post && $post->post_status === 'publish' && $resend_on_future_publish === '1') { $selected_group_id = get_post_meta($post_id, '_alertwise_group_id', true); $this->auto_send_notification($post_id, true, $selected_group_id); delete_post_meta($post_id, '_alertwise_resend_on_future_publish'); } } $post = get_post($post_id); if (!$post) { error_log("[Alertwise] No post found for post ID: $post_id"); return; } if ($post->post_status !== 'publish') { error_log("[Alertwise] Post status is '{$post->post_status}' not 'publish' for post ID: $post_id"); return; } if (get_post_meta($post_id, '_alertwise_auto_send', true) !== '1') { error_log("[Alertwise] Auto-send not enabled in DB for post ID: $post_id"); return; } if (get_post_meta($post_id, '_alertwise_sent', true)) { error_log("[Alertwise] Notification already sent for post ID: $post_id"); return; } error_log("[Alertwise] Triggering auto-send notification for post ID: $post_id"); $selected_group_id = get_post_meta($post_id, '_alertwise_group_id', true); $this->auto_send_notification($post_id, false, $selected_group_id); } public function auto_send_notification($post_id, $force_resend = false, $selected_group_id = '') { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; $post = get_post($post_id); if (!$post || $post->post_status !== 'publish') return; if (get_post_meta($post_id, '_alertwise_sent', true) && !$force_resend) return; if (get_post_meta($post_id, '_alertwise_auto_send', true) !== '1') return; if ($force_resend) update_post_meta($post_id, '_alertwise_sent', false); $title = get_post_meta($post_id, '_alertwise_custom_title', true) ?: get_the_title($post_id); $message = get_post_meta($post_id, '_alertwise_custom_msg', true) ?: wp_trim_words(wp_strip_all_tags($post->post_content), 20); $url = get_permalink($post_id); $action1 = [ 'title' => get_post_meta($post_id, '_alertwise_action1_title', true), 'url' => get_post_meta($post_id, '_alertwise_action1_url', true), 'iconUrl' => get_post_meta($post_id, '_alertwise_action1_icon', true), ]; $show_action2 = get_post_meta($post_id, '_alertwise_show_action2', true); $action2 = ($show_action2 === '1') ? [ 'title' => get_post_meta($post_id, '_alertwise_action2_title', true), 'url' => get_post_meta($post_id, '_alertwise_action2_url', true), 'iconUrl' => get_post_meta($post_id, '_alertwise_action2_icon', true), ] : []; $this->send_push_notification($title, $message, $url, $post_id, $selected_group_id, $action1, $action2); update_post_meta($post_id, '_alertwise_sent', true); } private function send_push_notification($title, $message, $url, $post_id, $selected_group_id = '', $action1 = [], $action2 = []) { $settings = Options::get_site_settings(); $app_id = $settings['alertwise_setting_app_id']; $icon_url = $settings['alertwise_setting_icon_url']; $notification_body = [ 'appId' => $app_id, 'url' => $url, 'icon' => $icon_url, 'message' => $message, 'requireInteraction' => true, 'title' => $title, 'ttl' => 259200, 'urgency' => 'normal', 'notificationAudience' => !empty($selected_group_id) ? 'audience' : 'all' ]; $image_url = get_the_post_thumbnail_url($post_id, 'full'); if ($image_url) $notification_body['largeImage'] = $image_url; if (!empty($action1['title']) && !empty($action1['url'])) { $notification_body['firstActionButton'] = $notification_body['action1'] = [ 'title' => sanitize_text_field($action1['title']), 'url' => esc_url_raw($action1['url']), 'iconUrl' => esc_url_raw($action1['iconUrl']), ]; } if (!empty($action2['title']) && !empty($action2['url'])) { $notification_body['secondActionButton'] = $notification_body['action2'] = [ 'title' => sanitize_text_field($action2['title']), 'url' => esc_url_raw($action2['url']), 'iconUrl' => esc_url_raw($action2['iconUrl']), ]; } $payload = [ 'appId' => $app_id, 'state' => 'NEW', 'type' => 'NOTIFICATION', 'scheduleType' => 'IMMEDIATE', 'notification' => ['body' => $notification_body], 'audience' => !empty($selected_group_id) ? ['audienceSize' => 0, 'audienceId' => $selected_group_id] : ['type' => 'ALL'] ]; if (!empty($selected_group_id)) { $payload['notification']['body']['audienceId'] = $selected_group_id; } error_log('[Alertwise Payload] Final Request Payload for Post ID ' . $post_id . ': ' . json_encode($payload, JSON_PRETTY_PRINT)); $token = TokenManager::get_token(); if ($token) { AlertwiseHttpClient::send_push_notification($token, $payload); } elseif (!empty($settings['alertwise_setting_username']) && !empty($settings['alertwise_setting_password'])) { unset($settings['alertwise_setting_username'], $settings['alertwise_setting_password']); Options::update_site_settings($settings); } } public function add_alertwise_site_not_connected_metabox() { if (Options::has_credentials()) return; $screens = array_column(PublicPostTypes::get_all(), 'value'); if (empty($screens)) return; add_meta_box( 'alertwise-site-not-connected-metabox', esc_html__('AlertWise Push Notification Settings', 'alertwise'), [$this, 'render_site_not_connected_metabox'], $screens, 'normal', apply_filters('alertwise_site_not_connected_metabox_priority', 'high') ); } public function render_site_not_connected_metabox() { Alertwise::output_view('site-not-connected-metabox.php'); } }