Skip to content

Commit

Permalink
Fix sw scope for non wp-content Wordpress setups
Browse files Browse the repository at this point in the history
* Some Wordpress setups (such as roots/bedrock) use "app"
instead of "wp-content" as URL path.
* Instead of assuming "wp-content" is present we always use the full
path URL.
   - Created a getOneSignalPluginPath function to generate the path
   without the domain to provide this.
* Tested with Both Standard Wordpress and
presslabs/wordpress-runtime:bedrock to ensure it works end-to-end.
  • Loading branch information
jkasten2 committed Jun 10, 2021
1 parent 58dc790 commit 9533278
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 9533278

Please sign in to comment.