block-editor.js
6 months ago
constants.js
6 months ago
field-processing.js
6 months ago
fieldMetadataCache.js
6 months ago
hooks.js
6 months ago
index.js
11 months ago
sources.js
6 months ago
utils.js
6 months ago
sources.js
92 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { registerBlockBindingsSource } from '@wordpress/blocks'; |
| 5 | import { store as coreDataStore } from '@wordpress/core-data'; |
| 6 | import { store as editorStore } from '@wordpress/editor'; |
| 7 | import { __ } from '@wordpress/i18n'; |
| 8 | |
| 9 | /** |
| 10 | * Internal dependencies |
| 11 | */ |
| 12 | import { |
| 13 | getSCFFields, |
| 14 | processFieldBinding, |
| 15 | formatFieldLabel, |
| 16 | } from './field-processing'; |
| 17 | import { getFieldMetadata } from './fieldMetadataCache'; |
| 18 | |
| 19 | /** |
| 20 | * Register the SCF field binding source. |
| 21 | */ |
| 22 | registerBlockBindingsSource( { |
| 23 | name: 'acf/field', |
| 24 | label: __( 'SCF Fields', 'secure-custom-fields' ), |
| 25 | getLabel( { args, select } ) { |
| 26 | const fieldKey = args?.key; |
| 27 | |
| 28 | if ( ! fieldKey ) { |
| 29 | return __( 'SCF Fields', 'secure-custom-fields' ); |
| 30 | } |
| 31 | |
| 32 | const fieldMetadata = getFieldMetadata( fieldKey ); |
| 33 | |
| 34 | if ( fieldMetadata?.label ) { |
| 35 | return fieldMetadata.label; |
| 36 | } |
| 37 | |
| 38 | return formatFieldLabel( fieldKey ); |
| 39 | }, |
| 40 | getValues( { context, bindings, select } ) { |
| 41 | const { getCurrentPostType } = select( editorStore ); |
| 42 | const currentPostType = getCurrentPostType(); |
| 43 | const isSiteEditor = currentPostType === 'wp_template'; |
| 44 | |
| 45 | // In site editor, return field labels as placeholder values |
| 46 | if ( isSiteEditor ) { |
| 47 | const result = {}; |
| 48 | Object.entries( bindings ).forEach( |
| 49 | ( [ attribute, { args } = {} ] ) => { |
| 50 | const fieldKey = args?.key; |
| 51 | if ( ! fieldKey ) { |
| 52 | result[ attribute ] = ''; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | const fieldMetadata = getFieldMetadata( fieldKey ); |
| 57 | result[ attribute ] = |
| 58 | fieldMetadata?.label || formatFieldLabel( fieldKey ); |
| 59 | } |
| 60 | ); |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | // Regular post editor - get actual field values |
| 65 | const { getEditedEntityRecord } = select( coreDataStore ); |
| 66 | |
| 67 | const post = |
| 68 | context?.postType && context?.postId |
| 69 | ? getEditedEntityRecord( |
| 70 | 'postType', |
| 71 | context.postType, |
| 72 | context.postId |
| 73 | ) |
| 74 | : undefined; |
| 75 | |
| 76 | const scfFields = getSCFFields( post ); |
| 77 | const result = {}; |
| 78 | |
| 79 | Object.entries( bindings ).forEach( |
| 80 | ( [ attribute, { args } = {} ] ) => { |
| 81 | const value = processFieldBinding( attribute, args, scfFields ); |
| 82 | result[ attribute ] = value; |
| 83 | } |
| 84 | ); |
| 85 | |
| 86 | return result; |
| 87 | }, |
| 88 | canUserEditValue() { |
| 89 | return false; |
| 90 | }, |
| 91 | } ); |
| 92 |