diff --git a/cleverpush-api.php b/cleverpush-api.php index 5236cab..488d35e 100644 --- a/cleverpush-api.php +++ b/cleverpush-api.php @@ -85,6 +85,33 @@ public static function send_notification($title, $body, $url, $options = array() return CleverPush_Api::request('/notification/send', $params); } + public static function delete_notification($notification_id) + { + $channel_id = get_option('cleverpush_channel_id'); + $api_key_private = get_option('cleverpush_apikey_private'); + + if (empty($channel_id) || empty($notification_id) || empty($api_key_private)) { + return false; + } + + $response = wp_remote_request( + CLEVERPUSH_API_ENDPOINT . '/channel/' . rawurlencode($channel_id) . '/notification/' . rawurlencode($notification_id), + array( + 'method' => 'DELETE', + 'timeout' => 20, // phpcs:ignore + 'headers' => array( + 'authorization' => $api_key_private + ) + ) + ); + + if (is_wp_error($response)) { + return false; + } + + return wp_remote_retrieve_response_code($response) >= 200 && wp_remote_retrieve_response_code($response) < 300; + } + public static function update_channel($channel_id, $params = array()) { if (empty($channel_id)) { diff --git a/cleverpush.php b/cleverpush.php index f7ee2da..e31fe89 100644 --- a/cleverpush.php +++ b/cleverpush.php @@ -4,7 +4,7 @@ Plugin URI: https://cleverpush.com Description: Send push notifications to your users right through your website. Visit CleverPush for more details. Author: CleverPush -Version: 1.9.8 +Version: 1.9.9 Author URI: https://cleverpush.com Text Domain: cleverpush Domain Path: /languages @@ -43,6 +43,8 @@ public function __construct() add_action('admin_notices', array($this, 'warn_nosettings')); add_action('add_meta_boxes', array($this, 'create_metabox')); add_action('save_post', array($this, 'save_post'), 10, 2); + add_action('transition_post_status', array($this, 'cleanup_draft_notification_for_status_change'), 10, 3); + add_action('before_delete_post', array($this, 'delete_draft_notification')); add_action('admin_notices', array($this, 'notices')); add_action('publish_post', array($this, 'publish_post'), 10, 1); @@ -414,6 +416,17 @@ function ajax_load_options() $hidden_notification_settings = get_option('cleverpush_channel_hidden_notification_settings'); if (!empty($api_key_private) && !empty($selected_channel_id)) { + $post_id = isset($_POST['post_id']) ? absint(wp_unslash($_POST['post_id'])) : 0; + $saved_targeting = array(); + if ($post_id && current_user_can('edit_post', $post_id)) { + $saved_targeting = get_post_meta($post_id, 'cleverpush_notification_targeting', true); + if (!is_array($saved_targeting)) { + $saved_targeting = array(); + } + } + $saved_topics = isset($saved_targeting['topics']) && is_array($saved_targeting['topics']) ? array_map('strval', $saved_targeting['topics']) : array(); + $saved_tags = isset($saved_targeting['tags']) && is_array($saved_targeting['tags']) ? array_map('strval', $saved_targeting['tags']) : array(); + $saved_segments = isset($saved_targeting['segments']) && is_array($saved_targeting['segments']) ? array_map('strval', $saved_targeting['segments']) : array(); $cleverpush_segments = array(); if (empty($hidden_notification_settings) || strpos($hidden_notification_settings, 'segments') === false) { @@ -540,24 +553,23 @@ function ajax_load_options()
- +
+ value="1" >