+ style="display: ; margin-left: 30px;" data-required="">
ID; ?>');
};
if (document.readyState === 'complete') {
@@ -1198,15 +1208,7 @@ public function publish_post($post_id)
}
$options = array();
- if (isset($_POST['cleverpush_use_segments']) && $_POST['cleverpush_use_segments'] == '1' && !empty($_POST['cleverpush_segments'])) {
- $options['segments'] = array_map('sanitize_text_field', $_POST['cleverpush_segments']);
- }
- if (isset($_POST['cleverpush_use_topics']) && $_POST['cleverpush_use_topics'] == '1' && !empty($_POST['cleverpush_topics'])) {
- $options['topics'] = array_map('sanitize_text_field', $_POST['cleverpush_topics']);
- }
- if (isset($_POST['cleverpush_use_tags']) && $_POST['cleverpush_use_tags'] == '1' && !empty($_POST['cleverpush_tags'])) {
- $options['tags'] = array_map('sanitize_text_field', $_POST['cleverpush_tags']);
- }
+ $options = array_merge($options, $this->get_notification_targeting($post_id, $metabox_submitted));
$thumbnail_url = get_the_post_thumbnail_url($post_id);
if (!empty($thumbnail_url)) {
$options['mediaUrl'] = $thumbnail_url;
@@ -1214,7 +1216,7 @@ public function publish_post($post_id)
if (!empty($_POST['cleverpush_scheduled_at'])) {
$options['scheduledAt'] = sanitize_text_field(wp_unslash($_POST['cleverpush_scheduled_at']));
- } else {
+ } elseif (!$metabox_submitted) {
$saved_scheduled_at = get_post_meta($post_id, 'cleverpush_scheduled_at', true);
$saved_timestamp = !empty($saved_scheduled_at) ? strtotime($saved_scheduled_at) : false;
if ($saved_timestamp && $saved_timestamp > time()) {
@@ -1262,6 +1264,9 @@ public function save_post($post_id, $post)
if (isset($_POST['cleverpush_metabox_form_data_available']) && isset($_POST['cleverpush_scheduled_at'])) {
update_post_meta($post_id, 'cleverpush_scheduled_at', sanitize_text_field(wp_unslash($_POST['cleverpush_scheduled_at'])));
}
+ if (isset($_POST['cleverpush_metabox_form_data_available'])) {
+ update_post_meta($post_id, 'cleverpush_notification_targeting', $this->get_notification_targeting($post_id, true));
+ }
$this->sync_draft_notification($post_id);
@@ -1297,12 +1302,13 @@ public function sync_draft_notification($post_id)
return;
}
- if (!isset($_POST['cleverpush_metabox_form_data_available'])) {
+ $status = get_post_status($post_id);
+ if (!in_array($status, array('draft', 'future', 'pending'), true)) {
+ $this->delete_draft_notification($post_id);
return;
}
- $status = get_post_status($post_id);
- if (!in_array($status, array('draft', 'future', 'pending'), true)) {
+ if (!isset($_POST['cleverpush_metabox_form_data_available'])) {
return;
}
@@ -1312,6 +1318,9 @@ public function sync_draft_notification($post_id)
}
$title = html_entity_decode(get_the_title($post_id));
+ if (get_option('cleverpush_notification_title_required') == 'on') {
+ $title = null;
+ }
if (!empty($_POST['cleverpush_title'])) {
$title = sanitize_text_field(wp_unslash($_POST['cleverpush_title']));
}
@@ -1320,6 +1329,7 @@ public function sync_draft_notification($post_id)
$text = sanitize_text_field(wp_unslash($_POST['cleverpush_text']));
}
if (empty($title)) {
+ $this->delete_draft_notification($post_id);
return;
}
@@ -1329,24 +1339,23 @@ public function sync_draft_notification($post_id)
$scheduled_at = '';
}
- $fingerprint = md5($title . '|' . $text . '|' . $scheduled_at);
+ $url = get_permalink($post_id);
+ $targeting = $this->get_notification_targeting($post_id, true);
+ $fingerprint = md5($title . '|' . $text . '|' . $url . '|' . $scheduled_at . '|' . wp_json_encode($targeting));
$existing_id = get_post_meta($post_id, 'cleverpush_draft_notification_id', true);
$existing_fingerprint = get_post_meta($post_id, 'cleverpush_draft_notification_hash', true);
if (!empty($existing_id) && $existing_fingerprint === $fingerprint) {
+ $this->retry_pending_draft_deletions($post_id);
return;
}
- if (!empty($existing_id)) {
- $this->delete_draft_notification($post_id);
- }
-
- $options = array('status' => 'draft');
+ $options = array_merge(array('status' => 'draft'), $targeting);
if (!empty($scheduled_at)) {
$options['scheduledAt'] = $scheduled_at;
}
try {
- $response = CleverPush_Api::send_notification($title, $text, get_permalink($post_id), $options);
+ $response = CleverPush_Api::send_notification($title, $text, $url, $options);
if (empty($response) || is_wp_error($response)) {
return;
}
@@ -1354,8 +1363,13 @@ public function sync_draft_notification($post_id)
if (empty($body->notificationId)) {
return;
}
- update_post_meta($post_id, 'cleverpush_draft_notification_id', sanitize_text_field($body->notificationId));
+ $new_notification_id = sanitize_text_field($body->notificationId);
+ update_post_meta($post_id, 'cleverpush_draft_notification_id', $new_notification_id);
update_post_meta($post_id, 'cleverpush_draft_notification_hash', $fingerprint);
+ if (!empty($existing_id) && $existing_id !== $new_notification_id) {
+ $this->delete_draft_notification_by_id($post_id, $existing_id);
+ }
+ $this->retry_pending_draft_deletions($post_id);
} catch (Exception $ex) {
update_option('cleverpush_notification_error', $ex->getMessage());
}
@@ -1365,10 +1379,83 @@ public function delete_draft_notification($post_id)
{
$notification_id = get_post_meta($post_id, 'cleverpush_draft_notification_id', true);
if (!empty($notification_id)) {
- CleverPush_Api::delete_notification($notification_id);
+ if (!CleverPush_Api::delete_notification($notification_id)) {
+ $this->queue_draft_notification_deletion($post_id, $notification_id);
+ return false;
+ }
}
delete_post_meta($post_id, 'cleverpush_draft_notification_id');
delete_post_meta($post_id, 'cleverpush_draft_notification_hash');
+ $this->retry_pending_draft_deletions($post_id);
+ return true;
+ }
+
+ public function delete_draft_notification_by_id($post_id, $notification_id)
+ {
+ if (!CleverPush_Api::delete_notification($notification_id)) {
+ $this->queue_draft_notification_deletion($post_id, $notification_id);
+ return false;
+ }
+ return true;
+ }
+
+ public function queue_draft_notification_deletion($post_id, $notification_id)
+ {
+ $pending_ids = get_post_meta($post_id, 'cleverpush_draft_notification_ids_to_delete', true);
+ $pending_ids = is_array($pending_ids) ? $pending_ids : array();
+ if (!in_array($notification_id, $pending_ids, true)) {
+ $pending_ids[] = $notification_id;
+ update_post_meta($post_id, 'cleverpush_draft_notification_ids_to_delete', $pending_ids);
+ }
+ }
+
+ public function retry_pending_draft_deletions($post_id)
+ {
+ $pending_ids = get_post_meta($post_id, 'cleverpush_draft_notification_ids_to_delete', true);
+ if (!is_array($pending_ids) || empty($pending_ids)) {
+ return;
+ }
+
+ $remaining_ids = array();
+ foreach ($pending_ids as $notification_id) {
+ if (!CleverPush_Api::delete_notification($notification_id)) {
+ $remaining_ids[] = $notification_id;
+ }
+ }
+ if (empty($remaining_ids)) {
+ delete_post_meta($post_id, 'cleverpush_draft_notification_ids_to_delete');
+ } else {
+ update_post_meta($post_id, 'cleverpush_draft_notification_ids_to_delete', $remaining_ids);
+ }
+ }
+
+ public function cleanup_draft_notification_for_status_change($new_status, $old_status, $post)
+ {
+ if (in_array($new_status, array('draft', 'future', 'pending'), true)) {
+ return;
+ }
+ $this->delete_draft_notification($post->ID);
+ }
+
+ public function get_notification_targeting($post_id, $from_request = false)
+ {
+ if (!$from_request) {
+ $targeting = get_post_meta($post_id, 'cleverpush_notification_targeting', true);
+ return is_array($targeting) ? $targeting : array();
+ }
+
+ $targeting = array();
+ if (isset($_POST['cleverpush_use_segments']) && $_POST['cleverpush_use_segments'] == '1' && !empty($_POST['cleverpush_segments'])) {
+ $targeting['segments'] = array_map('sanitize_text_field', wp_unslash($_POST['cleverpush_segments']));
+ }
+ if (isset($_POST['cleverpush_use_topics']) && $_POST['cleverpush_use_topics'] == '1' && !empty($_POST['cleverpush_topics'])) {
+ $targeting['topics'] = array_map('sanitize_text_field', wp_unslash($_POST['cleverpush_topics']));
+ }
+ if (isset($_POST['cleverpush_use_tags']) && $_POST['cleverpush_use_tags'] == '1' && !empty($_POST['cleverpush_tags'])) {
+ $targeting['tags'] = array_map('sanitize_text_field', wp_unslash($_POST['cleverpush_tags']));
+ }
+
+ return $targeting;
}
public function show_public_preview( $query )
From abbb3ca36f5ef2784e7dd3024cdc6f143f854a1a Mon Sep 17 00:00:00 2001
From: paavan-cleverpush
Date: Fri, 30 Oct 2026 14:19:32 +0530
Subject: [PATCH 3/3] Enhance notification targeting functionality in
CleverPush integration
- Introduce a new method to check if notification targeting options (topics, segments, tags) are submitted.
- Refactor the targeting retrieval process to streamline the handling of submitted data.
- Ensure that targeting options are updated correctly based on user input in the notification settings.
---
cleverpush.php | 39 ++++++++++++++++++++++++++++-----------
1 file changed, 28 insertions(+), 11 deletions(-)
diff --git a/cleverpush.php b/cleverpush.php
index 83e3286..e31fe89 100644
--- a/cleverpush.php
+++ b/cleverpush.php
@@ -1264,7 +1264,7 @@ public function save_post($post_id, $post)
if (isset($_POST['cleverpush_metabox_form_data_available']) && isset($_POST['cleverpush_scheduled_at'])) {
update_post_meta($post_id, 'cleverpush_scheduled_at', sanitize_text_field(wp_unslash($_POST['cleverpush_scheduled_at'])));
}
- if (isset($_POST['cleverpush_metabox_form_data_available'])) {
+ if (isset($_POST['cleverpush_metabox_form_data_available']) && $this->notification_targeting_submitted()) {
update_post_meta($post_id, 'cleverpush_notification_targeting', $this->get_notification_targeting($post_id, true));
}
@@ -1437,22 +1437,39 @@ public function cleanup_draft_notification_for_status_change($new_status, $old_s
$this->delete_draft_notification($post->ID);
}
+ public function notification_targeting_submitted()
+ {
+ return isset($_POST['cleverpush_use_topics'])
+ || isset($_POST['cleverpush_use_segments'])
+ || isset($_POST['cleverpush_use_tags']);
+ }
+
public function get_notification_targeting($post_id, $from_request = false)
{
+ $targeting = get_post_meta($post_id, 'cleverpush_notification_targeting', true);
+ if (!is_array($targeting)) {
+ $targeting = array();
+ }
if (!$from_request) {
- $targeting = get_post_meta($post_id, 'cleverpush_notification_targeting', true);
- return is_array($targeting) ? $targeting : array();
+ return $targeting;
}
- $targeting = array();
- if (isset($_POST['cleverpush_use_segments']) && $_POST['cleverpush_use_segments'] == '1' && !empty($_POST['cleverpush_segments'])) {
- $targeting['segments'] = array_map('sanitize_text_field', wp_unslash($_POST['cleverpush_segments']));
- }
- if (isset($_POST['cleverpush_use_topics']) && $_POST['cleverpush_use_topics'] == '1' && !empty($_POST['cleverpush_topics'])) {
- $targeting['topics'] = array_map('sanitize_text_field', wp_unslash($_POST['cleverpush_topics']));
+ $targeting = $this->replace_submitted_targeting($targeting, 'topics', 'cleverpush_use_topics', 'cleverpush_topics');
+ $targeting = $this->replace_submitted_targeting($targeting, 'segments', 'cleverpush_use_segments', 'cleverpush_segments');
+ $targeting = $this->replace_submitted_targeting($targeting, 'tags', 'cleverpush_use_tags', 'cleverpush_tags');
+
+ return $targeting;
+ }
+
+ public function replace_submitted_targeting($targeting, $key, $use_field, $value_field)
+ {
+ if (!isset($_POST[$use_field])) {
+ return $targeting;
}
- if (isset($_POST['cleverpush_use_tags']) && $_POST['cleverpush_use_tags'] == '1' && !empty($_POST['cleverpush_tags'])) {
- $targeting['tags'] = array_map('sanitize_text_field', wp_unslash($_POST['cleverpush_tags']));
+
+ unset($targeting[$key]);
+ if ($_POST[$use_field] == '1' && !empty($_POST[$value_field]) && is_array($_POST[$value_field])) {
+ $targeting[$key] = array_map('sanitize_text_field', wp_unslash($_POST[$value_field]));
}
return $targeting;