PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / trunk
Secure Custom Fields vtrunk
6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / assets / src / js / bindings / utils.js
secure-custom-fields / assets / src / js / bindings Last commit date
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
utils.js
178 lines
1 /**
2 * Utility functions for block bindings
3 */
4
5 import { BLOCK_BINDINGS_CONFIG } from './constants';
6
7 /**
8 * Gets the bindable attributes for a given block.
9 *
10 * @since 6.7.0
11 * @param {string} blockName The name of the block.
12 * @return {string[]} The bindable attributes for the block.
13 */
14 export function getBindableAttributes( blockName ) {
15 const config = BLOCK_BINDINGS_CONFIG[ blockName ];
16 return config ? Object.keys( config ) : [];
17 }
18
19 /**
20 * Gets the allowed field types for a specific block attribute.
21 *
22 * @since 6.7.0
23 * @param {string} blockName The name of the block.
24 * @param {string|null} attribute The attribute name, or null for all types.
25 * @return {string[]|null} The allowed field types, or null if no restrictions.
26 */
27 export function getAllowedFieldTypes( blockName, attribute = null ) {
28 const blockConfig = BLOCK_BINDINGS_CONFIG[ blockName ];
29
30 if ( ! blockConfig ) {
31 return null;
32 }
33
34 if ( attribute ) {
35 return blockConfig[ attribute ] || null;
36 }
37
38 // Get all unique field types for the block
39 return [ ...new Set( Object.values( blockConfig ).flat() ) ];
40 }
41
42 /**
43 * Filters field options based on allowed field types.
44 *
45 * @since 6.7.0
46 * @param {Array} fieldOptions Array of field option objects with value, label, and type.
47 * @param {string} blockName The name of the block.
48 * @param {string|null} attribute The attribute name, or null for all types.
49 * @return {Array} Filtered array of field options.
50 */
51 export function getFilteredFieldOptions(
52 fieldOptions,
53 blockName,
54 attribute = null
55 ) {
56 if ( ! fieldOptions || fieldOptions.length === 0 ) {
57 return [];
58 }
59
60 const allowedTypes = getAllowedFieldTypes( blockName, attribute );
61
62 if ( ! allowedTypes ) {
63 return fieldOptions;
64 }
65
66 return fieldOptions.filter( ( option ) =>
67 allowedTypes.includes( option.type )
68 );
69 }
70
71 /**
72 * Checks if all bindable attributes for a block support the same field types.
73 *
74 * @since 6.7.0
75 * @param {string} blockName The name of the block.
76 * @param {string[]} bindableAttributes Array of bindable attribute names.
77 * @return {boolean} True if all attributes support the same field types.
78 */
79 export function canUseUnifiedBinding( blockName, bindableAttributes ) {
80 if ( ! bindableAttributes || bindableAttributes.length <= 1 ) {
81 return false;
82 }
83
84 const blockConfig = BLOCK_BINDINGS_CONFIG[ blockName ];
85 if ( ! blockConfig ) {
86 return false;
87 }
88
89 const firstAttributeTypes = blockConfig[ bindableAttributes[ 0 ] ] || [];
90
91 return bindableAttributes.every( ( attr ) => {
92 const attrTypes = blockConfig[ attr ] || [];
93 return (
94 attrTypes.length === firstAttributeTypes.length &&
95 attrTypes.every( ( type ) => firstAttributeTypes.includes( type ) )
96 );
97 } );
98 }
99
100 /**
101 * Extracts the post type from a template slug.
102 *
103 * @since 6.7.0
104 * @param {string} templateSlug The template slug (e.g., 'single-product', 'archive-post').
105 * @return {string|null} The extracted post type, or null if not detected.
106 */
107 export function extractPostTypeFromTemplate( templateSlug ) {
108 if ( ! templateSlug ) {
109 return null;
110 }
111
112 // Handle single templates
113 if ( templateSlug.startsWith( 'single-' ) ) {
114 return templateSlug.replace( 'single-', '' );
115 }
116
117 // Handle archive templates
118 if ( templateSlug.startsWith( 'archive-' ) ) {
119 return templateSlug.replace( 'archive-', '' );
120 }
121
122 // Default single template maps to 'post'
123 if ( templateSlug === 'single' ) {
124 return 'post';
125 }
126
127 return null;
128 }
129
130 /**
131 * Formats field data from API response into a usable structure.
132 *
133 * @since 6.7.0
134 * @param {Array} fieldGroups Array of field group objects from the API.
135 * @return {Object} Formatted fields map with field name as key.
136 */
137 export function formatFieldGroupsData( fieldGroups ) {
138 const fieldsMap = {};
139
140 if ( ! Array.isArray( fieldGroups ) ) {
141 return fieldsMap;
142 }
143
144 fieldGroups.forEach( ( group ) => {
145 if ( Array.isArray( group.fields ) ) {
146 group.fields.forEach( ( field ) => {
147 fieldsMap[ field.name ] = {
148 label: field.label,
149 type: field.type,
150 };
151 } );
152 }
153 } );
154
155 return fieldsMap;
156 }
157
158 /**
159 * Converts fields map to options array for ComboboxControl.
160 *
161 * @since 6.7.0
162 * @param {Object} fieldsMap Object with field data.
163 * @return {Array} Array of option objects with value, label, and type.
164 */
165 export function fieldsToOptions( fieldsMap ) {
166 if ( ! fieldsMap || Object.keys( fieldsMap ).length === 0 ) {
167 return [];
168 }
169
170 return Object.entries( fieldsMap ).map(
171 ( [ fieldName, fieldConfig ] ) => ( {
172 value: fieldName,
173 label: fieldConfig.label,
174 type: fieldConfig.type,
175 } )
176 );
177 }
178