OneSignal – Free Web Push Notifications - Version 1.17.0

Version Description

  • Bug fixes, edge-case handling, refactoring
Download this release

Release Info

Developer OneSignal
Plugin Icon 128x128 OneSignal – Free Web Push Notifications
Version 1.17.0
Comparing to
See all releases

Code changes from version 1.16.16 to 1.17.0

Files changed (4) hide show
  1. notice.js +52 -41
  2. onesignal-admin.php +45 -15
  3. onesignal.php +1 -1
  4. readme.txt +5 -1
notice.js CHANGED
@@ -7,38 +7,48 @@ jQuery(document).ready(function() {
7
  const get_wp_attr = attr => {
8
  return editor.getEditedPostAttribute(attr);
9
  };
10
- var post_id = ajax_object.post_id;
11
- var started = false;
12
- var interval;
13
- var interval_count = 0;
 
 
 
 
14
 
15
  /*
16
- * Subscribes function to state-change listener
17
  * - checks change in post modified date
18
- * - triggers interval that checks if recipient data available in backend
19
  */
20
- var first_modified;
21
  wp.data.subscribe(() => {
22
- // runs with each change in state
23
  const post = wp.data.select("core/editor").getCurrentPost();
24
 
 
 
 
 
25
  // runs until post data loads
26
- if (!first_modified && post !== {}) {
27
- first_modified = post.modified;
 
28
  }
29
 
30
  // latest modified date
31
- const { modified } = post;
32
 
33
  // is checked
34
  const send_os_notif = jQuery("[name=send_onesignal_notification]").attr(
35
  "checked"
36
  );
37
 
 
 
38
  // if hasn't started and change is detected
39
- if (!started && modified !== first_modified && send_os_notif) {
40
- interval = setInterval(get_metadata, 3000);
41
- started = true;
42
  }
43
  });
44
 
@@ -49,20 +59,22 @@ jQuery(document).ready(function() {
49
  const get_metadata = () => {
50
  const data = {
51
  action: "has_metadata",
52
- post_id: post_id
53
  };
54
 
55
  jQuery.get(ajax_object.ajax_url, data, function(response) {
56
  response = JSON.parse(response);
57
  const { recipients, status_code, error_message } = response;
 
 
 
 
 
 
 
58
 
59
- if (status_code >= 400 || !status_code) {
60
- clearInterval(interval);
61
-
62
- if (!status_code) {
63
- error_notice("HTTP request failed");
64
- }
65
-
66
  if (!error_message) {
67
  error_notice(
68
  "OneSignal Push: there was a " +
@@ -73,40 +85,30 @@ jQuery(document).ready(function() {
73
  error_notice("OneSignal Push: " + error_message);
74
  }
75
 
76
- interval_count = 0;
77
- started = false;
78
- first_modified = null;
79
  return;
80
  }
81
 
82
  if (recipients == 0) {
83
- clearInterval(interval);
84
  error_notice(
85
- "OneSignal Push: there were no recipients. You either 1) have no subscribers yet or 2) you hit the rate-limit. Please try again in an hour"
86
  );
87
- interval_count = 0;
88
- started = false;
89
- first_modified = null;
90
  } else if (recipients) {
91
- clearInterval(interval);
92
  show_notice(recipients);
93
- interval_count = 0;
94
- started = false;
95
- first_modified = null;
96
  }
97
 
98
  // try for 1 minute
99
- if (interval_count > 20) {
100
- clearInterval(interval);
101
  error_notice(
102
  "OneSignal Push: Did not receive a response status from last notification sent"
103
  );
104
- interval_count = 0;
105
- started = false;
106
- first_modified = null;
107
  }
108
  });
109
- interval_count += 1;
110
  };
111
 
112
  /*
@@ -133,9 +135,18 @@ jQuery(document).ready(function() {
133
  isDismissible: true
134
  });
135
  };
 
 
 
 
 
 
 
 
 
136
  });
137
  const isWpCoreEditorDefined = () => {
138
- var unloadable = ""; // variable that couldn't be loaded
139
  if (!wp || !wp.data || !wp.data.select("core/editor")) {
140
  if (!wp) {
141
  unloadable = "wp";
7
  const get_wp_attr = attr => {
8
  return editor.getEditedPostAttribute(attr);
9
  };
10
+
11
+ var state = {
12
+ post_id : ajax_object.post_id,
13
+ first_modified : undefined,
14
+ started : false,
15
+ interval: undefined,
16
+ interval_count : 0
17
+ }
18
 
19
  /*
20
+ * Subscribes function to WP's state-change listener
21
  * - checks change in post modified date
22
+ * - triggers interval that checks if recipient meta data available in backend
23
  */
 
24
  wp.data.subscribe(() => {
25
+ // runs with each change in wp state
26
  const post = wp.data.select("core/editor").getCurrentPost();
27
 
28
+ if(!post || post === {}){
29
+ return;
30
+ }
31
+
32
  // runs until post data loads
33
+ if (!state.first_modified) {
34
+ // captures last modified date of loaded post
35
+ state.first_modified = post.modified;
36
  }
37
 
38
  // latest modified date
39
+ const { modified, status } = post;
40
 
41
  // is checked
42
  const send_os_notif = jQuery("[name=send_onesignal_notification]").attr(
43
  "checked"
44
  );
45
 
46
+ const post_modified = modified !== state.first_modified;
47
+
48
  // if hasn't started and change is detected
49
+ if (!state.started && post_modified && send_os_notif && (status === "publish")) {
50
+ state.interval = setInterval(get_metadata, 3000);
51
+ state.started = true;
52
  }
53
  });
54
 
59
  const get_metadata = () => {
60
  const data = {
61
  action: "has_metadata",
62
+ post_id: state.post_id
63
  };
64
 
65
  jQuery.get(ajax_object.ajax_url, data, function(response) {
66
  response = JSON.parse(response);
67
  const { recipients, status_code, error_message } = response;
68
+
69
+ // status 0: HTTP request failed
70
+ if (status_code == 0) {
71
+ error_notice("OneSignal Push: HTTP request failed");
72
+ reset_state();
73
+ return;
74
+ }
75
 
76
+ // 400 & 500 level errors
77
+ if (status_code >= 400) {
 
 
 
 
 
78
  if (!error_message) {
79
  error_notice(
80
  "OneSignal Push: there was a " +
85
  error_notice("OneSignal Push: " + error_message);
86
  }
87
 
88
+ reset_state();
 
 
89
  return;
90
  }
91
 
92
  if (recipients == 0) {
 
93
  error_notice(
94
+ "OneSignal Push: there were no recipients. You either 1) have no subscribers yet or 2) you hit the rate-limit. Please try again in an hour. Learn more: https://bit.ly/2UDplAS"
95
  );
96
+ reset_state();
97
+
 
98
  } else if (recipients) {
 
99
  show_notice(recipients);
100
+ reset_state();
 
 
101
  }
102
 
103
  // try for 1 minute
104
+ if (state.interval_count > 20) {
 
105
  error_notice(
106
  "OneSignal Push: Did not receive a response status from last notification sent"
107
  );
108
+ reset_state();
 
 
109
  }
110
  });
111
+ state.interval_count += 1;
112
  };
113
 
114
  /*
135
  isDismissible: true
136
  });
137
  };
138
+
139
+ const reset_state = () => {
140
+ clearInterval(state.interval);
141
+ state.interval = undefined;
142
+ state.interval_count = 0;
143
+ state.started = false;
144
+ state.first_modified = undefined;
145
+ }
146
+
147
  });
148
  const isWpCoreEditorDefined = () => {
149
+ var unloadable = ""; // variable name that couldn't be loaded
150
  if (!wp || !wp.data || !wp.data.select("core/editor")) {
151
  if (!wp) {
152
  unloadable = "wp";
onesignal-admin.php CHANGED
@@ -12,20 +12,47 @@ function onesignal_change_footer_admin() {
12
  add_action('admin_enqueue_scripts', 'load_javascript');
13
  function load_javascript() {
14
  global $post;
15
- wp_register_script('notice_script', plugins_url('notice.js', __FILE__), array('jquery'), '1.1', true);
16
- wp_enqueue_script('notice_script');
17
- wp_localize_script('notice_script', 'ajax_object', array('ajax_url' => admin_url("admin-ajax.php"), 'post_id' => $post->ID));
 
 
18
  }
19
 
20
  add_action( 'wp_ajax_has_metadata', 'has_metadata' );
21
  function has_metadata() {
22
- $post_id = $_GET['post_id'];
23
- $recipients = get_post_meta($post_id, "recipients")[0];
24
- $status = get_post_meta($post_id, "status")[0];
25
- $error_message = get_post_meta($post_id, "error_message")[0];
26
- $data = array('recipients' => $recipients, 'status_code' => $status, 'error_message' => $error_message);
27
- echo json_encode($data);
28
- exit;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  }
31
 
@@ -776,15 +803,18 @@ public static function uuid($title) {
776
 
777
  $response = wp_remote_post($onesignal_post_url, $request);
778
 
779
- if ( isset( $response['body'] ) ) {
780
- $response_body = json_decode($response["body"], true);
781
- }
782
-
783
  if ( is_wp_error($response) || !is_array( $response ) || !isset( $response['body']) ) {
784
  $status = $response->get_error_code(); // custom code for WP_ERROR
785
  error_log("There was a ".$status." error returned from OneSignal");
786
  update_post_meta($post->ID, "error_message", $response->get_error_message());
787
- } elseif ( isset( $response_body["errors"] ) ) {
 
 
 
 
 
 
 
788
  update_post_meta($post->ID, "error_message", $response_body["errors"][0]);
789
  }
790
 
12
  add_action('admin_enqueue_scripts', 'load_javascript');
13
  function load_javascript() {
14
  global $post;
15
+ if($post){
16
+ wp_register_script('notice_script', plugins_url('notice.js', __FILE__), array('jquery'), '1.1', true);
17
+ wp_enqueue_script('notice_script');
18
+ wp_localize_script('notice_script', 'ajax_object', array('ajax_url' => admin_url("admin-ajax.php"), 'post_id' => $post->ID));
19
+ }
20
  }
21
 
22
  add_action( 'wp_ajax_has_metadata', 'has_metadata' );
23
  function has_metadata() {
24
+ $post_id = $_GET['post_id'];
25
+
26
+ if(is_null($post_id)){
27
+ error_log("OneSignal: could not get post_id");
28
+ $data = array('error' => "could not get post id");
29
+ }else{
30
+ $recipients = get_post_meta($post_id, "recipients");
31
+ if($recipients && is_array($recipients)){
32
+ $recipients = $recipients[0];
33
+ }
34
+
35
+ $status = get_post_meta($post_id, "status");
36
+ if($status && is_array($status)){
37
+ $status = $status[0];
38
+ }
39
+
40
+ $error_message = get_post_meta($post_id, "error_message");
41
+ if($error_message && is_array($error_message)){
42
+ $error_message = $error_message[0];
43
+ }
44
+
45
+ // reset meta
46
+ delete_post_meta($post_id, "status");
47
+ delete_post_meta($post_id, "recipients");
48
+ delete_post_meta($post_id, "error_message");
49
+
50
+ $data = array('recipients' => $recipients, 'status_code' => $status, 'error_message' => $error_message);
51
+ }
52
+
53
+ echo json_encode($data);
54
+
55
+ exit;
56
 
57
  }
58
 
803
 
804
  $response = wp_remote_post($onesignal_post_url, $request);
805
 
 
 
 
 
806
  if ( is_wp_error($response) || !is_array( $response ) || !isset( $response['body']) ) {
807
  $status = $response->get_error_code(); // custom code for WP_ERROR
808
  error_log("There was a ".$status." error returned from OneSignal");
809
  update_post_meta($post->ID, "error_message", $response->get_error_message());
810
+ return;
811
+ }
812
+
813
+ if ( isset( $response['body'] ) ) {
814
+ $response_body = json_decode($response["body"], true);
815
+ }
816
+
817
+ if ( isset( $response_body["errors"] ) ) {
818
  update_post_meta($post->ID, "error_message", $response_body["errors"][0]);
819
  }
820
 
onesignal.php CHANGED
@@ -6,7 +6,7 @@ defined( 'ABSPATH' ) or die('This page may not be accessed directly.');
6
  * Plugin Name: OneSignal Push Notifications
7
  * Plugin URI: https://onesignal.com/
8
  * Description: Free web push notifications.
9
- * Version: 1.16.16
10
  * Author: OneSignal
11
  * Author URI: https://onesignal.com
12
  * License: MIT
6
  * Plugin Name: OneSignal Push Notifications
7
  * Plugin URI: https://onesignal.com/
8
  * Description: Free web push notifications.
9
+ * Version: 1.17.0
10
  * Author: OneSignal
11
  * Author URI: https://onesignal.com
12
  * License: MIT
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://onesignal.com
4
  Tags: chrome, firefox, safari, push, push notifications, push notification, chrome push, safari push, firefox push, notification, notifications, web push, notify, mavericks, android, android push, android notifications, android notification, mobile notification, mobile notifications, mobile, desktop notification, roost, goroost, desktop notifications, gcm, push messages, onesignal
5
  Requires at least: 3.8
6
  Tested up to: 5.0.3
7
- Stable tag: 1.16.16
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -65,6 +65,10 @@ HTTPS Setup Video: [youtube https://www.youtube.com/watch?v=BeTZ2KgytC0]
65
 
66
  == Changelog ==
67
 
 
 
 
 
68
  = 1.16.16 =
69
 
70
  - Code to catch error where core/editor is not defined for old versions of the editor
4
  Tags: chrome, firefox, safari, push, push notifications, push notification, chrome push, safari push, firefox push, notification, notifications, web push, notify, mavericks, android, android push, android notifications, android notification, mobile notification, mobile notifications, mobile, desktop notification, roost, goroost, desktop notifications, gcm, push messages, onesignal
5
  Requires at least: 3.8
6
  Tested up to: 5.0.3
7
+ Stable tag: 1.17.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
65
 
66
  == Changelog ==
67
 
68
+ = 1.17.0 =
69
+
70
+ - Bug fixes, edge-case handling, refactoring
71
+
72
  = 1.16.16 =
73
 
74
  - Code to catch error where core/editor is not defined for old versions of the editor