PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.2
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.2
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / class-form.php

class-form.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.4.2, at includes/class-form.php

580 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The Form Class
5 *
6 * @since 1.1.0
7 */
8 class WeForms_Form {
9
10 /**
11 * The form id
12 *
13 * @var integer
14 */
15 public $id = 0;
16
17 /**
18 * The form title
19 *
20 * @var string
21 */
22 public $name = '';
23
24 /**
25 * Holds the post data object
26 *
27 * @var null|WP_post
28 */
29 public $data = null;
30
31 /**
32 * Form fields
33 *
34 * @var array
35 */
36 public $form_fields = array();
37
38 /**
39 * The Constructor
40 *
41 * @param int|WP_Post $form
42 */
43 public function __construct( $form = null ) {
44
45 if ( is_numeric( $form ) ) {
46
47 $the_post = get_post( $form );
48
49 if ( $the_post ) {
50 $this->id = $the_post->ID;
51 $this->name = $the_post->post_title;
52 $this->data = $the_post;
53 }
54
55 } elseif ( is_a( $form, 'WP_Post' ) ) {
56 $this->id = $form->ID;
57 $this->name = $form->post_title;
58 $this->data = $form;
59 }
60 }
61
62 /**
63 * Get the form ID
64 *
65 * @return integer
66 */
67 public function get_id() {
68 return $this->id;
69 }
70
71 /**
72 * Get the form name
73 *
74 * @return string
75 */
76 public function get_name() {
77 return $this->name;
78 }
79
80 /**
81 * Get all form fields of this form
82 *
83 * @return array
84 */
85 public function get_fields() {
86
87 // return if already fetched
88 if ( $this->form_fields ) {
89 return $this->form_fields;
90 }
91
92 $fields = get_children(array(
93 'post_parent' => $this->id,
94 'post_status' => 'publish',
95 'post_type' => 'wpuf_input',
96 'numberposts' => '-1',
97 'orderby' => 'menu_order',
98 'order' => 'ASC',
99 ));
100
101 $form_fields = array();
102
103 foreach ( $fields as $key => $content ) {
104
105 $field = maybe_unserialize( $content->post_content );
106
107 if ( empty( $field['template'] ) ) {
108 continue;
109 }
110
111
112 $field['id'] = $content->ID;
113
114 // Add inline property for radio and checkbox fields
115 $inline_supported_fields = apply_filters( 'inline_supported_fields_list', array( 'radio_field', 'checkbox_field' ) );
116 if ( in_array( $field['template'] , $inline_supported_fields ) ) {
117 if ( ! isset( $field['inline'] ) ) {
118 $field['inline'] = 'no';
119 }
120 }
121
122 // Add 'selected' property
123 $option_based_fields = apply_filters( 'option_based_fields_list', array( 'dropdown_field', 'multiple_select', 'radio_field', 'checkbox_field' ) );
124 if ( in_array( $field['template'] , $option_based_fields ) ) {
125 if ( ! isset( $field['selected'] ) ) {
126
127 if ( 'dropdown_field' === $field['template'] || 'radio_field' === $field['template'] ) {
128 $field['selected'] = '';
129 } else {
130 $field['selected'] = array();
131 }
132
133 }
134 }
135
136 // Add 'multiple' key for template:repeat
137 if ( 'repeat_field' === $field['template'] && ! isset( $field['multiple'] ) ) {
138 $field['multiple'] = '';
139 }
140
141 if ( 'recaptcha' === $field['template'] ) {
142 $field['name'] = 'recaptcha';
143 $field['enable_no_captcha'] = isset( $field['enable_no_captcha'] ) ? $field['enable_no_captcha'] : '';
144 $field['recaptcha_theme'] = isset( $field['recaptcha_theme'] ) ? $field['recaptcha_theme'] : 'light';
145 }
146
147 $form_fields[] = apply_filters( 'weforms-get-form-field', $field );
148 }
149
150 $this->form_fields = apply_filters( 'weforms-get-form-fields', $form_fields );
151
152 return $this->form_fields;
153 }
154
155 /**
156 * Check if any perticular field template is used in a form
157 *
158 * @return array
159 */
160 public function has_field( $field_template ) {
161
162 foreach ( $this->get_fields() as $key => $field) {
163 if ( isset( $field['template'] ) && $field['template'] == $field_template) {
164 return true;
165 }
166 }
167 }
168
169
170 /**
171 * Get all fields by a template
172 *
173 * @return array
174 */
175 public function search_fields( $field_template ) {
176
177 $fields = array();
178
179 foreach ( $this->get_fields() as $key => $field) {
180 if ( isset( $field['template'] ) && $field['template'] == $field_template) {
181 $fields[] = $field;
182 }
183 }
184
185 return $fields;
186 }
187
188 /**
189 * Search fileds and get the first one
190 *
191 * @return array
192 */
193 public function search_field( $field_template ) {
194
195 $fields = $this->search_fields($field_template);
196
197 return isset($fields[0]) ? $fields[0] : array();
198 }
199
200 /**
201 * Get formatted field name/values
202 *
203 * @return array
204 */
205 public function get_field_values() {
206 $values = array();
207 $fields = $this->get_fields();
208
209 if ( ! $fields ) {
210 return $values;
211 }
212
213 $ignore_fields = apply_filters( 'ignore_fields_list', array( 'recaptcha', 'section_break' ) );
214 $options_fields = apply_filters( 'option_fields_list', array( 'dropdown_field', 'radio_field', 'multiple_select', 'checkbox_field' ) );
215
216 foreach ($fields as $field) {
217
218 if ( in_array( $field['template'], $ignore_fields ) ) {
219 continue;
220 }
221
222 // Prepare column field data
223 if ( $field['template'] == 'column_field' ) {
224 $inner_columns = $field['inner_fields'];
225
226 if ( !empty( $inner_columns ) ) {
227 foreach ( $inner_columns as $column => $column_fields ) {
228
229 if ( !empty( $column_fields ) ) {
230 foreach ($column_fields as $field_obj) {
231 if ( in_array( $field_obj['template'], $ignore_fields ) ) {
232 continue;
233 }
234
235 if ( ! isset( $field_obj['name'] ) ) {
236 continue;
237 }
238
239 $field_value = array(
240 'label' => isset( $field_obj['label'] ) ? $field_obj['label'] : '',
241 'type' => $field_obj['template'],
242 );
243
244 // put options if this is an option field
245 if ( in_array( $field_obj['template'], $options_fields ) ) {
246 $field_value['options'] = $field_obj['options'];
247 }
248
249 $values[ $field_obj['name'] ] = array_merge( $field_obj, $field_value);
250 }
251 }
252
253 }
254 }
255 }
256
257 if ( ! isset( $field['name'] ) ) {
258 continue;
259 }
260
261 $value = array(
262 'label' => isset( $field['label'] ) ? $field['label'] : '',
263 'type' => $field['template'],
264 );
265
266 // put options if this is an option field
267 if ( in_array( $field['template'], $options_fields ) ) {
268 $value['options'] = $field['options'];
269 }
270
271 $values[ $field['name'] ] = array_merge( $field, $value);
272 }
273
274 return apply_filters( 'weforms_get_field_values', $values );;
275 }
276
277 /**
278 * Get form settings
279 *
280 * @return array
281 */
282 public function get_settings() {
283
284 $settings = get_post_meta( $this->id, 'wpuf_form_settings', true );
285 $default = weforms_get_default_form_settings();
286
287 return array_merge( $default, $settings);
288 }
289
290 /**
291 * Check if the form submission is open
292 *
293 * @return boolean|WP_Error
294 */
295 public function is_submission_open() {
296 $settings = $this->get_settings();
297
298 $needs_login = ( isset( $settings['require_login'] ) && $settings['require_login'] == 'true' ) ? true : false;
299 $has_limit = ( isset( $settings['limit_entries'] ) && $settings['limit_entries'] == 'true' ) ? true : false;
300 $is_scheduled = ( isset( $settings['schedule_form'] ) && $settings['schedule_form'] == 'true' ) ? true : false;
301
302 if ( $this->data->post_status != 'publish' ) {
303 return new WP_Error( 'needs-publish', __( 'The form is not published yet.', 'weforms' ) );
304 }
305
306 if ( $needs_login && !is_user_logged_in() ) {
307 return new WP_Error( 'needs-login', $settings['req_login_message'] );
308 }
309
310 if ( $has_limit ) {
311 $limit = (int) $settings['limit_number'];
312 $form_entries = $this->num_form_entries();
313
314 if ( $limit <= $form_entries ) {
315 return new WP_Error( 'entry-limit', $settings['limit_message'] );
316 }
317 }
318
319 if ( $is_scheduled ) {
320 $start_time = strtotime( $settings['schedule_start'] );
321 $end_time = strtotime( $settings['schedule_end'] );
322 $current_time = current_time( 'timestamp' );
323
324 // too early?
325 if ( $current_time < $start_time ) {
326 return new WP_Error( 'form-pending', $settings['sc_pending_message'] );
327 } elseif ( $current_time > $end_time ) {
328 return new WP_Error( 'form-expired', $settings['sc_expired_message'] );
329 }
330 }
331
332 return apply_filters( 'weforms_is_submission_open', true, $settings, $this);
333 }
334
335 /**
336 * Get form notifications
337 *
338 * @return array
339 */
340 public function get_notifications() {
341 $notifications = get_post_meta( $this->id, 'notifications', true );
342 $defualt = weforms_get_default_form_notification();
343
344 if ( ! $notifications ) {
345 $notifications = array();
346 }
347
348 $notifications = array_map( function( $notification ) use ( $defualt ) {
349 if( empty( $notification ) ) {
350 $notification = array();
351 }
352 return array_merge( $defualt, $notification);
353 }, $notifications);
354
355 return $notifications;
356 }
357
358 /**
359 * Get all the integrations
360 *
361 * @return array
362 */
363 public function get_integrations() {
364 $integrations = get_post_meta( $this->id, 'integrations', true );
365
366 if ( ! $integrations ) {
367 return array();
368 }
369
370 return $integrations;
371 }
372
373 /**
374 * Get entries of this form
375 *
376 * @return \WeForms_Form_Entry_Manager
377 */
378 public function entries() {
379 return new WeForms_Form_Entry_Manager( $this->id, $this );
380 }
381
382 /**
383 * Get number of form entries
384 *
385 * @return integer
386 */
387 public function num_form_entries() {
388 return weforms_count_form_entries( $this->id );
389 }
390
391 /**
392 * Get number of form payments
393 *
394 * @return integer
395 */
396 public function num_form_payments() {
397 return weforms_count_form_payments( $this->id );
398 }
399
400 /**
401 * Get form author details
402 *
403 * @return array
404 */
405 public function get_form_author_details() {
406 return weforms_get_form_author_details( $this->id );
407 }
408
409 /**
410 * Get the number of form views
411 *
412 * @return integer
413 */
414 public function num_form_views() {
415 return weforms_get_form_views( $this->id );
416 }
417
418
419 /**
420 * prepare_entries
421 *
422 * @return array
423 */
424 public function prepare_entries( $args = [] ) {
425
426 $fields = weforms()->fields->get_fields();
427 $form_fields = $this->get_fields();
428
429 $entry_fields = array();
430
431 $ignore_list = apply_filters('wefroms_entry_ignore_list', array(
432 'recaptcha', 'section_break','step_start'
433 ) );
434
435 foreach ($form_fields as $field) {
436
437 if ( in_array( $field['template'], $ignore_list ) ) {
438 continue;
439 }
440
441 if ( ! array_key_exists( $field['template'], $fields ) ) {
442 continue;
443 }
444
445 // Prepare column field data
446 if ( $field['template'] == 'column_field' ) {
447 $inner_columns = $field['inner_fields'];
448
449 if ( !empty( $inner_columns ) ) {
450 foreach ( $inner_columns as $column => $column_fields ) {
451
452 if ( !empty( $column_fields ) ) {
453 foreach ($column_fields as $field_obj) {
454 if ( in_array( $field_obj['template'], $ignore_list ) ) {
455 continue;
456 }
457
458 $fieldClass = $fields[ $field_obj['template'] ];
459 $entry_fields[ $field_obj['name'] ] = $fieldClass->prepare_entry( $field_obj );
460 }
461 }
462
463 }
464 }
465 }
466
467 $field_class = $fields[ $field['template'] ];
468
469 $entry_fields[ $field['name'] ] = $field_class->prepare_entry( $field, $args );
470 }
471
472 return apply_filters( 'weforms_prepare_entries', $entry_fields );
473 }
474
475 /**
476 * Check if the form submission is open On API
477 *
478 * @return boolean|WP_Error
479 */
480 public function is_api_form_submission_open() {
481 $response = array();
482 $settings = $this->get_settings();
483 $form_entries = $this->num_form_entries();
484 $needs_login = isset( $settings['require_login'] ) ? $settings['require_login'] : 'false';
485 $limit_entries = isset( $settings['limit_entries'] ) ? $settings['limit_entries'] : 'false';
486 $schedule_form = isset( $settings['schedule_form'] ) ? $settings['schedule_form'] : 'false';
487 $limit_number = isset( $settings['limit_number'] ) ? $settings['limit_number'] : '';
488 $schedule_start = isset( $settings['schedule_start'] ) ? $settings['schedule_start'] : '';
489 $schedule_end = isset( $settings['schedule_end'] ) ? $settings['schedule_end']: '';
490 $schedule_start_date = date_i18n( "M d, Y", strtotime( $schedule_start ));
491 $schedule_end_date = date_i18n( "M d, Y", strtotime( $schedule_end ));
492
493 if ( $this->data->post_status != 'publish' ) {
494 $response['type'] = __( 'Closed','weforms' );
495 }
496
497 if( $this->isFormStatusClosed( $settings, $form_entries ) ) {
498 $response['type'] = __( 'Closed','weforms' );
499 } else {
500 $response['type'] = __( 'Open', 'weforms');
501 }
502
503 if ( $limit_entries === 'true' ) {
504 if ( $schedule_form === 'true' && $this->isExpiredForm( $schedule_end ) ) {
505 $response['message'] = __( "Expired at {$schedule_end_date}", "weforms" );
506 } elseif ( $schedule_form === 'true' && $this->isPendingForm( $schedule_start ) ) {
507 $response['message'] = __( "Starts at {$schedule_start_date}", "weforms" );
508 } elseif( $form_entries >= $limit_number ) {
509 $response['message'] = __( "Reached maximum entry limit", "weforms" );
510 } else {
511 $remaining_entries = $limit_number - $form_entries;
512 $response['message'] = __("{$remaining_entries} entries remaining ", "weforms" );
513 }
514 } elseif( $schedule_form === 'true' ) {
515 if ( $this->isPendingForm( $schedule_start ) ) {
516 $response['message'] = __( "Starts at {$schedule_start_date}", "weforms" );
517 } elseif( $this->isExpiredForm( $schedule_end ) ) {
518 $response['message'] = __( "Expired at {$schedule_end_date}", "weforms");
519 } elseif( $this->isOpenForm( $schedule_start, $schedule_end ) ) {
520 $response['message'] = __( "Expires at {$schedule_end_date}", "weforms");
521 }
522 } elseif( $needs_login === 'true' ) {
523 $response['message'] =__( "Requires login", "weforms" );
524 }
525
526 return apply_filters( 'weforms_is_submission_open', $response, $settings, $this );
527 }
528
529
530 public function isPendingForm( $scheduleStart ) {
531 $currentTime = current_time( 'timestamp' );
532 $startTime = strtotime( $scheduleStart );
533
534 if ( $currentTime < $startTime ) {
535 return true;
536 }
537
538 return false;
539 }
540
541 public function isExpiredForm( $scheduleEnd ) {
542 $currentTime = current_time( 'timestamp' );
543 $endTime = strtotime( $scheduleEnd );
544
545 if ( $currentTime > $endTime ) {
546 return true;
547 }
548
549 return false;
550 }
551
552 public function isOpenForm ( $scheduleStart, $scheduleEnd ) {
553 $currentTime = current_time( 'timestamp' );
554 $startTime = strtotime( $scheduleStart );
555 $endTime = strtotime( $scheduleEnd );
556
557 if ( $currentTime > $startTime && $currentTime < $endTime ) {
558 return true;
559 }
560 return false;
561 }
562
563 public function isFormStatusClosed( $formSettings, $entries ) {
564
565 if ( $formSettings['schedule_form'] === 'true' && $this->isPendingForm( $formSettings['schedule_start'] ) ) {
566 return true;
567 }
568
569 if ( $formSettings['schedule_form'] === 'true' && $this->isExpiredForm( $formSettings['schedule_end'] ) ) {
570 return true;
571 }
572
573 if ( $formSettings['limit_entries'] === 'true' && $entries >= $formSettings['limit_number'] ) {
574 return true;
575 }
576
577 return false;
578 }
579 }
580