PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.01.02
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.01.02
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmEntriesHelper.php

FrmEntriesHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 4.01.02, at classes/helpers/FrmEntriesHelper.php

658 lines 20.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmEntriesHelper {
7
8 public static function setup_new_vars( $fields, $form = '', $reset = false, $args = array() ) {
9 $values = array(
10 'name' => '',
11 'description' => '',
12 'item_key' => '',
13 );
14
15 $values['fields'] = array();
16 if ( empty( $fields ) ) {
17 return apply_filters( 'frm_setup_new_entry', $values );
18 }
19
20 foreach ( (array) $fields as $field ) {
21 $original_default = $field->default_value;
22 self::prepare_field_default_value( $field );
23 $new_value = self::get_field_value_for_new_entry( $field, $reset, $args );
24
25 $field_array = FrmAppHelper::start_field_array( $field );
26 $field_array['value'] = $new_value;
27 $field_array['type'] = apply_filters( 'frm_field_type', $field->type, $field, $new_value );
28 $field_array['parent_form_id'] = isset( $args['parent_form_id'] ) ? $args['parent_form_id'] : $field->form_id;
29 $field_array['reset_value'] = $reset;
30 $field_array['in_embed_form'] = isset( $args['in_embed_form'] ) ? $args['in_embed_form'] : '0';
31 $field_array['original_default'] = $original_default;
32
33 FrmFieldsHelper::prepare_new_front_field( $field_array, $field, $args );
34
35 $field_array = array_merge( $field->field_options, $field_array );
36
37 $values['fields'][] = $field_array;
38
39 if ( ! $form || ! isset( $form->id ) ) {
40 $form = FrmForm::getOne( $field->form_id );
41 }
42 }
43
44 $form->options = maybe_unserialize( $form->options );
45 if ( is_array( $form->options ) ) {
46 $values = array_merge( $values, $form->options );
47 }
48
49 $form_defaults = FrmFormsHelper::get_default_opts();
50 $frm_settings = FrmAppHelper::get_settings();
51
52 $form_defaults['custom_style'] = ( $frm_settings->load_style != 'none' );
53
54 $values = array_merge( $form_defaults, $values );
55
56 return apply_filters( 'frm_setup_new_entry', $values );
57 }
58
59 /**
60 * @since 2.05
61 *
62 * @param object $field
63 */
64 private static function prepare_field_default_value( &$field ) {
65 //If checkbox, multi-select dropdown, or checkbox data from entries field, the value should be an array
66 $return_array = FrmField::is_field_with_multiple_values( $field );
67
68 /**
69 * Do any shortcodes in default value and allow customization of default value.
70 * Calls FrmProFieldsHelper::get_default_value
71 */
72 $field->default_value = apply_filters( 'frm_get_default_value', $field->default_value, $field, true, $return_array );
73
74 if ( isset( $field->field_options['placeholder'] ) ) {
75 $field->field_options['placeholder'] = apply_filters( 'frm_get_default_value', $field->field_options['placeholder'], $field, false, false );
76 }
77 }
78
79 /**
80 * Set the value for each field
81 * This function is used when the form is first loaded and on all page turns *for a new entry*
82 *
83 * @since 2.0.13
84 *
85 * @param object $field - this is passed by reference since it is an object
86 * @param boolean $reset
87 * @param array $args
88 *
89 * @return string|array $new_value
90 */
91 private static function get_field_value_for_new_entry( $field, $reset, $args ) {
92 $new_value = $field->default_value;
93
94 if ( ! $reset && self::value_is_posted( $field, $args ) ) {
95 self::get_posted_value( $field, $new_value, $args );
96 }
97
98 if ( ! is_array( $new_value ) ) {
99 $new_value = str_replace( '"', '&quot;', $new_value );
100 }
101
102 return $new_value;
103 }
104
105 /**
106 * Check if a field has a posted value
107 *
108 * @since 2.01.0
109 *
110 * @param object $field
111 * @param array $args
112 *
113 * @return boolean $value_is_posted
114 */
115 public static function value_is_posted( $field, $args ) {
116 $value_is_posted = false;
117 if ( $_POST ) {
118 $repeating = isset( $args['repeating'] ) && $args['repeating'];
119 if ( $repeating ) {
120 if ( isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field->id ] ) ) {
121 $value_is_posted = true;
122 }
123 } elseif ( isset( $_POST['item_meta'][ $field->id ] ) ) {
124 $value_is_posted = true;
125 }
126 }
127
128 return $value_is_posted;
129 }
130
131 public static function setup_edit_vars( $values, $record ) {
132 $values['item_key'] = FrmAppHelper::get_post_param( 'item_key', $record->item_key, 'sanitize_title' );
133 $values['form_id'] = $record->form_id;
134 $values['is_draft'] = $record->is_draft;
135
136 return apply_filters( 'frm_setup_edit_entry_vars', $values, $record );
137 }
138
139 public static function replace_default_message( $message, $atts ) {
140 if ( strpos( $message, '[default-message' ) === false &&
141 strpos( $message, '[default_message' ) === false &&
142 ! empty( $message ) ) {
143 return $message;
144 }
145
146 if ( empty( $message ) ) {
147 $message = '[default-message]';
148 }
149
150 preg_match_all( "/\[(default-message|default_message)\b(.*?)(?:(\/))?\]/s", $message, $shortcodes, PREG_PATTERN_ORDER );
151
152 foreach ( $shortcodes[0] as $short_key => $tag ) {
153 $add_atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] );
154 if ( ! empty( $add_atts ) ) {
155 $this_atts = array_merge( $atts, $add_atts );
156 } else {
157 $this_atts = $atts;
158 }
159
160 $default = FrmEntriesController::show_entry_shortcode( $this_atts );
161
162 // Add the default message.
163 $message = str_replace( $shortcodes[0][ $short_key ], $default, $message );
164 }
165
166 return $message;
167 }
168
169 public static function prepare_display_value( $entry, $field, $atts ) {
170 $field_value = isset( $entry->metas[ $field->id ] ) ? $entry->metas[ $field->id ] : false;
171
172 if ( FrmAppHelper::pro_is_installed() ) {
173 FrmProEntriesHelper::get_dynamic_list_values( $field, $entry, $field_value );
174 }
175
176 if ( $field->form_id == $entry->form_id || empty( $atts['embedded_field_id'] ) ) {
177 return self::display_value( $field_value, $field, $atts );
178 }
179
180 // This is an embeded form.
181 $val = '';
182
183 if ( strpos( $atts['embedded_field_id'], 'form' ) === 0 ) {
184 // This is a repeating section.
185 $child_entries = FrmEntry::getAll( array( 'it.parent_item_id' => $entry->id ) );
186 } else {
187 // Get all values for this field.
188 $child_values = isset( $entry->metas[ $atts['embedded_field_id'] ] ) ? $entry->metas[ $atts['embedded_field_id'] ] : false;
189
190 if ( $child_values ) {
191 $child_entries = FrmEntry::getAll( array( 'it.id' => (array) $child_values ) );
192 }
193 }
194
195 $field_value = array();
196
197 if ( ! isset( $child_entries ) || ! $child_entries || ! FrmAppHelper::pro_is_installed() ) {
198 return $val;
199 }
200
201 foreach ( $child_entries as $child_entry ) {
202 $atts['item_id'] = $child_entry->id;
203 $atts['post_id'] = $child_entry->post_id;
204
205 // Fet the value for this field -- check for post values as well.
206 $entry_val = FrmProEntryMetaHelper::get_post_or_meta_value( $child_entry, $field );
207
208 if ( $entry_val ) {
209 // foreach entry get display_value.
210 $field_value[] = self::display_value( $entry_val, $field, $atts );
211 }
212
213 unset( $child_entry );
214 }
215
216 $val = implode( ', ', (array) $field_value );
217
218 return FrmAppHelper::kses( $val, 'all' );
219 }
220
221 /**
222 * Prepare the saved value for display
223 *
224 * @param array|string $value
225 * @param object $field
226 * @param array $atts
227 *
228 * @return string
229 */
230 public static function display_value( $value, $field, $atts = array() ) {
231
232 $defaults = array(
233 'type' => '',
234 'html' => false,
235 'show_filename' => true,
236 'truncate' => false,
237 'sep' => ', ',
238 'post_id' => 0,
239 'form_id' => $field->form_id,
240 'field' => $field,
241 'keepjs' => 0,
242 'return_array' => false,
243 );
244
245 $atts = wp_parse_args( $atts, $defaults );
246
247 if ( FrmField::is_image( $field ) || $field->type == 'star' ) {
248 $atts['truncate'] = false;
249 $atts['html'] = true;
250 }
251
252 $atts = apply_filters( 'frm_display_value_atts', $atts, $field, $value );
253
254 if ( ! isset( $field->field_options['post_field'] ) ) {
255 $field->field_options['post_field'] = '';
256 }
257
258 if ( ! isset( $field->field_options['custom_field'] ) ) {
259 $field->field_options['custom_field'] = '';
260 }
261
262 if ( FrmAppHelper::pro_is_installed() && $atts['post_id'] && ( $field->field_options['post_field'] || $atts['type'] == 'tag' ) ) {
263 $atts['pre_truncate'] = $atts['truncate'];
264 $atts['truncate'] = true;
265 $atts['exclude_cat'] = isset( $field->field_options['exclude_cat'] ) ? $field->field_options['exclude_cat'] : 0;
266
267 $value = FrmProEntryMetaHelper::get_post_value( $atts['post_id'], $field->field_options['post_field'], $field->field_options['custom_field'], $atts );
268 $atts['truncate'] = $atts['pre_truncate'];
269 }
270
271 if ( $value == '' ) {
272 return $value;
273 }
274
275 $unfiltered_value = maybe_unserialize( $value );
276
277 $value = apply_filters( 'frm_display_value_custom', $unfiltered_value, $field, $atts );
278 $value = apply_filters( 'frm_display_' . $field->type . '_value_custom', $value, compact( 'field', 'atts' ) );
279
280 if ( $value == $unfiltered_value ) {
281 $value = FrmFieldsHelper::get_unfiltered_display_value( compact( 'value', 'field', 'atts' ) );
282 }
283
284 if ( $atts['truncate'] && $atts['type'] != 'url' ) {
285 $value = FrmAppHelper::truncate( $value, 50 );
286 }
287
288 if ( ! $atts['keepjs'] && ! is_array( $value ) ) {
289 $value = FrmAppHelper::kses( $value, 'all' );
290 }
291
292 return apply_filters( 'frm_display_value', $value, $field, $atts );
293 }
294
295 public static function set_posted_value( $field, $value, $args ) {
296 // If validating a field with "other" opt, set back to prev value now.
297 if ( isset( $args['other'] ) && $args['other'] ) {
298 $value = $args['temp_value'];
299 }
300 if ( empty( $args['parent_field_id'] ) ) {
301 $_POST['item_meta'][ $field->id ] = $value;
302 } else {
303 self::set_parent_field_posted_value( $field, $value, $args );
304 }
305 }
306
307 /**
308 * Init arrays if necessary, else we get fatal error.
309 *
310 * @since 4.01
311 */
312 private static function set_parent_field_posted_value( $field, $value, $args ) {
313 if ( isset( $_POST['item_meta'][ $args['parent_field_id'] ] ) &&
314 is_array( $_POST['item_meta'][ $args['parent_field_id'] ] ) ) {
315
316 if ( ! isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] ) ||
317 ! is_array( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] ) ) {
318 $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] = array();
319 }
320 } else {
321 // All of the section was probably removed.
322 $_POST['item_meta'][ $args['parent_field_id'] ] = array();
323 $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] = array();
324 }
325
326 $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field->id ] = $value;
327 }
328
329 public static function get_posted_value( $field, &$value, $args ) {
330 $field_id = is_object( $field ) ? $field->id : $field;
331
332 if ( empty( $args['parent_field_id'] ) ) {
333 $value = isset( $_POST['item_meta'][ $field_id ] ) ? wp_unslash( $_POST['item_meta'][ $field_id ] ) : '';
334 } else {
335 $value = isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] ) ? wp_unslash( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] ) : '';
336 }
337
338 if ( is_array( $field ) ) {
339 $field_obj = FrmFieldFactory::get_field_object( $field['id'] );
340 } elseif ( is_object( $field ) || is_numeric( $field ) ) {
341 $field_obj = FrmFieldFactory::get_field_object( $field );
342 } else {
343 return;
344 }
345
346 $field_obj->sanitize_value( $value );
347 }
348
349 /**
350 * Check if field has an "Other" option and if any other values are posted
351 *
352 * @since 2.0
353 *
354 * @param object $field
355 * @param string|array $value
356 * @param array $args
357 */
358 public static function maybe_set_other_validation( $field, &$value, &$args ) {
359 $args['other'] = false;
360 if ( ! $value || empty( $value ) || ! FrmAppHelper::pro_is_installed() ) {
361 return;
362 }
363
364 // Get other value for fields in repeating section.
365 self::set_other_repeating_vals( $field, $value, $args );
366
367 // Check if there are any posted "Other" values.
368 if ( FrmField::is_option_true( $field, 'other' ) && isset( $_POST['item_meta']['other'][ $field->id ] ) ) {
369
370 // Save original value.
371 $args['temp_value'] = $value;
372 $args['other'] = true;
373 $other_vals = wp_unslash( $_POST['item_meta']['other'][ $field->id ] );
374
375 // Set the validation value now
376 self::set_other_validation_val( $value, $other_vals, $field, $args );
377 }
378 }
379
380 /**
381 * Sets radio or checkbox value equal to "other" value if it is set - FOR REPEATING SECTIONS
382 *
383 * @since 2.0
384 *
385 * @param object $field
386 * @param string|array $value
387 * @param array $args
388 */
389 public static function set_other_repeating_vals( $field, &$value, &$args ) {
390 if ( ! $args['parent_field_id'] ) {
391 return;
392 }
393
394 // Check if there are any other posted "other" values for this field.
395 if ( FrmField::is_option_true( $field, 'other' ) && isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ]['other'][ $field->id ] ) ) {
396 // Save original value
397 $args['temp_value'] = $value;
398 $args['other'] = true;
399
400 $other_vals = wp_unslash( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ]['other'][ $field->id ] );
401
402 // Set the validation value now.
403 self::set_other_validation_val( $value, $other_vals, $field, $args );
404 }
405 }
406
407 /**
408 * Modify value used for validation
409 * This function essentially removes the "Other" radio or checkbox value from the $value being validated.
410 * It also adds any text from the free text fields to the value
411 *
412 * Needs to accommodate for times when other opt is selected, but no other free text is entered
413 *
414 * @since 2.0
415 *
416 * @param string|array $value
417 * @param string|array $other_vals (usually of posted values)
418 * @param object $field
419 * @param array $args
420 */
421 public static function set_other_validation_val( &$value, $other_vals, $field, &$args ) {
422 // Checkboxes and multi-select dropdowns.
423 if ( is_array( $value ) && $field->type == 'checkbox' ) {
424 // Combine "Other" values with checked values. "Other" values will override checked box values.
425 $value = array_merge( $value, $other_vals );
426 $value = array_filter( $value );
427 if ( count( $value ) == 0 ) {
428 $value = '';
429 }
430 } else {
431 // Radio and dropdowns.
432 $other_key = array_filter( array_keys( $field->options ), 'is_string' );
433 $other_key = reset( $other_key );
434
435 // Multi-select dropdown.
436 if ( is_array( $value ) ) {
437 $o_key = array_search( $field->options[ $other_key ], $value );
438
439 if ( $o_key !== false ) {
440 // Modify the original value so other key will be preserved.
441 $value[ $other_key ] = $value[ $o_key ];
442
443 // By default, the array keys will be numeric for multi-select dropdowns.
444 // If going backwards and forwards between pages, the array key will match the other key.
445 if ( $o_key !== $other_key ) {
446 unset( $value[ $o_key ] );
447 }
448
449 $args['temp_value'] = $value;
450 $value[ $other_key ] = reset( $other_vals );
451 if ( FrmAppHelper::is_empty_value( $value[ $other_key ] ) ) {
452 unset( $value[ $other_key ] );
453 }
454 }
455 } elseif ( $field->options[ $other_key ] == $value ) {
456 $value = $other_vals;
457 }
458 }
459 }
460
461 /**
462 * Add submitted values to a string for spam checking.
463 *
464 * @param array $values
465 */
466 public static function entry_array_to_string( $values ) {
467 $content = '';
468 foreach ( $values['item_meta'] as $val ) {
469 if ( $content != '' ) {
470 $content .= "\n\n";
471 }
472
473 if ( is_array( $val ) ) {
474 $val = FrmAppHelper::array_flatten( $val );
475 $val = implode( ', ', $val );
476 }
477
478 $content .= $val;
479 }
480
481 return $content;
482 }
483
484 /**
485 * Get the browser from the user agent
486 *
487 * @since 2.04
488 *
489 * @param string $u_agent
490 *
491 * @return string
492 */
493 public static function get_browser( $u_agent ) {
494 $bname = __( 'Unknown', 'formidable' );
495 $platform = __( 'Unknown', 'formidable' );
496 $ub = '';
497
498 // Get the operating system
499 if ( preg_match( '/windows|win32/i', $u_agent ) ) {
500 $platform = 'Windows';
501 } elseif ( preg_match( '/android/i', $u_agent ) ) {
502 $platform = 'Android';
503 } elseif ( preg_match( '/linux/i', $u_agent ) ) {
504 $platform = 'Linux';
505 } elseif ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
506 $platform = 'OS X';
507 }
508
509 $agent_options = array(
510 'Chrome' => 'Google Chrome',
511 'Safari' => 'Apple Safari',
512 'Opera' => 'Opera',
513 'Netscape' => 'Netscape',
514 'Firefox' => 'Mozilla Firefox',
515 );
516
517 // Next get the name of the useragent yes seperately and for good reason
518 if ( strpos( $u_agent, 'MSIE' ) !== false && strpos( $u_agent, 'Opera' ) === false ) {
519 $bname = 'Internet Explorer';
520 $ub = 'MSIE';
521 } else {
522 foreach ( $agent_options as $agent_key => $agent_name ) {
523 if ( strpos( $u_agent, $agent_key ) !== false ) {
524 $bname = $agent_name;
525 $ub = $agent_key;
526 break;
527 }
528 }
529 }
530
531 // finally get the correct version number
532 $known = array( 'Version', $ub, 'other' );
533 $pattern = '#(?<browser>' . join( '|', $known ) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
534 preg_match_all( $pattern, $u_agent, $matches ); // get the matching numbers
535
536 // see how many we have
537 $i = count( $matches['browser'] );
538
539 if ( $i > 1 ) {
540 //we will have two since we are not using 'other' argument yet
541 //see if version is before or after the name
542 if ( strripos( $u_agent, 'Version' ) < strripos( $u_agent, $ub ) ) {
543 $version = $matches['version'][0];
544 } else {
545 $version = $matches['version'][1];
546 }
547 } elseif ( $i === 1 ) {
548 $version = $matches['version'][0];
549 } else {
550 $version = '';
551 }
552
553 // check if we have a number
554 if ( $version == '' ) {
555 $version = '?';
556 }
557
558 return $bname . ' ' . $version . ' / ' . $platform;
559 }
560
561 /**
562 * @since 3.0
563 */
564 public static function actions_dropdown( $atts ) {
565 $id = isset( $atts['id'] ) ? $atts['id'] : FrmAppHelper::get_param( 'id', 0, 'get', 'absint' );
566 $links = self::get_action_links( $id, $atts['entry'] );
567
568 foreach ( $links as $link ) {
569 ?>
570 <div class="misc-pub-section">
571 <a href="<?php echo esc_url( FrmAppHelper::maybe_full_screen_link( $link['url'] ) ); ?>"
572 <?php
573 if ( isset( $link['data'] ) ) {
574 foreach ( $link['data'] as $data => $value ) {
575 echo 'data-' . esc_attr( $data ) . '="' . esc_attr( $value ) . '" ';
576 }
577 }
578 if ( isset( $link['class'] ) ) {
579 echo 'class="' . esc_attr( $link['class'] ) . '" ';
580 }
581 if ( isset( $link['id'] ) ) {
582 echo 'id="' . esc_attr( $link['id'] ) . '" ';
583 }
584 ?>
585 >
586 <?php FrmAppHelper::icon_by_class( $link['icon'], array( 'aria-hidden' => 'true' ) ); ?>
587 <span class="frm_link_label"><?php echo esc_html( $link['label'] ); ?></span>
588 </a>
589 </div>
590 <?php
591 }
592 }
593
594 /**
595 * @since 3.0
596 */
597 private static function get_action_links( $id, $entry ) {
598 $page = FrmAppHelper::get_param( 'frm_action' );
599 $actions = array();
600
601 if ( $page != 'show' ) {
602 $actions['frm_view'] = array(
603 'url' => admin_url( 'admin.php?page=formidable-entries&frm_action=show&id=' . $id . '&form=' . $entry->form_id ),
604 'label' => __( 'View Entry', 'formidable' ),
605 'icon' => 'frm_icon_font frm_save_icon',
606 );
607 }
608
609 if ( current_user_can( 'frm_delete_entries' ) ) {
610 $actions['frm_delete'] = array(
611 'url' => admin_url( 'admin.php?page=formidable-entries&frm_action=destroy&id=' . $id . '&form=' . $entry->form_id ),
612 'label' => __( 'Delete Entry', 'formidable' ),
613 'icon' => 'frm_icon_font frm_delete_icon',
614 'data' => array(
615 'frmverify' => __( 'Delete this form entry?', 'formidable' ),
616 ),
617 );
618 }
619
620 if ( $page == 'show' ) {
621 $actions['frm_print'] = array(
622 'url' => '#',
623 'label' => __( 'Print Entry', 'formidable' ),
624 'data' => array(
625 'frmprint' => '1',
626 ),
627 'icon' => 'frm_icon_font frm_printer_icon',
628 );
629 }
630
631 $actions['frm_resend'] = array(
632 'url' => '#',
633 'label' => __( 'Resend Emails', 'formidable' ),
634 'class' => 'frm_noallow',
635 'data' => array(
636 'upgrade' => __( 'Resend Emails', 'formidable' ),
637 'medium' => 'resend-email',
638 'content' => 'entry',
639 ),
640 'icon' => 'frm_icon_font frm_email_icon',
641 );
642
643 $actions['frm_edit'] = array(
644 'url' => '#',
645 'label' => __( 'Edit Entry', 'formidable' ),
646 'class' => 'frm_noallow',
647 'data' => array(
648 'upgrade' => __( 'Entry edits', 'formidable' ),
649 'medium' => 'edit-entries',
650 'content' => 'entry',
651 ),
652 'icon' => 'frm_icon_font frm_pencil_icon',
653 );
654
655 return apply_filters( 'frm_entry_actions_dropdown', $actions, compact( 'id', 'entry' ) );
656 }
657 }
658