Skip to content

Commit

Permalink
Stabilize the PreSavePost and SavePost filters (WordPress#64198)
Browse files Browse the repository at this point in the history
* Stabilize the PreSavePost and SavePost filters

* Use lowercase names

* Use async hooks
  • Loading branch information
jsnajdr committed Sep 27, 2024
1 parent de01da2 commit 3e0d294
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
23 changes: 8 additions & 15 deletions packages/edit-post/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import deprecated from '@wordpress/deprecated';
import { addFilter } from '@wordpress/hooks';
import { addAction } from '@wordpress/hooks';
import { store as coreStore } from '@wordpress/core-data';

/**
Expand Down Expand Up @@ -478,21 +478,14 @@ export const initializeMetaBoxes =
metaBoxesInitialized = true;

// Save metaboxes on save completion, except for autosaves.
addFilter(
'editor.__unstableSavePost',
addAction(
'editor.savePost',
'core/edit-post/save-metaboxes',
( previous, options ) =>
previous.then( () => {
if ( options.isAutosave ) {
return;
}

if ( ! select.hasMetaBoxes() ) {
return;
}

return dispatch.requestMetaBoxUpdates();
} )
async ( options ) => {
if ( ! options.isAutosave && select.hasMetaBoxes() ) {
await dispatch.requestMetaBoxUpdates();
}
}
);

dispatch( {
Expand Down
37 changes: 26 additions & 11 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
import { store as noticesStore } from '@wordpress/notices';
import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { applyFilters } from '@wordpress/hooks';
import {
applyFilters,
applyFiltersAsync,
doActionAsync,
} from '@wordpress/hooks';
import { store as preferencesStore } from '@wordpress/preferences';
import { __ } from '@wordpress/i18n';

Expand Down Expand Up @@ -184,7 +188,7 @@ export const savePost =
}

const previousRecord = select.getCurrentPost();
const edits = {
let edits = {
id: previousRecord.id,
...registry
.select( coreStore )
Expand All @@ -199,9 +203,9 @@ export const savePost =

let error = false;
try {
error = await applyFilters(
'editor.__unstablePreSavePost',
Promise.resolve( false ),
edits = await applyFiltersAsync(
'editor.preSavePost',
edits,
options
);
} catch ( err ) {
Expand Down Expand Up @@ -236,14 +240,25 @@ export const savePost =
);
}

// Run the hook with legacy unstable name for backward compatibility
if ( ! error ) {
await applyFilters(
'editor.__unstableSavePost',
Promise.resolve(),
options
).catch( ( err ) => {
try {
await applyFilters(
'editor.__unstableSavePost',
Promise.resolve(),
options
);
} catch ( err ) {
error = err;
} );
}
}

if ( ! error ) {
try {
await doActionAsync( 'editor.savePost', options );
} catch ( err ) {
error = err;
}
}
dispatch( { type: 'REQUEST_POST_UPDATE_FINISH', options } );

Expand Down

0 comments on commit 3e0d294

Please sign in to comment.