Completed
Push — master ( a354bc...aa3cec )
by Jamie
03:45
created

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