-
Notifications
You must be signed in to change notification settings - Fork 42
/
notice.js
201 lines (170 loc) · 5.38 KB
/
notice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
jQuery(document).ready(notice);
var state = {
post_id: ajax_object.post_id, // post id sent from php backend
first_modified: undefined, // when the post was first modified
started: false, // post notification requests started
interval: undefined, // global interval for reattempting requests
interval_count: 0, // how many times has the request been attempted
status: undefined // whether the post is scheduled or published
}
function notice() {
if (!isWpCoreEditorDefined()) {
return;
}
const editor = wp.data.select("core/editor");
/*
* Subscribes function to WP's state-change listener
* - checks change in post modified date
* - triggers interval that checks if recipient meta data available in backend
*/
wp.data.subscribe(() => {
// runs with each change in wp state
const post = editor.getCurrentPost();
// runs until post data loads
if (!post || post === {}) {
return;
}
// post is defined now
if (!state.first_modified) {
// captures last modified date of loaded post
state.first_modified = post.modified;
}
// latest modified date, status of the post
const { modified, status } = post;
state.status = status;
// is checked
let send_os_notif;
const htmlElement = jQuery("#send_onesignal_notification")[0];
if (!!htmlElement) {
send_os_notif = htmlElement.checked;
}
// if last modified differs from first modified times, post_modified = true
const post_modified = modified !== state.first_modified;
const is_published = status === "publish";
// if hasn't started, change detected, box checked, and the status is 'publish'
if (!state.started && post_modified && send_os_notif && is_published) {
state.interval = setInterval(get_metadata, 3000); // starts requests
state.started = true;
}
});
/*
* Checks if post has meta for "recipients" on server
* - means request to OS has finished
*/
const get_metadata = () => {
const data = {
action: "has_metadata",
post_id: state.post_id
};
jQuery.get(ajax_object.ajax_url, data, function (response) {
response = JSON.parse(response);
let { status_code, response_body } = response;
if (window.DEBUG_MODE) {
console.log(response);
}
const is_status_empty = status_code.length == 0;
if (!is_status_empty) {
status_code = parseInt(status_code);
// status 0: HTTP request failed
if (status_code === 0) {
error_notice("OneSignal Push: request failed with status code 0. " + response_body);
reset_state();
return;
}
// 400 & 500 level errors
if (status_code >= 400) {
if (!response_body) {
error_notice(
"OneSignal Push: there was a " +
status_code +
" error sending your notification"
);
} else {
error_notice("OneSignal Push: there was a " + status_code + " error sending your notification: " + response_body);
}
reset_state();
return;
}
show_notice();
reset_state();
}
});
// try for 1 minute (each interval = 3s)
if (state.interval_count > 20) {
error_notice(
"OneSignal Push: Did not receive a response status from last notification sent"
);
reset_state();
}
state.interval_count += 1;
};
/*
* Gets recipient count and shows notice
*/
const show_notice = () => {
var delivery_link_text = "";
if (state.status === "publish") {
var notice_text = "OneSignal Push: Successfully sent a notification.";
delivery_link_text = " Go to your app's Delivery tab to check sent messages: https://dashboard.onesignal.com/apps/";
} else if (state.status === "future") {
var notice_text = "OneSignal Push: Successfully scheduled a notification.";
}
wp.data
.dispatch("core/notices")
.createNotice(
"info",
notice_text + delivery_link_text,
{
id: 'onesignal-notice',
isDismissible: true
}
);
};
const error_notice = error => {
wp.data.dispatch("core/notices").createNotice("error", error, {
isDismissible: true,
id: 'onesignal-error'
});
};
const reset_state = () => {
clearInterval(state.interval);
state.interval = undefined;
state.interval_count = 0;
state.started = false;
state.first_modified = undefined;
}
};
const isWpCoreEditorDefined = () => {
var unloadable = ""; // variable name that couldn't be loaded
if (!wp || !wp.data || !wp.data.select("core/editor")) {
if (!wp) {
unloadable = "wp";
} else if (!wp.data) {
unloadable = "wp.data";
} else if (!wp.data.select("core/editor")) {
unloadable = 'wp.data.select("core/editor")';
}
console.warn(
`OneSignal Push: could not load ${unloadable}. https:\/\/bit.ly/2F4G0bt`
);
return false;
} else {
return true;
}
};
/**
* - use the debug method in the console to show data about the request
* - works in Gutenberg editor
*
* returns an object in the format
* { status : "200",
* recipients : "1374",
* response_body : []
* }
*/
window.OneSignal = {
debug: () => {
window.DEBUG_MODE = window.DEBUG_MODE ? !window.DEBUG_MODE : true;
notice();
}
};