block-render.php
229 lines
| 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 | $class_name = $this->args['class_name'] ?? ''; |
| 157 | |
| 158 | $name = esc_attr( $this->block_type->get_field_name() . $this->get_name_suffix() ); |
| 159 | $checkbox = sprintf( |
| 160 | '<input %1$s value="">', |
| 161 | Builder_Helper::attrs( |
| 162 | array( |
| 163 | array( 'type', 'checkbox' ), |
| 164 | array( 'checked', 'checked' ), |
| 165 | array( 'data-field-name', esc_attr( $this->args['name'] ) ), |
| 166 | array( 'data-custom', 1 ), |
| 167 | array( |
| 168 | 'class', |
| 169 | 'jet-form-builder__field checkboxes-field checkradio-field' . ( |
| 170 | $class_name ? " {$class_name}" : '' |
| 171 | ), |
| 172 | ), |
| 173 | ) |
| 174 | ) |
| 175 | ); |
| 176 | |
| 177 | $input = sprintf( |
| 178 | '<input %s>', |
| 179 | Builder_Helper::attrs( |
| 180 | array( |
| 181 | array( 'type', 'text' ), |
| 182 | array( 'name', $name ), |
| 183 | array( 'data-field-name', esc_attr( $this->args['name'] ) ), |
| 184 | array( 'class', 'jet-form-builder__field text-field' ), |
| 185 | ) |
| 186 | ) |
| 187 | ); |
| 188 | |
| 189 | /** |
| 190 | * @since 3.2.0 |
| 191 | */ |
| 192 | $item_template = apply_filters( |
| 193 | 'jet-form-builder/render/checkbox-field/custom-option', |
| 194 | sprintf( |
| 195 | '<label class="jet-form-builder__field-label for-checkbox">%1$s<span>%2$s</span><label>', |
| 196 | $checkbox, |
| 197 | $input |
| 198 | ) |
| 199 | ); |
| 200 | |
| 201 | $custom_label = $this->args['custom_option']['label'] ?? ''; |
| 202 | $custom_label = $custom_label ?: __( '+ Add New', 'jet-form-builder' ); |
| 203 | |
| 204 | $button = sprintf( |
| 205 | '<button type="button" class="add-custom-option">%1$s<template>%2$s</template></button>', |
| 206 | $custom_label, |
| 207 | $html . $item_template . '</div>' |
| 208 | ); |
| 209 | |
| 210 | $html .= $button . '</div>'; |
| 211 | |
| 212 | return $html; |
| 213 | } |
| 214 | |
| 215 | public function get_name_suffix(): string { |
| 216 | return count( $this->args['field_options'] ) > 1 ? '[]' : ''; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @see \Jet_Form_Builder\Blocks\Render\Calculated_Field_Render::get_fields_label_tag |
| 221 | * |
| 222 | * @return string |
| 223 | */ |
| 224 | protected function get_fields_label_tag(): string { |
| 225 | return 'div'; |
| 226 | } |
| 227 | |
| 228 | } |
| 229 |