Completed
Push — master ( 61a5f4...04aa53 )
by Jamie
03:43
created

FrmEntriesHelper::enqueue_scripts()   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
				'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);
55
            $field_array = array_merge( $field->field_options, $field_array );
56
57
            $values['fields'][] = $field_array;
58
59
            if ( ! $form || ! isset($form->id) ) {
60
                $form = FrmForm::getOne($field->form_id);
61
            }
62
        }
63
64
        $form->options = maybe_unserialize($form->options);
65
        if ( is_array($form->options) ) {
66
            foreach ( $form->options as $opt => $value ) {
67
                $values[ $opt ] = FrmAppHelper::get_post_param( $opt, $value );
68
                unset($opt, $value);
69
            }
70
        }
71
72
		$form_defaults = FrmFormsHelper::get_default_opts();
73
74
		$frm_settings = FrmAppHelper::get_settings();
75
		$form_defaults['custom_style']  = ( $frm_settings->load_style != 'none' );
76
77
		$values = array_merge( $form_defaults, $values );
78
79
		return apply_filters( 'frm_setup_new_entry', $values );
80
    }
81
82
	/**
83
	* Set the value for each field
84
	* This function is used when the form is first loaded and on all page turns *for a new entry*
85
	*
86
	* @since 2.0.13
87
	*
88
	* @param object $field - this is passed by reference since it is an object
89
	* @param boolean $reset
90
	* @param array $args
91
	* @return string|array $new_value
92
	*/
93
	private static function get_field_value_for_new_entry( $field, $reset, $args ) {
94
		//If checkbox, multi-select dropdown, or checkbox data from entries field, the value should be an array
95
		$return_array = FrmField::is_field_with_multiple_values( $field );
96
97
		// Do any shortcodes in default value and allow customization of default value
98
		$field->default_value = apply_filters('frm_get_default_value', $field->default_value, $field, true, $return_array);
99
		// Calls FrmProFieldsHelper::get_default_value
100
101
		$new_value = $field->default_value;
102
103
		if ( ! $reset && self::value_is_posted( $field, $args ) ) {
104
			self::get_posted_value( $field, $new_value, $args );
105
		} else if ( FrmField::is_option_true( $field, 'clear_on_focus' ) ) {
106
			// If clear on focus is selected, the value should be blank (unless it was posted, of course)
107
			$new_value = '';
108
		}
109
110
		if ( ! is_array( $new_value ) ) {
111
			$new_value = str_replace('"', '&quot;', $new_value);
112
		}
113
114
		return $new_value;
115
	}
116
117
	/**
118
	* Check if a field has a posted value
119
	*
120
	* @since 2.01.0
121
	* @param object $field
122
	* @param array $args
123
	* @return boolean $value_is_posted
124
	*/
125
	public static function value_is_posted( $field, $args ) {
126
		$value_is_posted = false;
127
		if ( $_POST ) {
128
			$repeating = isset( $args['repeating'] ) && $args['repeating'];
129
			if ( $repeating ) {
130
				if ( isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field->id ] ) ) {
131
					$value_is_posted = true;
132
				}
133
			} else if ( isset( $_POST['item_meta'][ $field->id ] ) ) {
134
				$value_is_posted = true;
135
			}
136
		}
137
		return $value_is_posted;
138
	}
139
140
	public static function setup_edit_vars( $values, $record ) {
141
		$values['item_key'] = FrmAppHelper::get_post_param( 'item_key', $record->item_key, 'sanitize_title' );
142
        $values['form_id'] = $record->form_id;
143
        $values['is_draft'] = $record->is_draft;
144
        return apply_filters('frm_setup_edit_entry_vars', $values, $record);
145
    }
146
147
    public static function get_admin_params( $form = null ) {
148
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::get_admin_params' );
149
		return FrmForm::set_current_form( $form );
150
    }
151
152
	public static function set_current_form( $form_id ) {
153
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::set_current_form' );
154
		return FrmForm::set_current_form( $form_id );
155
	}
156
157
	public static function get_current_form( $form_id = 0 ) {
158
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::get_current_form' );
159
		return FrmForm::get_current_form( $form_id );
160
	}
161
162
    public static function get_current_form_id() {
163
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::get_current_form_id' );
164
		return FrmForm::get_current_form_id();
165
    }
166
167
    public static function maybe_get_entry( &$entry ) {
168
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntry::maybe_get_entry' );
169
		FrmEntry::maybe_get_entry( $entry );
170
    }
171
172
	public static function replace_default_message( $message, $atts ) {
173
        if ( strpos($message, '[default-message') === false &&
174
            strpos($message, '[default_message') === false &&
175
            ! empty( $message ) ) {
176
            return $message;
177
        }
178
179
        if ( empty($message) ) {
180
            $message = '[default-message]';
181
        }
182
183
        preg_match_all("/\[(default-message|default_message)\b(.*?)(?:(\/))?\]/s", $message, $shortcodes, PREG_PATTERN_ORDER);
184
185
        foreach ( $shortcodes[0] as $short_key => $tag ) {
186
            $add_atts = shortcode_parse_atts( $shortcodes[2][ $short_key ] );
187
            if ( $add_atts ) {
188
                $this_atts = array_merge($atts, $add_atts);
189
            } else {
190
                $this_atts = $atts;
191
            }
192
193
			$default = FrmEntryFormat::show_entry( $this_atts );
194
195
            // Add the default message
196
            $message = str_replace( $shortcodes[0][ $short_key ], $default, $message );
197
        }
198
199
        return $message;
200
    }
201
202
	public static function prepare_display_value( $entry, $field, $atts ) {
203
		$field_value = isset( $entry->metas[ $field->id ] ) ? $entry->metas[ $field->id ] : false;
204
205
        if ( FrmAppHelper::pro_is_installed() ) {
206
			FrmProEntriesHelper::get_dynamic_list_values( $field, $entry, $field_value );
207
        }
208
209
        if ( $field->form_id == $entry->form_id || empty($atts['embedded_field_id']) ) {
210
            return self::display_value($field_value, $field, $atts);
211
        }
212
213
        // this is an embeded form
214
        $val = '';
215
216
	    if ( strpos($atts['embedded_field_id'], 'form') === 0 ) {
217
            //this is a repeating section
218
			$child_entries = FrmEntry::getAll( array( 'it.parent_item_id' => $entry->id ) );
219
        } else {
220
            // get all values for this field
221
	        $child_values = isset( $entry->metas[ $atts['embedded_field_id'] ] ) ? $entry->metas[ $atts['embedded_field_id'] ] : false;
222
223
            if ( $child_values ) {
224
	            $child_entries = FrmEntry::getAll( array( 'it.id' => (array) $child_values ) );
225
	        }
226
	    }
227
228
	    $field_value = array();
229
230
        if ( ! isset($child_entries) || ! $child_entries || ! FrmAppHelper::pro_is_installed() ) {
231
            return $val;
232
        }
233
234
        foreach ( $child_entries as $child_entry ) {
235
            $atts['item_id'] = $child_entry->id;
236
            $atts['post_id'] = $child_entry->post_id;
237
238
            // get the value for this field -- check for post values as well
239
            $entry_val = FrmProEntryMetaHelper::get_post_or_meta_value($child_entry, $field);
240
241
            if ( $entry_val ) {
242
                // foreach entry get display_value
243
                $field_value[] = self::display_value($entry_val, $field, $atts);
244
            }
245
246
            unset($child_entry);
247
        }
248
249
        $val = implode(', ', (array) $field_value );
250
		$val = wp_kses_post( $val );
251
252
        return $val;
253
    }
254
255
    /**
256
     * Prepare the saved value for display
257
     * @return string
258
     */
259
	public static function display_value( $value, $field, $atts = array() ) {
260
261
        $defaults = array(
262
            'type' => '', 'html' => false, 'show_filename' => true,
263
            'truncate' => false, 'sep' => ', ', 'post_id' => 0,
264
            'form_id' => $field->form_id, 'field' => $field, 'keepjs' => 0,
265
			'return_array' => false,
266
        );
267
268
        $atts = wp_parse_args( $atts, $defaults );
269
        $atts = apply_filters('frm_display_value_atts', $atts, $field, $value);
270
271
        if ( ! isset($field->field_options['post_field']) ) {
272
            $field->field_options['post_field'] = '';
273
        }
274
275
        if ( ! isset($field->field_options['custom_field']) ) {
276
            $field->field_options['custom_field'] = '';
277
        }
278
279
        if ( FrmAppHelper::pro_is_installed() && $atts['post_id'] && ( $field->field_options['post_field'] || $atts['type'] == 'tag' ) ) {
280
            $atts['pre_truncate'] = $atts['truncate'];
281
            $atts['truncate'] = true;
282
            $atts['exclude_cat'] = isset($field->field_options['exclude_cat']) ? $field->field_options['exclude_cat'] : 0;
283
284
            $value = FrmProEntryMetaHelper::get_post_value($atts['post_id'], $field->field_options['post_field'], $field->field_options['custom_field'], $atts);
285
            $atts['truncate'] = $atts['pre_truncate'];
286
        }
287
288
        if ( $value == '' ) {
289
            return $value;
290
        }
291
292
        $value = apply_filters('frm_display_value_custom', maybe_unserialize($value), $field, $atts);
293
		$value = apply_filters( 'frm_display_' . $field->type . '_value_custom', $value, compact( 'field', 'atts' ) );
294
295
        $new_value = '';
296
297
        if ( is_array($value) && $atts['type'] != 'file' ) {
298
            foreach ( $value as $val ) {
299
                if ( is_array($val) ) {
300
					//TODO: add options for display (li or ,)
301
                    $new_value .= implode($atts['sep'], $val);
302
                    if ( $atts['type'] != 'data' ) {
303
                        $new_value .= '<br/>';
304
                    }
305
                }
306
                unset($val);
307
            }
308
        }
309
310
        if ( ! empty($new_value) ) {
311
            $value = $new_value;
312
        } else if ( is_array($value) && $atts['type'] != 'file' && ! $atts['return_array'] ) {
313
            $value = implode($atts['sep'], $value);
314
        }
315
316
        if ( $atts['truncate'] && $atts['type'] != 'image' ) {
317
            $value = FrmAppHelper::truncate($value, 50);
318
        }
319
320
		if ( ! $atts['keepjs'] && ! is_array( $value ) ) {
321
			$value = wp_kses_post( $value );
322
		}
323
324
        return apply_filters('frm_display_value', $value, $field, $atts);
325
    }
326
327
	public static function set_posted_value( $field, $value, $args ) {
328
        // If validating a field with "other" opt, set back to prev value now
329
        if ( isset( $args['other'] ) && $args['other'] ) {
330
            $value = $args['temp_value'];
331
        }
332
        if ( empty($args['parent_field_id']) ) {
333
            $_POST['item_meta'][ $field->id ] = $value;
334
        } else {
335
            $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field->id ] = $value;
336
        }
337
    }
338
339
	public static function get_posted_value( $field, &$value, $args ) {
340
		$field_id = is_object( $field ) ? $field->id : $field;
341
342
        if ( empty($args['parent_field_id']) ) {
343
            $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...
344
        } else {
345
            $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...
346
        }
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 = 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...
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 = $_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...
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
				}
452
            } else if ( $field->options[ $other_key ] == $value ) {
453
                $value = $other_vals;
454
            }
455
        }
456
    }
457
458
	public static function enqueue_scripts( $params ) {
459
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmFormsController::enqueue_scripts' );
460
		FrmFormsController::enqueue_scripts( $params );
461
	}
462
463
    // Add submitted values to a string for spam checking
464
	public static function entry_array_to_string( $values ) {
465
        $content = '';
466
		foreach ( $values['item_meta'] as $val ) {
467
			if ( $content != '' ) {
468
				$content .= "\n\n";
469
			}
470
471
			if ( is_array($val) ) {
472
				$val = FrmAppHelper::array_flatten( $val );
473
			    $val = implode(',', $val);
474
			}
475
476
			$content .= $val;
477
		}
478
479
		return $content;
480
    }
481
482
	public static function fill_entry_values( $atts, $f, array &$values ) {
483
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntryFormat::fill_entry_values' );
484
		FrmEntryFormat::fill_entry_values( $atts, $f, $values );
485
	}
486
487
	public static function flatten_multi_file_upload( &$val, $field ) {
488
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntryFormat::flatten_multi_file_upload' );
489
		FrmEntryFormat::flatten_multi_file_upload( $field, $val );
490
	}
491
492
	public static function textarea_display_value( &$value, $type, $plain_text ) {
493
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntryFormat::textarea_display_value' );
494
		FrmEntryFormat::textarea_display_value( $type, $plain_text, $value );
495
	}
496
497
	public static function fill_entry_user_info( $atts, array &$values ) {
498
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntryFormat::fill_entry_user_info' );
499
		FrmEntryFormat::fill_entry_user_info( $atts, $values );
500
	}
501
502
	public static function get_entry_description_data( $atts ) {
503
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntryFormat::get_entry_description_data' );
504
		return FrmEntryFormat::get_entry_description_data( $atts );
505
	}
506
507
	public static function convert_entry_to_content( $values, $atts, array &$content ) {
508
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntryFormat::convert_entry_to_content' );
509
		FrmEntryFormat::convert_entry_to_content( $values, $atts, $content );
510
	}
511
512
	public static function get_browser( $u_agent ) {
513
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntryFormat::get_browser' );
514
		return FrmEntryFormat::get_browser( $u_agent );
515
	}
516
517
	public static function entries_dropdown() {
518
		_deprecated_function( __FUNCTION__, '1.07.09');
519
	}
520
}
521