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