Skip to content

Commit

Permalink
Merge pull request #339 from OneSignal/3.0.4
Browse files Browse the repository at this point in the history
3.0.4 - Release - Add back features, bug fixes
Rodrigo Gomez Palacio authored Jan 3, 2025
2 parents 42b4ba6 + 03b8a9d commit 7483da9
Showing 9 changed files with 134 additions and 592 deletions.
2 changes: 1 addition & 1 deletion onesignal.php
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
* Plugin Name: OneSignal Push Notifications
* Plugin URI: https://onesignal.com/
* Description: Free web push notifications.
* Version: 3.0.3
* Version: 3.0.4
* Author: OneSignal
* Author URI: https://onesignal.com
* License: MIT
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -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.3
Stable tag: 3.0.4
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

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

== Changelog ==

= 3.0.4 =
- Added features: auto-send on publish, UTM tags
- Bug fixes: validation issues on settings form

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

34 changes: 31 additions & 3 deletions v3/onesignal-admin/onesignal-admin.js
Original file line number Diff line number Diff line change
@@ -13,9 +13,18 @@ window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("DOMContentLoaded", () => {
const appIdInput = document.querySelector("#appid");
const apiKeyInput = document.querySelector("#apikey");
const utmInput = document.querySelector("#utm-params");
const autoSendCheckbox = document.querySelector("#auto-send");
const sendToMobileCheckbox = document.querySelector("#send-to-mobile");
const saveButton = document.querySelector("#save-settings-button");

if (appIdInput && apiKeyInput && saveButton) {
if (appIdInput && apiKeyInput && autoSendCheckbox && sendToMobileCheckbox && utmInput && saveButton) {
const initialAppId = appIdInput.value;
const initialApiKey = apiKeyInput.value;
const initialUtmInput = utmInput.value;
const initialAutoSend = autoSendCheckbox.checked;
const initialSendToMobile = sendToMobileCheckbox.checked;

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;
@@ -38,10 +47,24 @@ window.addEventListener("DOMContentLoaded", () => {
}
}

function hasFormChanged() {
const appIdChanged = appIdInput.value !== initialAppId;
const apiKeyChanged = apiKeyInput.value !== initialApiKey;
const utmChanged = utmInput.value !== initialUtmInput;
const autoSendChanged = autoSendCheckbox.checked !== initialAutoSend;
const sendToMobileChanged = sendToMobileCheckbox.checked !== initialSendToMobile;

return appIdChanged || apiKeyChanged || autoSendChanged || sendToMobileChanged || utmChanged;
}

function toggleSaveButton() {
const appIdValid = isValidUUID(appIdInput.value);
const apiKeyValid = isValidApiKey(apiKeyInput.value);
saveButton.disabled = !(appIdValid && apiKeyValid); // Enable button only if both are valid
const apiKeyValid = apiKeyInput.value.length == 0 || isValidApiKey(apiKeyInput.value);
const formChanged = hasFormChanged();

// Enable button if either text inputs are valid or toggles have changed
const enabled = formChanged && appIdValid && apiKeyValid;
saveButton.disabled = !enabled;
}

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

utmInput.addEventListener("input", toggleSaveButton);

autoSendCheckbox.addEventListener("change", toggleSaveButton);
sendToMobileCheckbox.addEventListener("change", toggleSaveButton);

// Initial state on page load
toggleSaveButton();
}
49 changes: 37 additions & 12 deletions v3/onesignal-admin/onesignal-admin.php
Original file line number Diff line number Diff line change
@@ -22,21 +22,29 @@ function admin_files()

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

$onesignal_settings = get_option('OneSignalWPSetting', array());
if (isset($_POST['onesignal_app_id']) && !empty($_POST['onesignal_app_id'])) {
$onesignal_settings['app_id'] = sanitize_text_field($_POST['onesignal_app_id']);
}

if (isset($_POST['onesignal_app_id']) && !empty($_POST['onesignal_app_id'])) {
$onesignal_settings['app_id'] = sanitize_text_field($_POST['onesignal_app_id']);
}
if (isset($_POST['onesignal_rest_api_key']) && !empty($_POST['onesignal_rest_api_key'])) {
$onesignal_settings['app_rest_api_key'] = sanitize_text_field($_POST['onesignal_rest_api_key']);
}

if (isset($_POST['onesignal_rest_api_key']) && !empty($_POST['onesignal_rest_api_key'])) {
$onesignal_settings['app_rest_api_key'] = sanitize_text_field($_POST['onesignal_rest_api_key']);
}
if (isset($_POST['utm_additional_url_params'])) {
$onesignal_settings['utm_additional_url_params'] = sanitize_text_field($_POST['utm_additional_url_params']);
}

// Save the auto send notifications setting
$auto_send = isset($_POST['onesignal_auto_send']) ? 1 : 0;
$onesignal_settings['notification_on_post'] = $auto_send;

$send_to_mobile = isset($_POST['onesignal_send_to_mobile']) ? 1 : 0;
$onesignal_settings['send_to_mobile_platforms'] = $send_to_mobile;
// Save the mobile subscribers setting
$send_to_mobile = isset($_POST['onesignal_send_to_mobile']) ? 1 : 0;
$onesignal_settings['send_to_mobile_platforms'] = $send_to_mobile;

update_option('OneSignalWPSetting', $onesignal_settings);
update_option('OneSignalWPSetting', $onesignal_settings);
}
}

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

<h3>Advanced Settings</h3>
<div class="ui borderless shadowless segment">
<div class="field">
<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>
<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']); ?>">
</div>
</div>

<!-- Auto Send Checkbox -->
<div class="checkbox-wrapper">
<label for="auto-send">
<input id="auto-send" type="checkbox" name="onesignal_auto_send"
<?php echo (get_option('OneSignalWPSetting')['notification_on_post'] ?? 0) == 1 ? 'checked' : ''; ?>>
<span class="checkbox"></span>
Automatically send notifications when a post is published or updated
</label>
</div>

<!-- Mobile App Checkbox -->
<div class="checkbox-wrapper">
<label for="send-to-mobile">
<input id="send-to-mobile" type="checkbox" name="onesignal_send_to_mobile"
<input id="send-to-mobile" type="checkbox" name="onesignal_send_to_mobile"
<?php echo (get_option('OneSignalWPSetting')['send_to_mobile_platforms'] ?? 0) == 1 ? 'checked' : ''; ?>>
<span class="checkbox"></span>
Send notification to Mobile app subscribers
@@ -133,7 +159,6 @@ function onesignal_admin_page()
<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>
</div>
</div>

<?php submit_button('Save Settings', 'primary', 'submit', true, array('id' => 'save-settings-button')); ?>
</form>
</div>
12 changes: 6 additions & 6 deletions v3/onesignal-init.php
Original file line number Diff line number Diff line change
@@ -8,10 +8,9 @@
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' : '');
$filename = 'OneSignalSDKWorker.js';
?>
<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
<script>
@@ -21,11 +20,12 @@ 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: "<?php echo $use_root_scope ? '/' : $scope ?>" },
serviceWorkerParam: { scope: "<?php echo $scope ?>" },
serviceWorkerPath: "<?php echo $filename; ?>",
});
});
// TO DO: move this to a separate file

// Unregister the legacy OneSignal service worker to prevent scope conflicts
navigator.serviceWorker.getRegistrations().then((registrations) => {
// Iterate through all registered service workers
registrations.forEach((registration) => {
@@ -34,9 +34,9 @@ function onesignal_init()
// Unregister the service worker
registration.unregister().then((success) => {
if (success) {
console.log('Successfully unregistered:', registration.active.scriptURL);
console.log('OneSignalSW: Successfully unregistered:', registration.active.scriptURL);
} else {
console.log('Failed to unregister:', registration.active.scriptURL);
console.log('OneSignalSW: Failed to unregister:', registration.active.scriptURL);
}
});
}
7 changes: 7 additions & 0 deletions v3/onesignal-metabox/onesignal-metabox.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,13 @@ window.addEventListener("DOMContentLoaded", () => {
const optionsWrap = document.getElementById("os_options");
const customisePost = document.getElementById("os_customise");
const customiseWrap = document.getElementById("os_customisations");

// Guard against missing elements
if (!sendPost || !optionsWrap || !customisePost || !customiseWrap) {
console.error("OneSignal: required elements are missing in the DOM.");
return;
}

const customiseWrapChild = customiseWrap.querySelectorAll("input");

function setDisplay(elem, checked) {
47 changes: 36 additions & 11 deletions v3/onesignal-metabox/onesignal-metabox.php
Original file line number Diff line number Diff line change
@@ -28,26 +28,51 @@ function onesignal_metabox($post)

// Make the API request, log errors, get segment names.
$response = wp_remote_get('https://onesignal.com/api/v1/apps/' . get_option('OneSignalWPSetting')['app_id'] . '/segments', $args);

if (is_wp_error($response)) {
error_log('API request failed: ' . $response->get_error_message());
error_log('API request failed: ' . $response->get_error_message());
$json = null; // Handle error case
} else {
$body = wp_remote_retrieve_body($response);
$json = json_decode($body);

// Check if segments exist and are an array
if (!isset($json->segments) || !is_array($json->segments)) {
error_log('Unexpected API response: Missing or invalid key');
$json = null;
}
}
$json = json_decode(wp_remote_retrieve_body($response));

// Meta box content -> js file hides sections depending on whats checked.
?>
<label for="os_update">
<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>
<input type="checkbox" name="os_update" id="os_update"
<?php
$os_update_checked = isset($os_meta['os_update'])
? $os_meta['os_update'] == 'on'
: (get_option('OneSignalWPSetting')['notification_on_post'] ?? 0) == 1;

echo $os_update_checked ? 'checked' : '';
?>>
Send notification when post is published or updated
</label>
<div id="os_options">
<label for="os_segment">Send to segment</label>
<select name="os_segment" id="os_segment">
<option value="All">All</option>
<?php
for ($i = 0; $i < count($json->segments); $i++) {
$selected = isset($post->os_meta['os_segment']) && $post->os_meta['os_segment'] === $json->segments[$i]->name ? 'selected' : '';
echo '<option value="' . $json->segments[$i]->name . '"' . $selected . '>' . $json->segments[$i]->name . '</option>';
}
?>
</select>
<option value="All">All</option>
<?php
if ($json && is_array($json->segments)) {
foreach ($json->segments as $segment) {
if (isset($segment->name)) {
$selected = isset($post->os_meta['os_segment']) && $post->os_meta['os_segment'] === $segment->name ? 'selected' : '';
echo '<option value="' . esc_attr($segment->name) . '"' . $selected . '>' . esc_html($segment->name) . '</option>';
}
}
} else {
echo '<option disabled>No segments available</option>';
}
?>
</select>
<hr>
<label for="os_customise">
<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>
14 changes: 11 additions & 3 deletions v3/onesignal-notification.php
Original file line number Diff line number Diff line change
@@ -9,7 +9,8 @@
// Function to schedule notification
function onesignal_schedule_notification($new_status, $old_status, $post)
{
if (($new_status === 'publish') || ($new_status === 'future')) {
if (($new_status === 'publish') || ($new_status === 'future')) {
$onesignal_wp_settings = get_option("OneSignalWPSetting");

// check if update is on.
$update = !empty($_POST['os_update']) ? $_POST['os_update'] : $post->os_update;
@@ -24,6 +25,13 @@ function onesignal_schedule_notification($new_status, $old_status, $post)
$content = !empty($_POST['os_content']) ? $_POST['os_content'] : $post->post_content;
$excerpt = $excerpt = substr($content, 0, 120);
$segment = $_POST['os_segment'] ?? 'All';
$config_utm_additional_url_params = $onesignal_wp_settings['utm_additional_url_params'] ?? '';
$url = get_permalink($post->ID);

// Append UTM parameters to the URL
if (!empty($config_utm_additional_url_params)) {
$url = $url . (strpos($url, '?') === false ? '?' : '&') . $config_utm_additional_url_params;
}

$apiKeyType = onesignal_get_api_key_type();
$authorizationHeader = $apiKeyType === "Rich"
@@ -67,10 +75,10 @@ function onesignal_schedule_notification($new_status, $old_status, $post)
$fields['app_url'] = $_POST['os_mobile_url'];
$fields['web_url'] = get_permalink($post->ID);
} else {
$fields['url'] = get_permalink($post->ID);
$fields['url'] = $url;
}
} else {
$fields['url'] = get_permalink($post->ID);
$fields['url'] = $url;
}
// Set notification images based on the post's featured image
if (has_post_thumbnail($post->ID)) {
555 changes: 0 additions & 555 deletions v3/readme.txt

This file was deleted.

0 comments on commit 7483da9

Please sign in to comment.