Skip to content

Commit

Permalink
Merge pull request #337 from OneSignal/3.0.3
Browse files Browse the repository at this point in the history
Fix SW scope issue & unregister old SW
  • Loading branch information
rgomezp authored Dec 21, 2024
2 parents 9f95b4e + 0dcd83f commit 42b4ba6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 42 deletions.
2 changes: 1 addition & 1 deletion onesignal.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Plugin Name: OneSignal Push Notifications
* Plugin URI: https://onesignal.com/
* Description: Free web push notifications.
* Version: 3.0.2
* Version: 3.0.3
* Author: OneSignal
* Author URI: https://onesignal.com
* License: MIT
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate link: https://onesignal.com
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
Requires at least: 3.8
Tested up to: 6.7
Stable tag: 3.0.2
Stable tag: 3.0.3
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

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

== Changelog ==

= 3.0.3 =
- Bug fix: fix service worker registration issue.

= 3.0.2 =
- Adding an admin notice and updated styles to encourage settings migration.

Expand Down
84 changes: 46 additions & 38 deletions v3/onesignal-admin/onesignal-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,61 @@ window.addEventListener("DOMContentLoaded", () => {
const helpIcon = document.querySelector(".help");
const infoDiv = document.querySelector(".information");

helpIcon.addEventListener("click", () => {
infoDiv.style.display =
infoDiv.style.display === "none" ? "inherit" : "none";
});
if (helpIcon && infoDiv) {
helpIcon.addEventListener("click", () => {
infoDiv.style.display =
infoDiv.style.display === "none" ? "inherit" : "none";
});
}
});

window.addEventListener("DOMContentLoaded", () => {
const appIdInput = document.querySelector('#appid');
const apiKeyInput = document.querySelector('#apikey');
const saveButton = document.querySelector('#save-settings-button');
const appIdInput = document.querySelector("#appid");
const apiKeyInput = document.querySelector("#apikey");
const saveButton = document.querySelector("#save-settings-button");

if (appIdInput && apiKeyInput && saveButton) {
function isValidUUID(uuid) {
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;
return uuid.length > 0 && uuidRegex.test(uuid); // Ensure it's not empty and matches regex
}

function isValidUUID(uuid) {
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;
return uuid.length > 0 && uuidRegex.test(uuid); // Ensure it's not empty and matches regex
}
function isValidApiKey(apiKey) {
const base64Regex =
/^(?:[A-Za-z0-9+/]{4}){12,}(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; // At least 48 characters in Base64
const opaqueTokenRegex = /^os_v[2-9]_app_[2-7a-z]{56,}$/;
return (
base64Regex.test(apiKey) || opaqueTokenRegex.test(apiKey)
); // Ensure it's not empty and matches regex
}

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

function updateValidationIcon(input, isValid) {
const icon = input.parentElement.querySelector('.validation-icon');
if (icon) {
icon.textContent = isValid ? '✅' : '❌';
function toggleSaveButton() {
const appIdValid = isValidUUID(appIdInput.value);
const apiKeyValid = isValidApiKey(apiKeyInput.value);
saveButton.disabled = !(appIdValid && apiKeyValid); // Enable button only if both are valid
}
}

function toggleSaveButton() {
const appIdValid = isValidUUID(appIdInput.value);
const apiKeyValid = isValidApiKey(apiKeyInput.value);
saveButton.disabled = !(appIdValid && apiKeyValid); // Enable button only if both are valid
}
appIdInput.addEventListener("input", () => {
const isValid = isValidUUID(appIdInput.value);
updateValidationIcon(appIdInput, isValid);
toggleSaveButton();
});

appIdInput.addEventListener('input', () => {
const isValid = isValidUUID(appIdInput.value);
updateValidationIcon(appIdInput, isValid);
toggleSaveButton();
});
apiKeyInput.addEventListener("input", () => {
const isValid = isValidApiKey(apiKeyInput.value);
updateValidationIcon(apiKeyInput, isValid);
toggleSaveButton();
});

apiKeyInput.addEventListener('input', () => {
const isValid = isValidApiKey(apiKeyInput.value);
updateValidationIcon(apiKeyInput, isValid);
// Initial state on page load
toggleSaveButton();
});

// Initial state on page load
toggleSaveButton();
}
});
27 changes: 25 additions & 2 deletions v3/onesignal-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
function onesignal_init()
{
$onesignal_wp_settings = get_option('OneSignalWPSetting');
$use_root_scope = array_key_exists('onesignal_sw_js', $onesignal_wp_settings) ? false : true;
$path = rtrim(parse_url(ONESIGNAL_PLUGIN_URL)['path'], '/');
$scope = $path . '/sdk_files/push/onesignal/';
$filename = 'OneSignalSDKWorker.js' . ($use_root_scope ? '.php' : '');
?>
<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
<script>
Expand All @@ -17,10 +21,29 @@ function onesignal_init()
appId: "<?php echo esc_html($onesignal_wp_settings['app_id']); ?>",
serviceWorkerOverrideForTypical: true,
path: "<?php echo ONESIGNAL_PLUGIN_URL; ?>sdk_files/",
serviceWorkerParam: { scope: '/' },
serviceWorkerPath: 'OneSignalSDKWorker.js.php',
serviceWorkerParam: { scope: "<?php echo $use_root_scope ? '/' : $scope ?>" },
serviceWorkerPath: "<?php echo $filename; ?>",
});
});
// TO DO: move this to a separate file
navigator.serviceWorker.getRegistrations().then((registrations) => {
// Iterate through all registered service workers
registrations.forEach((registration) => {
// Check the script URL to identify the specific service worker
if (registration.active && registration.active.scriptURL.includes('OneSignalSDKWorker.js.php')) {
// Unregister the service worker
registration.unregister().then((success) => {
if (success) {
console.log('Successfully unregistered:', registration.active.scriptURL);
} else {
console.log('Failed to unregister:', registration.active.scriptURL);
}
});
}
});
}).catch((error) => {
console.error('Error fetching service worker registrations:', error);
});
</script>
<?php
}

0 comments on commit 42b4ba6

Please sign in to comment.