PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.6
JetFormBuilder — Dynamic Blocks Form Builder v1.2.6
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 / live-form.php
jetformbuilder / includes Last commit date
actions 4 years ago admin 4 years ago blocks 4 years ago classes 4 years ago compatibility 4 years ago dev-mode 4 years ago exceptions 4 years ago form-actions 4 years ago form-messages 4 years ago form-patterns 4 years ago form-response 4 years ago gateways 4 years ago generators 4 years ago integrations 4 years ago license 4 years ago presets 4 years ago request 4 years ago shortcodes 4 years ago widgets 4 years ago autoloader.php 4 years ago file-upload.php 4 years ago form-handler.php 4 years ago form-manager.php 4 years ago live-form.php 4 years ago plugin.php 4 years ago post-type.php 4 years ago
live-form.php
395 lines
1 <?php
2
3 namespace Jet_Form_Builder;
4
5 use Jet_Form_Builder\Blocks\Types\Base as Block_Type_Base;
6 use Jet_Form_Builder\Classes\Attributes_Trait;
7 use Jet_Form_Builder\Classes\Get_Template_Trait;
8 use Jet_Form_Builder\Classes\Instance_Trait;
9 use Jet_Form_Builder\Classes\Tools;
10
11 // If this file is called directly, abort.
12 if ( ! defined( 'WPINC' ) ) {
13 die;
14 }
15
16
17 /**
18 * @method static Live_Form instance()
19 *
20 * Class Live_Form
21 * @package Jet_Form_Builder
22 */
23 class Live_Form {
24
25 use Attributes_Trait;
26 use Get_Template_Trait;
27 use Instance_Trait;
28
29 public $form_id = false;
30 private $form = false;
31
32
33 private $field_name;
34 private $current_field_data;
35 private $start_new_page = true;
36 public $pages = 0;
37 public $rendered_rows = 0;
38
39 public $is_hidden_row;
40 public $is_submit_row;
41 public $is_page_break_row;
42
43 public $current_repeater;
44 public $current_repeater_i;
45 public $preset;
46 public $spec_data;
47
48 public $page = 0;
49 public $has_prev = false;
50 public $post;
51
52 // for progress
53 public $form_breaks;
54
55 /**
56 * Create form instance
57 *
58 * @param [type] $form_id [description]
59 */
60 private function __construct() {
61 $this->post = get_post();
62 }
63
64 public function set_form_id( $form_id ) {
65 $this->form_id = $form_id;
66
67 return $this;
68 }
69
70
71 public function set_repeater( $item, $count = false ) {
72 $this->current_repeater = $item;
73 $this->current_repeater_i = $count;
74
75 return $this;
76 }
77
78 /**
79 * It turns out the inheritance of such an image
80 * Incoming Form Attributes (from block/widget/shortcode)
81 * (if not) -> Individual Form Arguments
82 * (if not) -> Default PHP Values
83 *
84 * @param array $incoming_attributes
85 *
86 * @return $this
87 */
88 public function set_specific_data_for_render( $incoming_attributes = array() ) {
89 $jf_default_args = Plugin::instance()->post_type->get_default_args();
90 $jf_args = Plugin::instance()->post_type->get_args( $this->form_id );
91
92 $attributes_from_post_type = array_diff( $jf_args, $jf_default_args );
93
94 $render_attributes = array_merge( ...apply_filters(
95 'jet-form-builder/form-render/attributes',
96 array( $attributes_from_post_type, $incoming_attributes )
97 ) );
98 $this->spec_data = ( object ) array_merge( $jf_default_args, $render_attributes );
99
100 return $this;
101 }
102
103 public function is_not_field( $block ) {
104 return Plugin::instance()->form->is_not_field( $block['blockName'] );
105 }
106
107 public function is_field( $block, $needle ) {
108 return Plugin::instance()->form->is_field( $block['blockName'], $needle );
109 }
110
111 public function get_fields( $content ) {
112 return Plugin::instance()->form->get_fields( $content );
113 }
114
115 /**
116 * Setup fields prop
117 *
118 * @param $content
119 */
120 public function setup_fields( $content ) {
121 $blocks = parse_blocks( $content );
122 $count_blocks = count( $blocks );
123 $break_after_submit = false;
124
125 foreach ( $blocks as $index => $field ) {
126 if ( $this->is_field( $field, 'form-break' ) ) {
127 $form_break = Plugin::instance()->blocks->get_field_attrs( $field['blockName'], $field['attrs'] );
128
129 if ( $index + 1 === $count_blocks ) {
130 $break_after_submit = $form_break;
131 unset( $blocks[ $index ] );
132 continue;
133 }
134
135 $this->pages ++;
136 $this->form_breaks[] = $form_break;
137 }
138 }
139 if ( ! empty( $this->form_breaks ) ) {
140 $this->form_breaks[] = $break_after_submit ? $break_after_submit : array( 'label' => __( 'Last Page' ) );
141 }
142
143 return $blocks;
144 }
145
146 public function maybe_progress_pages() {
147 if ( ! $this->spec_data->enable_progress || 0 === $this->pages ) {
148 return '';
149 }
150
151 ob_start();
152 include $this->get_global_template( 'common/progress-pages.php' );
153
154 return ob_get_clean();
155 }
156
157 public function get_progress_item_class( $index ) {
158 $classes = array( 'jet-form-builder-progress-pages__item--wrapper' );
159
160 if ( $index === $this->page ) {
161 $classes[] = 'active-page';
162 } elseif ( - 1 === $index ) {
163 $classes[] = 'passed-page';
164 }
165
166 return implode( ' ', $classes );
167 }
168
169
170 public static function force_render_field( $name, $arguments = array() ) {
171 if ( empty( $name ) ) {
172 return;
173 }
174 $field = jet_form_builder()->blocks->get_field_by_name( $name );
175
176 if ( ! $field instanceof Block_Type_Base ) {
177 return;
178 }
179 $field->set_block_data( $arguments );
180
181 return $field->get_block_renderer();
182 }
183
184 /**
185 * Open form wrapper
186 *
187 * @param $field
188 *
189 * @return false|string [type] [description]
190 */
191 public function start_form_row( $field ) {
192
193 if ( $this->is_field( $field, 'hidden' ) ) {
194 return '';
195 }
196
197 if ( ! $this->is_hidden_row ) {
198 $this->rendered_rows ++;
199 }
200
201 ob_start();
202
203 do_action( 'jet-form-builder/before-start-form-row', $this );
204
205 $this->add_attribute( 'class', 'jet-form-builder-row' );
206 $this->add_attribute( 'class', 'field-type-' . $field['type'] );
207
208 if ( $this->is_hidden_row ) {
209 $this->add_attribute( 'class', 'jet-form-builder-row--hidden' );
210 }
211
212 if ( $this->is_field( $field, 'submit' ) ) {
213 $this->add_attribute( 'class', 'jet-form-builder-row--submit' );
214 }
215
216 if ( $this->is_field( $field, 'form-break' ) ) {
217 $this->add_attribute( 'class', 'jet-form-builder-row--page-break' );
218 }
219
220 if ( 1 === $this->rendered_rows ) {
221 $this->add_attribute( 'class', 'jet-form-builder-row--first-visible' );
222 }
223
224 include $this->get_global_template( 'common/start-form-row.php' );
225
226 do_action( 'jet-form-builder/after-start-form-row', $this );
227
228 return ob_get_clean();
229 }
230
231 /**
232 * Close form wrapper
233 *
234 * @param $field
235 *
236 * @return false|string [type] [description]
237 */
238 public function end_form_row( $field ) {
239
240 if ( $this->is_field( $field, 'hidden' ) ) {
241 return '';
242 }
243
244 ob_start();
245 do_action( 'jet-form-builder/before-end-form-row', $this );
246
247 include $this->get_global_template( 'common/end-form-row.php' );
248
249 do_action( 'jet-form-builder/after-end-form-row', $this );
250
251 return ob_get_clean();
252 }
253
254
255 /**
256 * Maybe start new page
257 *
258 * @return false|string|void
259 */
260 public function maybe_start_page() {
261
262 if ( 0 >= $this->pages ) {
263 return '';
264 }
265
266 if ( false === $this->start_new_page ) {
267 return '';
268 }
269
270 $this->start_new_page = false;
271 $this->page ++;
272
273 ob_start();
274 do_action( 'jet-form-builder/before-page-start', $this );
275
276 $hidden_class = '';
277
278 if ( 1 < $this->page ) {
279 $hidden_class = 'jet-form-builder-page--hidden';
280 }
281
282 include $this->get_global_template( 'common/start-page.php' );
283
284 do_action( 'jet-form-builder/after-page-start', $this );
285
286 return ob_get_clean();
287 }
288
289 /**
290 * Maybe start new page
291 *
292 * @param $is_last
293 * @param $field
294 *
295 * @return false|string|void [type] [description]
296 */
297 public function maybe_end_page( $is_last = false, $field = false ) {
298
299 if ( 0 >= $this->pages ) {
300 return;
301 }
302
303 if ( ! $is_last && ! $this->is_field( $field, 'form-break' ) ) {
304 return;
305 }
306
307 $this->start_new_page = true;
308 $this->has_prev = true;
309
310 ob_start();
311 do_action( 'jet-form-builder/before-page-end', $this );
312
313 include $this->get_global_template( 'common/end-page.php' );
314
315 do_action( 'jet-form-builder/after-page-end', $this );
316
317 return ob_get_clean();
318 }
319
320 /**
321 * Returns true if field is visible
322 *
323 * @param $field
324 *
325 * @return boolean [description]
326 */
327 public function is_field_visible( $field ) {
328
329 // For backward compatibility and hidden fields
330 if ( empty( $field['visibility'] ) ) {
331 return true;
332 }
333
334 // If is visible for all - show field
335 if ( 'all' === $field['visibility'] ) {
336 return true;
337 }
338
339 // If is visible for logged in users and user is logged in - show field
340 if ( 'logged_id' === $field['visibility'] && is_user_logged_in() ) {
341 return true;
342 }
343
344 // If is visible for not logged in users and user is not logged in - show field
345 if ( 'not_logged_in' === $field['visibility'] && ! is_user_logged_in() ) {
346 return true;
347 }
348
349 return false;
350
351 }
352
353 /**
354 * Returns field ID with repeater prefix if needed
355 */
356 public function get_field_id( $name ) {
357
358 if ( is_array( $name ) ) {
359 $name = $name['name'];
360 }
361 //Find some solution for the repeater field
362
363 if ( $this->current_repeater ) {
364 $repeater_name = ! empty( $this->current_repeater['name'] ) ? $this->current_repeater['name'] : 'repeater';
365 $index = ( false !== $this->current_repeater_i ) ? $this->current_repeater_i : '__i__';
366 $name = sprintf( '%1$s_%2$s_%3$s', $repeater_name, $index, $name );
367 }
368
369 return $name;
370 }
371
372 /**
373 * Returns field name with repeater prefix if needed
374 */
375 public function get_field_name( $name ) {
376
377 //Find some solution for the repeater field
378 if ( $this->current_repeater ) {
379 $repeater_name = ! empty( $this->current_repeater['name'] ) ? $this->current_repeater['name'] : '_undefined_repeater_name';
380 $index = ( false !== $this->current_repeater_i ) ? $this->current_repeater_i : '__i__';
381
382 $name = sprintf( '%1$s[%2$s][%3$s]', $repeater_name, $index, $name );
383 }
384
385 return $name;
386
387 }
388
389 public function get_nonce_id() {
390 return "jet-form-builder-wp-nonce-{$this->form_id}";
391 }
392
393
394 }
395