PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.5.6.3
JetFormBuilder — Dynamic Blocks Form Builder v3.5.6.3
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 / checkbox / block-render.php
jetformbuilder / modules / option-field / blocks / checkbox Last commit date
block-render.php 7 months ago block-type.php 1 year ago
block-render.php
182 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
8 // If this file is called directly, abort.
9 if ( ! defined( 'WPINC' ) ) {
10 die;
11 }
12
13 /**
14 * Define text field renderer class
15 */
16 class Block_Render extends Base {
17
18 public function get_name() {
19 return 'checkbox-field';
20 }
21
22 public function render_without_layout( $template = null, $args = array() ) {
23 parent::render_without_layout( $template, $args );
24
25 return $this->render_options();
26 }
27
28 public function render_options(): string {
29 $required = $this->block_type->get_required_val();
30
31 $this->add_attribute( 'class', 'jet-form-builder__field checkboxes-field checkradio-field' );
32 $this->add_attribute( 'class', $this->args['class_name'] );
33 $this->add_attribute( 'required', $required );
34
35 $html = '<div class="jet-form-builder__fields-group checkradio-wrap" data-jfb-sync>';
36
37 if ( ! empty( $this->args['field_options'] ) ) {
38 foreach ( $this->args['field_options'] as $value => $option ) {
39 $html .= $this->render_option( $value, $option );
40 }
41 }
42
43 if ( ! empty( $this->args['custom_option']['allow'] ) ) {
44 $html .= $this->render_custom_option();
45 }
46
47 $html .= '</div>';
48 $this->reset_attributes();
49
50 return $html;
51 }
52
53 public function render_option( $value, $option ): string {
54 $default = $this->args['default'] ?? array();
55
56 if ( is_array( $option ) ) {
57 $val = $option['value'] ?? $value;
58 $label = $option['label'] ?? $val;
59 $keep_commas = ! empty( $option['keep_commas'] );
60 } else {
61 $val = $value;
62 $label = $option;
63 $keep_commas = false;
64 }
65
66 $html = '<div class="jet-form-builder__field-wrap checkboxes-wrap checkradio-wrap">';
67
68 $attrs = array(
69 array( 'type', 'checkbox' ),
70 array( 'name', esc_attr( $this->block_type->get_field_name() . $this->get_name_suffix() ) ),
71 array( 'value', esc_attr( $val ) ),
72 array( 'data-field-name', esc_attr( $this->args['name'] ) ),
73 array( 'checked', in_array( (string) $val, $default, true ) ? 'checked' : '' ),
74 array(
75 'data-calculate',
76 ( is_array( $option ) && isset( $option['calculate'] ) && '' !== $option['calculate'] )
77 ? esc_attr( $option['calculate'] )
78 : '',
79 ),
80 );
81
82 if ( $keep_commas ) {
83 $attrs[] = array( 'data-keep-commas', '1' );
84 }
85
86 $item = sprintf(
87 '<label class="jet-form-builder__field-label for-checkbox">
88 <input %1$s %2$s><span>%3$s</span></label>',
89 Builder_Helper::attrs( $attrs ),
90 $this->get_attributes_string_save(),
91 wp_kses_post( $label )
92 );
93
94 $html .= apply_filters(
95 'jet-form-builder/render/checkbox-field/option',
96 $item,
97 $val,
98 $option,
99 $this
100 );
101
102 $html .= '</div>';
103
104 return $html;
105 }
106
107 protected function render_custom_option(): string {
108 $html = '<div class="jet-form-builder__field-wrap checkboxes-wrap checkradio-wrap custom-option">';
109 $class_name = $this->args['class_name'] ?? '';
110
111 $name = esc_attr( $this->block_type->get_field_name() . $this->get_name_suffix() );
112 $checkbox = sprintf(
113 '<input %1$s value="">',
114 Builder_Helper::attrs(
115 array(
116 array( 'type', 'checkbox' ),
117 array( 'checked', 'checked' ),
118 array( 'data-field-name', esc_attr( $this->args['name'] ) ),
119 array( 'data-custom', 1 ),
120 array(
121 'class',
122 'jet-form-builder__field checkboxes-field checkradio-field' . (
123 $class_name ? " {$class_name}" : ''
124 ),
125 ),
126 )
127 )
128 );
129
130 $input = sprintf(
131 '<input %s>',
132 Builder_Helper::attrs(
133 array(
134 array( 'type', 'text' ),
135 array( 'name', $name ),
136 array( 'data-field-name', esc_attr( $this->args['name'] ) ),
137 array( 'class', 'jet-form-builder__field text-field' ),
138 )
139 )
140 );
141
142 /**
143 * @since 3.2.0
144 */
145 $item_template = apply_filters(
146 'jet-form-builder/render/checkbox-field/custom-option',
147 sprintf(
148 '<label class="jet-form-builder__field-label for-checkbox">%1$s<span>%2$s</span><label>',
149 $checkbox,
150 $input
151 )
152 );
153
154 $custom_label = $this->args['custom_option']['label'] ?? '';
155 $custom_label = $custom_label ?: __( '+ Add New', 'jet-form-builder' );
156
157 $button = sprintf(
158 '<button type="button" class="add-custom-option">%1$s<template>%2$s</template></button>',
159 $custom_label,
160 $html . $item_template . '</div>'
161 );
162
163 $html .= $button . '</div>';
164
165 return $html;
166 }
167
168 public function get_name_suffix(): string {
169 return count( $this->args['field_options'] ) > 1 ? '[]' : '';
170 }
171
172 /**
173 * @see \Jet_Form_Builder\Blocks\Render\Calculated_Field_Render::get_fields_label_tag
174 *
175 * @return string
176 */
177 protected function get_fields_label_tag(): string {
178 return 'div';
179 }
180
181 }
182