Completed
Push — master ( 3860cc...10e837 )
by Stephanie
9s
created

FrmEntryFormat::show_entry()   D

Complexity

Conditions 16
Paths 436

Size

Total Lines 68
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 44
nc 436
nop 1
dl 0
loc 68
rs 4.1081
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class FrmEntryFormat {
4
	public static function show_entry( $atts ) {
5
		$atts = shortcode_atts( array(
6
			'id' => false, 'entry' => false, 'fields' => false, 'plain_text' => false,
7
			'user_info' => false, 'include_blank' => false, 'default_email' => false,
8
			'form_id' => false, 'format' => 'text', 'direction' => 'ltr',
9
			'font_size' => '', 'text_color' => '',
10
			'border_width' => '', 'border_color' => '',
11
			'bg_color' => '', 'alt_bg_color' => '',
12
			'clickable' => false,
13
			'exclude_fields' => '', 'include_fields' => '',
14
			'include_extras' => 'section', 'inline_style' => 1,
15
		), $atts );
16
17
		$atts['exclude_fields'] = self::comma_list_to_array( $atts['exclude_fields'] );
18
		$atts['include_fields'] = self::comma_list_to_array( $atts['include_fields'] );
19
		$atts['include_extras'] = self::comma_list_to_array( $atts['include_extras'] );
20
21
		if ( $atts['format'] != 'text' ) {
22
			//format options are text, array, or json
23
			$atts['plain_text'] = true;
24
		}
25
26
		if ( is_object( $atts['entry'] ) && ! isset( $atts['entry']->metas ) ) {
27
			// if the entry does not include metas, force it again
28
			$atts['entry'] = false;
29
		}
30
31
		if ( ! $atts['entry'] || ! is_object( $atts['entry'] ) ) {
32
			if ( ! $atts['id'] && ! $atts['default_email'] ) {
33
				return '';
34
			}
35
36
			if ( $atts['id'] ) {
37
				$atts['entry'] = FrmEntry::getOne( $atts['id'], true );
38
			}
39
		}
40
41
		if ( $atts['entry'] ) {
42
			$atts['form_id'] = $atts['entry']->form_id;
43
			$atts['id'] = $atts['entry']->id;
44
		}
45
46
		if ( ! $atts['fields'] || ! is_array( $atts['fields'] ) ) {
47
			$atts['fields'] = FrmField::get_all_for_form( $atts['form_id'], '', 'include' );
48
		}
49
50
		$values = array();
51
		foreach ( $atts['fields'] as $f ) {
52
			if ( ! self::skip_field( $atts, $f ) ) {
53
				self::fill_entry_values( $atts, $f, $values );
54
			}
55
			unset($f);
56
		}
57
58
		self::fill_entry_user_info( $atts, $values );
59
		$values = apply_filters( 'frm_show_entry_array', $values, $atts );
60
61
		if ( $atts['format'] == 'json' ) {
62
			$content = json_encode( $values );
63
		} else if ( $atts['format'] == 'array' ) {
64
			$content = $values;
65
		} else {
66
			$content = array();
67
			self::prepare_text_output( $values, $atts, $content );
68
		}
69
70
		return $content;
71
	}
72
73
	private static function comma_list_to_array( $list ) {
74
		$array = array_map( 'strtolower', array_map( 'trim', explode( ',', $list ) ) );
75
		$field_types = array(
76
			'section' => 'divider',
77
			'heading' => 'divider',
78
			'page'    => 'break',
79
		);
80
		foreach ( $field_types as $label => $field_type ) {
81
			if ( in_array( $label, $array ) ) {
82
				$array[] = $field_type;
83
			}
84
		}
85
		return $array;
86
	}
87
88
	private static function skip_field( $atts, $field ) {
89
		$skip = ( $field->type == 'password' || $field->type == 'credit_card' );
90
91
		if ( $skip && ! empty( $atts['include_extras'] ) ) {
92
			$skip = ! in_array( $field->type, $atts['include_extras'] );
93
		}
94
95
		if ( ! $skip && ! empty( $atts['exclude_fields'] ) ) {
96
			$skip = self::field_in_list( $field, $atts['exclude_fields'] );
97
		}
98
99
		if ( $skip && ! empty( $atts['include_fields'] ) ) {
100
			$skip = ! self::field_in_list( $field, $atts['include_fields'] );
101
		}
102
103
		return $skip;
104
	}
105
106
	private static function field_in_list( $field, $list ) {
107
		return ( in_array( $field->id, $list ) || in_array( $field->field_key, $list ) );
108
	}
109
110
	/**
111
	 * Get the labels and value shortcodes for fields in the Default HTML email message
112
	 *
113
	 * @since 2.0.23
114
	 * @param object $f
115
	 * @param array $values
116
	 */
117
	public static function get_field_shortcodes_for_default_email( $f, &$values ) {
118
		$field_shortcodes = array(
119
			'label' => '[' . $f->id . ' show=field_label]',
120
			'val'   => '[' . $f->id . ']',
121
			'type'  => $f->type,
122
		);
123
124
		$values[ $f->id ] = apply_filters( 'frm_field_shortcodes_for_default_html_email', $field_shortcodes, $f );
125
	}
126
127
	public static function fill_entry_values( $atts, $f, array &$values ) {
128
		$no_save_field = FrmField::is_no_save_field( $f->type );
129
		if ( $no_save_field ) {
130
			if ( ! in_array( $f->type, $atts['include_extras'] ) ) {
131
				return;
132
			}
133
			$atts['include_blank'] = true;
134
		}
135
136
		if ( $atts['default_email'] ) {
137
			self::get_field_shortcodes_for_default_email( $f, $values );
138
			return;
139
		}
140
141
		$atts['field'] = $f;
142
143
		self::fill_missing_fields( $atts, $values );
144
145
		$val = '';
146
		self::get_field_value( $atts, $val );
147
148
		// Don't include blank values
149
		if ( ! $atts['include_blank'] && FrmAppHelper::is_empty_value( $val ) ) {
150
			return;
151
		}
152
153
		self::textarea_display_value( $f->type, $atts['plain_text'], $val );
154
		$val = apply_filters( 'frm_display_' . $f->type . '_value_custom', $val, array( 'field' => $f, 'atts' => $atts ) );
155
156
		if ( is_array( $val ) ) {
157
			if ( $atts['format'] == 'text' ) {
158
				$val = implode( ', ', $val );
159
			} else if ( $f->type == 'checkbox' ) {
160
				$val = array_values( $val );
161
			}
162
		}
163
164
		self::maybe_strip_html( $atts['plain_text'], $val );
165
166
		if ( $atts['format'] != 'text' ) {
167
			$values[ $f->field_key ] = $val;
168
			if ( $atts['entry'] && $f->type != 'textarea' ) {
169
				$prev_val = maybe_unserialize( $atts['entry']->metas[ $f->id ] );
170
				if ( $prev_val != $val ) {
171
					$values[ $f->field_key . '-value' ] = $prev_val;
172
				}
173
			}
174
		} else {
175
			$values[ $f->id ] = array( 'label' => $f->name, 'val' => $val, 'type' => $f->type );
176
		}
177
	}
178
179
	private static function fill_missing_fields( $atts, &$values ) {
180
		if ( $atts['entry'] && ! isset( $atts['entry']->metas[ $atts['field']->id ] ) ) {
181
			// In case include_blank is set
182
			$atts['entry']->metas[ $atts['field']->id ] = '';
183
			$atts['entry'] = apply_filters( 'frm_prepare_entry_content', $atts['entry'], array( 'field' => $atts['field'] ) );
184
			self::fill_values_from_entry( $atts, $values );
185
		}
186
	}
187
188
	public static function fill_values_from_entry( $atts, &$values ) {
189
		$values = apply_filters( 'frm_prepare_entry_array', $values, $atts );
190
	}
191
192
	private static function get_field_value( $atts, &$val ) {
193
		$f = $atts['field'];
194
		if ( $atts['entry'] ) {
195
			$prev_val = maybe_unserialize( $atts['entry']->metas[ $f->id ] );
196
			$meta = array( 'item_id' => $atts['id'], 'field_id' => $f->id, 'meta_value' => $prev_val, 'field_type' => $f->type );
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
197
198
			//This filter applies to the default-message shortcode and frm-show-entry shortcode only
199
			if ( in_array( $f->type, array( 'html', 'divider', 'break' ) ) ) {
200
				$val = apply_filters( 'frm_content', $f->description, $atts['form_id'], $atts['entry'] );
201
			} elseif ( isset( $atts['filter'] ) && $atts['filter'] == false ) {
202
				$val = $prev_val;
203
			} else {
204
				$val = apply_filters( 'frm_email_value', $prev_val, (object) $meta, $atts['entry'], compact( 'field' ) );
205
			}
206
		}
207
	}
208
209
	/**
210
	* Flatten multi-dimensional array for multi-file upload fields
211
	* @since 2.0.9
212
	*/
213
	public static function flatten_multi_file_upload( $field, &$val ) {
214
		if ( $field->type == 'file' && FrmField::is_option_true( $field, 'multiple' ) ) {
215
			$val = FrmAppHelper::array_flatten( $val );
216
		}
217
	}
218
219
    /**
220
     * Replace returns with HTML line breaks for display
221
     * @since 2.0.9
222
     */
223
	public static function textarea_display_value( $type, $plain_text, &$value ) {
224
		if ( $type == 'textarea' && ! $plain_text ) {
225
			$value = str_replace( array( "\r\n", "\r", "\n" ), ' <br/>', $value );
226
		}
227
	}
228
229
	/**
230
	 * Strip HTML if from email value if plain text is selected
231
	 *
232
	 * @since 2.0.21
233
	 * @param boolean $plain_text
234
	 * @param mixed $val
235
	 */
236
	private static function maybe_strip_html( $plain_text, &$val ) {
237
		if ( $plain_text && ! is_array( $val ) ) {
238
			if ( strpos( $val, '<img' ) !== false ) {
239
				$val = str_replace( array( '<img', 'src=', '/>', '"' ), '', $val );
240
				$val = trim( $val );
241
			}
242
			$val = strip_tags( $val );
243
		}
244
	}
245
246
	public static function fill_entry_user_info( $atts, array &$values ) {
247
		if ( ! $atts['user_info'] || empty( $atts['entry'] ) ) {
248
			return;
249
		}
250
251
		$data  = self::get_entry_description_data( $atts );
252
253
		if ( $atts['default_email'] ) {
254
			$atts['entry']->ip = '[ip]';
255
		}
256
257
		if ( $atts['format'] != 'text' ) {
258
			$values['ip'] = $atts['entry']->ip;
259
			$values['browser'] = self::get_browser( $data['browser'] );
260
			$values['referrer'] = $data['referrer'];
261
		} else {
262
			$values['ip'] = array( 'label' => __( 'IP Address', 'formidable' ), 'val' => $atts['entry']->ip );
263
			$values['browser'] = array(
264
				'label' => __( 'User-Agent (Browser/OS)', 'formidable' ),
265
				'val'   => self::get_browser( $data['browser'] ),
266
			);
267
			$values['referrer'] = array( 'label' => __( 'Referrer', 'formidable' ), 'val' => $data['referrer'] );
268
		}
269
	}
270
271
	/**
272
	 * @param array $atts - include (object) entry, (boolean) default_email
273
	 * @since 2.0.9
274
	 */
275
	public static function get_entry_description_data( $atts ) {
276
		$default_data = array(
277
			'browser' => '',
278
			'referrer' => '',
279
		);
280
		$data = $default_data;
281
282
		if ( isset( $atts['entry']->description ) ) {
283
			$data = (array) maybe_unserialize( $atts['entry']->description );
284
		} else if ( $atts['default_email'] ) {
285
			$data = array(
286
				'browser'  => '[browser]',
287
				'referrer' => '[referrer]',
288
			);
289
		}
290
291
		return array_merge( $default_data, $data );
292
	}
293
294
	public static function get_browser( $u_agent ) {
295
		$bname = __( 'Unknown', 'formidable' );
296
		$platform = __( 'Unknown', 'formidable' );
297
		$ub = '';
298
299
		// Get the operating system
300
		if ( preg_match( '/windows|win32/i', $u_agent ) ) {
301
			$platform = 'Windows';
302
		} else if ( preg_match( '/android/i', $u_agent ) ) {
303
			$platform = 'Android';
304
		} else if ( preg_match( '/linux/i', $u_agent ) ) {
305
			$platform = 'Linux';
306
		} else if ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
307
			$platform = 'OS X';
308
		}
309
310
		$agent_options = array(
311
			'Chrome'   => 'Google Chrome',
312
			'Safari'   => 'Apple Safari',
313
			'Opera'    => 'Opera',
314
			'Netscape' => 'Netscape',
315
			'Firefox'  => 'Mozilla Firefox',
316
		);
317
318
		// Next get the name of the useragent yes seperately and for good reason
319
		if ( strpos( $u_agent, 'MSIE' ) !== false && strpos( $u_agent, 'Opera' ) === false ) {
320
			$bname = 'Internet Explorer';
321
			$ub = 'MSIE';
322
		} else {
323
			foreach ( $agent_options as $agent_key => $agent_name ) {
324
				if ( strpos( $u_agent, $agent_key ) !== false ) {
325
					$bname = $agent_name;
326
					$ub = $agent_key;
327
					break;
328
				}
329
			}
330
		}
331
332
		// finally get the correct version number
333
		$known = array( 'Version', $ub, 'other' );
334
		$pattern = '#(?<browser>' . join( '|', $known ) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
335
		preg_match_all( $pattern, $u_agent, $matches ); // get the matching numbers
336
337
		// see how many we have
338
		$i = count($matches['browser']);
339
		if ( $i != 1 ) {
340
			//we will have two since we are not using 'other' argument yet
341
			//see if version is before or after the name
342
			if ( strripos( $u_agent, 'Version' ) < strripos( $u_agent, $ub ) ) {
343
				$version = $matches['version'][0];
344
			} else {
345
				$version = $matches['version'][1];
346
			}
347
		} else {
348
			$version = $matches['version'][0];
349
		}
350
351
		// check if we have a number
352
		if ( $version == '' ) {
353
			$version = '?';
354
		}
355
356
		return $bname . ' ' . $version . ' / ' . $platform;
357
	}
358
359
	private static function prepare_text_output( $values, $atts, &$content ) {
360
		self::convert_entry_to_content( $values, $atts, $content );
361
362
		if ( 'text' == $atts['format'] ) {
363
			$content = implode('', $content);
364
		}
365
366
		if ( $atts['clickable'] ) {
367
			$content = make_clickable( $content );
368
		}
369
	}
370
371
	public static function convert_entry_to_content( $values, $atts, array &$content ) {
372
		if ( $atts['plain_text'] ) {
373
			self::plain_text_content( $values, $atts, $content );
374
		} else {
375
			self::html_content( $values, $atts, $content );
376
		}
377
	}
378
379
	private static function plain_text_content( $values, $atts, &$content ) {
380
		foreach ( $values as $id => $value ) {
381
			$atts['id'] = $id;
382
			$atts['value'] = $value;
383
			self::single_plain_text_row( $atts, $content );
384
		}
385
	}
386
387
	private static function html_content( $values, $atts, &$content ) {
388
		self::setup_defaults( $atts );
389
		self::prepare_inline_styles( $atts );
390
391
		$content[] = '<table cellspacing="0" ' . $atts['table_style'] . '><tbody>' . "\r\n";
392
393
		$atts['odd'] = true;
394
		foreach ( $values as $id => $value ) {
395
			$atts['id'] = $id;
396
			$atts['value'] = $value;
397
			self::single_html_row( $atts, $content );
398
			$atts['odd'] = ! $atts['odd'];
399
		}
400
401
		$content[] = '</tbody></table>';
402
	}
403
404
	private static function setup_defaults( &$atts ) {
405
		$default_settings = apply_filters( 'frm_show_entry_styles', array(
406
			'border_color' => 'dddddd',
407
			'bg_color'     => 'f7f7f7',
408
			'text_color'   => '444444',
409
			'font_size'    => '12px',
410
			'border_width' => '1px',
411
			'alt_bg_color' => 'ffffff',
412
		) );
413
414
		// merge defaults, global settings, and shortcode options
415
		foreach ( $default_settings as $key => $setting ) {
416
			if ( $atts[ $key ] != '' ) {
417
				continue;
418
			}
419
420
			$atts[ $key ] = $setting;
421
			unset( $key, $setting );
422
		}
423
	}
424
425
	private static function prepare_inline_styles( &$atts ) {
426
		if ( empty( $atts['inline_style'] ) ) {
427
			$atts['table_style'] = $atts['bg_color'] = $atts['bg_color_alt'] = $atts['row_style'] = '';
428
		} else {
429
			$atts['table_style'] = ' style="' . esc_attr( 'font-size:' . $atts['font_size'] . ';line-height:135%; border-bottom:' . $atts['border_width'] . ' solid #' . $atts['border_color'] . ';' ) . '"';
430
431
			$row_style_attributes = 'text-align:' . ( $atts['direction'] == 'rtl' ? 'right' : 'left' ) . ';';
432
			$row_style_attributes .= 'color:#' . $atts['text_color'] . ';padding:7px 9px;vertical-align:top;';
433
			$row_style_attributes .= 'border-top:' . $atts['border_width'] . ' solid #' . $atts['border_color'] . ';';
434
			$atts['row_style'] = ' style="' . $row_style_attributes . '"';
435
436
			if ( $atts['default_email'] ) {
437
				$atts['bg_color'] = $atts['bg_color_alt'] = ' style="[frm-alt-color]"';
438
			} else {
439
				$atts['bg_color'] = ' style="background-color:#' . $atts['bg_color'] . ';"';
440
				$atts['bg_color_alt'] = ' style="background-color:#' . $atts['alt_bg_color'] . ';"';
441
			}
442
		}
443
	}
444
445
	public static function single_plain_text_row( $atts, &$content ) {
446
		$row = array();
447
		if ( 'rtl' == $atts['direction'] ) {
448
			$row[] = $atts['value']['val'] . ' :' . $atts['value']['label'] . "\r\n";
449
		} else {
450
			$row[] = $atts['value']['label'] . ': ' . $atts['value']['val'] . "\r\n";
451
		}
452
		$row = apply_filters( 'frm_entry_plain_text_row', $row, $atts );
453
		$content = array_merge( $content, $row );
454
	}
455
456
	public static function single_html_row( $atts, &$content ) {
457
		$row = array();
458
		if ( $atts['default_email'] && is_numeric( $atts['id'] ) ) {
459
			self::default_email_row( $atts, $row );
460
		} else {
461
			self::row_content( $atts, $row );
462
		}
463
		$row = apply_filters( 'frm_entry_html_row', $row, $atts );
464
		$content = array_merge( $content, $row );
465
	}
466
467
	public static function html_field_row( $atts, &$content ) {
468
		$content[] = '<tr ' . self::table_row_style( $atts ) . '>';
469
		$content[] = '<td colspan="2" ' . $atts['row_style'] . '>' . $atts['value']['val'] . '</td>';
470
		$content[] = '</tr>' . "\r\n";
471
	}
472
473
	private static function default_email_row( $atts, &$content ) {
474
		$content[] = '[if ' . $atts['id'] . ']';
475
		self::row_content( $atts, $content );
476
		$content[] = '[/if ' . $atts['id'] . ']' . "\r\n";
477
	}
478
479
	private static function row_content( $atts, &$content ) {
480
		$content[] = '<tr' . self::table_row_style( $atts ) . '>';
481
482
		$atts['value']['val'] = str_replace( "\r\n", '<br/>', $atts['value']['val'] );
483
484
		if ( 'rtl' == $atts['direction'] ) {
485
			$first = $atts['value']['val'];
486
			$second = $atts['value']['label'];
487
		} else {
488
			$first = $atts['value']['label'];
489
			$second = $atts['value']['val'];
490
		}
491
492
		$content[] = '<td ' . $atts['row_style'] . '>' . $first . '</td>';
493
		$content[] = '<td ' . $atts['row_style'] . '>' . $second . '</td>';
494
495
		$content[] = '</tr>' . "\r\n";
496
	}
497
498
	private static function table_row_style( $atts ) {
499
		return ( $atts['odd'] ? $atts['bg_color'] : $atts['bg_color_alt'] );
500
	}
501
}
502