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 2 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
21 changes: 21 additions & 0 deletions v3/onesignal-metabox/onesignal-metabox.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,25 @@ window.addEventListener("DOMContentLoaded", () => {
setDisplay(customiseWrap, customisePost.checked);
setDisabled(customiseWrapChild, !customisePost.checked);
});

// 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.


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.

if (sendPost && sendPost.checked) {
sendPost.click();
}
}, 1000);
});
obs.disconnect(); // Stop observing once we've found and handled the button
}
});

observer.observe(document.body, {
childList: true,
subtree: true
});
});
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