@@ -35,58 +35,61 @@ import { unlock } from '../lock-unlock';
3535 * ```
3636 */
3737function getPostMetaFields ( select , context ) {
38- const { getEditedEntityRecord } = select ( coreDataStore ) ;
3938 const { getRegisteredPostMeta } = unlock ( select ( coreDataStore ) ) ;
4039
41- let entityMetaValues ;
42- // Try to get the current entity meta values.
43- if ( context ?. postType && context ?. postId ) {
44- entityMetaValues = getEditedEntityRecord (
45- 'postType' ,
46- context ?. postType ,
47- context ?. postId
48- ) . meta ;
49- }
50-
5140 const registeredFields = getRegisteredPostMeta ( context ?. postType ) ;
52- const metaFields = { } ;
53- Object . entries ( registeredFields || { } ) . forEach ( ( [ key , props ] ) => {
41+ const metaFields = [ ] ;
42+ Object . entries ( registeredFields ) . forEach ( ( [ key , props ] ) => {
5443 // Don't include footnotes or private fields.
55- if ( key !== 'footnotes' && key . charAt ( 0 ) !== '_' ) {
56- metaFields [ key ] = {
57- label : props . title || key ,
58- value :
59- // When using the entity value, an empty string IS a valid value.
60- entityMetaValues ?. [ key ] ??
61- // When using the default, an empty string IS NOT a valid value.
62- ( props . default || undefined ) ,
63- type : props . type ,
64- } ;
44+ if ( key === 'footnotes' || key . charAt ( 0 ) === '_' ) {
45+ return ;
6546 }
66- } ) ;
6747
68- if ( ! Object . keys ( metaFields || { } ) . length ) {
69- return null ;
70- }
48+ metaFields . push ( {
49+ label : props . title || key ,
50+ args : { key } ,
51+ type : props . type ,
52+ } ) ;
53+ } ) ;
7154
7255 return metaFields ;
7356}
7457
58+ function getValue ( { select, context, args } ) {
59+ const { getEditedEntityRecord } = select ( coreDataStore ) ;
60+ const entityMetaValues = getEditedEntityRecord (
61+ 'postType' ,
62+ context ?. postType ,
63+ context ?. postId
64+ ) . meta ;
65+
66+ const metaFields = getPostMetaFields ( select , context ) ;
67+ const metaField = metaFields . find ( ( field ) => field . args . key === args . key ) ;
68+
69+ return (
70+ entityMetaValues ?. [ args . key ] ??
71+ metaField ?. label ??
72+ args . key
73+ ) ;
74+ }
75+
7576/**
7677 * @type {WPBlockBindingsSource }
7778 */
7879export default {
7980 name : 'core/post-meta' ,
8081 getValues ( { select, context, bindings } ) {
81- const metaFields = getPostMetaFields ( select , context ) ;
82+ if ( ! context ?. postType || ! context ?. postId ) {
83+ return { } ;
84+ }
8285
8386 const newValues = { } ;
84- for ( const [ attributeName , source ] of Object . entries ( bindings ) ) {
85- // Use the value, the field label, or the field key.
86- const fieldKey = source . args . key ;
87- const { value : fieldValue , label : fieldLabel } =
88- metaFields ?. [ fieldKey ] || { } ;
89- newValues [ attributeName ] = fieldValue ?? fieldLabel ?? fieldKey ;
87+ for ( const [ attributeName , binding ] of Object . entries ( bindings ) ) {
88+ newValues [ attributeName ] = getValue ( {
89+ select ,
90+ context ,
91+ args : binding . args ,
92+ } ) ;
9093 }
9194 return newValues ;
9295 } ,
@@ -116,10 +119,8 @@ export default {
116119 return false ;
117120 }
118121
119- const fieldValue = getPostMetaFields ( select , context ) ?. [ args . key ]
120- ?. value ;
121122 // Empty string or `false` could be a valid value, so we need to check if the field value is undefined.
122- if ( fieldValue === undefined ) {
123+ if ( getValue ( { select , context , args } ) === undefined ) {
123124 return false ;
124125 }
125126 // Check that custom fields metabox is not enabled.
@@ -142,14 +143,6 @@ export default {
142143 return true ;
143144 } ,
144145 getFieldsList ( { select, context } ) {
145- const metaFields = getPostMetaFields ( select , context ) ;
146- if ( ! metaFields ) {
147- return [ ] ;
148- }
149- return Object . entries ( metaFields ) . map ( ( [ key , field ] ) => ( {
150- label : field . label ,
151- type : field . type ,
152- args : { key } ,
153- } ) ) ;
146+ return getPostMetaFields ( select , context ) ;
154147 } ,
155148} ;
0 commit comments