Completed
Push — master ( c0d103...e95ed8 )
by Stephanie
02:17
created

FrmEntriesHelper::get_posted_meta()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 11
rs 9.9
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
		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
			foreach ( $other_vals as $k => $v ) {
0 ignored issues
show
Bug introduced by
The expression $other_vals of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
456
				if ( isset( $value[ $k ] ) && trim( $v ) === '' ) {
457
					// If the other box is checked, but doesn't have a value.
458
					$value = '';
459
					break;
460
				}
461
			}
462
463
			if ( is_array( $value ) && ! empty( $value ) ) {
464
				$value = array_merge( $value, $other_vals );
465
			}
466
		} else {
467
			// Radio and dropdowns.
468
			$other_key = array_filter( array_keys( $field->options ), 'is_string' );
469
			$other_key = reset( $other_key );
470
471
			// Multi-select dropdown.
472
			if ( is_array( $value ) ) {
473
				$o_key = array_search( $field->options[ $other_key ], $value );
474
475
				if ( $o_key !== false ) {
476
					// Modify the original value so other key will be preserved.
477
					$value[ $other_key ] = $value[ $o_key ];
478
479
					// By default, the array keys will be numeric for multi-select dropdowns.
480
					// If going backwards and forwards between pages, the array key will match the other key.
481
					if ( $o_key !== $other_key ) {
482
						unset( $value[ $o_key ] );
483
					}
484
485
					$args['temp_value']  = $value;
486
					$value[ $other_key ] = reset( $other_vals );
487
					if ( FrmAppHelper::is_empty_value( $value[ $other_key ] ) ) {
488
						unset( $value[ $other_key ] );
489
					}
490
				}
491
			} elseif ( $field->options[ $other_key ] == $value ) {
492
				$value = $other_vals;
493
			}
494
		}
495
	}
496
497
	/**
498
	 * Add submitted values to a string for spam checking.
499
	 *
500
	 * @param array $values
501
	 */
502
	public static function entry_array_to_string( $values ) {
503
		$content = '';
504
		foreach ( $values['item_meta'] as $val ) {
505
			if ( $content != '' ) {
506
				$content .= "\n\n";
507
			}
508
509
			if ( is_array( $val ) ) {
510
				$val = FrmAppHelper::array_flatten( $val );
511
				$val = implode( ', ', $val );
512
			}
513
514
			$content .= $val;
515
		}
516
517
		return $content;
518
	}
519
520
	/**
521
	 * Get the browser from the user agent
522
	 *
523
	 * @since 2.04
524
	 *
525
	 * @param string $u_agent
526
	 *
527
	 * @return string
528
	 */
529
	public static function get_browser( $u_agent ) {
530
		$bname    = __( 'Unknown', 'formidable' );
531
		$platform = __( 'Unknown', 'formidable' );
532
		$ub       = '';
533
534
		// Get the operating system
535
		if ( preg_match( '/windows|win32/i', $u_agent ) ) {
536
			$platform = 'Windows';
537
		} elseif ( preg_match( '/android/i', $u_agent ) ) {
538
			$platform = 'Android';
539
		} elseif ( preg_match( '/linux/i', $u_agent ) ) {
540
			$platform = 'Linux';
541
		} elseif ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
542
			$platform = 'OS X';
543
		}
544
545
		$agent_options = array(
546
			'Chrome'   => 'Google Chrome',
547
			'Safari'   => 'Apple Safari',
548
			'Opera'    => 'Opera',
549
			'Netscape' => 'Netscape',
550
			'Firefox'  => 'Mozilla Firefox',
551
		);
552
553
		// Next get the name of the useragent yes seperately and for good reason
554
		if ( strpos( $u_agent, 'MSIE' ) !== false && strpos( $u_agent, 'Opera' ) === false ) {
555
			$bname = 'Internet Explorer';
556
			$ub    = 'MSIE';
557
		} else {
558
			foreach ( $agent_options as $agent_key => $agent_name ) {
559
				if ( strpos( $u_agent, $agent_key ) !== false ) {
560
					$bname = $agent_name;
561
					$ub    = $agent_key;
562
					break;
563
				}
564
			}
565
		}
566
567
		// finally get the correct version number
568
		$known   = array( 'Version', $ub, 'other' );
569
		$pattern = '#(?<browser>' . join( '|', $known ) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
570
		preg_match_all( $pattern, $u_agent, $matches ); // get the matching numbers
571
572
		// see how many we have
573
		$i = count( $matches['browser'] );
574
575
		if ( $i > 1 ) {
576
			//we will have two since we are not using 'other' argument yet
577
			//see if version is before or after the name
578
			if ( strripos( $u_agent, 'Version' ) < strripos( $u_agent, $ub ) ) {
579
				$version = $matches['version'][0];
580
			} else {
581
				$version = $matches['version'][1];
582
			}
583
		} elseif ( $i === 1 ) {
584
			$version = $matches['version'][0];
585
		} else {
586
			$version = '';
587
		}
588
589
		// check if we have a number
590
		if ( $version == '' ) {
591
			$version = '?';
592
		}
593
594
		return $bname . ' ' . $version . ' / ' . $platform;
595
	}
596
597
	/**
598
	 * @since 3.0
599
	 */
600
	public static function actions_dropdown( $atts ) {
601
		$id    = isset( $atts['id'] ) ? $atts['id'] : FrmAppHelper::get_param( 'id', 0, 'get', 'absint' );
602
		$links = self::get_action_links( $id, $atts['entry'] );
603
604
		foreach ( $links as $link ) {
605
			?>
606
		<div class="misc-pub-section">
607
			<a href="<?php echo esc_url( FrmAppHelper::maybe_full_screen_link( $link['url'] ) ); ?>"
608
				<?php
609 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...
610
					foreach ( $link['data'] as $data => $value ) {
611
						echo 'data-' . esc_attr( $data ) . '="' . esc_attr( $value ) . '" ';
612
					}
613
				}
614
				if ( isset( $link['class'] ) ) {
615
					echo 'class="' . esc_attr( $link['class'] ) . '" ';
616
				}
617
				if ( isset( $link['id'] ) ) {
618
					echo 'id="' . esc_attr( $link['id'] ) . '" ';
619
				}
620
				?>
621
				>
622
				<?php FrmAppHelper::icon_by_class( $link['icon'], array( 'aria-hidden' => 'true' ) ); ?>
623
				<span class="frm_link_label"><?php echo esc_html( $link['label'] ); ?></span>
624
			</a>
625
		</div>
626
			<?php
627
		}
628
	}
629
630
	/**
631
	 * @since 3.0
632
	 */
633
	private static function get_action_links( $id, $entry ) {
634
		$page    = FrmAppHelper::get_param( 'frm_action' );
635
		$actions = array();
636
637
		if ( $page != 'show' ) {
638
			$actions['frm_view'] = array(
639
				'url'   => admin_url( 'admin.php?page=formidable-entries&frm_action=show&id=' . $id . '&form=' . $entry->form_id ),
640
				'label' => __( 'View Entry', 'formidable' ),
641
				'icon'  => 'frm_icon_font frm_save_icon',
642
			);
643
		}
644
645
		if ( current_user_can( 'frm_delete_entries' ) ) {
646
			$actions['frm_delete'] = array(
647
				'url'   => admin_url( 'admin.php?page=formidable-entries&frm_action=destroy&id=' . $id . '&form=' . $entry->form_id ),
648
				'label' => __( 'Delete Entry', 'formidable' ),
649
				'icon'  => 'frm_icon_font frm_delete_icon',
650
				'data'  => array(
651
					'frmverify' => __( 'Delete this form entry?', 'formidable' ),
652
				),
653
			);
654
		}
655
656
		if ( $page == 'show' ) {
657
			$actions['frm_print'] = array(
658
				'url'   => '#',
659
				'label' => __( 'Print Entry', 'formidable' ),
660
				'data'  => array(
661
					'frmprint' => '1',
662
				),
663
				'icon'  => 'frm_icon_font frm_printer_icon',
664
			);
665
		}
666
667
		$actions['frm_resend'] = array(
668
			'url'   => '#',
669
			'label' => __( 'Resend Emails', 'formidable' ),
670
			'class' => 'frm_noallow',
671
			'data'  => array(
672
				'upgrade' => __( 'Resend Emails', 'formidable' ),
673
				'medium'  => 'resend-email',
674
				'content' => 'entry',
675
			),
676
			'icon'  => 'frm_icon_font frm_email_icon',
677
		);
678
679
		$actions['frm_edit'] = array(
680
			'url'   => '#',
681
			'label' => __( 'Edit Entry', 'formidable' ),
682
			'class' => 'frm_noallow',
683
			'data'  => array(
684
				'upgrade' => __( 'Entry edits', 'formidable' ),
685
				'medium'  => 'edit-entries',
686
				'content' => 'entry',
687
			),
688
			'icon'  => 'frm_icon_font frm_pencil_icon',
689
		);
690
691
		return apply_filters( 'frm_entry_actions_dropdown', $actions, compact( 'id', 'entry' ) );
692
	}
693
}
694