Completed
Push — master ( 3c8881...d57ce4 )
by Stephanie
02:46
created

FrmEntriesHelper::get_current_form()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
		foreach ( array( 'name' => '', 'description' => '', 'item_key' => '' ) as $var => $default ) {
11
			$values[ $var ] = FrmAppHelper::get_post_param( $var, $default, 'wp_kses_post' );
12
        }
13
14
        $values['fields'] = array();
15
        if ( empty($fields) ) {
16
            return apply_filters('frm_setup_new_entry', $values);
17
        }
18
19
        foreach ( (array) $fields as $field ) {
20
			self::prepare_field_default_value( $field );
21
            $new_value = self::get_field_value_for_new_entry( $field, $reset, $args );
22
23
            $field_array = array(
24
                'id' => $field->id,
25
                'value' => $new_value,
26
                'default_value' => $field->default_value,
27
                'name' => $field->name,
28
                'description' => $field->description,
29
                'type' => apply_filters('frm_field_type', $field->type, $field, $new_value),
30
                'options' => $field->options,
31
                'required' => $field->required,
32
                'field_key' => $field->field_key,
33
                'field_order' => $field->field_order,
34
                'form_id' => $field->form_id,
35
				'parent_form_id' => isset( $args['parent_form_id'] ) ? $args['parent_form_id'] : $field->form_id,
36
	            'reset_value' => $reset,
37
				'in_embed_form' => isset( $args['in_embed_form'] ) ? $args['in_embed_form'] : '0',
38
            );
39
40
            $opt_defaults = FrmFieldsHelper::get_default_field_opts($field_array['type'], $field, true);
41
            $opt_defaults['required_indicator'] = '';
42
			$opt_defaults['original_type'] = $field->type;
43
44
			foreach ( $opt_defaults as $opt => $default_opt ) {
45
                $field_array[ $opt ] = ( isset( $field->field_options[ $opt ] ) && $field->field_options[ $opt ] != '' ) ? $field->field_options[ $opt ] : $default_opt;
46
                unset($opt, $default_opt);
47
            }
48
49
            unset($opt_defaults);
50
51
            if ( $field_array['custom_html'] == '' ) {
52
                $field_array['custom_html'] = FrmFieldsHelper::get_default_html($field->type);
53
            }
54
55
            $field_array = apply_filters('frm_setup_new_fields_vars', $field_array, $field, $args );
56
            $field_array = array_merge( $field->field_options, $field_array );
57
58
            $values['fields'][] = $field_array;
59
60
            if ( ! $form || ! isset($form->id) ) {
61
                $form = FrmForm::getOne($field->form_id);
62
            }
63
        }
64
65
        $form->options = maybe_unserialize($form->options);
66
        if ( is_array($form->options) ) {
67
            foreach ( $form->options as $opt => $value ) {
68
                $values[ $opt ] = FrmAppHelper::get_post_param( $opt, $value );
69
                unset($opt, $value);
70
            }
71
        }
72
73
		$form_defaults = FrmFormsHelper::get_default_opts();
74
75
		$frm_settings = FrmAppHelper::get_settings();
76
		$form_defaults['custom_style']  = ( $frm_settings->load_style != 'none' );
77
78
		$values = array_merge( $form_defaults, $values );
79
80
		return apply_filters( 'frm_setup_new_entry', $values );
81
    }
82
83
	/**
84
	 * @since 2.04.02
85
	 */
86
	private static function prepare_field_default_value( &$field ) {
87
		//If checkbox, multi-select dropdown, or checkbox data from entries field, the value should be an array
88
		$return_array = FrmField::is_field_with_multiple_values( $field );
89
90
		// Do any shortcodes in default value and allow customization of default value
91
		$field->default_value = apply_filters( 'frm_get_default_value', $field->default_value, $field, true, $return_array );
92
		// Calls FrmProFieldsHelper::get_default_value
93
	}
94
95
	/**
96
	* Set the value for each field
97
	* This function is used when the form is first loaded and on all page turns *for a new entry*
98
	*
99
	* @since 2.0.13
100
	*
101
	* @param object $field - this is passed by reference since it is an object
102
	* @param boolean $reset
103
	* @param array $args
104
	* @return string|array $new_value
105
	*/
106
	private static function get_field_value_for_new_entry( $field, $reset, $args ) {
107
		$new_value = $field->default_value;
108
109
		if ( ! $reset && self::value_is_posted( $field, $args ) ) {
110
			self::get_posted_value( $field, $new_value, $args );
111
		} else if ( FrmField::is_option_true( $field, 'clear_on_focus' ) ) {
112
			// If clear on focus is selected, the value should be blank (unless it was posted, of course)
113
114
			// TODO: move to Pro
115
			if ( 'address' == $field->type && isset( $new_value['country'] ) ) {
116
				$new_value = array( 'country' => $new_value['country'] );
117
			} else {
118
				$new_value = '';
119
			}
120
		}
121
122
		if ( ! is_array( $new_value ) ) {
123
			$new_value = str_replace('"', '&quot;', $new_value);
124
		}
125
126
		return $new_value;
127
	}
128
129
	/**
130
	* Check if a field has a posted value
131
	*
132
	* @since 2.01.0
133
	* @param object $field
134
	* @param array $args
135
	* @return boolean $value_is_posted
136
	*/
137
	public static function value_is_posted( $field, $args ) {
138
		$value_is_posted = false;
139
		if ( $_POST ) {
140
			$repeating = isset( $args['repeating'] ) && $args['repeating'];
141
			if ( $repeating ) {
142
				if ( isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field->id ] ) ) {
143
					$value_is_posted = true;
144
				}
145
			} else if ( isset( $_POST['item_meta'][ $field->id ] ) ) {
146
				$value_is_posted = true;
147
			}
148
		}
149
		return $value_is_posted;
150
	}
151
152
	public static function setup_edit_vars( $values, $record ) {
153
		$values['item_key'] = FrmAppHelper::get_post_param( 'item_key', $record->item_key, 'sanitize_title' );
154
        $values['form_id'] = $record->form_id;
155
        $values['is_draft'] = $record->is_draft;
156
        return apply_filters('frm_setup_edit_entry_vars', $values, $record);
157
    }
158
159
    public static function get_admin_params( $form = null ) {
160
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::get_admin_params' );
161
		return FrmForm::set_current_form( $form );
162
    }
163
164
	public static function set_current_form( $form_id ) {
165
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::set_current_form' );
166
		return FrmForm::set_current_form( $form_id );
167
	}
168
169
	public static function get_current_form( $form_id = 0 ) {
170
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::get_current_form' );
171
		return FrmForm::get_current_form( $form_id );
172
	}
173
174
    public static function get_current_form_id() {
175
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::get_current_form_id' );
176
		return FrmForm::get_current_form_id();
177
    }
178
179
    public static function maybe_get_entry( &$entry ) {
180
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntry::maybe_get_entry' );
181
		FrmEntry::maybe_get_entry( $entry );
182
    }
183
184
	public static function replace_default_message( $message, $atts ) {
185
        if ( strpos($message, '[default-message') === false &&
186
            strpos($message, '[default_message') === false &&
187
            ! empty( $message ) ) {
188
            return $message;
189
        }
190
191
        if ( empty($message) ) {
192
            $message = '[default-message]';
193
        }
194
195
        preg_match_all("/\[(default-message|default_message)\b(.*?)(?:(\/))?\]/s", $message, $shortcodes, PREG_PATTERN_ORDER);
196
197
        foreach ( $shortcodes[0] as $short_key => $tag ) {
198
			$add_atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] );
199
			if ( ! empty( $add_atts ) ) {
200
                $this_atts = array_merge($atts, $add_atts);
201
            } else {
202
                $this_atts = $atts;
203
            }
204
205
			$default = FrmEntriesController::show_entry_shortcode( $this_atts );
206
207
            // Add the default message
208
            $message = str_replace( $shortcodes[0][ $short_key ], $default, $message );
209
        }
210
211
        return $message;
212
    }
213
214
	public static function prepare_display_value( $entry, $field, $atts ) {
215
		$field_value = isset( $entry->metas[ $field->id ] ) ? $entry->metas[ $field->id ] : false;
216
217
        if ( FrmAppHelper::pro_is_installed() ) {
218
			FrmProEntriesHelper::get_dynamic_list_values( $field, $entry, $field_value );
219
        }
220
221
        if ( $field->form_id == $entry->form_id || empty($atts['embedded_field_id']) ) {
222
            return self::display_value($field_value, $field, $atts);
223
        }
224
225
        // this is an embeded form
226
        $val = '';
227
228
	    if ( strpos($atts['embedded_field_id'], 'form') === 0 ) {
229
            //this is a repeating section
230
			$child_entries = FrmEntry::getAll( array( 'it.parent_item_id' => $entry->id ) );
231
        } else {
232
            // get all values for this field
233
	        $child_values = isset( $entry->metas[ $atts['embedded_field_id'] ] ) ? $entry->metas[ $atts['embedded_field_id'] ] : false;
234
235
            if ( $child_values ) {
236
	            $child_entries = FrmEntry::getAll( array( 'it.id' => (array) $child_values ) );
237
	        }
238
	    }
239
240
	    $field_value = array();
241
242
        if ( ! isset($child_entries) || ! $child_entries || ! FrmAppHelper::pro_is_installed() ) {
243
            return $val;
244
        }
245
246
        foreach ( $child_entries as $child_entry ) {
247
            $atts['item_id'] = $child_entry->id;
248
            $atts['post_id'] = $child_entry->post_id;
249
250
            // get the value for this field -- check for post values as well
251
            $entry_val = FrmProEntryMetaHelper::get_post_or_meta_value($child_entry, $field);
252
253
            if ( $entry_val ) {
254
                // foreach entry get display_value
255
                $field_value[] = self::display_value($entry_val, $field, $atts);
256
            }
257
258
            unset($child_entry);
259
        }
260
261
        $val = implode(', ', (array) $field_value );
262
		$val = wp_kses_post( $val );
263
264
        return $val;
265
    }
266
267
    /**
268
     * Prepare the saved value for display
269
     * @return string
270
     */
271
	public static function display_value( $value, $field, $atts = array() ) {
272
273
        $defaults = array(
274
            'type' => '', 'html' => false, 'show_filename' => true,
275
            'truncate' => false, 'sep' => ', ', 'post_id' => 0,
276
            'form_id' => $field->form_id, 'field' => $field, 'keepjs' => 0,
277
			'return_array' => false,
278
        );
279
280
        $atts = wp_parse_args( $atts, $defaults );
281
        $atts = apply_filters('frm_display_value_atts', $atts, $field, $value);
282
283
        if ( ! isset($field->field_options['post_field']) ) {
284
            $field->field_options['post_field'] = '';
285
        }
286
287
        if ( ! isset($field->field_options['custom_field']) ) {
288
            $field->field_options['custom_field'] = '';
289
        }
290
291
        if ( FrmAppHelper::pro_is_installed() && $atts['post_id'] && ( $field->field_options['post_field'] || $atts['type'] == 'tag' ) ) {
292
            $atts['pre_truncate'] = $atts['truncate'];
293
            $atts['truncate'] = true;
294
            $atts['exclude_cat'] = isset($field->field_options['exclude_cat']) ? $field->field_options['exclude_cat'] : 0;
295
296
            $value = FrmProEntryMetaHelper::get_post_value($atts['post_id'], $field->field_options['post_field'], $field->field_options['custom_field'], $atts);
297
            $atts['truncate'] = $atts['pre_truncate'];
298
        }
299
300
        if ( $value == '' ) {
301
            return $value;
302
        }
303
304
        $value = apply_filters('frm_display_value_custom', maybe_unserialize($value), $field, $atts);
305
		$value = apply_filters( 'frm_display_' . $field->type . '_value_custom', $value, compact( 'field', 'atts' ) );
306
307
        $new_value = '';
308
309
        if ( is_array($value) && $atts['type'] != 'file' ) {
310
            foreach ( $value as $val ) {
311
                if ( is_array($val) ) {
312
					//TODO: add options for display (li or ,)
313
                    $new_value .= implode($atts['sep'], $val);
314
                    if ( $atts['type'] != 'data' ) {
315
                        $new_value .= '<br/>';
316
                    }
317
                }
318
                unset($val);
319
            }
320
        }
321
322
        if ( ! empty($new_value) ) {
323
            $value = $new_value;
324
        } else if ( is_array($value) && $atts['type'] != 'file' && ! $atts['return_array'] ) {
325
            $value = implode($atts['sep'], $value);
326
        }
327
328
        if ( $atts['truncate'] && $atts['type'] != 'image' ) {
329
            $value = FrmAppHelper::truncate($value, 50);
330
        }
331
332
		if ( ! $atts['keepjs'] && ! is_array( $value ) ) {
333
			$value = wp_kses_post( $value );
334
		}
335
336
        return apply_filters('frm_display_value', $value, $field, $atts);
337
    }
338
339
	public static function set_posted_value( $field, $value, $args ) {
340
        // If validating a field with "other" opt, set back to prev value now
341
        if ( isset( $args['other'] ) && $args['other'] ) {
342
            $value = $args['temp_value'];
343
        }
344
        if ( empty($args['parent_field_id']) ) {
345
            $_POST['item_meta'][ $field->id ] = $value;
346
        } else {
347
            $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field->id ] = $value;
348
        }
349
    }
350
351
	public static function get_posted_value( $field, &$value, $args ) {
352
		$field_id = is_object( $field ) ? $field->id : $field;
353
354
        if ( empty($args['parent_field_id']) ) {
355
            $value = isset( $_POST['item_meta'][ $field_id ] ) ? $_POST['item_meta'][ $field_id ] : '';
356
        } else {
357
            $value = isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] ) ? $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] : '';
358
        }
359
		FrmAppHelper::sanitize_value( 'wp_kses_post', $value );
360
		$value = stripslashes_deep( $value );
361
    }
362
363
    /**
364
    * Check if field has an "Other" option and if any other values are posted
365
    *
366
    * @since 2.0
367
    *
368
    * @param object $field
369
    * @param string|array $value
370
    * @param array $args
371
    */
372
    public static function maybe_set_other_validation( $field, &$value, &$args ) {
373
        $args['other'] = false;
374
        if ( ! $value || empty( $value ) || ! FrmAppHelper::pro_is_installed() ) {
375
            return;
376
        }
377
378
        // Get other value for fields in repeating section
379
        self::set_other_repeating_vals( $field, $value, $args );
380
381
        // Check if there are any posted "Other" values
382
		if ( FrmField::is_option_true( $field, 'other' ) && isset( $_POST['item_meta']['other'][ $field->id ] ) ) {
383
384
            // Save original value
385
            $args['temp_value'] = $value;
386
            $args['other'] = true;
387
            $other_vals = stripslashes_deep( $_POST['item_meta']['other'][ $field->id ] );
388
389
            // Set the validation value now
390
            self::set_other_validation_val( $value, $other_vals, $field, $args );
391
        }
392
    }
393
394
    /**
395
    * Sets radio or checkbox value equal to "other" value if it is set - FOR REPEATING SECTIONS
396
    *
397
    * @since 2.0
398
    *
399
    * @param object $field
400
    * @param string|array $value
401
    * @param array $args
402
    */
403
    public static function set_other_repeating_vals( $field, &$value, &$args ) {
404
        if ( ! $args['parent_field_id'] ) {
405
            return;
406
        }
407
408
        // Check if there are any other posted "other" values for this field
409
		if ( FrmField::is_option_true( $field, 'other' ) && isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ]['other'][ $field->id ] ) ) {
410
            // Save original value
411
            $args['temp_value'] = $value;
412
            $args['other'] = true;
413
414
            $other_vals = $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ]['other'][ $field->id ];
415
416
            // Set the validation value now
417
            self::set_other_validation_val( $value, $other_vals, $field, $args );
418
        }
419
    }
420
421
    /**
422
    * Modify value used for validation
423
    * This function essentially removes the "Other" radio or checkbox value from the $value being validated.
424
    * It also adds any text from the free text fields to the value
425
    *
426
    * Needs to accommodate for times when other opt is selected, but no other free text is entered
427
    *
428
    * @since 2.0
429
    *
430
    * @param string|array $value
431
    * @param string|array $other_vals (usually of posted values)
432
    * @param object $field
433
    * @param array $args
434
    */
435
    public static function set_other_validation_val( &$value, $other_vals, $field, &$args ) {
436
        // Checkboxes and multi-select dropdowns
437
        if ( is_array( $value ) && $field->type == 'checkbox' ) {
438
            // Combine "Other" values with checked values. "Other" values will override checked box values.
439
            $value = array_merge( $value, $other_vals );
440
            $value = array_filter( $value );
441
            if ( count( $value ) == 0 ) {
442
                $value = '';
443
            }
444
        } else {
445
			// Radio and dropdowns
446
            $other_key = array_filter( array_keys($field->options), 'is_string');
447
            $other_key = reset( $other_key );
448
449
            // Multi-select dropdown
450
            if ( is_array( $value ) ) {
451
                $o_key = array_search( $field->options[ $other_key ], $value );
452
453
				if ( $o_key !== false ) {
454
					// Modify the original value so other key will be preserved
455
					$value[ $other_key ] = $value[ $o_key ];
456
457
					// By default, the array keys will be numeric for multi-select dropdowns
458
					// If going backwards and forwards between pages, the array key will match the other key
459
					if ( $o_key != $other_key ) {
460
						unset( $value[ $o_key ] );
461
					}
462
463
					$args['temp_value'] = $value;
464
					$value[ $other_key ] = reset( $other_vals );
465
				}
466
            } else if ( $field->options[ $other_key ] == $value ) {
467
                $value = $other_vals;
468
            }
469
        }
470
    }
471
472
	public static function enqueue_scripts( $params ) {
473
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmFormsController::enqueue_scripts' );
474
		FrmFormsController::enqueue_scripts( $params );
475
	}
476
477
    // Add submitted values to a string for spam checking
478
	public static function entry_array_to_string( $values ) {
479
        $content = '';
480
		foreach ( $values['item_meta'] as $val ) {
481
			if ( $content != '' ) {
482
				$content .= "\n\n";
483
			}
484
485
			if ( is_array($val) ) {
486
				$val = FrmAppHelper::array_flatten( $val );
487
				$val = implode( ', ', $val );
488
			}
489
490
			$content .= $val;
491
		}
492
493
		return $content;
494
    }
495
496
	/**
497
	 * Get the browser from the user agent
498
	 *
499
	 * @since 2.04
500
	 *
501
	 * @param string $u_agent
502
	 *
503
	 * @return string
504
	 */
505
	public static function get_browser( $u_agent ) {
506
		$bname = __( 'Unknown', 'formidable' );
507
		$platform = __( 'Unknown', 'formidable' );
508
		$ub = '';
509
510
		// Get the operating system
511
		if ( preg_match( '/windows|win32/i', $u_agent ) ) {
512
			$platform = 'Windows';
513
		} else if ( preg_match( '/android/i', $u_agent ) ) {
514
			$platform = 'Android';
515
		} else if ( preg_match( '/linux/i', $u_agent ) ) {
516
			$platform = 'Linux';
517
		} else if ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
518
			$platform = 'OS X';
519
		}
520
521
		$agent_options = array(
522
			'Chrome'   => 'Google Chrome',
523
			'Safari'   => 'Apple Safari',
524
			'Opera'    => 'Opera',
525
			'Netscape' => 'Netscape',
526
			'Firefox'  => 'Mozilla Firefox',
527
		);
528
529
		// Next get the name of the useragent yes seperately and for good reason
530
		if ( strpos( $u_agent, 'MSIE' ) !== false && strpos( $u_agent, 'Opera' ) === false ) {
531
			$bname = 'Internet Explorer';
532
			$ub = 'MSIE';
533
		} else {
534
			foreach ( $agent_options as $agent_key => $agent_name ) {
535
				if ( strpos( $u_agent, $agent_key ) !== false ) {
536
					$bname = $agent_name;
537
					$ub = $agent_key;
538
					break;
539
				}
540
			}
541
		}
542
543
		// finally get the correct version number
544
		$known = array( 'Version', $ub, 'other' );
545
		$pattern = '#(?<browser>' . join( '|', $known ) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
546
		preg_match_all( $pattern, $u_agent, $matches ); // get the matching numbers
547
548
		// see how many we have
549
		$i = count($matches['browser']);
550
551
		if ( $i > 1 ) {
552
			//we will have two since we are not using 'other' argument yet
553
			//see if version is before or after the name
554
			if ( strripos( $u_agent, 'Version' ) < strripos( $u_agent, $ub ) ) {
555
				$version = $matches['version'][0];
556
			} else {
557
				$version = $matches['version'][1];
558
			}
559
		} else if ( $i === 1 ) {
560
			$version = $matches['version'][0];
561
		} else {
562
			$version = '';
563
		}
564
565
		// check if we have a number
566
		if ( $version == '' ) {
567
			$version = '?';
568
		}
569
570
		return $bname . ' ' . $version . ' / ' . $platform;
571
	}
572
573
}
574