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

Fix: Store post meta changes are overridden when custom fields meta box is enabled #64505

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/reference-guides/data/data-core-edit-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ _Returns_

Update a metabox.

_Parameters_

- _originalPost_ `Object[]`: Values of the original post without modifications.

### setAvailableMetaBoxesPerLocation

Stores info about which Meta boxes are available in which location.
Expand Down
39 changes: 36 additions & 3 deletions packages/edit-post/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,11 @@ export function setAvailableMetaBoxesPerLocation( metaBoxesPerLocation ) {

/**
* Update a metabox.
*
* @param {Object[]} originalPost Values of the original post without modifications.
*/
export const requestMetaBoxUpdates =
() =>
( originalPost ) =>
async ( { registry, select, dispatch } ) => {
dispatch( {
type: 'REQUEST_META_BOX_UPDATES',
Expand Down Expand Up @@ -335,6 +337,32 @@ export const requestMetaBoxUpdates =
formData.append( key, value )
);

// Get the list of custom fields that have been modified in the store.
const modifiedFields = [];
Object.entries( post.meta ).forEach( ( [ key, value ] ) => {
if ( value !== originalPost?.meta[ key ] ) {
modifiedFields.push( key );
}
} );

/*
* Override the fields included in metaboxes with the values modified in the store.
*
* Meta fields used in meta box include two properties in the request form:
*
* meta[1234][key]: my_custom_field
* meta[1234][value]: Custom field value
*/
for ( const [ formKey, metaFieldKey ] of formData.entries() ) {
if ( modifiedFields.includes( metaFieldKey ) ) {
formData.set(
// Override the value, not the key.
formKey.replace( '[key]', '[value]' ),
post.meta[ metaFieldKey ]
);
}
}

try {
// Save the metaboxes.
await apiFetch( {
Expand Down Expand Up @@ -470,14 +498,19 @@ export const initializeMetaBoxes =
if ( metaBoxesInitialized ) {
return;
}
const postType = registry.select( editorStore ).getCurrentPostType();
const { id: postId, type: postType } = registry
.select( editorStore )
.getCurrentPost();
if ( window.postboxes.page !== postType ) {
window.postboxes.add_postbox_toggles( postType );
}

metaBoxesInitialized = true;

// Save metaboxes on save completion, except for autosaves.
const originalPost = registry
.select( coreStore )
.getEntityRecord( 'postType', postType, postId );
addFilter(
'editor.__unstableSavePost',
'core/edit-post/save-metaboxes',
Expand All @@ -491,7 +524,7 @@ export const initializeMetaBoxes =
return;
}

return dispatch.requestMetaBoxUpdates();
return dispatch.requestMetaBoxUpdates( originalPost );
} )
);

Expand Down
Loading