Skip to content

Instantly share code, notes, and snippets.

@sethrubenstein
Created November 6, 2024 17:23
Show Gist options
  • Save sethrubenstein/297e3293d19c7fedb520a2aa40417af7 to your computer and use it in GitHub Desktop.
Save sethrubenstein/297e3293d19c7fedb520a2aa40417af7 to your computer and use it in GitHub Desktop.
Gutenberg Post Publish Pipeline

Post Publish Pipeline

Client Side Hooks

All client side hooks received {edits, postId, postStatus, postType} properties:

  • prc-platform.onSiteEdit Occurs every time an update is saved in the site editor.
  • prc-platform.onPostInit Occurs once, when a post transition from non existent to draft state.
  • prc-platform.onIncrementalSave Occurs often, whenever a post in a draft state is updated.
  • prc-platform.onPublish Occurs when a post transitions from draft to publish state.
  • prc-platform.onUpdate Occurs when a post is either in draft or publish state and is updated.

Client Side Example

import { addAction } from '@wordpress/hooks';

addAction('prc-platform.onPublish', 'my-plugin', ({postId, postStatus, postType}) => {
  console.log(`A post of type ${postType} with ID ${postId} was published with status ${postStatus}`);
});
/**
* WordPress Dependencies
*/
import { addFilter, doAction } from '@wordpress/hooks';
import { select, dispatch, register, createReduxStore } from '@wordpress/data';
const NAMESPACE = 'prc-platform/post-publish-pipeline';
const DEFAULT_STATE = {
postStatus: null,
};
const actions = {
setPostStatus( postStatus ) {
return {
type: 'SET_POST_STATUS',
postStatus,
};
},
};
const reducer = ( state = DEFAULT_STATE, action ) => {
switch ( action.type ) {
case 'SET_POST_STATUS':
return {
...state,
postStatus: action.postStatus,
};
default:
return state;
}
};
const selectors = {
getPostStatus( state ) {
return state.postStatus;
},
};
const store = createReduxStore( NAMESPACE, {
reducer,
actions,
selectors,
} );
register( store );
addFilter('editor.preSavePost', 'editor', (edits) => {
const priorStatus = select(NAMESPACE).getPostStatus();
const postStatus = select( 'core/editor' ).getEditedPostAttribute( 'status' );
const postId = select( 'core/editor' ).getCurrentPostId();
const postType = select( 'core/editor' ).getCurrentPostType();
const isSiteEditor = select( 'core/edit-site' )?.getEditorMode() || false;
console.log('prc-platform.onPreSavePost', {postStatus, priorStatus, postId});
if ( isSiteEditor ) {
doAction('prc-platform.onSiteEdit', {edits, postId, postType, postStatus});
return edits;
}
if ( null === priorStatus && 'auto-draft' === postStatus ) {
doAction('prc-platform.onPostInit', {edits, postId, postType, isSiteEditor});
}
if ( ('draft' === priorStatus || null === priorStatus) && 'draft' === postStatus ) {
doAction('prc-platform.onIncrementalSave', {edits, postId, postType, isSiteEditor});
}
if ( 'draft' === priorStatus && 'publish' === postStatus ) {
doAction('prc-platform.onPublish', {edits, postId, postType, isSiteEditor});
}
if ( ('publish' === priorStatus || null === priorStatus) && 'publish' === postStatus ) {
doAction('prc-platform.onUpdate', {edits, postId, postType, isSiteEditor});
}
dispatch(NAMESPACE).setPostStatus(postStatus);
return edits;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment