| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Option_Field\Blocks\Radio; |
| 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 'radio-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'] ?? ''; |
| 32 |
|
| 33 |
$this->add_attribute( 'class', 'jet-form-builder__field radio-field checkradio-field' ); |
| 34 |
$this->add_attribute( 'class', $this->args['class_name'] ); |
| 35 |
$this->add_attribute( 'required', $required ); |
| 36 |
if ( |
| 37 |
is_string( $default ) && |
| 38 |
jet_form_builder()->regexp->has_macro( $default ) |
| 39 |
) { |
| 40 |
wp_enqueue_script( \Jet_Form_Builder\Blocks\Dynamic_Value::HANDLE ); |
| 41 |
$this->add_attribute( 'data-default-val', $default ); |
| 42 |
} |
| 43 |
|
| 44 |
$auto_update_attrs = Html_Attributes_Injector::render_data_attributes( $this->args ); |
| 45 |
$auto_update_attrs = $auto_update_attrs ? ' ' . $auto_update_attrs : ''; |
| 46 |
|
| 47 |
$html = '<div class="jet-form-builder__fields-group checkradio-wrap" data-jfb-sync' . $auto_update_attrs . '>'; |
| 48 |
|
| 49 |
if ( ! empty( $this->args['field_options'] ) ) { |
| 50 |
foreach ( $this->args['field_options'] as $value => $option ) { |
| 51 |
$html .= $this->render_option( $value, $option ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! empty( $this->args['custom_option'] ) ) { |
| 56 |
$html .= $this->render_custom_option(); |
| 57 |
} |
| 58 |
|
| 59 |
$html .= '</div>'; |
| 60 |
$this->reset_attributes(); |
| 61 |
|
| 62 |
return $html; |
| 63 |
} |
| 64 |
|
| 65 |
public function render_option( $value, $option ): string { |
| 66 |
$default = (string) ( $this->args['default'] ?? false ); |
| 67 |
|
| 68 |
if ( is_array( $option ) ) { |
| 69 |
$val = $option['value'] ?? $value; |
| 70 |
$label = $option['label'] ?? $val; |
| 71 |
} else { |
| 72 |
$val = $value; |
| 73 |
$label = $option; |
| 74 |
} |
| 75 |
|
| 76 |
$html = '<div class="jet-form-builder__field-wrap radio-wrap checkradio-wrap">'; |
| 77 |
|
| 78 |
$item = sprintf( |
| 79 |
'<label class="jet-form-builder__field-label for-radio"> |
| 80 |
<input %1$s %2$s><span>%3$s</span></label>', |
| 81 |
Builder_Helper::attrs( |
| 82 |
array( |
| 83 |
array( 'type', 'radio' ), |
| 84 |
array( 'name', esc_attr( $this->block_type->get_field_name() ) ), |
| 85 |
array( 'value', esc_attr( $val ) ), |
| 86 |
array( 'data-field-name', esc_attr( $this->args['name'] ) ), |
| 87 |
array( 'checked', ( (string) $val ) === $default ? 'checked' : '' ), |
| 88 |
array( |
| 89 |
'data-calculate', |
| 90 |
( is_array( $option ) && isset( $option['calculate'] ) && '' !== $option['calculate'] ) |
| 91 |
? esc_attr( $option['calculate'] ) |
| 92 |
: '', |
| 93 |
), |
| 94 |
) |
| 95 |
), |
| 96 |
$this->get_attributes_string_save(), |
| 97 |
wp_kses_post( $label ) |
| 98 |
); |
| 99 |
|
| 100 |
/** |
| 101 |
* @since 3.1.0 |
| 102 |
*/ |
| 103 |
$html .= apply_filters( |
| 104 |
'jet-form-builder/render/radio-field/option', |
| 105 |
$item, |
| 106 |
$val, |
| 107 |
$option, |
| 108 |
$this |
| 109 |
); |
| 110 |
|
| 111 |
$html .= '</div>'; |
| 112 |
|
| 113 |
return $html; |
| 114 |
} |
| 115 |
|
| 116 |
protected function render_custom_option(): string { |
| 117 |
$default = (string) ( $this->args['default'] ?? '' ); |
| 118 |
$use_custom = '' !== $default && ! jet_form_builder()->regexp->has_macro( $default ); |
| 119 |
|
| 120 |
foreach ( $this->args['field_options'] ?? array() as $value => $option ) { |
| 121 |
$val = is_array( $option ) ? ( $option['value'] ?? $value ) : $value; |
| 122 |
|
| 123 |
if ( (string) $val === $default ) { |
| 124 |
$use_custom = false; |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
$html = '<div class="jet-form-builder__field-wrap radio-wrap checkradio-wrap custom-option">'; |
| 130 |
$class_name = $this->args['class_name'] ?? ''; |
| 131 |
|
| 132 |
$item = sprintf( |
| 133 |
'<label class="jet-form-builder__field-label for-radio"> |
| 134 |
<input %1$s value=""><span>%2$s</span></label>', |
| 135 |
Builder_Helper::attrs( |
| 136 |
array( |
| 137 |
array( 'type', 'radio' ), |
| 138 |
array( 'name', esc_attr( $this->block_type->get_field_name() ) ), |
| 139 |
array( 'data-field-name', esc_attr( $this->args['name'] ) ), |
| 140 |
array( 'data-custom', 1 ), |
| 141 |
array( 'checked', $use_custom ? 'checked' : '' ), |
| 142 |
array( |
| 143 |
'class', |
| 144 |
'jet-form-builder__field radio-field checkradio-field' . ( $class_name ? " {$class_name}" : '' ), |
| 145 |
), |
| 146 |
) |
| 147 |
), |
| 148 |
sprintf( |
| 149 |
'<input %s>', |
| 150 |
Builder_Helper::attrs( |
| 151 |
array( |
| 152 |
array( 'type', 'text' ), |
| 153 |
array( 'name', esc_attr( $this->block_type->get_field_name() ) ), |
| 154 |
array( 'data-field-name', esc_attr( $this->args['name'] ) ), |
| 155 |
array( 'class', 'jet-form-builder__field text-field' ), |
| 156 |
array( 'value', $use_custom ? esc_attr( $default ) : '' ), |
| 157 |
array( 'disabled', $use_custom ? '' : 'disabled' ), |
| 158 |
array( 'required', $this->block_type->get_required_val() ), |
| 159 |
) |
| 160 |
) |
| 161 |
) |
| 162 |
); |
| 163 |
|
| 164 |
/** |
| 165 |
* @since 3.2.0 |
| 166 |
*/ |
| 167 |
$html .= apply_filters( |
| 168 |
'jet-form-builder/render/radio-field/custom-option', |
| 169 |
$item, |
| 170 |
$this |
| 171 |
); |
| 172 |
|
| 173 |
$html .= '</div>'; |
| 174 |
|
| 175 |
return $html; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @return string |
| 180 |
* @see \Jet_Form_Builder\Blocks\Render\Calculated_Field_Render::get_fields_label_tag |
| 181 |
*/ |
| 182 |
protected function get_fields_label_tag(): string { |
| 183 |
return 'div'; |
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|