Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow users to define custom post types #341

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions v3/onesignal-admin/onesignal-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ window.addEventListener("DOMContentLoaded", () => {
const sendToMobileInfoDiv = document.querySelector(".mobile-app .information");
const utmParamsHelpIcon = document.querySelector(".utm-params .help");
const utmParamsInfoDiv = document.querySelector(".utm-params .information");
const customPostTypesHelpIcon = document.querySelector(".custom-post-types .help");
const customPostTypesInfoDiv = document.querySelector(".custom-post-types .information");
const notificationOnPostFromPluginHelpIcon = document.querySelector(".notification-on-post-from-plugin .help");
const notificationOnPostFromPluginInfoDiv = document.querySelector(".notification-on-post-from-plugin .information");

const setupToggleAction = (helpIcon, infoDiv) => {
if (helpIcon && infoDiv) {
Expand All @@ -15,6 +19,8 @@ window.addEventListener("DOMContentLoaded", () => {

setupToggleAction(sendToMobileHelpIcon, sendToMobileInfoDiv);
setupToggleAction(utmParamsHelpIcon, utmParamsInfoDiv);
setupToggleAction(customPostTypesHelpIcon, customPostTypesInfoDiv);
setupToggleAction(notificationOnPostFromPluginHelpIcon, notificationOnPostFromPluginInfoDiv);
});

window.addEventListener("DOMContentLoaded", () => {
Expand All @@ -24,13 +30,17 @@ window.addEventListener("DOMContentLoaded", () => {
const autoSendCheckbox = document.querySelector("#auto-send");
const sendToMobileCheckbox = document.querySelector("#send-to-mobile");
const saveButton = document.querySelector("#save-settings-button");
const customPostTypesInput = document.querySelector("#custom-post-types");
const notificationOnPostFromPluginCheckbox = document.querySelector("#notification-on-post-from-plugin");

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

function isValidUUID(uuid) {
const uuidRegex =
Expand Down Expand Up @@ -60,8 +70,10 @@ window.addEventListener("DOMContentLoaded", () => {
const utmChanged = utmInput.value !== initialUtmInput;
const autoSendChanged = autoSendCheckbox.checked !== initialAutoSend;
const sendToMobileChanged = sendToMobileCheckbox.checked !== initialSendToMobile;
const customPostTypesChanged = customPostTypesInput.value !== initialCustomPostTypes;
const notificationOnPostFromPluginChanged = notificationOnPostFromPluginCheckbox.checked !== initialNotificationOnPostFromPlugin;

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

function toggleSaveButton() {
Expand All @@ -87,9 +99,10 @@ window.addEventListener("DOMContentLoaded", () => {
});

utmInput.addEventListener("input", toggleSaveButton);

autoSendCheckbox.addEventListener("change", toggleSaveButton);
sendToMobileCheckbox.addEventListener("change", toggleSaveButton);
customPostTypesInput.addEventListener("input", toggleSaveButton);
notificationOnPostFromPluginCheckbox.addEventListener("change", toggleSaveButton);

// Initial state on page load
toggleSaveButton();
Expand Down
54 changes: 54 additions & 0 deletions v3/onesignal-admin/onesignal-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ function admin_files()
$onesignal_settings['utm_additional_url_params'] = sanitize_text_field($_POST['utm_additional_url_params']);
}

if (isset($_POST['allowed_custom_post_types'])) {
$onesignal_settings['allowed_custom_post_types'] = sanitize_text_field($_POST['allowed_custom_post_types']);
}

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

// Save the notification on post from plugin setting
$notification_on_post_from_plugin = isset($_POST['notification_on_post_from_plugin']) ? 1 : 0;
$onesignal_settings['notification_on_post_from_plugin'] = $notification_on_post_from_plugin;

// 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;
Expand Down Expand Up @@ -147,6 +155,31 @@ function onesignal_admin_page()
</div>
</div>

<?php
$oneSignalSettings = get_option('OneSignalWPSetting');
$customPostTypes = ''; // Default empty value

// Check if the settings are an array and if the key exists
if (is_array($oneSignalSettings) && isset($oneSignalSettings['allowed_custom_post_types'])) {
$customPostTypes = esc_attr($oneSignalSettings['allowed_custom_post_types']);
}
?>
<br>
<div class="field custom-post-types">
<label>Additional Custom Post Types for Automatic Notifications Created From Plugins</label>
<div class="help" aria-label="More information">
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" fill="currentColor">
<g fill="currentColor">
<path d="M8 0a8 8 0 108 8 8.009 8.009 0 00-8-8zm0 12.667a1 1 0 110-2 1 1 0 010 2zm1.067-4.054a.667.667 0 00-.4.612.667.667 0 01-1.334 0 2 2 0 011.2-1.834A1.333 1.333 0 106.667 6.17a.667.667 0 01-1.334 0 2.667 2.667 0 113.734 2.444z"></path>
</g>
</svg>
</div>
<div class="information" style="display: none;">
<p>Enter a comma-separated list of custom post type names. Anytime a post is published with one of the listed post types, a notification will be sent to all your users. <strong class='least-strong'>The setting</strong> <em>Automatically send a push notification when I publish a post from 3rd party plugins</em> <strong class='least-strong'>must be enabled for this feature to work</strong>."</p>
</div>
<input id="custom-post-types"type="text" placeholder="forum,reply,topic (comma separated, no spaces between commas)" name="allowed_custom_post_types" value="<?php echo esc_attr($customPostTypes); ?>">
</div>

<!-- Auto Send Checkbox -->
<div class="checkbox-wrapper auto-send">
<label for="auto-send">
Expand All @@ -157,6 +190,27 @@ function onesignal_admin_page()
</label>
</div>

<!-- 3rd Party Plugins Checkbox -->
<div class="checkbox-wrapper notification-on-post-from-plugin">
<label for="notification-on-post-from-plugin">
<input id="notification-on-post-from-plugin" type="checkbox" name="notification_on_post_from_plugin" value="true"
<?php echo (get_option('OneSignalWPSetting')['notification_on_post_from_plugin'] ?? 0) == 1 ? 'checked' : ''; ?>
>
<span class="checkbox"></span>
Automatically send a push notification when I publish a post from 3<sup>rd</sup> party plugins
</label>
<div class="help" aria-label="More information">
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" fill="currentColor">
<g fill="currentColor">
<path d="M8 0a8 8 0 108 8 8.009 8.009 0 00-8-8zm0 12.667a1 1 0 110-2 1 1 0 010 2zm1.067-4.054a.667.667 0 00-.4.612.667.667 0 01-1.334 0 2 2 0 011.2-1.834A1.333 1.333 0 106.667 6.17a.667.667 0 01-1.334 0 2.667 2.667 0 113.734 2.444z"></path>
</g>
</svg>
</div>
<div class="information" style="display: none;">
<p>If checked, when a post is created outside of WordPress's editor, a push notification will automatically be sent. Must be the built-in WordPress post type 'post' and the post must be published.</p>
</div>
</div>

<!-- Mobile App Checkbox -->
<div class="checkbox-wrapper mobile-app">
<label for="send-to-mobile">
Expand Down
Loading