-
-
Save laxmariappan/37e046ed35647b44f2a208244b786883 to your computer and use it in GitHub Desktop.
ajax call - checking notifications via REST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'rest_api_init', 'gnotice_rest_init' ); | |
add_action( 'admin_footer-post.php', 'email_notification_script' ); | |
add_action( 'admin_footer-post-new.php', 'email_notification_script' ); | |
/** | |
* Adds script to make ajax call - checking notifications via REST. | |
* | |
* @return void | |
* @since 1.0 | |
* @author Lax Mariappan <lax@webdevstudios.com> | |
*/ | |
function email_notification_script() { | |
// @see https://wordpress.stackexchange.com/a/398805/103640 | |
// @see https://github.com/WordPress/gutenberg/issues/17632#issuecomment-583772895 | |
?> | |
<script type="text/javascript"> | |
const { subscribe,select } = wp.data; | |
const { isSavingPost } = select( 'core/editor' ); | |
var checked = true; | |
subscribe( () => { | |
if ( isSavingPost() ) { | |
checked = false; | |
} else { | |
if ( ! checked ) { | |
checkNotificationAfterPublish(); | |
checked = true; | |
} | |
} | |
} ); | |
function checkNotificationAfterPublish(){ | |
const postId = wp.data.select("core/editor").getCurrentPostId(); | |
const url = wp.url.addQueryArgs( | |
'/wp-json/api-gnotice/v1/check-email-response', | |
{ id: postId }, | |
); | |
wp.apiFetch({ | |
url, | |
}).then( | |
function(response){ | |
if(response.message){ | |
wp.data.dispatch("core/notices").createNotice( | |
response.code, | |
response.message, | |
{ | |
id: 'email_status_notice', | |
isDismissible: true | |
} | |
); | |
} | |
} | |
); | |
}; | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment