| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Option_Field\Blocks\Checkbox; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Blocks\Render\Base; |
| 6 |
use Jet_Form_Builder\Classes\Builder_Helper; |
| 7 |
use JFB_Modules\Option_Field\Html_Attributes_Injector; |
| 8 |
|
| 9 |
// If this file is called directly, abort. |
| 10 |
if ( ! defined( 'WPINC' ) ) { |
| 11 |
die; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Define text field renderer class |
| 16 |
*/ |
| 17 |
class Block_Render extends Base { |
| 18 |
|
| 19 |
public function get_name() { |
| 20 |
return 'checkbox-field'; |
| 21 |
} |
| 22 |
|
| 23 |
public function render_without_layout( $template = null, $args = array() ) { |
| 24 |
parent::render_without_layout( $template, $args ); |
| 25 |
|
| 26 |
return $this->render_options(); |
| 27 |
} |
| 28 |
|
| 29 |
public function render_options(): string { |
| 30 |
$required = $this->block_type->get_required_val(); |
| 31 |
$default = $this->args['default'] ?? array(); |
| 32 |
$is_array_like_string = static function ( $value ) { |
| 33 |
if ( ! is_string( $value ) ) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
$value = trim( $value ); |
| 38 |
|
| 39 |
return '[' === substr( $value, 0, 1 ) && ']' === substr( $value, -1 ); |
| 40 |
}; |
| 41 |
|
| 42 |
$this->add_attribute( 'class', 'jet-form-builder__field checkboxes-field checkradio-field' ); |
| 43 |
$this->add_attribute( 'class', $this->args['class_name'] ); |
| 44 |
$this->add_attribute( 'required', $required ); |
| 45 |
$dynamic_default = null; |
| 46 |
|
| 47 |
if ( |
| 48 |
is_string( $default ) && ( |
| 49 |
jet_form_builder()->regexp->has_macro( $default ) || |
| 50 |
$is_array_like_string( $default ) |
| 51 |
) |
| 52 |
) { |
| 53 |
$dynamic_default = $default; |
| 54 |
} elseif ( is_array( $default ) ) { |
| 55 |
$normalized_default = array_values( $default ); |
| 56 |
$single_default = $normalized_default[0] ?? ''; |
| 57 |
|
| 58 |
if ( |
| 59 |
1 === count( $normalized_default ) && |
| 60 |
is_string( $single_default ) && |
| 61 |
'[' === substr( trim( $single_default ), 0, 1 ) |
| 62 |
) { |
| 63 |
$dynamic_default = $single_default; |
| 64 |
} else { |
| 65 |
$dynamic_default = wp_json_encode( $normalized_default ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if ( |
| 70 |
is_string( $dynamic_default ) && ( |
| 71 |
jet_form_builder()->regexp->has_macro( $dynamic_default ) || |
| 72 |
$is_array_like_string( $dynamic_default ) |
| 73 |
) |
| 74 |
) { |
| 75 |
wp_enqueue_script( \Jet_Form_Builder\Blocks\Dynamic_Value::HANDLE ); |
| 76 |
$this->add_attribute( 'data-default-val', $dynamic_default ); |
| 77 |
} |
| 78 |
|
| 79 |
$auto_update_attrs = Html_Attributes_Injector::render_data_attributes( $this->args ); |
| 80 |
$auto_update_attrs = $auto_update_attrs ? ' ' . $auto_update_attrs : ''; |
| 81 |
|
| 82 |
$html = '<div class="jet-form-builder__fields-group checkradio-wrap" data-jfb-sync' . $auto_update_attrs . '>'; |
| 83 |
|
| 84 |
if ( ! empty( $this->args['field_options'] ) ) { |
| 85 |
foreach ( $this->args['field_options'] as $value => $option ) { |
| 86 |
$html .= $this->render_option( $value, $option ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! empty( $this->args['custom_option']['allow'] ) ) { |
| 91 |
$html .= $this->render_custom_option(); |
| 92 |
} |
| 93 |
|
| 94 |
$html .= '</div>'; |
| 95 |
$this->reset_attributes(); |
| 96 |
|
| 97 |
return $html; |
| 98 |
} |
| 99 |
|
| 100 |
public function render_option( $value, $option ): string { |
| 101 |
$default = $this->args['default'] ?? array(); |
| 102 |
|
| 103 |
if ( is_array( $option ) ) { |
| 104 |
$val = $option['value'] ?? $value; |
| 105 |
$label = $option['label'] ?? $val; |
| 106 |
$keep_commas = ! empty( $option['keep_commas'] ); |
| 107 |
} else { |
| 108 |
$val = $value; |
| 109 |
$label = $option; |
| 110 |
$keep_commas = false; |
| 111 |
} |
| 112 |
|
| 113 |
$html = '<div class="jet-form-builder__field-wrap checkboxes-wrap checkradio-wrap">'; |
| 114 |
|
| 115 |
$attrs = array( |
| 116 |
array( 'type', 'checkbox' ), |
| 117 |
array( 'name', esc_attr( $this->block_type->get_field_name() . $this->get_name_suffix() ) ), |
| 118 |
array( 'value', esc_attr( $val ) ), |
| 119 |
array( 'data-field-name', esc_attr( $this->args['name'] ) ), |
| 120 |
array( 'checked', in_array( (string) $val, $default, true ) ? 'checked' : '' ), |
| 121 |
array( |
| 122 |
'data-calculate', |
| 123 |
( is_array( $option ) && isset( $option['calculate'] ) && '' !== $option['calculate'] ) |
| 124 |
? esc_attr( $option['calculate'] ) |
| 125 |
: '', |
| 126 |
), |
| 127 |
); |
| 128 |
|
| 129 |
if ( $keep_commas ) { |
| 130 |
$attrs[] = array( 'data-keep-commas', '1' ); |
| 131 |
} |
| 132 |
|
| 133 |
$item = sprintf( |
| 134 |
'<label class="jet-form-builder__field-label for-checkbox"> |
| 135 |
<input %1$s %2$s><span>%3$s</span></label>', |
| 136 |
Builder_Helper::attrs( $attrs ), |
| 137 |
$this->get_attributes_string_save(), |
| 138 |
wp_kses_post( $label ) |
| 139 |
); |
| 140 |
|
| 141 |
$html .= apply_filters( |
| 142 |
'jet-form-builder/render/checkbox-field/option', |
| 143 |
$item, |
| 144 |
$val, |
| 145 |
$option, |
| 146 |
$this |
| 147 |
); |
| 148 |
|
| 149 |
$html .= '</div>'; |
| 150 |
|
| 151 |
return $html; |
| 152 |
} |
| 153 |
|
| 154 |
protected function render_custom_option(): string { |
| 155 |
$html = '<div class="jet-form-builder__field-wrap checkboxes-wrap checkradio-wrap custom-option">'; |
| 156 |
$has_options = ! empty( $this->args['field_options'] ); |
| 157 |
|
| 158 |
$name = esc_attr( $this->block_type->get_field_name() . $this->get_name_suffix() ); |
| 159 |
$checkbox = sprintf( |
| 160 |
'<input %1$s %2$s value="">', |
| 161 |
Builder_Helper::attrs( |
| 162 |
array( |
| 163 |
array( 'type', 'checkbox' ), |
| 164 |
array( 'checked', $has_options ? 'checked' : '' ), |
| 165 |
array( 'data-field-name', esc_attr( $this->args['name'] ) ), |
| 166 |
array( 'data-custom', 1 ), |
| 167 |
) |
| 168 |
), |
| 169 |
$this->get_attributes_string_save() |
| 170 |
); |
| 171 |
|
| 172 |
$input = sprintf( |
| 173 |
'<input %s>', |
| 174 |
Builder_Helper::attrs( |
| 175 |
array( |
| 176 |
array( 'type', 'text' ), |
| 177 |
array( 'name', $name ), |
| 178 |
array( 'data-field-name', esc_attr( $this->args['name'] ) ), |
| 179 |
array( 'class', 'jet-form-builder__field text-field' ), |
| 180 |
) |
| 181 |
) |
| 182 |
); |
| 183 |
|
| 184 |
/** |
| 185 |
* @since 3.2.0 |
| 186 |
*/ |
| 187 |
$item_template = apply_filters( |
| 188 |
'jet-form-builder/render/checkbox-field/custom-option', |
| 189 |
sprintf( |
| 190 |
'<label class="jet-form-builder__field-label for-checkbox">%1$s<span>%2$s</span><label>', |
| 191 |
$checkbox, |
| 192 |
$input |
| 193 |
) |
| 194 |
); |
| 195 |
|
| 196 |
$custom_label = $this->args['custom_option']['label'] ?? ''; |
| 197 |
$custom_label = $custom_label ?: __( '+ Add New', 'jet-form-builder' ); |
| 198 |
|
| 199 |
$button = sprintf( |
| 200 |
'<button type="button" class="add-custom-option">%1$s<template>%2$s</template></button>', |
| 201 |
$custom_label, |
| 202 |
$html . $item_template . '</div>' |
| 203 |
); |
| 204 |
|
| 205 |
if ( ! $has_options ) { |
| 206 |
return $html . $item_template . '</div>' . $html . $button . '</div>'; |
| 207 |
} |
| 208 |
|
| 209 |
$html .= $button . '</div>'; |
| 210 |
|
| 211 |
return $html; |
| 212 |
} |
| 213 |
|
| 214 |
public function get_name_suffix(): string { |
| 215 |
return ( |
| 216 |
count( $this->args['field_options'] ) > 1 |
| 217 |
|| ! empty( $this->args['custom_option']['allow'] ) |
| 218 |
) ? '[]' : ''; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* @see \Jet_Form_Builder\Blocks\Render\Calculated_Field_Render::get_fields_label_tag |
| 223 |
* |
| 224 |
* @return string |
| 225 |
*/ |
| 226 |
protected function get_fields_label_tag(): string { |
| 227 |
return 'div'; |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|