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: uncheck the "Send notification" checkbox when a post is published #342

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
34 changes: 34 additions & 0 deletions v3/onesignal-metabox/onesignal-metabox.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,38 @@ window.addEventListener("DOMContentLoaded", () => {
setDisplay(customiseWrap, customisePost.checked);
setDisabled(customiseWrapChild, !customisePost.checked);
});

// make sure WordPress editor and API are available
if (typeof wp === 'undefined' || !wp.data || !wp.data.select) {
console.warn('wp.data is not available.');
return;
}

const editorStore = wp.data.select('core/editor');

// track initial state of checkbox
const osUpdateCheckbox = document.querySelector('#os_update');
const wasCheckedInitially = osUpdateCheckbox ? osUpdateCheckbox.checked : false;

// track previous post status to detect changes
let previousStatus = editorStore.getCurrentPostAttribute('status');

// subscribe to state changes
wp.data.subscribe(() => {
const currentStatus = editorStore.getCurrentPostAttribute('status');

// check if the post status changed to "publish"
if (previousStatus !== currentStatus && currentStatus === 'publish') {
previousStatus = currentStatus;

if (wasCheckedInitially) {
// uncheck the os_update checkbox
if (osUpdateCheckbox && osUpdateCheckbox.checked) {
osUpdateCheckbox.checked = false;
}
}
Copy link
Author

@sherwinski sherwinski Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: think we can just reuse the element defined on L2

Suggested change
// track initial state of checkbox
const osUpdateCheckbox = document.querySelector('#os_update');
const wasCheckedInitially = osUpdateCheckbox ? osUpdateCheckbox.checked : false;
// track previous post status to detect changes
let previousStatus = editorStore.getCurrentPostAttribute('status');
// subscribe to state changes
wp.data.subscribe(() => {
const currentStatus = editorStore.getCurrentPostAttribute('status');
// check if the post status changed to "publish"
if (previousStatus !== currentStatus && currentStatus === 'publish') {
previousStatus = currentStatus;
if (wasCheckedInitially) {
// uncheck the os_update checkbox
if (osUpdateCheckbox && osUpdateCheckbox.checked) {
osUpdateCheckbox.checked = false;
}
}
const wasCheckedInitially = sendPost ? sendPost.checked : false;
// track previous post status to detect changes
let previousStatus = editorStore.getCurrentPostAttribute('status');
// subscribe to state changes
wp.data.subscribe(() => {
const currentStatus = editorStore.getCurrentPostAttribute('status');
// check if the post status changed to "publish"
if (previousStatus !== currentStatus && currentStatus === 'publish') {
previousStatus = currentStatus;
if (wasCheckedInitially) {
// uncheck the sendPost checkbox
if (sendPost && sendPost.checked) {
sendPost.checked = false;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Added a fix for that

} else {
previousStatus = currentStatus;
}
});
});
5 changes: 4 additions & 1 deletion v3/onesignal-metabox/onesignal-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ function onesignal_metabox($post)
<label for="os_update">
<input type="checkbox" name="os_update" id="os_update"
<?php
$post_status = get_post_status($post->ID);
$default_enabled = get_option('OneSignalWPSetting')['notification_on_post'] ?? 0;

$os_update_checked = isset($os_meta['os_update'])
? $os_meta['os_update'] == 'on'
: (get_option('OneSignalWPSetting')['notification_on_post'] ?? 0) == 1;
: ($default_enabled == 1 && $post_status !== 'publish');

echo $os_update_checked ? 'checked' : '';
?>>
Expand Down
Loading