| 1 |
<?php |
| 2 |
/** |
| 3 |
* Maximum Character Length ↔ Form Builder bridge. |
| 4 |
* |
| 5 |
* Injects `maximum-character-length` into allowlisted field blocks and registers |
| 6 |
* the ExtraFieldPropertiesPanel control. Stored as string (legacy casts at enforce time). |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
// Loaded by wppb_fb_load_addon_integrations(); do not include directly. |
| 12 |
|
| 13 |
/** |
| 14 |
* Field types that get the Maximum Character Length control (classic updateFields allowlist). |
| 15 |
* |
| 16 |
* @return string[] |
| 17 |
*/ |
| 18 |
function wppb_mcl_fb_supported_field_types() { |
| 19 |
return array( |
| 20 |
'Input', |
| 21 |
'Textarea', |
| 22 |
'Default - Biographical Info', |
| 23 |
'URL', |
| 24 |
'Email', |
| 25 |
'Default - Website', |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Inject `maximum-character-length` into the allowlisted field blocks' schema. |
| 31 |
*/ |
| 32 |
add_filter( 'wppb_fb_block_attributes', 'wppb_mcl_fb_inject_block_attributes', 10, 3 ); |
| 33 |
function wppb_mcl_fb_inject_block_attributes( $attributes, $field_type, $context ) { |
| 34 |
if ( ! in_array( $field_type, wppb_mcl_fb_supported_field_types(), true ) ) { |
| 35 |
return $attributes; |
| 36 |
} |
| 37 |
if ( ! isset( $attributes['maximum-character-length'] ) ) { |
| 38 |
$attributes['maximum-character-length'] = array( 'type' => 'string', 'default' => '' ); |
| 39 |
} |
| 40 |
return $attributes; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Publish the client schema mirror and the editable number control. |
| 45 |
*/ |
| 46 |
add_action( 'wppb_fb_enqueue_editor_assets_late', 'wppb_mcl_fb_enqueue_editor_payload' ); |
| 47 |
function wppb_mcl_fb_enqueue_editor_payload() { |
| 48 |
$field_types = array_intersect( wppb_mcl_fb_supported_field_types(), wppb_fb_all_field_type_labels() ); |
| 49 |
if ( empty( $field_types ) ) return; |
| 50 |
|
| 51 |
$schema = array( 'maximum-character-length' => array( 'type' => 'string', 'default' => '' ) ); |
| 52 |
$extra = array(); |
| 53 |
foreach ( $field_types as $field_type ) { |
| 54 |
$extra[ $field_type ] = $schema; |
| 55 |
} |
| 56 |
wppb_fb_extra_attributes_inline_script( $extra ); |
| 57 |
|
| 58 |
wppb_fb_register_extra_field_control( array( |
| 59 |
'attribute' => 'maximum-character-length', |
| 60 |
'label' => __( 'Maximum Character Length', 'profile-builder' ), |
| 61 |
'help' => __( 'Specify the maximum number of characters a user can type in this field.', 'profile-builder' ), |
| 62 |
'control' => 'number', |
| 63 |
) ); |
| 64 |
} |
| 65 |
|