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

Conversation

sherwinski
Copy link

@sherwinski sherwinski commented Jan 15, 2025

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 Reviewable

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
Copy link
Author

@sherwinski sherwinski left a 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(() => {
Copy link
Author

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');
Copy link
Author

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.

@marclucraft
Copy link
Contributor

marclucraft commented Jan 15, 2025

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; if this is a post update, don't do anything

Could also add another option in the Settings screen "Automatically send notifications on Post Update", so the above would become: if this is a post update, don't do anything – unless "Automatically send notifications on Post Update" is checked

@rgomezp
Copy link
Contributor

rgomezp commented Jan 15, 2025

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.

@rgomezp rgomezp requested review from jkasten2 and removed request for rgomezp January 15, 2025 18:45
…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.
@rgomezp rgomezp force-pushed the uncheck-after-publish branch from b9585fc to 76d2a1a Compare January 15, 2025 18:46
Copy link
Author

@sherwinski sherwinski left a 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

Comment on lines 44 to 64
// 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

@sherwinski
Copy link
Author

sherwinski commented Jan 16, 2025

@rgomezp Hmm now there are different issues with the behavior:

  • A notification isn't sent the first time a post is published
    • My best guess as to why is that it could be related to this
  • A notification is sent when the post is updated but the checkbox UI doesn't update unless the page is refreshed
    • May just need a call to updateUI() when a post is updated, assuming that flow is any different than publishing for the first time
Screen.Recording.2025-01-16.at.11.33.41.AM.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants