Skip to content

Commit

Permalink
Merge pull request #275 from OneSignal/fix/sw_scope_path_for_non_wp-c…
Browse files Browse the repository at this point in the history
…ontent_config

Fix sw scope for non wp-content Wordpress setups
  • Loading branch information
jkasten2 authored Jun 12, 2021
2 parents 58dc790 + 9533278 commit 333db19
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions onesignal-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ private static function valid_for_key($key, $array) {
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();
Expand Down Expand Up @@ -68,15 +78,10 @@ public static function onesignal_header()
OneSignal.push( function() {
<?php
if(array_key_exists('onesignal_sw_js', $onesignal_wp_settings)) {
/**
* strstr method will remove the unecessary path before /wp-content
* Ex. It will give you a string like this:
* /wp-content/plugins/onesignal-free-web-push-notifications/
*/
$path = strstr(plugin_dir_url(__FILE__), '/wp-content');
$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: '$path'+'sdk_files/push/onesignal/' };";
OneSignal.SERVICE_WORKER_PARAM = { scope: '$swScope' };";
} else {
echo 'OneSignal.SERVICE_WORKER_UPDATER_PATH = "OneSignalSDKUpdaterWorker.js.php";
OneSignal.SERVICE_WORKER_PATH = "OneSignalSDKWorker.js.php";
Expand Down

0 comments on commit 333db19

Please sign in to comment.