blocks-v3
2 months ago
_acf-blocks-v3.js
3 months ago
_acf-blocks.js
3 months ago
_acf-field-flexible-content.js
4 months ago
_acf-field-gallery.js
3 months ago
_acf-field-repeater.js
4 months ago
_acf-jsx-names.js
1 year ago
_acf-setting-clone.js
1 year ago
_acf-setting-flexible-content.js
1 year ago
_acf-setting-repeater.js
1 year ago
_acf-ui-options-page.js
9 months ago
acf-datastore.js
3 months ago
acf-field-bindings.js
3 months ago
acf-pro-blocks.js
9 months ago
acf-pro-field-group.js
1 year ago
acf-pro-input.js
1 year ago
acf-pro-ui-options-page.js
1 year ago
acf-field-bindings.js
143 lines
| 1 | ( () => { |
| 2 | 'use strict'; |
| 3 | |
| 4 | const STORE_NAME = 'acf/fields'; |
| 5 | |
| 6 | const fieldSupportsBindings = ( field ) => { |
| 7 | return ( |
| 8 | false !== field.supportsBindings && false !== field.allowInBindings |
| 9 | ); |
| 10 | }; |
| 11 | |
| 12 | const isScalarValue = ( value ) => { |
| 13 | const valueType = typeof value; |
| 14 | return ( |
| 15 | 'string' === valueType || |
| 16 | 'number' === valueType || |
| 17 | 'boolean' === valueType |
| 18 | ); |
| 19 | }; |
| 20 | |
| 21 | const valueToBindingString = ( value ) => { |
| 22 | if ( null === value || undefined === value ) { |
| 23 | return null; |
| 24 | } |
| 25 | |
| 26 | if ( isScalarValue( value ) ) { |
| 27 | return String( value ); |
| 28 | } |
| 29 | |
| 30 | if ( Array.isArray( value ) && value.every( isScalarValue ) ) { |
| 31 | return value.join( ', ' ); |
| 32 | } |
| 33 | |
| 34 | return null; |
| 35 | }; |
| 36 | |
| 37 | if ( ! window.wp?.blocks?.registerBlockBindingsSource ) { |
| 38 | // eslint-disable-next-line no-console |
| 39 | console.warn( |
| 40 | 'SCF: wp.blocks.registerBlockBindingsSource is unavailable; SCF block bindings will not be registered. Requires WordPress 6.7+.' |
| 41 | ); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | wp.blocks.registerBlockBindingsSource( { |
| 46 | name: 'acf/field', |
| 47 | usesContext: [ 'postType', 'postId' ], |
| 48 | getValues( { select, bindings } ) { |
| 49 | const store = select( STORE_NAME ); |
| 50 | |
| 51 | if ( ! store.isInitialized() ) { |
| 52 | return {}; |
| 53 | } |
| 54 | |
| 55 | const values = {}; |
| 56 | for ( const attributeName of Object.keys( bindings ) ) { |
| 57 | const fieldKey = bindings[ attributeName ].args?.key; |
| 58 | if ( ! fieldKey ) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | const value = valueToBindingString( |
| 63 | store.getFieldValue( fieldKey ) |
| 64 | ); |
| 65 | if ( null !== value ) { |
| 66 | values[ attributeName ] = value; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return values; |
| 71 | }, |
| 72 | setValues( { select, dispatch, bindings } ) { |
| 73 | const store = select( STORE_NAME ); |
| 74 | if ( ! store.isInitialized() ) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | for ( const attributeName of Object.keys( bindings ) ) { |
| 79 | const binding = bindings[ attributeName ]; |
| 80 | const fieldKey = binding.args?.key; |
| 81 | if ( |
| 82 | ! fieldKey || |
| 83 | undefined === binding.newValue || |
| 84 | ! isScalarValue( binding.newValue ) |
| 85 | ) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | const field = store.getField( fieldKey ); |
| 90 | if ( field && fieldSupportsBindings( field ) ) { |
| 91 | dispatch( STORE_NAME ).setFieldValue( |
| 92 | fieldKey, |
| 93 | binding.newValue |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | }, |
| 98 | canUserEditValue( { select, args } ) { |
| 99 | if ( ! args?.key ) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | const store = select( STORE_NAME ); |
| 104 | if ( ! store.isInitialized() ) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | const field = store.getField( args.key ); |
| 109 | if ( ! field || ! fieldSupportsBindings( field ) ) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | const value = store.getFieldValue( args.key ); |
| 114 | return ! ( |
| 115 | null !== value && |
| 116 | undefined !== value && |
| 117 | ! isScalarValue( value ) |
| 118 | ); |
| 119 | }, |
| 120 | getFieldsList( { select } ) { |
| 121 | const store = select( STORE_NAME ); |
| 122 | if ( ! store.isInitialized() ) { |
| 123 | return []; |
| 124 | } |
| 125 | |
| 126 | const fields = store.getFields(); |
| 127 | const list = []; |
| 128 | for ( const fieldKey of Object.keys( fields ) ) { |
| 129 | const field = fields[ fieldKey ]; |
| 130 | if ( fieldSupportsBindings( field ) ) { |
| 131 | list.push( { |
| 132 | label: field.label, |
| 133 | args: { key: field.key }, |
| 134 | type: 'string', |
| 135 | } ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return list; |
| 140 | }, |
| 141 | } ); |
| 142 | } )(); |
| 143 |