Completed
Push — master ( ff36be...05e347 )
by Stephanie
02:14
created

FrmEntriesHelper   F

Complexity

Total Complexity 122

Size/Duplication

Total Lines 682
Duplicated Lines 0.73 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 5
loc 682
rs 1.918
c 0
b 0
f 0
wmc 122
lcom 1
cbo 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
B setup_new_vars() 0 50 8
A prepare_field_default_value() 0 14 2
A get_field_value_for_new_entry() 0 13 4
A value_is_posted() 0 15 6
A setup_edit_vars() 0 7 1
B replace_default_message() 0 29 7
C prepare_display_value() 0 51 13
D display_value() 0 65 16
A set_posted_value() 0 11 4
A set_parent_field_posted_value() 0 16 5
A get_posted_value() 0 20 4
A get_posted_meta() 0 11 4
B maybe_set_other_validation() 0 32 7
A set_other_repeating_vals() 0 19 4
B set_other_validation_val() 0 39 9
A entry_array_to_string() 0 17 4
C get_browser() 0 67 13
B actions_dropdown() 5 29 7
B get_action_links() 0 60 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like FrmEntriesHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FrmEntriesHelper, and based on these observations, apply Extract Interface, too.

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
		FrmAppHelper::unserialize_or_decode( $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
		$frm_settings  = FrmAppHelper::get_settings();
51
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
		/**
69
		 * Do any shortcodes in default value and allow customization of default value.
70
		 * Calls FrmProFieldsHelper::get_default_value
71
		 */
72
		$field->default_value = apply_filters( 'frm_get_default_value', $field->default_value, $field, true, $return_array );
73
74
		if ( isset( $field->field_options['placeholder'] ) ) {
75
			$field->field_options['placeholder'] = apply_filters( 'frm_get_default_value', $field->field_options['placeholder'], $field, false, false );
76
		}
77
	}
78
79
	/**
80
	 * Set the value for each field
81
	 * This function is used when the form is first loaded and on all page turns *for a new entry*
82
	 *
83
	 * @since 2.0.13
84
	 *
85
	 * @param object $field - this is passed by reference since it is an object
86
	 * @param boolean $reset
87
	 * @param array $args
88
	 *
89
	 * @return string|array $new_value
90
	 */
91
	private static function get_field_value_for_new_entry( $field, $reset, $args ) {
92
		$new_value = $field->default_value;
93
94
		if ( ! $reset && self::value_is_posted( $field, $args ) ) {
95
			self::get_posted_value( $field, $new_value, $args );
96
		}
97
98
		if ( ! is_array( $new_value ) ) {
99
			$new_value = str_replace( '"', '&quot;', $new_value );
100
		}
101
102
		return $new_value;
103
	}
104
105
	/**
106
	 * Check if a field has a posted value
107
	 *
108
	 * @since 2.01.0
109
	 *
110
	 * @param object $field
111
	 * @param array $args
112
	 *
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
			} elseif ( isset( $_POST['item_meta'][ $field->id ] ) ) {
124
				$value_is_posted = true;
125
			}
126
		}
127
128
		return $value_is_posted;
129
	}
130
131
	public static function setup_edit_vars( $values, $record ) {
132
		$values['item_key'] = FrmAppHelper::get_post_param( 'item_key', $record->item_key, 'sanitize_title' );
133
		$values['form_id']  = $record->form_id;
134
		$values['is_draft'] = $record->is_draft;
135
136
		return apply_filters( 'frm_setup_edit_entry_vars', $values, $record );
137
	}
138
139
	public static function replace_default_message( $message, $atts ) {
140
		if ( strpos( $message, '[default-message' ) === false &&
141
			strpos( $message, '[default_message' ) === false &&
142
			! empty( $message ) ) {
143
			return $message;
144
		}
145
146
		if ( empty( $message ) ) {
147
			$message = '[default-message]';
148
		}
149
150
		preg_match_all( "/\[(default-message|default_message)\b(.*?)(?:(\/))?\]/s", $message, $shortcodes, PREG_PATTERN_ORDER );
151
152
		foreach ( $shortcodes[0] as $short_key => $tag ) {
153
			$add_atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] );
154
			if ( ! empty( $add_atts ) ) {
155
				$this_atts = array_merge( $atts, $add_atts );
156
			} else {
157
				$this_atts = $atts;
158
			}
159
160
			$default = FrmEntriesController::show_entry_shortcode( $this_atts );
161
162
			// Add the default message.
163
			$message = str_replace( $shortcodes[0][ $short_key ], $default, $message );
164
		}
165
166
		return $message;
167
	}
168
169
	public static function prepare_display_value( $entry, $field, $atts ) {
170
		$field_value = isset( $entry->metas[ $field->id ] ) ? $entry->metas[ $field->id ] : false;
171
172
		if ( FrmAppHelper::pro_is_installed() ) {
173
			FrmProEntriesHelper::get_dynamic_list_values( $field, $entry, $field_value );
174
		}
175
176
		if ( $field->form_id == $entry->form_id || empty( $atts['embedded_field_id'] ) ) {
177
			return self::display_value( $field_value, $field, $atts );
178
		}
179
180
		// This is an embeded form.
181
		$val = '';
182
183
		if ( strpos( $atts['embedded_field_id'], 'form' ) === 0 ) {
184
			// This is a repeating section.
185
			$child_entries = FrmEntry::getAll( array( 'it.parent_item_id' => $entry->id ) );
186
		} else {
187
			// Get all values for this field.
188
			$child_values = isset( $entry->metas[ $atts['embedded_field_id'] ] ) ? $entry->metas[ $atts['embedded_field_id'] ] : false;
189
190
			if ( $child_values ) {
191
				$child_entries = FrmEntry::getAll( array( 'it.id' => (array) $child_values ) );
192
			}
193
		}
194
195
		$field_value = array();
196
197
		if ( ! isset( $child_entries ) || ! $child_entries || ! FrmAppHelper::pro_is_installed() ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $child_entries of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
198
			return $val;
199
		}
200
201
		foreach ( $child_entries as $child_entry ) {
202
			$atts['item_id'] = $child_entry->id;
203
			$atts['post_id'] = $child_entry->post_id;
204
205
			// Fet the value for this field -- check for post values as well.
206
			$entry_val = FrmProEntryMetaHelper::get_post_or_meta_value( $child_entry, $field );
207
208
			if ( $entry_val ) {
209
				// foreach entry get display_value.
210
				$field_value[] = self::display_value( $entry_val, $field, $atts );
211
			}
212
213
			unset( $child_entry );
214
		}
215
216
		$val = implode( ', ', (array) $field_value );
217
218
		return FrmAppHelper::kses( $val, 'all' );
219
	}
220
221
	/**
222
	 * Prepare the saved value for display
223
	 *
224
	 * @param array|string $value
225
	 * @param object $field
226
	 * @param array $atts
227
	 *
228
	 * @return string
229
	 */
230
	public static function display_value( $value, $field, $atts = array() ) {
231
232
		$defaults = array(
233
			'type'          => '',
234
			'html'          => false,
235
			'show_filename' => true,
236
			'truncate'      => false,
237
			'sep'           => ', ',
238
			'post_id'       => 0,
239
			'form_id'       => $field->form_id,
240
			'field'         => $field,
241
			'keepjs'        => 0,
242
			'return_array'  => false,
243
		);
244
245
		$atts = wp_parse_args( $atts, $defaults );
246
247
		if ( FrmField::is_image( $field ) || $field->type == 'star' ) {
248
			$atts['truncate'] = false;
249
			$atts['html']     = true;
250
		}
251
252
		$atts = apply_filters( 'frm_display_value_atts', $atts, $field, $value );
253
254
		if ( ! isset( $field->field_options['post_field'] ) ) {
255
			$field->field_options['post_field'] = '';
256
		}
257
258
		if ( ! isset( $field->field_options['custom_field'] ) ) {
259
			$field->field_options['custom_field'] = '';
260
		}
261
262
		if ( FrmAppHelper::pro_is_installed() && $atts['post_id'] && ( $field->field_options['post_field'] || $atts['type'] == 'tag' ) ) {
263
			$atts['pre_truncate'] = $atts['truncate'];
264
			$atts['truncate']     = true;
265
			$atts['exclude_cat']  = isset( $field->field_options['exclude_cat'] ) ? $field->field_options['exclude_cat'] : 0;
266
267
			$value            = FrmProEntryMetaHelper::get_post_value( $atts['post_id'], $field->field_options['post_field'], $field->field_options['custom_field'], $atts );
268
			$atts['truncate'] = $atts['pre_truncate'];
269
		}
270
271
		if ( $value == '' ) {
272
			return $value;
273
		}
274
275
		$unfiltered_value = $value;
276
		FrmAppHelper::unserialize_or_decode( $unfiltered_value );
277
278
		$value = apply_filters( 'frm_display_value_custom', $unfiltered_value, $field, $atts );
279
		$value = apply_filters( 'frm_display_' . $field->type . '_value_custom', $value, compact( 'field', 'atts' ) );
280
281
		if ( $value == $unfiltered_value ) {
282
			$value = FrmFieldsHelper::get_unfiltered_display_value( compact( 'value', 'field', 'atts' ) );
283
		}
284
285
		if ( $atts['truncate'] && $atts['type'] != 'url' ) {
286
			$value = FrmAppHelper::truncate( $value, 50 );
287
		}
288
289
		if ( ! $atts['keepjs'] && ! is_array( $value ) ) {
290
			$value = FrmAppHelper::kses( $value, 'all' );
291
		}
292
293
		return apply_filters( 'frm_display_value', $value, $field, $atts );
294
	}
295
296
	public static function set_posted_value( $field, $value, $args ) {
297
		// If validating a field with "other" opt, set back to prev value now.
298
		if ( isset( $args['other'] ) && $args['other'] ) {
299
			$value = $args['temp_value'];
300
		}
301
		if ( empty( $args['parent_field_id'] ) ) {
302
			$_POST['item_meta'][ $field->id ] = $value;
303
		} else {
304
			self::set_parent_field_posted_value( $field, $value, $args );
305
		}
306
	}
307
308
	/**
309
	 * Init arrays if necessary, else we get fatal error.
310
	 *
311
	 * @since 4.01
312
	 */
313
	private static function set_parent_field_posted_value( $field, $value, $args ) {
314
		if ( isset( $_POST['item_meta'][ $args['parent_field_id'] ] ) &&
315
			 is_array( $_POST['item_meta'][ $args['parent_field_id'] ] ) ) {
316
317
			if ( ! isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] ) ||
318
				 ! is_array( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] ) ) {
319
				$_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] = array();
320
			}
321
		} else {
322
			// All of the section was probably removed.
323
			$_POST['item_meta'][ $args['parent_field_id'] ] = array();
324
			$_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] = array();
325
		}
326
327
		$_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field->id ] = $value;
328
	}
329
330
	public static function get_posted_value( $field, &$value, $args ) {
331
		if ( is_array( $field ) ) {
332
			$field_id  = $field['id'];
333
			$field_obj = FrmFieldFactory::get_field_object( $field['id'] );
334
		} else if ( is_object( $field ) ) {
335
			$field_id  = $field->id;
336
			$field_obj = FrmFieldFactory::get_field_object( $field );
337
		} else if ( is_numeric( $field ) ) {
338
			$field_id  = $field;
339
			$field_obj = FrmFieldFactory::get_field_object( $field );
340
		} else {
341
			$value = self::get_posted_meta( $field, $args );
342
			FrmAppHelper::sanitize_value( 'sanitize_text_field', $value );
343
			return;
344
		}
345
346
		$value = self::get_posted_meta( $field_id, $args );
347
348
		$field_obj->sanitize_value( $value );
349
	}
350
351
	/**
352
	 * @since 4.02.04
353
	 */
354
	private static function get_posted_meta( $field_id, $args ) {
355
		if ( empty( $args['parent_field_id'] ) ) {
356
			// Sanitizing is done next.
357
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
358
			$value = isset( $_POST['item_meta'][ $field_id ] ) ? wp_unslash( $_POST['item_meta'][ $field_id ] ) : '';
359
		} else {
360
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
361
			$value = isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] ) ? wp_unslash( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] ) : '';
362
		}
363
		return $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
		// Trim excess values if selection limit is exceeded for checkbox. Necessary to do here
382
		// as the value set here will be used later in this class's set_posted_value() method.
383
		if ( FrmField::is_checkbox( $field ) ) {
384
			$field_obj = FrmFieldFactory::get_field_object( $field );
385
			$field_obj->maybe_trim_excess_values( $value );
386
		}
387
388
		// Get other value for fields in repeating section.
389
		self::set_other_repeating_vals( $field, $value, $args );
390
391
		// Check if there are any posted "Other" values.
392
		if ( FrmField::is_option_true( $field, 'other' ) && isset( $_POST['item_meta']['other'][ $field->id ] ) ) {
393
394
			// Save original value.
395
			$args['temp_value'] = $value;
396
			$args['other']      = true;
397
398
			// Sanitizing is done next.
399
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
400
			$other_vals = wp_unslash( $_POST['item_meta']['other'][ $field->id ] );
401
			FrmAppHelper::sanitize_value( 'sanitize_text_field', $other_vals );
402
403
			// Set the validation value now
404
			self::set_other_validation_val( $value, $other_vals, $field, $args );
405
		}
406
	}
407
408
	/**
409
	 * Sets radio or checkbox value equal to "other" value if it is set - FOR REPEATING SECTIONS
410
	 *
411
	 * @since 2.0
412
	 *
413
	 * @param object $field
414
	 * @param string|array $value
415
	 * @param array $args
416
	 */
417
	public static function set_other_repeating_vals( $field, &$value, &$args ) {
418
		if ( ! $args['parent_field_id'] ) {
419
			return;
420
		}
421
422
		// Check if there are any other posted "other" values for this field.
423
		if ( FrmField::is_option_true( $field, 'other' ) && isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ]['other'][ $field->id ] ) ) {
424
			// Save original value
425
			$args['temp_value'] = $value;
426
			$args['other']      = true;
427
428
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
429
			$other_vals = wp_unslash( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ]['other'][ $field->id ] );
430
			FrmAppHelper::sanitize_value( 'sanitize_text_field', $other_vals );
431
432
			// Set the validation value now.
433
			self::set_other_validation_val( $value, $other_vals, $field, $args );
434
		}
435
	}
436
437
	/**
438
	 * Modify value used for validation
439
	 * This function essentially removes the "Other" radio or checkbox value from the $value being validated.
440
	 * It also adds any text from the free text fields to the value
441
	 *
442
	 * Needs to accommodate for times when other opt is selected, but no other free text is entered
443
	 *
444
	 * @since 2.0
445
	 *
446
	 * @param string|array $value
447
	 * @param string|array $other_vals (usually of posted values)
448
	 * @param object $field
449
	 * @param array $args
450
	 */
451
	public static function set_other_validation_val( &$value, $other_vals, $field, &$args ) {
452
		// Checkboxes and multi-select dropdowns.
453
		if ( is_array( $value ) && $field->type == 'checkbox' ) {
454
			// Combine "Other" values with checked values. "Other" values will override checked box values.
455
			$value = array_merge( $value, $other_vals );
456
			$value = array_filter( $value );
457
			if ( count( $value ) == 0 ) {
458
				$value = '';
459
			}
460
		} else {
461
			// Radio and dropdowns.
462
			$other_key = array_filter( array_keys( $field->options ), 'is_string' );
463
			$other_key = reset( $other_key );
464
465
			// Multi-select dropdown.
466
			if ( is_array( $value ) ) {
467
				$o_key = array_search( $field->options[ $other_key ], $value );
468
469
				if ( $o_key !== false ) {
470
					// Modify the original value so other key will be preserved.
471
					$value[ $other_key ] = $value[ $o_key ];
472
473
					// By default, the array keys will be numeric for multi-select dropdowns.
474
					// If going backwards and forwards between pages, the array key will match the other key.
475
					if ( $o_key !== $other_key ) {
476
						unset( $value[ $o_key ] );
477
					}
478
479
					$args['temp_value']  = $value;
480
					$value[ $other_key ] = reset( $other_vals );
481
					if ( FrmAppHelper::is_empty_value( $value[ $other_key ] ) ) {
482
						unset( $value[ $other_key ] );
483
					}
484
				}
485
			} elseif ( $field->options[ $other_key ] == $value ) {
486
				$value = $other_vals;
487
			}
488
		}
489
	}
490
491
	/**
492
	 * Add submitted values to a string for spam checking.
493
	 *
494
	 * @param array $values
495
	 */
496
	public static function entry_array_to_string( $values ) {
497
		$content = '';
498
		foreach ( $values['item_meta'] as $val ) {
499
			if ( $content != '' ) {
500
				$content .= "\n\n";
501
			}
502
503
			if ( is_array( $val ) ) {
504
				$val = FrmAppHelper::array_flatten( $val );
505
				$val = implode( ', ', $val );
506
			}
507
508
			$content .= $val;
509
		}
510
511
		return $content;
512
	}
513
514
	/**
515
	 * Get the browser from the user agent
516
	 *
517
	 * @since 2.04
518
	 *
519
	 * @param string $u_agent
520
	 *
521
	 * @return string
522
	 */
523
	public static function get_browser( $u_agent ) {
524
		$bname    = __( 'Unknown', 'formidable' );
525
		$platform = __( 'Unknown', 'formidable' );
526
		$ub       = '';
527
528
		// Get the operating system
529
		if ( preg_match( '/windows|win32/i', $u_agent ) ) {
530
			$platform = 'Windows';
531
		} elseif ( preg_match( '/android/i', $u_agent ) ) {
532
			$platform = 'Android';
533
		} elseif ( preg_match( '/linux/i', $u_agent ) ) {
534
			$platform = 'Linux';
535
		} elseif ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
536
			$platform = 'OS X';
537
		}
538
539
		$agent_options = array(
540
			'Chrome'   => 'Google Chrome',
541
			'Safari'   => 'Apple Safari',
542
			'Opera'    => 'Opera',
543
			'Netscape' => 'Netscape',
544
			'Firefox'  => 'Mozilla Firefox',
545
		);
546
547
		// Next get the name of the useragent yes seperately and for good reason
548
		if ( strpos( $u_agent, 'MSIE' ) !== false && strpos( $u_agent, 'Opera' ) === false ) {
549
			$bname = 'Internet Explorer';
550
			$ub    = 'MSIE';
551
		} else {
552
			foreach ( $agent_options as $agent_key => $agent_name ) {
553
				if ( strpos( $u_agent, $agent_key ) !== false ) {
554
					$bname = $agent_name;
555
					$ub    = $agent_key;
556
					break;
557
				}
558
			}
559
		}
560
561
		// finally get the correct version number
562
		$known   = array( 'Version', $ub, 'other' );
563
		$pattern = '#(?<browser>' . join( '|', $known ) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
564
		preg_match_all( $pattern, $u_agent, $matches ); // get the matching numbers
565
566
		// see how many we have
567
		$i = count( $matches['browser'] );
568
569
		if ( $i > 1 ) {
570
			//we will have two since we are not using 'other' argument yet
571
			//see if version is before or after the name
572
			if ( strripos( $u_agent, 'Version' ) < strripos( $u_agent, $ub ) ) {
573
				$version = $matches['version'][0];
574
			} else {
575
				$version = $matches['version'][1];
576
			}
577
		} elseif ( $i === 1 ) {
578
			$version = $matches['version'][0];
579
		} else {
580
			$version = '';
581
		}
582
583
		// check if we have a number
584
		if ( $version == '' ) {
585
			$version = '?';
586
		}
587
588
		return $bname . ' ' . $version . ' / ' . $platform;
589
	}
590
591
	/**
592
	 * @since 3.0
593
	 */
594
	public static function actions_dropdown( $atts ) {
595
		$id    = isset( $atts['id'] ) ? $atts['id'] : FrmAppHelper::get_param( 'id', 0, 'get', 'absint' );
596
		$links = self::get_action_links( $id, $atts['entry'] );
597
598
		foreach ( $links as $link ) {
599
			?>
600
		<div class="misc-pub-section">
601
			<a href="<?php echo esc_url( FrmAppHelper::maybe_full_screen_link( $link['url'] ) ); ?>"
602
				<?php
603 View Code Duplication
				if ( isset( $link['data'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
604
					foreach ( $link['data'] as $data => $value ) {
605
						echo 'data-' . esc_attr( $data ) . '="' . esc_attr( $value ) . '" ';
606
					}
607
				}
608
				if ( isset( $link['class'] ) ) {
609
					echo 'class="' . esc_attr( $link['class'] ) . '" ';
610
				}
611
				if ( isset( $link['id'] ) ) {
612
					echo 'id="' . esc_attr( $link['id'] ) . '" ';
613
				}
614
				?>
615
				>
616
				<?php FrmAppHelper::icon_by_class( $link['icon'], array( 'aria-hidden' => 'true' ) ); ?>
617
				<span class="frm_link_label"><?php echo esc_html( $link['label'] ); ?></span>
618
			</a>
619
		</div>
620
			<?php
621
		}
622
	}
623
624
	/**
625
	 * @since 3.0
626
	 */
627
	private static function get_action_links( $id, $entry ) {
628
		$page    = FrmAppHelper::get_param( 'frm_action' );
629
		$actions = array();
630
631
		if ( $page != 'show' ) {
632
			$actions['frm_view'] = array(
633
				'url'   => admin_url( 'admin.php?page=formidable-entries&frm_action=show&id=' . $id . '&form=' . $entry->form_id ),
634
				'label' => __( 'View Entry', 'formidable' ),
635
				'icon'  => 'frm_icon_font frm_save_icon',
636
			);
637
		}
638
639
		if ( current_user_can( 'frm_delete_entries' ) ) {
640
			$actions['frm_delete'] = array(
641
				'url'   => admin_url( 'admin.php?page=formidable-entries&frm_action=destroy&id=' . $id . '&form=' . $entry->form_id ),
642
				'label' => __( 'Delete Entry', 'formidable' ),
643
				'icon'  => 'frm_icon_font frm_delete_icon',
644
				'data'  => array(
645
					'frmverify' => __( 'Delete this form entry?', 'formidable' ),
646
				),
647
			);
648
		}
649
650
		if ( $page == 'show' ) {
651
			$actions['frm_print'] = array(
652
				'url'   => '#',
653
				'label' => __( 'Print Entry', 'formidable' ),
654
				'data'  => array(
655
					'frmprint' => '1',
656
				),
657
				'icon'  => 'frm_icon_font frm_printer_icon',
658
			);
659
		}
660
661
		$actions['frm_resend'] = array(
662
			'url'   => '#',
663
			'label' => __( 'Resend Emails', 'formidable' ),
664
			'class' => 'frm_noallow',
665
			'data'  => array(
666
				'upgrade' => __( 'Resend Emails', 'formidable' ),
667
				'medium'  => 'resend-email',
668
				'content' => 'entry',
669
			),
670
			'icon'  => 'frm_icon_font frm_email_icon',
671
		);
672
673
		$actions['frm_edit'] = array(
674
			'url'   => '#',
675
			'label' => __( 'Edit Entry', 'formidable' ),
676
			'class' => 'frm_noallow',
677
			'data'  => array(
678
				'upgrade' => __( 'Entry edits', 'formidable' ),
679
				'medium'  => 'edit-entries',
680
				'content' => 'entry',
681
			),
682
			'icon'  => 'frm_icon_font frm_pencil_icon',
683
		);
684
685
		return apply_filters( 'frm_entry_actions_dropdown', $actions, compact( 'id', 'entry' ) );
686
	}
687
}
688