Skip to content

Commit 42b4ba6

Browse files
author
Rodrigo Gomez Palacio
authored
Merge pull request #337 from OneSignal/3.0.3
Fix SW scope issue & unregister old SW
2 parents 9f95b4e + 0dcd83f commit 42b4ba6

File tree

4 files changed

+76
-42
lines changed

4 files changed

+76
-42
lines changed

onesignal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Plugin Name: OneSignal Push Notifications
77
* Plugin URI: https://onesignal.com/
88
* Description: Free web push notifications.
9-
* Version: 3.0.2
9+
* Version: 3.0.3
1010
* Author: OneSignal
1111
* Author URI: https://onesignal.com
1212
* License: MIT

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Donate link: https://onesignal.com
44
Tags: push notification, push notifications, desktop notifications, mobile notifications, chrome push, android, android notification, android notifications, android push, desktop notification, firefox, firefox push, mobile, mobile notification, notification, notifications, notify, onesignal, push, push messages, safari, safari push, web push, chrome
55
Requires at least: 3.8
66
Tested up to: 6.7
7-
Stable tag: 3.0.2
7+
Stable tag: 3.0.3
88
License: GPLv2 or later
99
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -64,6 +64,9 @@ OneSignal is trusted by over 1.8M+ developers and marketing strategists. We powe
6464

6565
== Changelog ==
6666

67+
= 3.0.3 =
68+
- Bug fix: fix service worker registration issue.
69+
6770
= 3.0.2 =
6871
- Adding an admin notice and updated styles to encourage settings migration.
6972

v3/onesignal-admin/onesignal-admin.js

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,61 @@ window.addEventListener("DOMContentLoaded", () => {
22
const helpIcon = document.querySelector(".help");
33
const infoDiv = document.querySelector(".information");
44

5-
helpIcon.addEventListener("click", () => {
6-
infoDiv.style.display =
7-
infoDiv.style.display === "none" ? "inherit" : "none";
8-
});
5+
if (helpIcon && infoDiv) {
6+
helpIcon.addEventListener("click", () => {
7+
infoDiv.style.display =
8+
infoDiv.style.display === "none" ? "inherit" : "none";
9+
});
10+
}
911
});
1012

1113
window.addEventListener("DOMContentLoaded", () => {
12-
const appIdInput = document.querySelector('#appid');
13-
const apiKeyInput = document.querySelector('#apikey');
14-
const saveButton = document.querySelector('#save-settings-button');
14+
const appIdInput = document.querySelector("#appid");
15+
const apiKeyInput = document.querySelector("#apikey");
16+
const saveButton = document.querySelector("#save-settings-button");
17+
18+
if (appIdInput && apiKeyInput && saveButton) {
19+
function isValidUUID(uuid) {
20+
const uuidRegex =
21+
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
22+
return uuid.length > 0 && uuidRegex.test(uuid); // Ensure it's not empty and matches regex
23+
}
1524

16-
function isValidUUID(uuid) {
17-
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
18-
return uuid.length > 0 && uuidRegex.test(uuid); // Ensure it's not empty and matches regex
19-
}
25+
function isValidApiKey(apiKey) {
26+
const base64Regex =
27+
/^(?:[A-Za-z0-9+/]{4}){12,}(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; // At least 48 characters in Base64
28+
const opaqueTokenRegex = /^os_v[2-9]_app_[2-7a-z]{56,}$/;
29+
return (
30+
base64Regex.test(apiKey) || opaqueTokenRegex.test(apiKey)
31+
); // Ensure it's not empty and matches regex
32+
}
2033

21-
function isValidApiKey(apiKey) {
22-
const base64Regex = /^(?:[A-Za-z0-9+/]{4}){12,}(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; // At least 48 characters in Base64
23-
const opaqueTokenRegex = /^os_v[2-9]_app_[2-7a-z]{56,}$/;
24-
return (base64Regex.test(apiKey) || opaqueTokenRegex.test(apiKey)); // Ensure it's not empty and matches regex
25-
}
34+
function updateValidationIcon(input, isValid) {
35+
const icon = input.parentElement.querySelector(".validation-icon");
36+
if (icon) {
37+
icon.textContent = isValid ? "✅" : "❌";
38+
}
39+
}
2640

27-
function updateValidationIcon(input, isValid) {
28-
const icon = input.parentElement.querySelector('.validation-icon');
29-
if (icon) {
30-
icon.textContent = isValid ? '✅' : '❌';
41+
function toggleSaveButton() {
42+
const appIdValid = isValidUUID(appIdInput.value);
43+
const apiKeyValid = isValidApiKey(apiKeyInput.value);
44+
saveButton.disabled = !(appIdValid && apiKeyValid); // Enable button only if both are valid
3145
}
32-
}
3346

34-
function toggleSaveButton() {
35-
const appIdValid = isValidUUID(appIdInput.value);
36-
const apiKeyValid = isValidApiKey(apiKeyInput.value);
37-
saveButton.disabled = !(appIdValid && apiKeyValid); // Enable button only if both are valid
38-
}
47+
appIdInput.addEventListener("input", () => {
48+
const isValid = isValidUUID(appIdInput.value);
49+
updateValidationIcon(appIdInput, isValid);
50+
toggleSaveButton();
51+
});
3952

40-
appIdInput.addEventListener('input', () => {
41-
const isValid = isValidUUID(appIdInput.value);
42-
updateValidationIcon(appIdInput, isValid);
43-
toggleSaveButton();
44-
});
53+
apiKeyInput.addEventListener("input", () => {
54+
const isValid = isValidApiKey(apiKeyInput.value);
55+
updateValidationIcon(apiKeyInput, isValid);
56+
toggleSaveButton();
57+
});
4558

46-
apiKeyInput.addEventListener('input', () => {
47-
const isValid = isValidApiKey(apiKeyInput.value);
48-
updateValidationIcon(apiKeyInput, isValid);
59+
// Initial state on page load
4960
toggleSaveButton();
50-
});
51-
52-
// Initial state on page load
53-
toggleSaveButton();
61+
}
5462
});

v3/onesignal-init.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
function onesignal_init()
99
{
1010
$onesignal_wp_settings = get_option('OneSignalWPSetting');
11+
$use_root_scope = array_key_exists('onesignal_sw_js', $onesignal_wp_settings) ? false : true;
12+
$path = rtrim(parse_url(ONESIGNAL_PLUGIN_URL)['path'], '/');
13+
$scope = $path . '/sdk_files/push/onesignal/';
14+
$filename = 'OneSignalSDKWorker.js' . ($use_root_scope ? '.php' : '');
1115
?>
1216
<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
1317
<script>
@@ -17,10 +21,29 @@ function onesignal_init()
1721
appId: "<?php echo esc_html($onesignal_wp_settings['app_id']); ?>",
1822
serviceWorkerOverrideForTypical: true,
1923
path: "<?php echo ONESIGNAL_PLUGIN_URL; ?>sdk_files/",
20-
serviceWorkerParam: { scope: '/' },
21-
serviceWorkerPath: 'OneSignalSDKWorker.js.php',
24+
serviceWorkerParam: { scope: "<?php echo $use_root_scope ? '/' : $scope ?>" },
25+
serviceWorkerPath: "<?php echo $filename; ?>",
2226
});
2327
});
28+
// TO DO: move this to a separate file
29+
navigator.serviceWorker.getRegistrations().then((registrations) => {
30+
// Iterate through all registered service workers
31+
registrations.forEach((registration) => {
32+
// Check the script URL to identify the specific service worker
33+
if (registration.active && registration.active.scriptURL.includes('OneSignalSDKWorker.js.php')) {
34+
// Unregister the service worker
35+
registration.unregister().then((success) => {
36+
if (success) {
37+
console.log('Successfully unregistered:', registration.active.scriptURL);
38+
} else {
39+
console.log('Failed to unregister:', registration.active.scriptURL);
40+
}
41+
});
42+
}
43+
});
44+
}).catch((error) => {
45+
console.error('Error fetching service worker registrations:', error);
46+
});
2447
</script>
2548
<?php
2649
}

0 commit comments

Comments
 (0)