-
Notifications
You must be signed in to change notification settings - Fork 43
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
base: main
Are you sure you want to change the base?
Conversation
Current implementation will only show the checkbox as unchecked after the page is refreshed.
…h button is clicked Potential concerns with this approach: - A wait is needed, otherwise the notification won't send. This may because it takes the plugin time to structure the request and immediately unchecking it will mark it as "don't send". Generally, functionality should not be dependent on waiting - The publish is targeted from the document body (`.editor-post-publish-button__button`) but this is brittle since we don't control the class values on the button
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Open questions/concerns:
|
||
if (publishButton) { | ||
publishButton.addEventListener('click', function() { | ||
setTimeout(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A wait is needed here, otherwise the notification won't send. This may because it takes the plugin time to structure the request and immediately unchecking it will mark it as "don't send". Generally I would prefer that functionality not be dependent on waiting. Happy to hear any thoughts or feedback on this.
|
||
// Watch for the Publish button to be added to the DOM | ||
const observer = new MutationObserver((mutations, obs) => { | ||
const publishButton = document.querySelector('.editor-post-publish-button__button'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The publish button is targeted from the document body (.editor-post-publish-button__button
) but this might be too brittle since we don't control the class values on that button.
We can scrap the entire JS side changes if we'd rather the checkbox just be unchecked once the page is refreshed, which matches the current behavior in v2.
This might be better solved using PHP (in onesignal-notification.php) rather than JS client-side (since posts might be created automatically / remotely) Something along the lines of; Could also add another option in the Settings screen "Automatically send notifications on Post Update", so the above would become: |
It's fragile to rely on a 3rd party button class name to listen to. Especially since this can potentially change between WP versions. Instead, we can use the 'core/editor' API on |
…atus changes in JS Motivation: it's fragile to rely on a 3rd party button class name to listen to. Especially since this can potentially change between WP versions. Instead, we can use the 'core/editor' API on `wp.data` to listen to changes in the post status and uncheck the checkbox that way.
b9585fc
to
76d2a1a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One small thing that I noticed is that even though the checkbox does get unchecked, the options field underneath doesn't get hidden as it would when you manually click on the checkbox.
Screen.Recording.2025-01-15.at.5.32.06.PM.mov
// 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; | ||
} | ||
} |
There was a problem hiding this comment.
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
// 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; | |
} | |
} |
There was a problem hiding this comment.
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
…post-status changes in JS
@rgomezp Hmm now there are different issues with the behavior:
Screen.Recording.2025-01-16.at.11.33.41.AM.mov |
Description
After a post is published, uncheck the "Send notification when post is published or updated" checkbox.
Demo
v2 Demo
uncheck.send.notif.checkbox.mov
v3 Demo Gutenberg
uncheck.send.notif.checkbox.v3.gutenberg.mov
v3 Demo Classic
uncheck.send.notif.checkbox.v3.classic.mov
This change is