Created
March 17, 2019 13:30
-
-
Save davidfcarr/a26bf62b16e845b74b6f7e1b4ad4a3e7 to your computer and use it in GitHub Desktop.
Custom notification in WordPress Gutenberg
This file contains hidden or 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
// context check: if this is an rsvpmaker template, this will have be available as localized data from PHP | |
if((typeof rsvpmaker_json !== 'undefined' ) && rsvpmaker_json.projected_url) { | |
let wasSavingPost = wp.data.select( 'core/editor' ).isSavingPost(); | |
let wasAutosavingPost = wp.data.select( 'core/editor' ).isAutosavingPost(); | |
let wasPreviewingPost = wp.data.select( 'core/editor' ).isPreviewingPost(); | |
// determine whether to show notice | |
subscribe( () => { | |
const isSavingPost = wp.data.select( 'core/editor' ).isSavingPost(); | |
const isAutosavingPost = wp.data.select( 'core/editor' ).isAutosavingPost(); | |
const isPreviewingPost = wp.data.select( 'core/editor' ).isPreviewingPost(); | |
const hasActiveMetaBoxes = wp.data.select( 'core/edit-post' ).hasMetaBoxes(); | |
// Save metaboxes on save completion, except for autosaves that are not a post preview. | |
const shouldTriggerTemplateNotice = ( | |
( wasSavingPost && ! isSavingPost && ! wasAutosavingPost ) || | |
( wasAutosavingPost && wasPreviewingPost && ! isPreviewingPost ) | |
); | |
// Save current state for next inspection. | |
wasSavingPost = isSavingPost; | |
wasAutosavingPost = isAutosavingPost; | |
wasPreviewingPost = isPreviewingPost; | |
if ( shouldTriggerTemplateNotice ) { | |
wp.data.dispatch('core/notices').createNotice( | |
'info', // Can be one of: success, info, warning, error. | |
__('After updating this template, click'), // Text string to display. | |
{ | |
id: 'rsvptemplateupdate', //assigning an ID prevents the notice from being added repeatedly | |
isDismissible: true, // Whether the user can dismiss the notice. | |
// Any actions the user can perform. | |
actions: [ | |
{ | |
url: rsvpmaker_json.projected_url, | |
label: __('create / update events') | |
} | |
] | |
} | |
); | |
} | |
/* placeholder for logic to remove notice | |
else { | |
console.log('remove notice'); | |
} | |
*/ | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment