Skip to content

Commit

Permalink
Merge pull request #333 from OneSignal/3.0.0
Browse files Browse the repository at this point in the history
3.0.0
  • Loading branch information
rgomezp authored Dec 19, 2024
2 parents 511e6fb + 039f557 commit 31e0d22
Show file tree
Hide file tree
Showing 193 changed files with 1,708 additions and 306 deletions.
52 changes: 38 additions & 14 deletions onesignal.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,52 @@
* Plugin Name: OneSignal Push Notifications
* Plugin URI: https://onesignal.com/
* Description: Free web push notifications.
* Version: 2.4.4
* Version: 3.0.0
* Author: OneSignal
* Author URI: https://onesignal.com
* License: MIT
*/

// Define plugin constants
define('ONESIGNAL_PLUGIN_URL', plugin_dir_url(__FILE__));
/*
* The number of seconds required to wait between requests.
*/
define('ONESIGNAL_API_RATE_LIMIT_SECONDS', 1);
define('ONESIGNAL_URI_REVEAL_PROJECT_NUMBER', 'reveal_project_number=true');

require_once plugin_dir_path(__FILE__).'onesignal-utils.php';
require_once plugin_dir_path(__FILE__).'onesignal-admin.php';
require_once plugin_dir_path(__FILE__).'onesignal-public.php';
require_once plugin_dir_path(__FILE__).'onesignal-settings.php';
require_once plugin_dir_path(__FILE__).'onesignal-widget.php';
// Constants for plugin versions
define('ONESIGNAL_VERSION_V2', 'v2');
define('ONESIGNAL_VERSION_V3', 'v3');

if (file_exists(plugin_dir_path(__FILE__).'onesignal-extra.php')) {
require_once plugin_dir_path(__FILE__).'onesignal-extra.php';
}
// Check migration status and existing settings
$is_migrated = get_option('onesignal_plugin_migrated', false); // Tracks whether the plugin has been migrated to V3
$settings = get_option('OneSignalWPSetting'); // Fetch existing plugin settings (if any)
$is_new_install = !$settings || !isset($settings['app_id']); // Determine if this is a fresh install (no settings yet)

// Determine which plugin version to load
$plugin_version = $is_new_install || $is_migrated ? ONESIGNAL_VERSION_V3 : ONESIGNAL_VERSION_V2;

add_action('init', ['OneSignal_Admin', 'init']);
add_action('init', ['OneSignal_Public', 'init']);
// Load the appropriate plugin version based on the state
if ($plugin_version === ONESIGNAL_VERSION_V3) {
// Load V3 plugin files
require_once plugin_dir_path(__FILE__) . 'v3/onesignal-admin/onesignal-admin.php';
require_once plugin_dir_path(__FILE__) . 'v3/onesignal-init.php';
require_once plugin_dir_path(__FILE__) . 'v3/onesignal-metabox/onesignal-metabox.php';
require_once plugin_dir_path(__FILE__) . 'v3/onesignal-notification.php';

// Ensure migration is marked as complete after loading V3
if (!$is_migrated) {
update_option('onesignal_plugin_migrated', true);
}
} else {
// Load V2 plugin files
require_once plugin_dir_path(__FILE__) . 'v2/onesignal-utils.php';
require_once plugin_dir_path(__FILE__) . 'v2/onesignal-admin.php';
require_once plugin_dir_path(__FILE__) . 'v2/onesignal-public.php';
require_once plugin_dir_path(__FILE__) . 'v2/onesignal-settings.php';
require_once plugin_dir_path(__FILE__) . 'v2/onesignal-widget.php';
include_once plugin_dir_path(__FILE__) . 'v2/configuration-export.php';
include_once plugin_dir_path(__FILE__) . 'v2/complete-migration.php';

// Initialize V2 admin and public components
add_action('init', ['OneSignal_Admin', 'init']);
add_action('init', ['OneSignal_Public', 'init']);
}
18 changes: 17 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: OneSignal
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.5
Tested up to: 6.7
Stable tag: 2.4.4
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -64,6 +64,22 @@ OneSignal is trusted by over 1.8M+ developers and marketing strategists. We powe

== Changelog ==

= 3.0.0 =
WARNING: this update contains changes that may break specific setups, as detailed below:

If you have made any code modifications to your Wordpress installation to directly interact with the OneSignal SDK, this version will break your implementation. Please revert your code modifications prior to upgrading and use our improved dashboard functionality to configure your OneSignal integration instead.

Learn more: https://documentation.onesignal.com/docs/wordpress-plugin-30

- Support migration to new configuration flow.
- Simpler configuration interface.
- Ability to target specific segments.
- Automatically send Push Notifications when a WordPress post is published.
- Update "Tested up to" WP 6.7.
- Upgrades the OneSignal Web SDK (v15 --> v16).
- Web Topics are included by default.
- Send to mobile app subscribers, with an option to direct them to a different URL (Deep Link).

= 2.4.4 =
- Update "Tested up to" Wordpress version to 6.5
- Update OneSignal plugin menu icon
Expand Down
1 change: 0 additions & 1 deletion sdk_files/OneSignalSDKUpdaterWorker.js

This file was deleted.

14 changes: 0 additions & 14 deletions sdk_files/OneSignalSDKUpdaterWorker.js.php

This file was deleted.

2 changes: 1 addition & 1 deletion sdk_files/OneSignalSDKWorker.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');
importScripts("https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js");
21 changes: 9 additions & 12 deletions sdk_files/OneSignalSDKWorker.js.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<?php
/**
* Note: This file is intended to be publicly accessible.
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
*/

header("Service-Worker-Allowed: /");
header("Content-Type: application/javascript");
header("X-Robots-Tag: none");
if (defined('ONESIGNAL_DEBUG') && defined('ONESIGNAL_LOCAL')) {
echo "importScripts('https://localhost:3001/dev_sdks/OneSignalSDK.js');";
} else {
echo "importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');";
}
/**
* Note: This file is intended to be publicly accessible.
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
*/

header("Service-Worker-Allowed: /");
header("Content-Type: application/javascript");
header("X-Robots-Tag: none");
echo "importScripts('https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js');";
7 changes: 6 additions & 1 deletion sdk_files/index.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
<!-- Prevent search bots from indexing this folder if directory listing is enabled on the server -->
<html><head><meta name="robots" content="noindex, nofollow"></head><body></body></html>
<html>
<head>
<meta name="robots" content="noindex, nofollow" />
</head>
<body></body>
</html>
2 changes: 1 addition & 1 deletion sdk_files/push/onesignal/index.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<!-- Prevent search bots from indexing this folder if directory listing is enabled on the server -->
<html><head><meta name="robots" content="noindex, nofollow"></head><body></body></html>
<html><head><meta name="robots" content="noindex, nofollow"></head><body></body></html>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions v2/complete-migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

function onesignal_complete_migration()
{
if (isset($_POST['plugin_action']) && $_POST['plugin_action'] === 'complete_migration') {

// Security check
if (!check_admin_referer('onesignal_migration_nonce')) {
wp_die(__('Security check failed', 'onesignal-push'));
}

// Mark the plugin as migrated
update_option('onesignal_plugin_migrated', true);

// Provide feedback to the user
wp_redirect(admin_url('admin.php?page=onesignal-admin-page.php'));
exit;
}
}

add_action('admin_init', 'onesignal_complete_migration');
58 changes: 58 additions & 0 deletions v2/configuration-export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

function onesignal_handle_export()
{
if (isset($_POST['plugin_action']) && $_POST['plugin_action'] === 'export_settings') {

if (!check_admin_referer('onesignal_export_nonce')) {
wp_die(__('Security check failed', 'onesignal-push'));
}

$settings = get_option('OneSignalWPSetting');

// name and define settings groups
$groups = [
'OneSignal App ID' => '/^app_id/',
'General Options' => '/^(chrome_auto_|persist_|default_|utm_additional_)/',
'Slide Prompt Customizations' => '/^(prompt_customize_|prompt_action_|prompt_accept_|prompt_cancel_|prompt_auto_register)/',
'Subscription Bell Customizations' => '/^notifyButton_/',
'Welcome Notification Customizations' => '/^(send_welcome_|welcome_)/',
'Plugin Settings & HTTP Setup - NO LONGER REQUIRED / DEPRECATED' => '/^(allowed_custom_|customize_http_|custom_manifest_|gcm_|is_site_|notification_on_|notification_title|onesignal_sw_|origin|prompt_auto_accept_title|prompt_example_|prompt_site_name|send_to_mobile_|showNotification|show_gcm_|how_notification_send_|subdomain|use_)/'
];

// sort settings into the defined groups
foreach ($settings as $key => $value) {
foreach ($groups as $group_name => $pattern) {
if (preg_match($pattern, $key)) {
$grouped_settings[$group_name][$key] = is_array($value) ? json_encode($value) : $value;
break;
}
}
}

// create txt file with main title, group names, and settings.
$txt_data = "OneSignal Push Configuration Export\n\n\n";

foreach ($groups as $group_name => $pattern) {
if (isset($grouped_settings[$group_name])) {
$txt_data .= "=== $group_name ===\n";

foreach ($grouped_settings[$group_name] as $key => $value) {
$txt_data .= "$key: $value\n";
}

$txt_data .= "\n\n";
}
}

header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="onesignal-configuration-export.txt"');
header('Content-Length: ' . strlen($txt_data));
header('Pragma: public');

echo $txt_data;
exit;
}
}

add_action('admin_init', 'onesignal_handle_export');
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
44 changes: 20 additions & 24 deletions onesignal-public.php → v2/onesignal-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,34 @@ public static function onesignal_header()
wp_register_script('local_sdk', 'https://localhost:3001/sdks/OneSignalSDK.js', array(), '1.0.0', array('strategy' => 'async'));
wp_enqueue_script('local_sdk');
} else {
wp_register_script('remote_sdk', 'https://cdn.onesignal.com/sdks/OneSignalSDK.js', array(), '1.0.0', array('strategy' => 'async'));
wp_register_script('remote_sdk', 'https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js', array(), '1.0.0', array('strategy' => 'defer'));
wp_enqueue_script('remote_sdk');
} ?>
<script>

window.OneSignal = window.OneSignal || [];
window.OneSignalDeferred = window.OneSignalDeferred || [];

OneSignalDeferred.push(function(OneSignal) {
var oneSignal_options = {};
window._oneSignalInitOptions = oneSignal_options;

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' };";
echo "oneSignal_options['serviceWorkerParam'] = { scope: '$swScope' };\n";
echo "oneSignal_options['serviceWorkerPath'] = 'OneSignalSDKWorker.js';\n";
} else {
echo 'OneSignal.SERVICE_WORKER_UPDATER_PATH = "OneSignalSDKUpdaterWorker.js.php";
OneSignal.SERVICE_WORKER_PATH = "OneSignalSDKWorker.js.php";
OneSignal.SERVICE_WORKER_PARAM = { scope: "/" };';
echo "oneSignal_options['serviceWorkerParam'] = { scope: '/' };\n";
echo "oneSignal_options['serviceWorkerPath'] = 'OneSignalSDKWorker.js.php';\n";
}
?>

<?php
if (self::valid_for_key('default_url', $onesignal_wp_settings)) {
echo 'OneSignal.setDefaultNotificationUrl("'.esc_url($onesignal_wp_settings['default_url']).'");';
echo 'OneSignal.Notifications.setDefaultUrl("'.esc_url($onesignal_wp_settings['default_url']).'");';
} else {
echo 'OneSignal.setDefaultNotificationUrl("'.esc_url(get_site_url())."\");\n";
echo 'OneSignal.Notifications.setDefaultUrl("'.esc_url(get_site_url())."\");\n";
} ?>
var oneSignal_options = {};
window._oneSignalInitOptions = oneSignal_options;

<?php
echo "oneSignal_options['wordpress'] = true;\n";
Expand Down Expand Up @@ -189,10 +188,7 @@ public static function onesignal_header()

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;
});
return !OneSignal.User.PushSubscription.optedIn;
};\n";
}

Expand Down Expand Up @@ -305,17 +301,17 @@ public static function onesignal_header()
<?php
}
} else {
?>
OneSignal.init(window._oneSignalInitOptions);
<?php
?>
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();";
echo "OneSignal.Slidedown.promptPush()";
}

if (array_key_exists('use_native_prompt', $onesignal_wp_settings) && $onesignal_wp_settings['use_native_prompt'] === true) {
echo "OneSignal.showNativePrompt();";
echo "OneSignal.Notifications.requestPermission()";
}

} else {
Expand All @@ -330,9 +326,9 @@ function documentInitOneSignal() {

<?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(); };";
echo "var oneSignalLinkClickHandler = function(event) { OneSignal.Slidedown.promptPush(); event.preventDefault(); };";
} else {
echo "var oneSignalLinkClickHandler = function(event) { OneSignal.push(['registerForPushNotifications']); event.preventDefault(); };";
echo "var oneSignalLinkClickHandler = function(event) { OneSignal.Notifications.requestPermission(); event.preventDefault(); };";
} ?>
for(var i = 0; i < oneSignal_elements.length; i++)
oneSignal_elements[i].addEventListener('click', oneSignalLinkClickHandler, false);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 31e0d22

Please sign in to comment.