From 3c7d4d6aff43d1da924bdc6c501dc4389b6e9d9b Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 2 Aug 2024 03:34:14 -0600 Subject: [PATCH] Add an async `__unstablePreSavePost` hook; resolving with false prevents saving (#58022) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add an async `__unstablePreSavePost` hook; resolving with false prevents saving * Simplify preSave check * Filter edits & enable throwing Error in `__unstablePreSavePost` filter * Pass Promise.resolve( edits ) instead of edits * Move REQUEST_POST_UPDATE_START to before preSave async filter * Update saveEntityRecord error handling to use try/catch * Use explicit false check for error * reduce changes, test functionality, don’t pass data to filter * whitespace Unlinked contributors: aduth, lex127, audiovisuel-uqam, eballeste, jenilk, zhitaryksergey, christianMiguez. Co-authored-by: adamsilverstein Co-authored-by: jsnajdr Co-authored-by: ockham Co-authored-by: sadmansh Co-authored-by: sc0ttkclark Co-authored-by: youknowriad Co-authored-by: gziolo Co-authored-by: acafourek Co-authored-by: marcwieland95 Co-authored-by: margolisj Co-authored-by: noisysocks --- packages/editor/src/store/actions.js | 49 ++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index d934436800cbaf..59faa6b5b73624 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -196,22 +196,45 @@ export const savePost = content, }; dispatch( { type: 'REQUEST_POST_UPDATE_START', options } ); - await registry - .dispatch( coreStore ) - .saveEntityRecord( - 'postType', - previousRecord.type, - edits, + + let error = false; + try { + error = await applyFilters( + 'editor.__unstablePreSavePost', + Promise.resolve( false ), options ); + } catch ( err ) { + error = err; + } - let error = registry - .select( coreStore ) - .getLastEntitySaveError( - 'postType', - previousRecord.type, - previousRecord.id - ); + if ( ! error ) { + try { + await registry + .dispatch( coreStore ) + .saveEntityRecord( + 'postType', + previousRecord.type, + edits, + options + ); + } catch ( err ) { + error = + err.message && err.code !== 'unknown_error' + ? err.message + : __( 'An error occurred while updating.' ); + } + } + + if ( ! error ) { + error = registry + .select( coreStore ) + .getLastEntitySaveError( + 'postType', + previousRecord.type, + previousRecord.id + ); + } if ( ! error ) { await applyFilters(