Completed
Push — master ( 81a048...efda5a )
by Stephanie
03:03
created

FrmEntriesHelper::get_action_links()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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