PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.4.7
JetFormBuilder — Dynamic Blocks Form Builder v3.4.7
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 2 years ago block-type.php 1 year ago
block-render.php
176 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 } else {
60 $val = $value;
61 $label = $option;
62 }
63
64 $html = '<div class="jet-form-builder__field-wrap checkboxes-wrap checkradio-wrap">';
65
66 $item = sprintf(
67 '<label class="jet-form-builder__field-label for-checkbox">
68 <input %1$s %2$s><span>%3$s</span></label>',
69 Builder_Helper::attrs(
70 array(
71 array( 'type', 'checkbox' ),
72 array( 'name', esc_attr( $this->block_type->get_field_name() . $this->get_name_suffix() ) ),
73 array( 'value', esc_attr( $val ) ),
74 array( 'data-field-name', esc_attr( $this->args['name'] ) ),
75 array( 'checked', in_array( (string) $val, $default, true ) ? 'checked' : '' ),
76 array(
77 'data-calculate',
78 ( is_array( $option ) && isset( $option['calculate'] ) && '' !== $option['calculate'] )
79 ? esc_attr( $option['calculate'] )
80 : '',
81 ),
82 )
83 ),
84 $this->get_attributes_string_save(),
85 wp_kses_post( $label )
86 );
87
88 $html .= apply_filters(
89 'jet-form-builder/render/checkbox-field/option',
90 $item,
91 $val,
92 $option,
93 $this
94 );
95
96 $html .= '</div>';
97
98 return $html;
99 }
100
101 protected function render_custom_option(): string {
102 $html = '<div class="jet-form-builder__field-wrap checkboxes-wrap checkradio-wrap custom-option">';
103 $class_name = $this->args['class_name'] ?? '';
104
105 $name = esc_attr( $this->block_type->get_field_name() . $this->get_name_suffix() );
106 $checkbox = sprintf(
107 '<input %1$s value="">',
108 Builder_Helper::attrs(
109 array(
110 array( 'type', 'checkbox' ),
111 array( 'checked', 'checked' ),
112 array( 'data-field-name', esc_attr( $this->args['name'] ) ),
113 array( 'data-custom', 1 ),
114 array(
115 'class',
116 'jet-form-builder__field checkboxes-field checkradio-field' . (
117 $class_name ? " {$class_name}" : ''
118 ),
119 ),
120 )
121 )
122 );
123
124 $input = sprintf(
125 '<input %s>',
126 Builder_Helper::attrs(
127 array(
128 array( 'type', 'text' ),
129 array( 'name', $name ),
130 array( 'data-field-name', esc_attr( $this->args['name'] ) ),
131 array( 'class', 'jet-form-builder__field text-field' ),
132 )
133 )
134 );
135
136 /**
137 * @since 3.2.0
138 */
139 $item_template = apply_filters(
140 'jet-form-builder/render/checkbox-field/custom-option',
141 sprintf(
142 '<label class="jet-form-builder__field-label for-checkbox">%1$s<span>%2$s</span><label>',
143 $checkbox,
144 $input
145 )
146 );
147
148 $custom_label = $this->args['custom_option']['label'] ?? '';
149 $custom_label = $custom_label ?: __( '+ Add New', 'jet-form-builder' );
150
151 $button = sprintf(
152 '<button type="button" class="add-custom-option">%1$s<template>%2$s</template></button>',
153 $custom_label,
154 $html . $item_template . '</div>'
155 );
156
157 $html .= $button . '</div>';
158
159 return $html;
160 }
161
162 public function get_name_suffix(): string {
163 return count( $this->args['field_options'] ) > 1 ? '[]' : '';
164 }
165
166 /**
167 * @see \Jet_Form_Builder\Blocks\Render\Calculated_Field_Render::get_fields_label_tag
168 *
169 * @return string
170 */
171 protected function get_fields_label_tag(): string {
172 return 'div';
173 }
174
175 }
176