Skip to content

Commit

Permalink
Update post meta source
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Dec 12, 2024
1 parent d7a7a08 commit 8ec4bcf
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { store as coreDataStore } from '@wordpress/core-data';
import { createSelector } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -34,43 +35,57 @@ import { unlock } from '../lock-unlock';
* }
* ```
*/
function getPostMetaFields( select, context ) {
const { getEditedEntityRecord } = select( coreDataStore );
const { getRegisteredPostMeta } = unlock( select( coreDataStore ) );
const getPostMetaFields = createSelector(
( select, context ) => {
const { getEditedEntityRecord } = select( coreDataStore );
const { getRegisteredPostMeta } = unlock( select( coreDataStore ) );

let entityMetaValues;
// Try to get the current entity meta values.
if ( context?.postType && context?.postId ) {
entityMetaValues = getEditedEntityRecord(
'postType',
context?.postType,
context?.postId
).meta;
}

const registeredFields = getRegisteredPostMeta( context?.postType );
const metaFields = {};
Object.entries( registeredFields || {} ).forEach( ( [ key, props ] ) => {
// Don't include footnotes or private fields.
if ( key !== 'footnotes' && key.charAt( 0 ) !== '_' ) {
metaFields[ key ] = {
label: props.title || key,
value:
// When using the entity value, an empty string IS a valid value.
entityMetaValues?.[ key ] ??
// When using the default, an empty string IS NOT a valid value.
( props.default || undefined ),
type: props.type,
};
let entityMetaValues;
// Try to get the current entity meta values.
if ( context?.postType && context?.postId ) {
entityMetaValues = getEditedEntityRecord(
'postType',
context?.postType,
context?.postId
).meta;
}
} );

if ( ! Object.keys( metaFields || {} ).length ) {
return null;
}
const registeredFields = getRegisteredPostMeta( context?.postType );
const metaFields = {};
Object.entries( registeredFields || {} ).forEach(
( [ key, props ] ) => {
// Don't include footnotes or private fields.
if ( key !== 'footnotes' && key.charAt( 0 ) !== '_' ) {
metaFields[ key ] = {
label: props.title || key,
value:
// When using the entity value, an empty string IS a valid value.
entityMetaValues?.[ key ] ??
// When using the default, an empty string IS NOT a valid value.
( props.default || undefined ),
type: props.type,
};
}
}
);

return metaFields;
}
if ( ! Object.keys( metaFields || {} ).length ) {
return null;
}

return metaFields;
},
( select, context ) => [
select( coreDataStore ).getEditedEntityRecord(
'postType',
context.postType,
context.postId
).meta,
unlock( select( coreDataStore ) ).getRegisteredPostMeta(
context.postType
),
]
);

export default {
name: 'core/post-meta',
Expand Down

0 comments on commit 8ec4bcf

Please sign in to comment.