@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
134 lines (128 loc) • 4.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _coreData = require("@wordpress/core-data");
var _store = require("../store");
var _lockUnlock = require("../lock-unlock");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
function getMetadata(registry, context, registeredFields) {
let metaFields = {};
const type = registry.select(_store.store).getCurrentPostType();
const {
getEditedEntityRecord
} = registry.select(_coreData.store);
if (context?.postType && context?.postId) {
metaFields = 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;
}
});
}
return metaFields;
}
var _default = exports.default = {
name: 'core/post-meta',
getValues({
registry,
context,
bindings
}) {
const {
getRegisteredPostMeta
} = (0, _lockUnlock.unlock)(registry.select(_coreData.store));
const registeredFields = getRegisteredPostMeta(context?.postType);
const metaFields = getMetadata(registry, context, registeredFields);
const newValues = {};
for (const [attributeName, source] of Object.entries(bindings)) {
var _ref, _metaFields$metaKey;
// Use the value, the field label, or the field key.
const metaKey = source.args.key;
newValues[attributeName] = (_ref = (_metaFields$metaKey = metaFields?.[metaKey]) !== null && _metaFields$metaKey !== void 0 ? _metaFields$metaKey : registeredFields?.[metaKey]?.title) !== null && _ref !== void 0 ? _ref : metaKey;
}
return newValues;
},
setValues({
registry,
context,
bindings
}) {
const newMeta = {};
Object.values(bindings).forEach(({
args,
newValue
}) => {
newMeta[args.key] = newValue;
});
registry.dispatch(_coreData.store).editEntityRecord('postType', context?.postType, context?.postId, {
meta: newMeta
});
},
canUserEditValue({
select,
context,
args
}) {
// Lock editing in query loop.
if (context?.query || context?.queryId) {
return false;
}
const postType = context?.postType || select(_store.store).getCurrentPostType();
// Check that editing is happening in the post editor and not a template.
if (postType === 'wp_template') {
return false;
}
// Check that the custom field is not protected and available in the REST API.
// Empty string or `false` could be a valid value, so we need to check if the field value is undefined.
const fieldValue = select(_coreData.store).getEntityRecord('postType', postType, context?.postId)?.meta?.[args.key];
if (fieldValue === undefined) {
return false;
}
// Check that custom fields metabox is not enabled.
const areCustomFieldsEnabled = select(_store.store).getEditorSettings().enableCustomFields;
if (areCustomFieldsEnabled) {
return false;
}
// Check that the user has the capability to edit post meta.
const canUserEdit = select(_coreData.store).canUser('update', {
kind: 'postType',
name: context?.postType,
id: context?.postId
});
if (!canUserEdit) {
return false;
}
return true;
},
getFieldsList({
registry,
context
}) {
const {
getRegisteredPostMeta
} = (0, _lockUnlock.unlock)(registry.select(_coreData.store));
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
}]));
}
};
//# sourceMappingURL=post-meta.js.map