Skip to content

Commit 7483da9

Browse files
author
Rodrigo Gomez Palacio
authored
Merge pull request #339 from OneSignal/3.0.4
3.0.4 - Release - Add back features, bug fixes
2 parents 42b4ba6 + 03b8a9d commit 7483da9

File tree

9 files changed

+134
-592
lines changed

9 files changed

+134
-592
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.3
9+
* Version: 3.0.4
1010
* Author: OneSignal
1111
* Author URI: https://onesignal.com
1212
* License: MIT

readme.txt

Lines changed: 5 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.3
7+
Stable tag: 3.0.4
88
License: GPLv2 or later
99
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1010

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

6565
== Changelog ==
6666

67+
= 3.0.4 =
68+
- Added features: auto-send on publish, UTM tags
69+
- Bug fixes: validation issues on settings form
70+
6771
= 3.0.3 =
6872
- Bug fix: fix service worker registration issue.
6973

v3/onesignal-admin/onesignal-admin.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@ window.addEventListener("DOMContentLoaded", () => {
1313
window.addEventListener("DOMContentLoaded", () => {
1414
const appIdInput = document.querySelector("#appid");
1515
const apiKeyInput = document.querySelector("#apikey");
16+
const utmInput = document.querySelector("#utm-params");
17+
const autoSendCheckbox = document.querySelector("#auto-send");
18+
const sendToMobileCheckbox = document.querySelector("#send-to-mobile");
1619
const saveButton = document.querySelector("#save-settings-button");
1720

18-
if (appIdInput && apiKeyInput && saveButton) {
21+
if (appIdInput && apiKeyInput && autoSendCheckbox && sendToMobileCheckbox && utmInput && saveButton) {
22+
const initialAppId = appIdInput.value;
23+
const initialApiKey = apiKeyInput.value;
24+
const initialUtmInput = utmInput.value;
25+
const initialAutoSend = autoSendCheckbox.checked;
26+
const initialSendToMobile = sendToMobileCheckbox.checked;
27+
1928
function isValidUUID(uuid) {
2029
const uuidRegex =
2130
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
@@ -38,10 +47,24 @@ window.addEventListener("DOMContentLoaded", () => {
3847
}
3948
}
4049

50+
function hasFormChanged() {
51+
const appIdChanged = appIdInput.value !== initialAppId;
52+
const apiKeyChanged = apiKeyInput.value !== initialApiKey;
53+
const utmChanged = utmInput.value !== initialUtmInput;
54+
const autoSendChanged = autoSendCheckbox.checked !== initialAutoSend;
55+
const sendToMobileChanged = sendToMobileCheckbox.checked !== initialSendToMobile;
56+
57+
return appIdChanged || apiKeyChanged || autoSendChanged || sendToMobileChanged || utmChanged;
58+
}
59+
4160
function toggleSaveButton() {
4261
const appIdValid = isValidUUID(appIdInput.value);
43-
const apiKeyValid = isValidApiKey(apiKeyInput.value);
44-
saveButton.disabled = !(appIdValid && apiKeyValid); // Enable button only if both are valid
62+
const apiKeyValid = apiKeyInput.value.length == 0 || isValidApiKey(apiKeyInput.value);
63+
const formChanged = hasFormChanged();
64+
65+
// Enable button if either text inputs are valid or toggles have changed
66+
const enabled = formChanged && appIdValid && apiKeyValid;
67+
saveButton.disabled = !enabled;
4568
}
4669

4770
appIdInput.addEventListener("input", () => {
@@ -56,6 +79,11 @@ window.addEventListener("DOMContentLoaded", () => {
5679
toggleSaveButton();
5780
});
5881

82+
utmInput.addEventListener("input", toggleSaveButton);
83+
84+
autoSendCheckbox.addEventListener("change", toggleSaveButton);
85+
sendToMobileCheckbox.addEventListener("change", toggleSaveButton);
86+
5987
// Initial state on page load
6088
toggleSaveButton();
6189
}

v3/onesignal-admin/onesignal-admin.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,29 @@ function admin_files()
2222

2323
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
2424
if (isset($_POST["submit"])) {
25+
$onesignal_settings = get_option('OneSignalWPSetting', array());
2526

26-
$onesignal_settings = get_option('OneSignalWPSetting', array());
27+
if (isset($_POST['onesignal_app_id']) && !empty($_POST['onesignal_app_id'])) {
28+
$onesignal_settings['app_id'] = sanitize_text_field($_POST['onesignal_app_id']);
29+
}
2730

28-
if (isset($_POST['onesignal_app_id']) && !empty($_POST['onesignal_app_id'])) {
29-
$onesignal_settings['app_id'] = sanitize_text_field($_POST['onesignal_app_id']);
30-
}
31+
if (isset($_POST['onesignal_rest_api_key']) && !empty($_POST['onesignal_rest_api_key'])) {
32+
$onesignal_settings['app_rest_api_key'] = sanitize_text_field($_POST['onesignal_rest_api_key']);
33+
}
3134

32-
if (isset($_POST['onesignal_rest_api_key']) && !empty($_POST['onesignal_rest_api_key'])) {
33-
$onesignal_settings['app_rest_api_key'] = sanitize_text_field($_POST['onesignal_rest_api_key']);
34-
}
35+
if (isset($_POST['utm_additional_url_params'])) {
36+
$onesignal_settings['utm_additional_url_params'] = sanitize_text_field($_POST['utm_additional_url_params']);
37+
}
38+
39+
// Save the auto send notifications setting
40+
$auto_send = isset($_POST['onesignal_auto_send']) ? 1 : 0;
41+
$onesignal_settings['notification_on_post'] = $auto_send;
3542

36-
$send_to_mobile = isset($_POST['onesignal_send_to_mobile']) ? 1 : 0;
37-
$onesignal_settings['send_to_mobile_platforms'] = $send_to_mobile;
43+
// Save the mobile subscribers setting
44+
$send_to_mobile = isset($_POST['onesignal_send_to_mobile']) ? 1 : 0;
45+
$onesignal_settings['send_to_mobile_platforms'] = $send_to_mobile;
3846

39-
update_option('OneSignalWPSetting', $onesignal_settings);
47+
update_option('OneSignalWPSetting', $onesignal_settings);
4048
}
4149
}
4250

@@ -112,10 +120,28 @@ function onesignal_admin_page()
112120
</p>
113121
<p class="help-text">The REST API Key is hidden for security reasons. Enter a new key to update.</p>
114122

123+
<h3>Advanced Settings</h3>
124+
<div class="ui borderless shadowless segment">
125+
<div class="field">
126+
<label>Additional Notification URL Parameters<i class="tiny circular help icon link" role="popup" data-html="Adds the specified string as extra URL parameters to your notification URL so that they can be tracked as an event by your analytics system. <em>Please escape your parameter values</em>; your input will be added as-is to the end of your notification URL. Example:</p>If you want:<em><li><code>utm_medium</code> to be <code>ppc</code></li><li><code>utm_source</code> to be <code>adwords</code></li><li><code>utm_campaign</code> to be <code>snow boots</code></li><li><code>utm_content</code> to be <code>durable snow boots</code></li></em><p><p>Then use the following string:</p><p><code style='word-break: break-all;'>utm_medium=ppc&utm_source=adwords&utm_campaign=snow%20boots&utm_content=durable%20%snow%boots</code></p>" data-variation="wide"></i></label>
127+
<input id="utm-params" type="text" placeholder="utm_medium=ppc&utm_source=adwords&utm_campaign=snow%20boots&utm_content=durable%20%snow%boots" name="utm_additional_url_params" value="<?php echo esc_attr(get_option('OneSignalWPSetting')['utm_additional_url_params']); ?>">
128+
</div>
129+
</div>
130+
131+
<!-- Auto Send Checkbox -->
132+
<div class="checkbox-wrapper">
133+
<label for="auto-send">
134+
<input id="auto-send" type="checkbox" name="onesignal_auto_send"
135+
<?php echo (get_option('OneSignalWPSetting')['notification_on_post'] ?? 0) == 1 ? 'checked' : ''; ?>>
136+
<span class="checkbox"></span>
137+
Automatically send notifications when a post is published or updated
138+
</label>
139+
</div>
140+
115141
<!-- Mobile App Checkbox -->
116142
<div class="checkbox-wrapper">
117143
<label for="send-to-mobile">
118-
<input id="send-to-mobile" type="checkbox" name="onesignal_send_to_mobile"
144+
<input id="send-to-mobile" type="checkbox" name="onesignal_send_to_mobile"
119145
<?php echo (get_option('OneSignalWPSetting')['send_to_mobile_platforms'] ?? 0) == 1 ? 'checked' : ''; ?>>
120146
<span class="checkbox"></span>
121147
Send notification to Mobile app subscribers
@@ -133,7 +159,6 @@ function onesignal_admin_page()
133159
<p>If you do not include a different URL, it will direct them to your Website, rather than a specific page of your app.</p>
134160
</div>
135161
</div>
136-
137162
<?php submit_button('Save Settings', 'primary', 'submit', true, array('id' => 'save-settings-button')); ?>
138163
</form>
139164
</div>

v3/onesignal-init.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
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;
1211
$path = rtrim(parse_url(ONESIGNAL_PLUGIN_URL)['path'], '/');
1312
$scope = $path . '/sdk_files/push/onesignal/';
14-
$filename = 'OneSignalSDKWorker.js' . ($use_root_scope ? '.php' : '');
13+
$filename = 'OneSignalSDKWorker.js';
1514
?>
1615
<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
1716
<script>
@@ -21,11 +20,12 @@ function onesignal_init()
2120
appId: "<?php echo esc_html($onesignal_wp_settings['app_id']); ?>",
2221
serviceWorkerOverrideForTypical: true,
2322
path: "<?php echo ONESIGNAL_PLUGIN_URL; ?>sdk_files/",
24-
serviceWorkerParam: { scope: "<?php echo $use_root_scope ? '/' : $scope ?>" },
23+
serviceWorkerParam: { scope: "<?php echo $scope ?>" },
2524
serviceWorkerPath: "<?php echo $filename; ?>",
2625
});
2726
});
28-
// TO DO: move this to a separate file
27+
28+
// Unregister the legacy OneSignal service worker to prevent scope conflicts
2929
navigator.serviceWorker.getRegistrations().then((registrations) => {
3030
// Iterate through all registered service workers
3131
registrations.forEach((registration) => {
@@ -34,9 +34,9 @@ function onesignal_init()
3434
// Unregister the service worker
3535
registration.unregister().then((success) => {
3636
if (success) {
37-
console.log('Successfully unregistered:', registration.active.scriptURL);
37+
console.log('OneSignalSW: Successfully unregistered:', registration.active.scriptURL);
3838
} else {
39-
console.log('Failed to unregister:', registration.active.scriptURL);
39+
console.log('OneSignalSW: Failed to unregister:', registration.active.scriptURL);
4040
}
4141
});
4242
}

v3/onesignal-metabox/onesignal-metabox.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ window.addEventListener("DOMContentLoaded", () => {
33
const optionsWrap = document.getElementById("os_options");
44
const customisePost = document.getElementById("os_customise");
55
const customiseWrap = document.getElementById("os_customisations");
6+
7+
// Guard against missing elements
8+
if (!sendPost || !optionsWrap || !customisePost || !customiseWrap) {
9+
console.error("OneSignal: required elements are missing in the DOM.");
10+
return;
11+
}
12+
613
const customiseWrapChild = customiseWrap.querySelectorAll("input");
714

815
function setDisplay(elem, checked) {

v3/onesignal-metabox/onesignal-metabox.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,51 @@ function onesignal_metabox($post)
2828

2929
// Make the API request, log errors, get segment names.
3030
$response = wp_remote_get('https://onesignal.com/api/v1/apps/' . get_option('OneSignalWPSetting')['app_id'] . '/segments', $args);
31+
3132
if (is_wp_error($response)) {
32-
error_log('API request failed: ' . $response->get_error_message());
33+
error_log('API request failed: ' . $response->get_error_message());
34+
$json = null; // Handle error case
35+
} else {
36+
$body = wp_remote_retrieve_body($response);
37+
$json = json_decode($body);
38+
39+
// Check if segments exist and are an array
40+
if (!isset($json->segments) || !is_array($json->segments)) {
41+
error_log('Unexpected API response: Missing or invalid key');
42+
$json = null;
43+
}
3344
}
34-
$json = json_decode(wp_remote_retrieve_body($response));
3545

3646
// Meta box content -> js file hides sections depending on whats checked.
3747
?>
3848
<label for="os_update">
39-
<input type="checkbox" name="os_update" id="os_update" <?php echo isset($post->os_meta['os_update']) && $post->os_meta['os_update'] == 'on' ? 'checked' : '' ?>>Send notification when post is published or updated</label>
49+
<input type="checkbox" name="os_update" id="os_update"
50+
<?php
51+
$os_update_checked = isset($os_meta['os_update'])
52+
? $os_meta['os_update'] == 'on'
53+
: (get_option('OneSignalWPSetting')['notification_on_post'] ?? 0) == 1;
54+
55+
echo $os_update_checked ? 'checked' : '';
56+
?>>
57+
Send notification when post is published or updated
58+
</label>
4059
<div id="os_options">
4160
<label for="os_segment">Send to segment</label>
4261
<select name="os_segment" id="os_segment">
43-
<option value="All">All</option>
44-
<?php
45-
for ($i = 0; $i < count($json->segments); $i++) {
46-
$selected = isset($post->os_meta['os_segment']) && $post->os_meta['os_segment'] === $json->segments[$i]->name ? 'selected' : '';
47-
echo '<option value="' . $json->segments[$i]->name . '"' . $selected . '>' . $json->segments[$i]->name . '</option>';
48-
}
49-
?>
50-
</select>
62+
<option value="All">All</option>
63+
<?php
64+
if ($json && is_array($json->segments)) {
65+
foreach ($json->segments as $segment) {
66+
if (isset($segment->name)) {
67+
$selected = isset($post->os_meta['os_segment']) && $post->os_meta['os_segment'] === $segment->name ? 'selected' : '';
68+
echo '<option value="' . esc_attr($segment->name) . '"' . $selected . '>' . esc_html($segment->name) . '</option>';
69+
}
70+
}
71+
} else {
72+
echo '<option disabled>No segments available</option>';
73+
}
74+
?>
75+
</select>
5176
<hr>
5277
<label for="os_customise">
5378
<input type="checkbox" name="os_customise" id="os_customise" <?php echo isset($post->os_meta['os_customise']) && $post->os_meta['os_customise'] == 'on' ? 'checked' : '' ?>>Customize notification content</label>

v3/onesignal-notification.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
// Function to schedule notification
1010
function onesignal_schedule_notification($new_status, $old_status, $post)
1111
{
12-
if (($new_status === 'publish') || ($new_status === 'future')) {
12+
if (($new_status === 'publish') || ($new_status === 'future')) {
13+
$onesignal_wp_settings = get_option("OneSignalWPSetting");
1314

1415
// check if update is on.
1516
$update = !empty($_POST['os_update']) ? $_POST['os_update'] : $post->os_update;
@@ -24,6 +25,13 @@ function onesignal_schedule_notification($new_status, $old_status, $post)
2425
$content = !empty($_POST['os_content']) ? $_POST['os_content'] : $post->post_content;
2526
$excerpt = $excerpt = substr($content, 0, 120);
2627
$segment = $_POST['os_segment'] ?? 'All';
28+
$config_utm_additional_url_params = $onesignal_wp_settings['utm_additional_url_params'] ?? '';
29+
$url = get_permalink($post->ID);
30+
31+
// Append UTM parameters to the URL
32+
if (!empty($config_utm_additional_url_params)) {
33+
$url = $url . (strpos($url, '?') === false ? '?' : '&') . $config_utm_additional_url_params;
34+
}
2735

2836
$apiKeyType = onesignal_get_api_key_type();
2937
$authorizationHeader = $apiKeyType === "Rich"
@@ -67,10 +75,10 @@ function onesignal_schedule_notification($new_status, $old_status, $post)
6775
$fields['app_url'] = $_POST['os_mobile_url'];
6876
$fields['web_url'] = get_permalink($post->ID);
6977
} else {
70-
$fields['url'] = get_permalink($post->ID);
78+
$fields['url'] = $url;
7179
}
7280
} else {
73-
$fields['url'] = get_permalink($post->ID);
81+
$fields['url'] = $url;
7482
}
7583
// Set notification images based on the post's featured image
7684
if (has_post_thumbnail($post->ID)) {

0 commit comments

Comments
 (0)