Completed
Push — master ( 151d25...a80ee5 )
by Jamie
03:44
created

FrmEntriesHelper::set_current_form()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

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