From 95ac0635e0db3db77ae379da4ed2b05d1a06f301 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Fri, 2 Aug 2024 12:28:50 +0200 Subject: [PATCH 1/3] Stabilize the PreSavePost and SavePost filters --- packages/editor/src/store/actions.js | 35 ++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 4b519a9d8f6cc3..20c43f67d67a73 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -184,7 +184,7 @@ export const savePost = } const previousRecord = select.getCurrentPost(); - const edits = { + let edits = { id: previousRecord.id, ...registry .select( coreStore ) @@ -199,9 +199,9 @@ export const savePost = let error = false; try { - error = await applyFilters( - 'editor.__unstablePreSavePost', - Promise.resolve( false ), + edits = await applyFilters( + 'editor.PreSavePost', + Promise.resolve( edits ), options ); } catch ( err ) { @@ -236,14 +236,29 @@ 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 applyFilters( + 'editor.SavePost', + Promise.resolve(), + options + ); + } catch ( err ) { + error = err; + } } dispatch( { type: 'REQUEST_POST_UPDATE_FINISH', options } ); From 8adfe3ecbfbed38e753336fdc72008636751dddb Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Mon, 5 Aug 2024 13:21:18 +0200 Subject: [PATCH 2/3] Use lowercase names --- packages/editor/src/store/actions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 20c43f67d67a73..0851c977b7a44d 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -200,7 +200,7 @@ export const savePost = let error = false; try { edits = await applyFilters( - 'editor.PreSavePost', + 'editor.preSavePost', Promise.resolve( edits ), options ); @@ -252,7 +252,7 @@ export const savePost = if ( ! error ) { try { await applyFilters( - 'editor.SavePost', + 'editor.savePost', Promise.resolve(), options ); From f6c7cf538b1f9a510bedab2edddb4830296415f2 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Fri, 27 Sep 2024 10:44:24 +0200 Subject: [PATCH 3/3] Use async hooks --- packages/edit-post/src/store/actions.js | 23 ++++++++--------------- packages/editor/src/store/actions.js | 16 ++++++++-------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/packages/edit-post/src/store/actions.js b/packages/edit-post/src/store/actions.js index d00f7472382f80..1dc0401baf21c3 100644 --- a/packages/edit-post/src/store/actions.js +++ b/packages/edit-post/src/store/actions.js @@ -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'; /** @@ -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( { diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 0851c977b7a44d..fa720e1fc7d347 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -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'; @@ -199,9 +203,9 @@ export const savePost = let error = false; try { - edits = await applyFilters( + edits = await applyFiltersAsync( 'editor.preSavePost', - Promise.resolve( edits ), + edits, options ); } catch ( err ) { @@ -251,11 +255,7 @@ export const savePost = if ( ! error ) { try { - await applyFilters( - 'editor.savePost', - Promise.resolve(), - options - ); + await doActionAsync( 'editor.savePost', options ); } catch ( err ) { error = err; }