PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / trunk
JetFormBuilder — Dynamic Blocks Form Builder vtrunk
3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / option-field / blocks / radio / block-render.php
jetformbuilder / modules / option-field / blocks / radio Last commit date
block-render.php 1 month ago block-type.php 3 months ago
block-render.php
173 lines
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 $html = '<div class="jet-form-builder__field-wrap radio-wrap checkradio-wrap custom-option">';
118 $class_name = $this->args['class_name'] ?? '';
119
120 $item = sprintf(
121 '<label class="jet-form-builder__field-label for-radio">
122 <input %1$s value=""><span>%2$s</span></label>',
123 Builder_Helper::attrs(
124 array(
125 array( 'type', 'radio' ),
126 array( 'name', esc_attr( $this->block_type->get_field_name() ) ),
127 array( 'data-field-name', esc_attr( $this->args['name'] ) ),
128 array( 'data-custom', 1 ),
129 array(
130 'class',
131 'jet-form-builder__field radio-field checkradio-field' . ( $class_name ? " {$class_name}" : '' ),
132 ),
133 )
134 ),
135 sprintf(
136 '<input %s>',
137 Builder_Helper::attrs(
138 array(
139 array( 'type', 'text' ),
140 array( 'name', esc_attr( $this->block_type->get_field_name() ) ),
141 array( 'data-field-name', esc_attr( $this->args['name'] ) ),
142 array( 'class', 'jet-form-builder__field text-field' ),
143 array( 'disabled', 'disabled' ),
144 array( 'required', $this->block_type->get_required_val() ),
145 )
146 )
147 )
148 );
149
150 /**
151 * @since 3.2.0
152 */
153 $html .= apply_filters(
154 'jet-form-builder/render/radio-field/custom-option',
155 $item,
156 $this
157 );
158
159 $html .= '</div>';
160
161 return $html;
162 }
163
164 /**
165 * @return string
166 * @see \Jet_Form_Builder\Blocks\Render\Calculated_Field_Render::get_fields_label_tag
167 */
168 protected function get_fields_label_tag(): string {
169 return 'div';
170 }
171
172 }
173