PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.6
JetFormBuilder — Dynamic Blocks Form Builder v3.6.6
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 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 All 134 releases
← All changes | includes/live-form.php +221 -366 1.1.0 → 3.6.6 View file →
@@ -1,366 +1,221 @@
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 - * Main file
18 - */
19 -class Live_Form {
20 -
21 - use Attributes_Trait;
22 - use Get_Template_Trait;
23 - use Instance_Trait;
24 -
25 - public $form_id = false;
26 - private $form = false;
27 -
28 -
29 - private $field_name;
30 - private $current_field_data;
31 - private $start_new_page = true;
32 - public $pages = 0;
33 - public $rendered_rows = 0;
34 -
35 - public $is_hidden_row;
36 - public $is_submit_row;
37 - public $is_page_break_row;
38 -
39 - public $current_repeater;
40 - public $current_repeater_i;
41 - public $preset;
42 - public $spec_data;
43 -
44 - public $page = 0;
45 - public $has_prev = false;
46 - public $post;
47 -
48 - // for progress
49 - public $form_breaks;
50 -
51 - /**
52 - * Create form instance
53 - *
54 - * @param [type] $form_id [description]
55 - */
56 - private function __construct() {
57 - $this->post = get_post();
58 - }
59 -
60 - public function set_form_id( $form_id ) {
61 - $this->form_id = $form_id;
62 -
63 - return $this;
64 - }
65 -
66 -
67 - public function set_repeater( $item, $count = false ) {
68 - $this->current_repeater = $item;
69 - $this->current_repeater_i = $count;
70 -
71 - return $this;
72 - }
73 -
74 - /**
75 - * It turns out the inheritance of such an image
76 - * Individual Form Arguments (if not) -> General Form Attributes (if not) -> Default Values
77 - *
78 - * @param array $attributes
79 - *
80 - * @return $this
81 - */
82 - public function set_specific_data_for_render( $attributes = array() ) {
83 - $jf_default_args = Plugin::instance()->post_type->get_default_args();
84 - $jf_args = Plugin::instance()->post_type->get_args( $this->form_id );
85 -
86 - $spec_data = array_diff( $jf_args, $jf_default_args );
87 - $this->spec_data = ( object ) array_merge( $jf_default_args, $attributes, $spec_data );;
88 -
89 - return $this;
90 - }
91 -
92 - public function is_not_field( $block ) {
93 - return Plugin::instance()->form->is_not_field( $block['blockName'] );
94 - }
95 -
96 - public function is_field( $block, $needle ) {
97 - return Plugin::instance()->form->is_field( $block['blockName'], $needle );
98 - }
99 -
100 - public function get_fields( $content ) {
101 - return Plugin::instance()->form->get_fields( $content );
102 - }
103 -
104 - /**
105 - * Setup fields prop
106 - *
107 - * @param $content
108 - */
109 - public function setup_fields( $content ) {
110 - $blocks = $this->get_fields( $content );
111 -
112 - foreach ( $blocks as $field ) {
113 - if ( $this->is_field( $field, 'form-break' ) ) {
114 - $this->pages ++;
115 - $this->form_breaks[] = Plugin::instance()->blocks->get_field_attrs( $field['blockName'], $field['attrs'] );
116 - }
117 - }
118 - if ( ! empty( $this->form_breaks ) ) {
119 - $this->form_breaks[] = array( 'label' => __( 'Last Page' ) );
120 - }
121 - }
122 -
123 - public function maybe_progress_pages() {
124 - if ( ! $this->spec_data->enable_progress || 0 === $this->pages ) {
125 - return '';
126 - }
127 -
128 - ob_start();
129 - include $this->get_global_template( 'common/progress-pages.php' );
130 -
131 - return ob_get_clean();
132 - }
133 -
134 - public function get_progress_item_class( $index ) {
135 - $classes = array( 'jet-form-builder-progress-pages__item--wrapper' );
136 -
137 - if ( $index === $this->page ) {
138 - $classes[] = 'active-page';
139 - } elseif ( -1 === $index ) {
140 - $classes[] = 'passed-page';
141 - }
142 -
143 - return implode( ' ', $classes );
144 - }
145 -
146 -
147 - public static function force_render_field( $name, $arguments = array() ) {
148 - if ( empty( $name ) ) {
149 - return;
150 - }
151 - $field = jet_form_builder()->blocks->get_field_by_name( $name );
152 -
153 - if ( ! $field instanceof Block_Type_Base ) {
154 - return;
155 - }
156 - $field->set_block_data( $arguments );
157 -
158 - return $field->get_block_renderer();
159 - }
160 -
161 - /**
162 - * Open form wrapper
163 - *
164 - * @param $field
165 - *
166 - * @return false|string [type] [description]
167 - */
168 - public function start_form_row( $field ) {
169 -
170 - if ( $this->is_field( $field, 'hidden' ) ) {
171 - return '';
172 - }
173 -
174 - if ( ! $this->is_hidden_row ) {
175 - $this->rendered_rows ++;
176 - }
177 -
178 - ob_start();
179 -
180 - do_action( 'jet-form-builder/before-start-form-row', $this );
181 -
182 - $this->add_attribute( 'class', 'jet-form-builder-row' );
183 -
184 - if ( $this->is_hidden_row ) {
185 - $this->add_attribute( 'class', 'jet-form-builder-row--hidden' );
186 - }
187 -
188 - if ( $this->is_field( $field, 'submit' ) ) {
189 - $this->add_attribute( 'class', 'jet-form-builder-row--submit' );
190 - }
191 -
192 - if ( $this->is_field( $field, 'form-break' ) ) {
193 - $this->add_attribute( 'class', 'jet-form-builder-row--page-break' );
194 - }
195 -
196 - if ( 1 === $this->rendered_rows ) {
197 - $this->add_attribute( 'class', 'jet-form-builder-row--first-visible' );
198 - }
199 -
200 - include $this->get_global_template( 'common/start-form-row.php' );
201 -
202 - do_action( 'jet-form-builder/after-start-form-row', $this );
203 -
204 - return ob_get_clean();
205 - }
206 -
207 - /**
208 - * Close form wrapper
209 - *
210 - * @param $field
211 - *
212 - * @return false|string [type] [description]
213 - */
214 - public function end_form_row( $field ) {
215 -
216 - if ( $this->is_field( $field, 'hidden' ) ) {
217 - return '';
218 - }
219 -
220 - ob_start();
221 - do_action( 'jet-form-builder/before-end-form-row', $this );
222 -
223 - include $this->get_global_template( 'common/end-form-row.php' );
224 -
225 - do_action( 'jet-form-builder/after-end-form-row', $this );
226 -
227 - return ob_get_clean();
228 - }
229 -
230 -
231 - /**
232 - * Maybe start new page
233 - *
234 - * @return false|string|void
235 - */
236 - public function maybe_start_page() {
237 -
238 - if ( 0 >= $this->pages ) {
239 - return '';
240 - }
241 -
242 - if ( false === $this->start_new_page ) {
243 - return '';
244 - }
245 -
246 - $this->start_new_page = false;
247 - $this->page ++;
248 -
249 - ob_start();
250 - do_action( 'jet-form-builder/before-page-start', $this );
251 -
252 - $hidden_class = '';
253 -
254 - if ( 1 < $this->page ) {
255 - $hidden_class = 'jet-form-page--hidden';
256 - }
257 -
258 - include $this->get_global_template( 'common/start-page.php' );
259 -
260 - do_action( 'jet-form-builder/after-page-start', $this );
261 -
262 - return ob_get_clean();
263 - }
264 -
265 - /**
266 - * Maybe start new page
267 - *
268 - * @param $is_last
269 - * @param $field
270 - *
271 - * @return false|string|void [type] [description]
272 - */
273 - public function maybe_end_page( $is_last = false, $field = false ) {
274 -
275 - if ( 0 >= $this->pages ) {
276 - return;
277 - }
278 -
279 - if ( ! $is_last && ! $this->is_field( $field, 'form-break' ) ) {
280 - return;
281 - }
282 -
283 - $this->start_new_page = true;
284 - $this->has_prev = true;
285 -
286 - ob_start();
287 - do_action( 'jet-form-builder/before-page-end', $this );
288 -
289 - include $this->get_global_template( 'common/end-page.php' );
290 -
291 - do_action( 'jet-form-builder/after-page-end', $this );
292 -
293 - return ob_get_clean();
294 - }
295 -
296 - /**
297 - * Returns true if field is visible
298 - *
299 - * @param $field
300 - *
301 - * @return boolean [description]
302 - */
303 - public function is_field_visible( $field ) {
304 -
305 - // For backward compatibility and hidden fields
306 - if ( empty( $field['visibility'] ) ) {
307 - return true;
308 - }
309 -
310 - // If is visible for all - show field
311 - if ( 'all' === $field['visibility'] ) {
312 - return true;
313 - }
314 -
315 - // If is visible for logged in users and user is logged in - show field
316 - if ( 'logged_id' === $field['visibility'] && is_user_logged_in() ) {
317 - return true;
318 - }
319 -
320 - // If is visible for not logged in users and user is not logged in - show field
321 - if ( 'not_logged_in' === $field['visibility'] && ! is_user_logged_in() ) {
322 - return true;
323 - }
324 -
325 - return false;
326 -
327 - }
328 -
329 - /**
330 - * Returns field ID with repeater prefix if needed
331 - */
332 - public function get_field_id( $name ) {
333 -
334 - if ( is_array( $name ) ) {
335 - $name = $name['name'];
336 - }
337 - //Find some solution for the repeater field
338 -
339 - if ( $this->current_repeater ) {
340 - $repeater_name = ! empty( $this->current_repeater['name'] ) ? $this->current_repeater['name'] : 'repeater';
341 - $index = ( false !== $this->current_repeater_i ) ? $this->current_repeater_i : '__i__';
342 - $name = sprintf( '%1$s_%2$s_%3$s', $repeater_name, $index, $name );
343 - }
344 -
345 - return $name;
346 - }
347 -
348 - /**
349 - * Returns field name with repeater prefix if needed
350 - */
351 - public function get_field_name( $name ) {
352 -
353 - //Find some solution for the repeater field
354 - if ( $this->current_repeater ) {
355 - $repeater_name = ! empty( $this->current_repeater['name'] ) ? $this->current_repeater['name'] : '_undefined_repeater_name';
356 - $index = ( false !== $this->current_repeater_i ) ? $this->current_repeater_i : '__i__';
357 -
358 - $name = sprintf( '%1$s[%2$s][%3$s]', $repeater_name, $index, $name );
359 - }
360 -
361 - return $name;
362 -
363 - }
364 -
365 -
366 -}
1 +<?php
2 +
3 +namespace Jet_Form_Builder;
4 +
5 +use Jet_Form_Builder\Blocks\Block_Helper;
6 +use Jet_Form_Builder\Blocks\Types\Base as Block_Type_Base;
7 +use Jet_Form_Builder\Classes\Arguments\Form_Arguments;
8 +use Jet_Form_Builder\Classes\Attributes_Trait;
9 +use Jet_Form_Builder\Classes\Get_Template_Trait;
10 +use Jet_Form_Builder\Classes\Instance_Trait;
11 +
12 +// If this file is called directly, abort.
13 +if ( ! defined( 'WPINC' ) ) {
14 + die;
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 + /**
30 + * @var bool|int
31 + */
32 + public $form_id = false;
33 + /** @var Form_Arguments */
34 + public $spec_data;
35 + // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
36 + public $_conditional_blocks = array();
37 + public $blocks = array();
38 + // phpcs:enable PSR2.Classes.PropertyDeclaration.Underscore
39 +
40 + /**
41 + * @var Form_Break
42 + */
43 + public $form_break;
44 + public $field_names = array();
45 +
46 + private $ajax_post_id;
47 +
48 + /**
49 + * Create form instance
50 + *
51 + * @param [type] $form_id [description]
52 + */
53 + private function __construct() {
54 + $this->spec_data = new Form_Arguments();
55 +
56 + add_filter(
57 + 'jet-form-builder/setup-blocks',
58 + array( $this, 'remove_extra_page_breaks' )
59 + );
60 + add_filter(
61 + 'jet-form-builder/after-end-form',
62 + array( self::class, 'on_end_render_form' )
63 + );
64 + }
65 +
66 + public static function on_end_render_form( string $content ): string {
67 + self::clear();
68 +
69 + return $content;
70 + }
71 +
72 + public static function clear() {
73 + remove_filter(
74 + 'jet-form-builder/setup-blocks',
75 + array( self::$instance, 'remove_extra_page_breaks' )
76 + );
77 + remove_filter(
78 + 'jet-form-builder/after-end-form',
79 + array( self::class, 'on_end_render_form' )
80 + );
81 +
82 + self::$instance = null;
83 + }
84 +
85 + public function set_form_id( $form_id ) {
86 + $this->form_id = $form_id;
87 +
88 + return $this;
89 + }
90 +
91 + /**
92 + * @param array $incoming_attributes
93 + *
94 + * @return $this
95 + */
96 + public function set_specific_data_for_render( $incoming_attributes = array() ) {
97 + $this->spec_data
98 + ->set_form_id( $this->form_id )
99 + ->load( $incoming_attributes );
100 +
101 + return $this;
102 + }
103 +
104 + /**
105 + * Setup fields prop
106 + *
107 + * @return array[]
108 + */
109 + public function setup_fields(): array {
110 + $this->blocks = apply_filters(
111 + 'jet-form-builder/setup-blocks',
112 + Block_Helper::get_blocks_by_post( $this->form_id, true, true )
113 + );
114 +
115 + jet_form_builder()->regexp->set_field_names(
116 + Block_Helper::get_form_field_names( $this->blocks )
117 + );
118 +
119 + return $this->blocks;
120 + }
121 +
122 + public function remove_extra_page_breaks( array $blocks ) {
123 + return $this->get_form_break()->set_pages( $blocks );
124 + }
125 +
126 + public function maybe_progress_pages() {
127 + if ( ! $this->spec_data->enable_progress || 0 === $this->get_form_break()->get_pages() ) {
128 + return '';
129 + }
130 +
131 + return $this->get_form_break()->render_progress(
132 + 'default',
133 + array(
134 + 'jet-form-builder-progress-pages--global',
135 + )
136 + );
137 + }
138 +
139 +
140 + public static function force_render_field( $name, $arguments = array() ) {
141 + if ( empty( $name ) ) {
142 + return '';
143 + }
144 + $field = jet_form_builder()->blocks->get_field_by_name( $name );
145 +
146 + if ( ! $field instanceof Block_Type_Base ) {
147 + return '';
148 + }
149 + $arguments['_static_value'] = true;
150 +
151 + $field->set_block_data( $arguments );
152 +
153 + return $field->get_block_renderer();
154 + }
155 +
156 +
157 + /**
158 + * Maybe start new page
159 + *
160 + * @param bool $force_first
161 + *
162 + * @return false|string|void
163 + */
164 + public function maybe_start_page( $force_first = false ) {
165 + return $this->get_form_break()->maybe_start_page( $force_first );
166 + }
167 +
168 + /**
169 + * Maybe start new page
170 + *
171 + * @param $is_last
172 + * @param $field
173 + *
174 + * @return false|string|void [type] [description]
175 + */
176 + public function maybe_end_page( $is_last = false, $field = false ) {
177 + return $this->get_form_break()->maybe_end_page( $is_last, $field );
178 + }
179 +
180 + /**
181 + * @param string $name
182 + *
183 + * @return Form_Break
184 + */
185 + public function get_form_break( $name = '' ) {
186 + if ( ! $name && ! $this->form_break ) {
187 + $this->form_break = new Form_Break();
188 + }
189 + if ( $name && ! $this->isset_form_break( $name ) ) {
190 + $this->_conditional_blocks[ $name ] = array( 'break' => new Form_Break() );
191 + }
192 +
193 + return $name ? $this->_conditional_blocks[ $name ]['break'] : $this->form_break;
194 + }
195 +
196 + public function isset_form_break( $name ) {
197 + return isset( $this->_conditional_blocks[ $name ] );
198 + }
199 +
200 + public function __get( string $name ) {
201 + switch ( $name ) {
202 + case 'post':
203 + return $this->current_post();
204 + default:
205 + return null;
206 + }
207 + }
208 +
209 + private function current_post() {
210 + global $post;
211 +
212 + if ( wp_doing_ajax() && empty( $post->ID ) && ! $this->ajax_post_id ) {
213 + $url = wp_get_referer();
214 +
215 + $this->ajax_post_id = url_to_postid( $url );
216 + }
217 +
218 + return $this->ajax_post_id ? get_post( $this->ajax_post_id ) : $post;
219 + }
220 +
221 +}