-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
post-meta.js
73 lines (65 loc) · 1.66 KB
/
post-meta.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* WordPress dependencies
*/
import { store as coreDataStore } from '@wordpress/core-data';
import { _x } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { store as editorStore } from '../store';
export default {
name: 'core/post-meta',
label: _x( 'Post Meta', 'block bindings source' ),
getPlaceholder( { args } ) {
return args.key;
},
getValue( { registry, context, args } ) {
return registry
.select( coreDataStore )
.getEditedEntityRecord(
'postType',
context?.postType,
context?.postId
).meta?.[ args.key ];
},
setValue( { registry, context, args, value } ) {
registry
.dispatch( coreDataStore )
.editEntityRecord( 'postType', context?.postType, context?.postId, {
meta: {
[ args.key ]: value,
},
} );
},
canUserEditValue( { select, context, args } ) {
// Lock editing in query loop.
if ( context?.query || context?.queryId ) {
return false;
}
const postType =
context?.postType || select( editorStore ).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.
const isFieldExposed = !! select( coreDataStore ).getEntityRecord(
'postType',
postType,
context?.postId
)?.meta?.[ args.key ];
if ( ! isFieldExposed ) {
return false;
}
// Check that the user has the capability to edit post meta.
const canUserEdit = select( coreDataStore ).canUserEditEntityRecord(
'postType',
context?.postType,
context?.postId
);
if ( ! canUserEdit ) {
return false;
}
return true;
},
};