PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.7
JetFormBuilder — Dynamic Blocks Form Builder v3.0.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 / includes / blocks / render / base.php
jetformbuilder / includes / blocks / render Last commit date
action-button-render.php 3 years ago base-select-radio-check.php 3 years ago base.php 3 years ago calculated-field-render.php 3 years ago checkbox-field-render.php 3 years ago date-field-render.php 3 years ago datetime-field-render.php 3 years ago form-builder.php 3 years ago form-hidden-fields.php 3 years ago group-break-field-render.php 3 years ago heading-field-render.php 3 years ago media-field-render.php 3 years ago number-field-render.php 3 years ago radio-field-render.php 3 years ago range-field-render.php 3 years ago repeater-field-render.php 3 years ago select-field-render.php 3 years ago text-field-render.php 3 years ago textarea-field-render.php 3 years ago time-field-render.php 3 years ago wysiwyg-field-render.php 3 years ago
base.php
299 lines
1 <?php
2
3 namespace Jet_Form_Builder\Blocks\Render;
4
5 use Jet_Form_Builder\Blocks\Button_Types\Button_Next;
6 use Jet_Form_Builder\Blocks\Dynamic_Value;
7 use Jet_Form_Builder\Blocks\Modules\Fields_Errors\Error_Handler;
8 use Jet_Form_Builder\Classes\Attributes_Trait;
9 use Jet_Form_Builder\Classes\Builder_Helper;
10 use Jet_Form_Builder\Classes\Get_Template_Trait;
11 use Jet_Form_Builder\Classes\Tools;
12 use Jet_Form_Builder\Live_Form;
13 use Jet_Form_Builder\Classes\Regexp_Tools;
14
15 // If this file is called directly, abort.
16
17 if ( ! defined( 'WPINC' ) ) {
18 die;
19 }
20
21 /**
22 * Define Base_Type class
23 */
24 abstract class Base {
25
26 use Attributes_Trait;
27 use Get_Template_Trait;
28
29 public $form_id;
30
31 /**
32 * @var \Jet_Form_Builder\Blocks\Types\Base
33 */
34 public $block_type;
35 public $content;
36
37 /**
38 * @var Live_Form
39 */
40 public $live_form;
41
42 /**
43 * @var array
44 */
45 public $args;
46
47 const FIELD_ERROR_CLASS = 'field-has-error';
48
49 public function __construct( $block_type ) {
50 $this->form_id = Live_Form::instance()->form_id;
51
52 $this->set_live_form();
53 $this->set_block_type( $block_type );
54 }
55
56 abstract public function get_name();
57
58 public function set_live_form() {
59 $this->live_form = Live_Form::instance();
60 }
61
62 public function set_block_type( $block_type ) {
63 $this->block_type = $block_type;
64 }
65
66 public function maybe_add_error_class( $args ) {
67 if ( $this->has_error( $args ) ) {
68 $this->add_attribute( 'class', self::FIELD_ERROR_CLASS );
69 }
70 }
71
72 public function maybe_render_error( $args ) {
73 if ( $this->has_error( $args ) ) {
74 return '<div class="error-message">' . Error_Handler::instance()->error_by_name( $args['name'] ) . '</div>';
75 }
76
77 return '';
78 }
79
80 public function maybe_get_error_class( $args ) {
81 if ( $this->has_error( $args ) ) {
82 return self::FIELD_ERROR_CLASS;
83 }
84
85 return '';
86 }
87
88 private function has_error( $args ) {
89 return Error_Handler::instance()->has_error_by_name( $args['name'] );
90 }
91
92
93 /**
94 * Returns field label
95 *
96 * @return [type] [description]
97 */
98 public function get_field_label() {
99 if ( empty( $this->block_type->block_attrs['label'] ) || ! $this->label_allowed() ) {
100 return '';
101 }
102
103 $args = $this->block_type->block_attrs;
104
105 $label_wrapper = new class() {
106 use Attributes_Trait;
107 };
108 $label_text = clone $label_wrapper;
109
110 if ( isset( $args['type'] ) && 'heading-field' === $args['type'] ) {
111 $label_wrapper->add_attribute( 'class', 'jet-form-builder__heading' );
112 $label_wrapper->add_attribute( 'class', $args['class_name'] );
113 } else {
114 $label_wrapper->add_attribute( 'class', 'jet-form-builder__label' );
115 }
116
117 $label_text->add_attribute( 'class', 'jet-form-builder__label-text' );
118 $label_text_tag = esc_attr( jet_fb_live_args()->fields_label_tag );
119
120 if ( 'label' === $label_text_tag ) {
121 $label_text->add_attribute( 'for', $this->block_type->get_field_id( $args, 'label' ) );
122 }
123
124 ob_start();
125
126 include $this->block_type->get_common_template( 'field-label.php' );
127
128 return ob_get_clean();
129
130 }
131
132 /**
133 * Returns field description
134 *
135 * @return [type] [description]
136 */
137 public function get_field_desc() {
138
139 ob_start();
140 if ( ! empty( $this->block_type->block_attrs['desc'] ) && $this->label_allowed() ) {
141 $args = $this->block_type->block_attrs;
142 include $this->block_type->get_common_template( 'field-description.php' );
143 }
144
145 return ob_get_clean();
146
147 }
148
149 /**
150 * Defines if this form control supports label
151 *
152 * @return [type] [description]
153 */
154 public function label_allowed() {
155 return true;
156 }
157
158 public function render_without_layout( $template = null, $args = array() ) {
159 $template_name = $this->get_name();
160
161 if ( empty( $args ) ) {
162 $args = $this->get_default_args_with_filter();
163 }
164
165 $this->args = $args;
166 $this->before_render( $args );
167
168 if ( is_null( $template ) ) {
169 $template = $this->block_type->get_field_template( $template_name . '.php' );
170 }
171
172 if ( Tools::is_readable( $template ) ) {
173 ob_start();
174 include $template;
175 $template = ob_get_clean();
176 }
177
178 return $template;
179 }
180
181 public function get_default_args() {
182 $defaults = array(
183 'default' => '',
184 'name' => '',
185 'placeholder' => '',
186 'required' => false,
187 );
188
189 $sanitized_args = array();
190
191 foreach ( $this->block_type->block_attrs as $key => $value ) {
192 $sanitized_args[ $key ] = $value;
193 }
194
195 return wp_parse_args( $sanitized_args, $defaults );
196 }
197
198 public function get_default_args_with_filter() {
199 $args = $this->get_default_args();
200
201 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
202 return apply_filters( "jet-form-builder/render/{$args['type']}", $args, $this );
203 }
204
205 public function before_render( $args ) {
206 }
207
208 /**
209 * @param null $wp_block
210 * @param null $template
211 *
212 * @return false|string
213 */
214 public function render( $wp_block = null, $template = null ) {
215 $args = $this->get_default_args_with_filter();
216 $template = $this->render_without_layout( $template, $args );
217
218 $this->maybe_add_error_class( $args );
219
220 $label = $this->get_field_label();
221 $desc = $this->get_field_desc();
222 $layout = $this->live_form ? $this->live_form->spec_data->fields_layout : 'column';
223
224 if ( 'column' === $layout ) {
225 ob_start();
226 include $this->block_type->get_common_template( 'field-column.php' );
227 $result_field = ob_get_clean();
228 } else {
229 ob_start();
230 include $this->block_type->get_common_template( 'field-row.php' );
231 $result_field = ob_get_clean();
232 }
233
234 return $result_field;
235 }
236
237 public function render_disabled_message_form_break( $args ) {
238 $format = '<div class="jet-form-builder__next-page-msg" %2$s>%1$s</div>';
239
240 if ( ! empty( $args['page_break_disabled'] ) && ! Tools::is_editor() ) {
241 $attrs = array(
242 sprintf(
243 'data-jfb-conditional="%s"',
244 Tools::esc_attr( array(
245 array(
246 'page_state' => 'active'
247 ),
248 ) )
249 ),
250 sprintf(
251 'data-jfb-func="%s"',
252 Tools::esc_attr( array(
253 'show' => array( 'dom' => true ),
254 ) )
255 ),
256 );
257
258 return sprintf( $format, $args['page_break_disabled'], implode( ' ', $attrs ) );
259
260 } elseif ( Tools::is_editor() ) {
261
262 $message = ! empty( $args['page_break_disabled'] ) ? $args['page_break_disabled'] : 'Disabled message';
263
264 return sprintf( $format, $message, $this->get_style_attrs( array( 'display:block' ) ) );
265 }
266
267 return '';
268 }
269
270 public function get_style_attrs( $style ) {
271 return 'style=' . implode( ';', $style );
272 }
273
274 /**
275 * Render custom form item template
276 *
277 * @param int|string $object_id Object ID.
278 * @param array $args Field arguments.
279 * @param bool|string $checked
280 *
281 * @return string
282 */
283 public function get_custom_template( $object_id, $args, $checked = false ) {
284 return ( new Builder_Helper() )->get_custom_template( $object_id, $args, $checked );
285 }
286
287 protected function set_value() {
288 if ( ! jet_form_builder()->regexp->has_macro( $this->args['default'] ) ) {
289 $this->add_attribute( 'value', $this->args['default'] );
290
291 return;
292 }
293 wp_enqueue_script( Dynamic_Value::HANDLE );
294
295 $this->add_attribute( 'data-value', $this->args['default'] );
296 }
297
298 }
299