From a7c4bdf72da1421af1714e848974533e8087cdfd Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Wed, 25 Sep 2024 10:39:43 +0200 Subject: [PATCH 1/4] Move logic to `getPostMetaFields` function --- packages/editor/src/bindings/post-meta.js | 107 ++++++++++++---------- 1 file changed, 58 insertions(+), 49 deletions(-) diff --git a/packages/editor/src/bindings/post-meta.js b/packages/editor/src/bindings/post-meta.js index 572cd0b525a003..c353a119a57b57 100644 --- a/packages/editor/src/bindings/post-meta.js +++ b/packages/editor/src/bindings/post-meta.js @@ -9,26 +9,65 @@ import { store as coreDataStore } from '@wordpress/core-data'; import { store as editorStore } from '../store'; import { unlock } from '../lock-unlock'; -function getMetadata( registry, context, registeredFields ) { - let metaFields = {}; - const type = registry.select( editorStore ).getCurrentPostType(); +/** + * Get a list of post meta fields with their values and labels + * to be consumed in the needed callbacks. + * If the value is not available based on context, like in templates, + * it falls back to the default value, label, or key. + * + * @param {Object} registry The registry context exposed through `useRegistry`. + * @param {Object} context The context provided. + * @return {Object} List of post meta fields with their value and label. + * + * @example + * ```js + * { + * field_1_key: { + * label: 'Field 1 Label', + * value: 'Field 1 Value', + * }, + * field_2_key: { + * label: 'Field 2 Label', + * value: 'Field 2 Value', + * }, + * ... + * } + * ``` + */ +function getPostMetaFields( registry, context ) { const { getEditedEntityRecord } = registry.select( coreDataStore ); + const { getRegisteredPostMeta } = unlock( + registry.select( coreDataStore ) + ); + let entityMetaValues; + // Try to get the current entity meta values. if ( context?.postType && context?.postId ) { - metaFields = getEditedEntityRecord( + entityMetaValues = getEditedEntityRecord( 'postType', context?.postType, context?.postId ).meta; - } else if ( type === 'wp_template' ) { - // Populate the `metaFields` object with the default values. - Object.entries( registeredFields || {} ).forEach( - ( [ key, props ] ) => { - if ( props.default ) { - metaFields[ key ] = props.default; - } - } - ); + } + + 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: registeredFields?.[ key ]?.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 ), + }; + } + } ); + + if ( ! Object.keys( metaFields || {} ).length ) { + return null; } return metaFields; @@ -37,20 +76,15 @@ function getMetadata( registry, context, registeredFields ) { export default { name: 'core/post-meta', getValues( { registry, context, bindings } ) { - const { getRegisteredPostMeta } = unlock( - registry.select( coreDataStore ) - ); - const registeredFields = getRegisteredPostMeta( context?.postType ); - const metaFields = getMetadata( registry, context, registeredFields ); + const metaFields = getPostMetaFields( registry, context ); const newValues = {}; for ( const [ attributeName, source ] of Object.entries( bindings ) ) { // Use the value, the field label, or the field key. - const metaKey = source.args.key; - newValues[ attributeName ] = - metaFields?.[ metaKey ] ?? - registeredFields?.[ metaKey ]?.title ?? - metaKey; + const fieldKey = source.args.key; + const { value: fieldValue, label: fieldLabel } = + metaFields?.[ fieldKey ] || {}; + newValues[ attributeName ] = fieldValue ?? fieldLabel ?? fieldKey; } return newValues; }, @@ -110,31 +144,6 @@ export default { return true; }, getFieldsList( { registry, context } ) { - const { getRegisteredPostMeta } = unlock( - registry.select( coreDataStore ) - ); - const registeredFields = getRegisteredPostMeta( context?.postType ); - const metaFields = getMetadata( registry, context, registeredFields ); - - if ( ! metaFields || ! Object.keys( metaFields ).length ) { - return null; - } - - return Object.fromEntries( - Object.entries( metaFields ) - // Remove footnotes or private keys from the list of fields. - .filter( - ( [ key ] ) => - key !== 'footnotes' && key.charAt( 0 ) !== '_' - ) - // Return object with label and value. - .map( ( [ key, value ] ) => [ - key, - { - label: registeredFields?.[ key ]?.title || key, - value, - }, - ] ) - ); + return getPostMetaFields( registry, context ); }, }; From 44563f1d4f26f6f68c5b85a0708641218eb49ef0 Mon Sep 17 00:00:00 2001 From: Mario Santos <34552881+SantosGuillamot@users.noreply.github.com> Date: Wed, 25 Sep 2024 10:39:43 +0200 Subject: [PATCH 2/4] Update comment to use third-person MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Greg Ziółkowski --- packages/editor/src/bindings/post-meta.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/bindings/post-meta.js b/packages/editor/src/bindings/post-meta.js index c353a119a57b57..a6e7e916a89bcc 100644 --- a/packages/editor/src/bindings/post-meta.js +++ b/packages/editor/src/bindings/post-meta.js @@ -10,7 +10,7 @@ import { store as editorStore } from '../store'; import { unlock } from '../lock-unlock'; /** - * Get a list of post meta fields with their values and labels + * Gets a list of post meta fields with their values and labels * to be consumed in the needed callbacks. * If the value is not available based on context, like in templates, * it falls back to the default value, label, or key. From 0d5d6595009922ad92633b965667a72f43c15423 Mon Sep 17 00:00:00 2001 From: Mario Santos <34552881+SantosGuillamot@users.noreply.github.com> Date: Wed, 25 Sep 2024 10:39:44 +0200 Subject: [PATCH 3/4] Remove extra space from comment object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Greg Ziółkowski --- packages/editor/src/bindings/post-meta.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/editor/src/bindings/post-meta.js b/packages/editor/src/bindings/post-meta.js index a6e7e916a89bcc..6be4b8c77ddecb 100644 --- a/packages/editor/src/bindings/post-meta.js +++ b/packages/editor/src/bindings/post-meta.js @@ -24,11 +24,11 @@ import { unlock } from '../lock-unlock'; * { * field_1_key: { * label: 'Field 1 Label', - * value: 'Field 1 Value', + * value: 'Field 1 Value', * }, * field_2_key: { * label: 'Field 2 Label', - * value: 'Field 2 Value', + * value: 'Field 2 Value', * }, * ... * } From e4d1a70dbca4c30b4f9e2850b47adc207c562351 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Wed, 25 Sep 2024 11:18:36 +0200 Subject: [PATCH 4/4] Use props variable --- packages/editor/src/bindings/post-meta.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/bindings/post-meta.js b/packages/editor/src/bindings/post-meta.js index 6be4b8c77ddecb..fae010e72d1c8b 100644 --- a/packages/editor/src/bindings/post-meta.js +++ b/packages/editor/src/bindings/post-meta.js @@ -56,7 +56,7 @@ function getPostMetaFields( registry, context ) { // Don't include footnotes or private fields. if ( key !== 'footnotes' && key.charAt( 0 ) !== '_' ) { metaFields[ key ] = { - label: registeredFields?.[ key ]?.title || key, + label: props.title || key, value: // When using the entity value, an empty string IS a valid value. entityMetaValues?.[ key ] ??