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

Add an async __unstablePreSavePost hook; resolving with false prevents saving #58022

Merged
merged 12 commits into from
Aug 2, 2024
Merged
43 changes: 29 additions & 14 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export const savePost =
}

const previousRecord = select.getCurrentPost();
const edits = {
let edits = {
id: previousRecord.id,
...registry
.select( coreStore )
Expand All @@ -195,23 +195,38 @@ export const savePost =
),
content,
};
dispatch( { type: 'REQUEST_POST_UPDATE_START', options } );
await registry
.dispatch( coreStore )
.saveEntityRecord(
'postType',
previousRecord.type,
let error;
try {
edits = await applyFilters(
'editor.__unstablePreSavePost',
edits,
options
);
} catch ( err ) {
error = err;
}

let error = registry
.select( coreStore )
.getLastEntitySaveError(
'postType',
previousRecord.type,
previousRecord.id
);
dispatch( { type: 'REQUEST_POST_UPDATE_START', options } );
if ( ! error ) {
error = await registry
.dispatch( coreStore )
.saveEntityRecord(
'postType',
previousRecord.type,
edits,
options
);
}

if ( ! error ) {
error = registry
.select( coreStore )
.getLastEntitySaveError(
'postType',
previousRecord.type,
previousRecord.id
);
}

if ( ! error ) {
await applyFilters(
Expand Down
Loading