| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
die( 'You are not allowed to call this page directly.' ); |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Base controller for combo fields (fields with multiple sub-fields like Address). |
| 9 |
* |
| 10 |
* @since 6.34 |
| 11 |
*/ |
| 12 |
class FrmComboFieldsController { |
| 13 |
|
| 14 |
/** |
| 15 |
* Fill values with defaults. |
| 16 |
* |
| 17 |
* @since 6.34 |
| 18 |
* |
| 19 |
* @param mixed $value |
| 20 |
* @param array $defaults |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public static function fill_values( &$value, $defaults ) { |
| 25 |
$value = $value ? array_merge( $defaults, (array) $value ) : $defaults; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Include placeholder attribute for sub-field. |
| 30 |
* |
| 31 |
* @since 6.34 |
| 32 |
* |
| 33 |
* @param mixed $default_value |
| 34 |
* @param string $sub_field |
| 35 |
* @param array $field |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public static function include_placeholder( $default_value, $sub_field, $field = array() ) { |
| 40 |
if ( ! is_array( $default_value ) ) { |
| 41 |
$default_value = array(); |
| 42 |
} |
| 43 |
|
| 44 |
if ( $field ) { |
| 45 |
$use_placeholder = ! FrmField::is_option_empty( $field, 'placeholder' ); |
| 46 |
|
| 47 |
if ( ( ! $use_placeholder || empty( $default_value[ $sub_field ] ) ) && $sub_field === 'line1' ) { |
| 48 |
// Allow for 'inside' label position. |
| 49 |
$default_value[ $sub_field ] = FrmFieldsController::get_default_value_from_name( $field ); |
| 50 |
$use_placeholder = $default_value[ $sub_field ] !== ''; |
| 51 |
} |
| 52 |
|
| 53 |
if ( ! $use_placeholder ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
if ( ! $default_value ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
$placeholder = $default_value[ $sub_field ] ?? ''; |
| 63 |
echo ' placeholder="' . esc_attr( $placeholder ) . '" data-placeholder="' . esc_attr( $placeholder ) . '"'; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get dropdown label for select sub-fields. |
| 68 |
* |
| 69 |
* @since 6.34 |
| 70 |
* |
| 71 |
* @param array $atts |
| 72 |
* |
| 73 |
* @return string |
| 74 |
*/ |
| 75 |
public static function get_dropdown_label( $atts ) { |
| 76 |
$default = $atts['sub_field']['placeholder'] ?? ' '; |
| 77 |
|
| 78 |
if ( ! is_string( $default ) ) { |
| 79 |
$default = ''; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @since 6.34 |
| 84 |
* |
| 85 |
* @param string $default |
| 86 |
* @param array $atts |
| 87 |
*/ |
| 88 |
$result = apply_filters( 'frm_combo_dropdown_label', $default, $atts ); |
| 89 |
|
| 90 |
return is_string( $result ) ? $result : $default; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Add attributes to input field. |
| 95 |
* |
| 96 |
* @since 6.34 |
| 97 |
* |
| 98 |
* @param array $atts |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public static function add_atts_to_input( $atts ) { |
| 103 |
$placeholder = $atts['field']['placeholder'] ?? ''; |
| 104 |
$default_value = $atts['field']['default_value']; |
| 105 |
$field_obj = FrmFieldFactory::get_field_type( $atts['field']['type'], $atts['field'] ); |
| 106 |
|
| 107 |
if ( $atts['field']['type'] === 'address' ) { |
| 108 |
/** |
| 109 |
* @var FrmFieldAddress $field_obj |
| 110 |
*/ |
| 111 |
$placeholder = $field_obj->address_string_to_array( $placeholder ); |
| 112 |
$default_value = $field_obj->address_string_to_array( $default_value ); |
| 113 |
} |
| 114 |
|
| 115 |
self::include_placeholder( $placeholder, $atts['key'], $atts['field'] ); |
| 116 |
$atts['field']['placeholder'] = ''; |
| 117 |
|
| 118 |
$atts['field']['default_value'] = $default_value[ $atts['key'] ] ?? ''; |
| 119 |
|
| 120 |
if ( 'select' === $atts['sub_field']['type'] && ! empty( $atts['field']['read_only'] ) ) { |
| 121 |
$atts['sub_field']['atts']['disabled'] = 'disabled'; |
| 122 |
} |
| 123 |
|
| 124 |
if ( ! empty( $atts['sub_field']['optional'] ) ) { |
| 125 |
add_filter( 'frm_field_classes', 'FrmAddressesController::add_optional_class', 20, 2 ); |
| 126 |
do_action( 'frm_field_input_html', $atts['field'] ); |
| 127 |
remove_filter( 'frm_field_classes', 'FrmAddressesController::add_optional_class', 20 ); |
| 128 |
} else { |
| 129 |
do_action( 'frm_field_input_html', $atts['field'] ); |
| 130 |
} |
| 131 |
|
| 132 |
if ( ! isset( $atts['sub_field']['atts'] ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
foreach ( $atts['sub_field']['atts'] as $att_name => $att_value ) { |
| 137 |
echo ' ' . esc_attr( $att_name ) . '="' . esc_attr( $att_value ) . '"'; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Include sub-label for field. |
| 143 |
* |
| 144 |
* @since 6.34 |
| 145 |
* |
| 146 |
* @param array $atts |
| 147 |
* |
| 148 |
* @return void |
| 149 |
*/ |
| 150 |
public static function include_sub_label( $atts ) { |
| 151 |
self::show_sub_label( $atts ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Show sub-label for field. |
| 156 |
* |
| 157 |
* @since 6.34 |
| 158 |
* |
| 159 |
* @param array $atts |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public static function show_sub_label( $atts ) { |
| 164 |
$field = $atts['field']; |
| 165 |
$option_name = $atts['option_name']; |
| 166 |
|
| 167 |
if ( $field[ $option_name ] !== '' ) { |
| 168 |
echo '<div class="frm_description">' . wp_kses_post( $field[ $option_name ] ) . '</div>'; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Add error class if field has error. |
| 174 |
* |
| 175 |
* @since 6.34 |
| 176 |
* |
| 177 |
* @param array $atts |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
public static function maybe_add_error_class( $atts ) { |
| 182 |
$temp_id = ! empty( $atts['atts']['field_id'] ) ? $atts['atts']['field_id'] : $atts['field']['id']; |
| 183 |
$has_error = isset( $atts['errors'][ 'field' . $temp_id . '-' . $atts['key'] ] ); |
| 184 |
|
| 185 |
if ( $has_error ) { |
| 186 |
echo ' frm_blank_field'; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|