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

Block Bindings: Unify logic in getPostMetaFields function #65462

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
107 changes: 58 additions & 49 deletions packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
},
Expand Down Expand Up @@ -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 );
},
};
Loading