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
field-processing.js
153 lines
| 1 | /** |
| 2 | * Field processing utilities for block bindings |
| 3 | * |
| 4 | * @since 6.5.0 |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * Gets the SCF fields from the post entity. |
| 9 | * |
| 10 | * @since 6.5.0 |
| 11 | * |
| 12 | * @param {Object} post The post entity object. |
| 13 | * @return {Object} The SCF fields object with source data. |
| 14 | */ |
| 15 | export function getSCFFields( post ) { |
| 16 | if ( ! post?.acf ) { |
| 17 | return {}; |
| 18 | } |
| 19 | |
| 20 | // Extract only the _source fields which contain the formatted data |
| 21 | const sourceFields = {}; |
| 22 | Object.entries( post.acf ).forEach( ( [ key, value ] ) => { |
| 23 | if ( key.endsWith( '_source' ) ) { |
| 24 | // Remove the _source suffix to get the field name |
| 25 | const fieldName = key.replace( '_source', '' ); |
| 26 | sourceFields[ fieldName ] = value; |
| 27 | } |
| 28 | } ); |
| 29 | |
| 30 | return sourceFields; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Resolves image attribute values from an image object. |
| 35 | * |
| 36 | * @since 6.5.0 |
| 37 | * |
| 38 | * @param {Object} imageObj The image object from SCF field data. |
| 39 | * @param {string} attribute The attribute to resolve (url, alt, title, id). |
| 40 | * @return {string|number} The resolved attribute value. |
| 41 | */ |
| 42 | export function resolveImageAttribute( imageObj, attribute ) { |
| 43 | if ( ! imageObj ) { |
| 44 | return ''; |
| 45 | } |
| 46 | |
| 47 | switch ( attribute ) { |
| 48 | case 'url': |
| 49 | return imageObj.url || ''; |
| 50 | case 'alt': |
| 51 | return imageObj.alt || ''; |
| 52 | case 'title': |
| 53 | return imageObj.title || ''; |
| 54 | case 'id': |
| 55 | return imageObj.id || imageObj.ID || ''; |
| 56 | default: |
| 57 | return ''; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Processes a single field binding and returns its resolved value. |
| 63 | * |
| 64 | * @since 6.7.0 |
| 65 | * |
| 66 | * @param {string} attribute The attribute being bound. |
| 67 | * @param {Object} args The binding arguments. |
| 68 | * @param {Object} scfFields The SCF fields object. |
| 69 | * @return {string} The resolved field value. |
| 70 | */ |
| 71 | export function processFieldBinding( attribute, args, scfFields ) { |
| 72 | const fieldName = args?.key; |
| 73 | const fieldConfig = scfFields[ fieldName ]; |
| 74 | |
| 75 | if ( ! fieldConfig ) { |
| 76 | return ''; |
| 77 | } |
| 78 | |
| 79 | const fieldType = fieldConfig.type; |
| 80 | const fieldValue = fieldConfig.formatted_value; |
| 81 | |
| 82 | switch ( fieldType ) { |
| 83 | case 'image': |
| 84 | return resolveImageAttribute( fieldValue, attribute ); |
| 85 | |
| 86 | case 'checkbox': |
| 87 | // For checkbox fields, join array values or return as string |
| 88 | if ( Array.isArray( fieldValue ) ) { |
| 89 | return fieldValue.join( ', ' ); |
| 90 | } |
| 91 | return fieldValue ? String( fieldValue ) : ''; |
| 92 | |
| 93 | case 'number': |
| 94 | case 'range': |
| 95 | return fieldValue ? String( fieldValue ) : ''; |
| 96 | |
| 97 | case 'date_picker': |
| 98 | case 'text': |
| 99 | case 'textarea': |
| 100 | case 'url': |
| 101 | case 'email': |
| 102 | case 'select': |
| 103 | default: |
| 104 | return fieldValue ? String( fieldValue ) : ''; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Formats a field key into a human-readable label. |
| 110 | * |
| 111 | * @since 6.7.0 |
| 112 | * |
| 113 | * @param {string} fieldKey The field key (e.g., 'my_field_name'). |
| 114 | * @return {string} Formatted label (e.g., 'My Field Name'). |
| 115 | */ |
| 116 | export function formatFieldLabel( fieldKey ) { |
| 117 | if ( ! fieldKey ) { |
| 118 | return ''; |
| 119 | } |
| 120 | |
| 121 | return fieldKey |
| 122 | .split( '_' ) |
| 123 | .map( ( word ) => word.charAt( 0 ).toUpperCase() + word.slice( 1 ) ) |
| 124 | .join( ' ' ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Gets the field label from metadata or formats the field key. |
| 129 | * |
| 130 | * @since 6.7.0 |
| 131 | * @param {string} fieldKey The field key. |
| 132 | * @param {Object} fieldMetadata Optional field metadata object. |
| 133 | * @param {string} defaultLabel Optional default label to use. |
| 134 | * @return {string} The field label. |
| 135 | */ |
| 136 | export function getFieldLabel( |
| 137 | fieldKey, |
| 138 | fieldMetadata = null, |
| 139 | defaultLabel = '' |
| 140 | ) { |
| 141 | if ( ! fieldKey ) { |
| 142 | return defaultLabel; |
| 143 | } |
| 144 | |
| 145 | // Try to get the label from the provided metadata |
| 146 | if ( fieldMetadata && fieldMetadata[ fieldKey ]?.label ) { |
| 147 | return fieldMetadata[ fieldKey ].label; |
| 148 | } |
| 149 | |
| 150 | // Fallback: format field key as a label |
| 151 | return formatFieldLabel( fieldKey ); |
| 152 | } |
| 153 |