This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
forked from OneSignal/OneSignal-WordPress-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onesignal-public.php
350 lines (314 loc) · 21.4 KB
/
onesignal-public.php
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
defined('ABSPATH') or die('This page may not be accessed directly.');
function add_async_for_script($url)
{
if (strpos($url, '#asyncload') === false)
return $url;
else if (is_admin())
return str_replace('#asyncload', '', $url);
else
return str_replace('#asyncload', '', $url)."' async='async";
}
class OneSignal_Public
{
public function __construct()
{
}
public static function init()
{
add_action('wp_head', array(__CLASS__, 'onesignal_header'), 10);
}
// For easier debugging of sites by identifying them as WordPress
public static function insert_onesignal_stamp()
{
?>
<meta name="onesignal" content="wordpress-plugin"/>
<?php
}
private static function valid_for_key($key, $array) {
if(array_key_exists($key, $array) && $array[$key] !== ''){
return true;
}
return false;
}
// Returns the OneSignal plugin URL path
// Examples:
// /wp-content/plugins/onesignal-free-web-push-notifications
// /app/plugins/onesignal-free-web-push-notifications
private static function getOneSignalPluginPath()
{
$path = parse_url(ONESIGNAL_PLUGIN_URL)['path'];
return rtrim($path, '/');
}
public static function onesignal_header()
{
$onesignal_wp_settings = OneSignal::get_onesignal_settings();
if (array_key_exists('subdomain', $onesignal_wp_settings) && $onesignal_wp_settings['subdomain'] === '') {
if (strpos(ONESIGNAL_PLUGIN_URL, 'http://localhost') === false && strpos(ONESIGNAL_PLUGIN_URL, 'http://127.0.0.1') === false) {
$current_plugin_url = preg_replace("/(http:\/\/)/i", 'https://', ONESIGNAL_PLUGIN_URL);
} else {
$current_plugin_url = ONESIGNAL_PLUGIN_URL;
}
OneSignal_Public::insert_onesignal_stamp();
} ?>
<?php
add_filter('clean_url', 'add_async_for_script', 11, 1);
if (defined('ONESIGNAL_DEBUG') && defined('ONESIGNAL_LOCAL')) {
wp_register_script('local_sdk', 'https://localhost:3001/sdks/OneSignalSDK.js#asyncload', array(), false, true);
wp_enqueue_script('local_sdk');
} else {
wp_register_script('remote_sdk', 'https://cdn.onesignal.com/sdks/OneSignalSDK.js#asyncload', array(), false, true);
wp_enqueue_script('remote_sdk');
} ?>
<script>
window.OneSignal = window.OneSignal || [];
OneSignal.push( function() {
<?php
if(array_key_exists('onesignal_sw_js', $onesignal_wp_settings)) {
$swScope = self::getOneSignalPluginPath() . '/sdk_files/push/onesignal/';
echo "OneSignal.SERVICE_WORKER_UPDATER_PATH = 'OneSignalSDKUpdaterWorker.js';
OneSignal.SERVICE_WORKER_PATH = 'OneSignalSDKWorker.js';
OneSignal.SERVICE_WORKER_PARAM = { scope: '$swScope' };";
} else {
echo 'OneSignal.SERVICE_WORKER_UPDATER_PATH = "OneSignalSDKUpdaterWorker.js.php";
OneSignal.SERVICE_WORKER_PATH = "OneSignalSDKWorker.js.php";
OneSignal.SERVICE_WORKER_PARAM = { scope: "/" };';
}
?>
<?php
if (self::valid_for_key('default_url', $onesignal_wp_settings)) {
echo 'OneSignal.setDefaultNotificationUrl("'.esc_url($onesignal_wp_settings['default_url']).'");';
} else {
echo 'OneSignal.setDefaultNotificationUrl("'.esc_url(get_site_url())."\");\n";
} ?>
var oneSignal_options = {};
window._oneSignalInitOptions = oneSignal_options;
<?php
echo "oneSignal_options['wordpress'] = true;\n";
echo "oneSignal_options['appId'] = '".esc_html($onesignal_wp_settings['app_id'])."';\n";
echo "oneSignal_options['allowLocalhostAsSecureOrigin'] = true;\n";
if (array_key_exists('use_http_permission_request', $onesignal_wp_settings) && $onesignal_wp_settings['use_http_permission_request'] === true) {
echo "oneSignal_options['httpPermissionRequest'] = { };\n";
echo "oneSignal_options['httpPermissionRequest']['enable'] = true;\n";
}
if (array_key_exists('send_welcome_notification', $onesignal_wp_settings) && $onesignal_wp_settings['send_welcome_notification'] === true) {
echo "oneSignal_options['welcomeNotification'] = { };\n";
echo "oneSignal_options['welcomeNotification']['title'] = \"".esc_html($onesignal_wp_settings['welcome_notification_title'])."\";\n";
echo "oneSignal_options['welcomeNotification']['message'] = \"".esc_html($onesignal_wp_settings['welcome_notification_message'])."\";\n";
if ($onesignal_wp_settings['welcome_notification_url'] !== '') {
echo "oneSignal_options['welcomeNotification']['url'] = \"".esc_html($onesignal_wp_settings['welcome_notification_url'])."\";\n";
}
} else {
echo "oneSignal_options['welcomeNotification'] = { };\n";
echo "oneSignal_options['welcomeNotification']['disable'] = true;\n";
}
if (self::valid_for_key('subdomain', $onesignal_wp_settings)) {
echo "oneSignal_options['subdomainName'] = \"".esc_html($onesignal_wp_settings['subdomain'])."\";\n";
} else {
echo "oneSignal_options['path'] = \"".esc_html($current_plugin_url)."sdk_files/\";\n";
}
if ($onesignal_wp_settings['safari_web_id']) {
echo "oneSignal_options['safari_web_id'] = \"".esc_html($onesignal_wp_settings['safari_web_id'])."\";\n";
}
if (array_key_exists('persist_notifications', $onesignal_wp_settings) && $onesignal_wp_settings['persist_notifications'] === 'platform-default') {
echo "oneSignal_options['persistNotification'] = false;\n";
} elseif (array_key_exists('persist_notifications', $onesignal_wp_settings) && $onesignal_wp_settings['persist_notifications'] === 'yes-all') {
echo "oneSignal_options['persistNotification'] = true;\n";
}
echo "oneSignal_options['promptOptions'] = { };\n";
if (array_key_exists('prompt_customize_enable', $onesignal_wp_settings) && $onesignal_wp_settings['prompt_customize_enable'] === true) {
if (self::valid_for_key('prompt_action_message', $onesignal_wp_settings)) {
echo "oneSignal_options['promptOptions']['actionMessage'] = \"".OneSignalUtils::decode_entities(esc_html($onesignal_wp_settings["prompt_action_message"]))."\";\n";
}
if (self::valid_for_key('prompt_example_notification_title_desktop', $onesignal_wp_settings)) {
echo "oneSignal_options['promptOptions']['exampleNotificationTitleDesktop'] = \"".OneSignalUtils::decode_entities(esc_html($onesignal_wp_settings['prompt_example_notification_title_desktop']))."\";\n";
}
if (self::valid_for_key('prompt_example_notification_message_desktop', $onesignal_wp_settings)) {
echo "oneSignal_options['promptOptions']['exampleNotificationMessageDesktop'] = \"".OneSignalUtils::decode_entities(esc_html($onesignal_wp_settings['prompt_example_notification_message_desktop']))."\";\n";
}
if (self::valid_for_key('prompt_example_notification_title_mobile', $onesignal_wp_settings)) {
echo "oneSignal_options['promptOptions']['exampleNotificationTitleMobile'] = \"".OneSignalUtils::decode_entities(esc_html($onesignal_wp_settings['prompt_example_notification_title_mobile']))."\";\n";
}
if (self::valid_for_key('prompt_example_notification_message_mobile', $onesignal_wp_settings)) {
echo "oneSignal_options['promptOptions']['exampleNotificationMessageMobile'] = \"".OneSignalUtils::decode_entities(esc_html($onesignal_wp_settings['prompt_example_notification_message_mobile']))."\";\n";
}
if (self::valid_for_key('prompt_example_notification_caption', $onesignal_wp_settings)) {
echo "oneSignal_options['promptOptions']['exampleNotificationCaption'] = \"".OneSignalUtils::decode_entities(esc_html($onesignal_wp_settings['prompt_example_notification_caption']))."\";\n";
}
if (self::valid_for_key('prompt_accept_button_text', $onesignal_wp_settings)) {
echo "oneSignal_options['promptOptions']['acceptButtonText'] = \"".OneSignalUtils::decode_entities(esc_html($onesignal_wp_settings['prompt_accept_button_text']))."\";\n";
}
if (self::valid_for_key('prompt_cancel_button_text', $onesignal_wp_settings)) {
echo "oneSignal_options['promptOptions']['cancelButtonText'] = \"".OneSignalUtils::decode_entities(esc_html($onesignal_wp_settings['prompt_cancel_button_text']))."\";\n";
}
if (self::valid_for_key('prompt_site_name', $onesignal_wp_settings)) {
echo "oneSignal_options['promptOptions']['siteName'] = \"".OneSignalUtils::decode_entities(esc_html($onesignal_wp_settings['prompt_site_name']))."\";\n";
}
if (self::valid_for_key('prompt_auto_accept_title', $onesignal_wp_settings)) {
echo "oneSignal_options['promptOptions']['autoAcceptTitle'] = \"".OneSignalUtils::decode_entities(esc_html($onesignal_wp_settings['prompt_auto_accept_title']))."\";\n";
}
}
if (array_key_exists('notifyButton_enable', $onesignal_wp_settings) && $onesignal_wp_settings['notifyButton_enable'] === true) {
echo "oneSignal_options['notifyButton'] = { };\n";
echo "oneSignal_options['notifyButton']['enable'] = true;\n";
if (self::valid_for_key('notifyButton_position', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['position'] = '".esc_html($onesignal_wp_settings['notifyButton_position'])."';\n";
}
if (self::valid_for_key('notifyButton_theme', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['theme'] = '".esc_html($onesignal_wp_settings['notifyButton_theme'])."';\n";
}
if (self::valid_for_key('notifyButton_size', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['size'] = '".esc_html($onesignal_wp_settings['notifyButton_size'])."';\n";
}
if (array_key_exists('notifyButton_showAfterSubscribed', $onesignal_wp_settings) && $onesignal_wp_settings['notifyButton_showAfterSubscribed'] !== true) {
echo "oneSignal_options['notifyButton']['displayPredicate'] = function() {
return OneSignal.isPushNotificationsEnabled()
.then(function(isPushEnabled) {
return !isPushEnabled;
});
};\n";
}
if (array_key_exists('use_modal_prompt', $onesignal_wp_settings) && $onesignal_wp_settings['use_modal_prompt'] === true) {
echo "oneSignal_options['notifyButton']['modalPrompt'] = true;\n";
}
if (array_key_exists('notifyButton_showcredit', $onesignal_wp_settings) && $onesignal_wp_settings['notifyButton_showcredit'] === true) {
echo "oneSignal_options['notifyButton']['showCredit'] = true;\n";
} else {
echo "oneSignal_options['notifyButton']['showCredit'] = false;\n";
}
if (array_key_exists('notifyButton_customize_enable', $onesignal_wp_settings) && $onesignal_wp_settings['notifyButton_customize_enable'] === true) {
echo "oneSignal_options['notifyButton']['text'] = {};\n";
if (self::valid_for_key('notifyButton_tip_state_unsubscribed', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['tip.state.unsubscribed'] = '".esc_html($onesignal_wp_settings['notifyButton_tip_state_unsubscribed'])."';\n";
}
if (self::valid_for_key('notifyButton_tip_state_subscribed', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['tip.state.subscribed'] = '".esc_html($onesignal_wp_settings['notifyButton_tip_state_subscribed'])."';\n";
}
if (self::valid_for_key('notifyButton_tip_state_blocked', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['tip.state.blocked'] = '".esc_html($onesignal_wp_settings['notifyButton_tip_state_blocked'])."';\n";
}
if (self::valid_for_key('notifyButton_message_action_subscribed', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['message.action.subscribed'] = '".esc_html($onesignal_wp_settings['notifyButton_message_action_subscribed'])."';\n";
}
if (self::valid_for_key('notifyButton_message_action_resubscribed', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['message.action.resubscribed'] = '".esc_html($onesignal_wp_settings['notifyButton_message_action_resubscribed'])."';\n";
}
if (self::valid_for_key('notifyButton_message_action_unsubscribed', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['message.action.unsubscribed'] = '".esc_html($onesignal_wp_settings['notifyButton_message_action_unsubscribed'])."';\n";
}
if (self::valid_for_key('notifyButton_dialog_main_title', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['dialog.main.title'] = '".esc_html($onesignal_wp_settings['notifyButton_dialog_main_title'])."';\n";
}
if (self::valid_for_key('notifyButton_dialog_main_button_subscribe', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['dialog.main.button.subscribe'] = '".esc_html($onesignal_wp_settings['notifyButton_dialog_main_button_subscribe'])."';\n";
}
if (self::valid_for_key('notifyButton_dialog_main_button_unsubscribe', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['dialog.main.button.unsubscribe'] = '".esc_html($onesignal_wp_settings['notifyButton_dialog_main_button_unsubscribe'])."';\n";
}
if (self::valid_for_key('notifyButton_dialog_blocked_title', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['dialog.blocked.title'] = '".esc_html($onesignal_wp_settings['notifyButton_dialog_blocked_title'])."';\n";
}
if (self::valid_for_key('notifyButton_dialog_blocked_message', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['text']['dialog.blocked.message'] = '".esc_html($onesignal_wp_settings['notifyButton_dialog_blocked_message'])."';\n";
}
}
if (array_key_exists('notifyButton_customize_colors_enable', $onesignal_wp_settings) && $onesignal_wp_settings['notifyButton_customize_colors_enable']) {
echo "oneSignal_options['notifyButton']['colors'] = {};\n";
if (self::valid_for_key('notifyButton_color_background', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['colors']['circle.background'] = '".esc_html($onesignal_wp_settings['notifyButton_color_background'])."';\n";
}
if (self::valid_for_key('notifyButton_color_foreground', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['colors']['circle.foreground'] = '".esc_html($onesignal_wp_settings['notifyButton_color_foreground'])."';\n";
}
if (self::valid_for_key('notifyButton_color_badge_background', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['colors']['badge.background'] = '".esc_html($onesignal_wp_settings['notifyButton_color_badge_background'])."';\n";
}
if (self::valid_for_key('notifyButton_color_badge_foreground', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['colors']['badge.foreground'] = '".esc_html($onesignal_wp_settings['notifyButton_color_badge_foreground'])."';\n";
}
if (self::valid_for_key('notifyButton_color_badge_border', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['colors']['badge.bordercolor'] = '".esc_html($onesignal_wp_settings['notifyButton_color_badge_border'])."';\n";
}
if (self::valid_for_key('notifyButton_color_pulse', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['colors']['pulse.color'] = '".esc_html($onesignal_wp_settings['notifyButton_color_pulse'])."';\n";
}
if (self::valid_for_key('notifyButton_color_popup_button_background', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['colors']['dialog.button.background'] = '".esc_html($onesignal_wp_settings['notifyButton_color_popup_button_background'])."';\n";
}
if (self::valid_for_key('notifyButton_color_popup_button_background_hover', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['colors']['dialog.button.background.hovering'] = '".esc_html($onesignal_wp_settings['notifyButton_color_popup_button_background_hover'])."';\n";
}
if (self::valid_for_key('notifyButton_color_popup_button_background_active', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['colors']['dialog.button.background.active'] = '".esc_html($onesignal_wp_settings['notifyButton_color_popup_button_background_active'])."';\n";
}
if (self::valid_for_key('notifyButton_color_popup_button_color', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['colors']['dialog.button.foreground'] = '".esc_html($onesignal_wp_settings['notifyButton_color_popup_button_color'])."';\n";
}
}
if (array_key_exists('notifyButton_customize_offset_enable', $onesignal_wp_settings) && $onesignal_wp_settings['notifyButton_customize_offset_enable'] === true) {
echo "oneSignal_options['notifyButton']['offset'] = {};\n";
if (self::valid_for_key('notifyButton_offset_bottom', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['offset']['bottom'] = '".esc_html($onesignal_wp_settings['notifyButton_offset_bottom'])."';\n";
}
if (self::valid_for_key('notifyButton_offset_left', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['offset']['left'] = '".esc_html($onesignal_wp_settings['notifyButton_offset_left'])."';\n";
}
if (self::valid_for_key('notifyButton_offset_right', $onesignal_wp_settings)) {
echo "oneSignal_options['notifyButton']['offset']['right'] = '".esc_html($onesignal_wp_settings['notifyButton_offset_right'])."';\n";
}
}
}
$use_custom_sdk_init = $onesignal_wp_settings['use_custom_sdk_init'];
if (!$use_custom_sdk_init) {
if (has_filter('onesignal_initialize_sdk')) {
if (apply_filters('onesignal_initialize_sdk', $onesignal_wp_settings)) {
// If the filter returns "$do_initialize_sdk: true", initialize the web SDK
?>
OneSignal.init(window._oneSignalInitOptions);
<?php
} else {
?>
/* OneSignal: onesignal_initialize_sdk filter preventing SDK initialization. */
<?php
}
} else {
?>
OneSignal.init(window._oneSignalInitOptions);
<?php
}
if (array_key_exists('prompt_auto_register', $onesignal_wp_settings) && $onesignal_wp_settings['prompt_auto_register'] === true) {
echo "OneSignal.showSlidedownPrompt();";
}
if (array_key_exists('use_native_prompt', $onesignal_wp_settings) && $onesignal_wp_settings['use_native_prompt'] === true) {
echo "OneSignal.showNativePrompt();";
}
} else {
?>
/* OneSignal: Using custom SDK initialization. */
<?php
} ?>
});
function documentInitOneSignal() {
var oneSignal_elements = document.getElementsByClassName("OneSignal-prompt");
<?php
if (array_key_exists('use_modal_prompt', $onesignal_wp_settings) && $onesignal_wp_settings['use_modal_prompt'] === true) {
echo "var oneSignalLinkClickHandler = function(event) { OneSignal.push(['registerForPushNotifications', {modalPrompt: true}]); event.preventDefault(); };";
} else {
echo "var oneSignalLinkClickHandler = function(event) { OneSignal.push(['registerForPushNotifications']); event.preventDefault(); };";
} ?>
for(var i = 0; i < oneSignal_elements.length; i++)
oneSignal_elements[i].addEventListener('click', oneSignalLinkClickHandler, false);
}
if (document.readyState === 'complete') {
documentInitOneSignal();
}
else {
window.addEventListener("load", function(event){
documentInitOneSignal();
});
}
</script>
<?php
}
}
?>