Completed
Push — master ( 29b5ef...9e7308 )
by Stephanie
03:30
created

FrmEntryFormat   D

Complexity

Total Complexity 80

Size/Duplication

Total Lines 342
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 80
lcom 1
cbo 3
dl 0
loc 342
rs 4.8717

9 Methods

Rating   Name   Duplication   Size   Complexity  
F show_entry() 0 67 17
A flatten_multi_file_upload() 0 5 3
A textarea_display_value() 0 5 3
C fill_entry_values() 0 55 17
A maybe_strip_html() 0 8 4
B fill_entry_user_info() 0 24 5
A get_entry_description_data() 0 18 3
C get_browser() 0 64 12
C convert_entry_to_content() 0 67 16

How to fix   Complexity   

Complex Class

Complex classes like FrmEntryFormat 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 FrmEntryFormat, and based on these observations, apply Extract Interface, too.

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
		), $atts );
14
15
		if ( $atts['format'] != 'text' ) {
16
			//format options are text, array, or json
17
			$atts['plain_text'] = true;
18
		}
19
20
		if ( is_object( $atts['entry'] ) && ! isset( $atts['entry']->metas ) ) {
21
			// if the entry does not include metas, force it again
22
			$atts['entry'] = false;
23
		}
24
25
		if ( ! $atts['entry'] || ! is_object( $atts['entry'] ) ) {
26
			if ( ! $atts['id'] && ! $atts['default_email'] ) {
27
				return;
28
			}
29
30
			if ( $atts['id'] ) {
31
				$atts['entry'] = FrmEntry::getOne( $atts['id'], true );
32
			}
33
		}
34
35
		if ( $atts['entry'] ) {
36
			$atts['form_id'] = $atts['entry']->form_id;
37
			$atts['id'] = $atts['entry']->id;
38
		}
39
40
		if ( ! $atts['fields'] || ! is_array($atts['fields']) ) {
41
			$atts['fields'] = FrmField::get_all_for_form( $atts['form_id'], '', 'include' );
42
		}
43
44
		$values = array();
45
		foreach ( $atts['fields'] as $f ) {
46
			self::fill_entry_values( $atts, $f, $values );
47
			unset($f);
48
		}
49
50
		self::fill_entry_user_info( $atts, $values );
51
52
		if ( $atts['format'] == 'json' ) {
53
			return json_encode($values);
54
		} else if ( $atts['format'] == 'array' ) {
55
			return $values;
56
		}
57
58
		$content = array();
59
		self::convert_entry_to_content( $values, $atts, $content );
60
61
		if ( 'text' == $atts['format'] ) {
62
			$content = implode('', $content);
63
		}
64
65
		if ( $atts['clickable'] ) {
66
			$content = make_clickable( $content );
67
		}
68
69
		return $content;
70
	}
71
72
	public static function fill_entry_values( $atts, $f, array &$values ) {
73
		if ( FrmField::is_no_save_field( $f->type ) ) {
74
			return;
75
		}
76
77
		if ( $atts['default_email'] ) {
78
			$values[ $f->id ] = array( 'label' => '[' . $f->id . ' show=field_label]', 'val' => '[' . $f->id . ']' );
79
			return;
80
		}
81
82
		if ( $atts['entry'] && ! isset( $atts['entry']->metas[ $f->id ] ) ) {
83
			// In case include_blank is set
84
			$atts['entry']->metas[ $f->id ] = '';
85
86
			if ( FrmAppHelper::pro_is_installed() ) {
87
				FrmProEntryMeta::add_post_value_to_entry( $f, $atts['entry'] );
88
				FrmProEntryMeta::add_repeating_value_to_entry( $f, $atts['entry'] );
89
			}
90
		}
91
92
		$val = '';
93
		if ( $atts['entry'] ) {
94
			$prev_val = maybe_unserialize( $atts['entry']->metas[ $f->id ] );
95
			$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...
96
97
			//This filter applies to the default-message shortcode and frm-show-entry shortcode only
98
			if ( isset( $atts['filter'] ) && $atts['filter'] == false ) {
99
				$val = $prev_val;
100
			} else {
101
				$val = apply_filters( 'frm_email_value', $prev_val, (object) $meta, $atts['entry'] );
102
			}
103
		}
104
105
		// Don't include blank values
106
		if ( ! $atts['include_blank'] && FrmAppHelper::is_empty_value( $val ) ) {
107
			return;
108
		}
109
110
		self::textarea_display_value( $f->type, $atts['plain_text'], $val );
111
112
		if ( is_array( $val ) && $atts['format'] == 'text' ) {
113
			$val = implode( ', ', $val );
114
		}
115
116
		self::maybe_strip_html( $atts['plain_text'], $val );
117
118
		if ( $atts['format'] != 'text' ) {
119
			$values[ $f->field_key ] = $val;
120
			if ( isset( $prev_val ) && $prev_val != $val && $f->type != 'textarea' ) {
121
				$values[ $f->field_key .'-value' ] = $prev_val;
122
			}
123
		} else {
124
			$values[ $f->id ] = array( 'label' => $f->name, 'val' => $val );
125
		}
126
	}
127
128
	/**
129
	* Flatten multi-dimensional array for multi-file upload fields
130
	* @since 2.0.9
131
	*/
132
	public static function flatten_multi_file_upload( $field, &$val ) {
133
		if ( $field->type == 'file' && FrmField::is_option_true( $field, 'multiple' ) ) {
134
			$val = FrmAppHelper::array_flatten( $val );
135
		}
136
	}
137
138
    /**
139
     * Replace returns with HTML line breaks for display
140
     * @since 2.0.9
141
     */
142
	public static function textarea_display_value( $type, $plain_text, &$value ) {
143
		if ( $type == 'textarea' && ! $plain_text ) {
144
			$value = str_replace( array( "\r\n", "\r", "\n" ), ' <br/>', $value );
145
		}
146
	}
147
148
	/**
149
	 * Strip HTML if from email value if plain text is selected
150
	 *
151
	 * @since 2.0.21
152
	 * @param boolean $plain_text
153
	 * @param mixed $val
154
	 */
155
	private static function maybe_strip_html( $plain_text, &$val ) {
156
		if ( $plain_text && ! is_array( $val ) ) {
157
			if ( strpos( $val, '<img' ) !== false ) {
158
				$val = str_replace( array( '<img', 'src=', '/>', '"' ), '', $val );
159
			}
160
			$val = strip_tags( $val );
161
		}
162
	}
163
164
	public static function fill_entry_user_info( $atts, array &$values ) {
165
		if ( ! $atts['user_info'] || empty( $atts['entry'] ) ) {
166
			return;
167
		}
168
169
		$data  = self::get_entry_description_data( $atts );
170
171
		if ( $atts['default_email'] ) {
172
			$atts['entry']->ip = '[ip]';
173
		}
174
175
		if ( $atts['format'] != 'text' ) {
176
			$values['ip'] = $atts['entry']->ip;
177
			$values['browser'] = self::get_browser( $data['browser'] );
178
			$values['referrer'] = $data['referrer'];
179
		} else {
180
			$values['ip'] = array( 'label' => __( 'IP Address', 'formidable' ), 'val' => $atts['entry']->ip );
181
			$values['browser'] = array(
182
				'label' => __( 'User-Agent (Browser/OS)', 'formidable' ),
183
				'val'   => self::get_browser( $data['browser'] ),
184
			);
185
			$values['referrer'] = array( 'label' => __( 'Referrer', 'formidable' ), 'val' => $data['referrer'] );
186
		}
187
	}
188
189
	/**
190
	 * @param array $atts - include (object) entry, (boolean) default_email
191
	 * @since 2.0.9
192
	 */
193
	public static function get_entry_description_data( $atts ) {
194
		$default_data = array(
195
			'browser' => '',
196
			'referrer' => '',
197
		);
198
		$data = $default_data;
199
200
		if ( isset( $atts['entry']->description ) ) {
201
			$data = (array) maybe_unserialize( $atts['entry']->description );
202
		} else if ( $atts['default_email'] ) {
203
			$data = array(
204
				'browser'  => '[browser]',
205
				'referrer' => '[referrer]',
206
			);
207
		}
208
209
		return array_merge( $default_data, $data );
210
	}
211
212
	public static function get_browser( $u_agent ) {
213
		$bname = __( 'Unknown', 'formidable' );
214
		$platform = __( 'Unknown', 'formidable' );
215
		$ub = '';
216
217
		// Get the operating system
218
		if ( preg_match( '/windows|win32/i', $u_agent ) ) {
219
			$platform = 'Windows';
220
		} else if ( preg_match( '/android/i', $u_agent ) ) {
221
			$platform = 'Android';
222
		} else if ( preg_match( '/linux/i', $u_agent ) ) {
223
			$platform = 'Linux';
224
		} else if ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
225
			$platform = 'OS X';
226
		}
227
228
		$agent_options = array(
229
			'Chrome'   => 'Google Chrome',
230
			'Safari'   => 'Apple Safari',
231
			'Opera'    => 'Opera',
232
			'Netscape' => 'Netscape',
233
			'Firefox'  => 'Mozilla Firefox',
234
		);
235
236
		// Next get the name of the useragent yes seperately and for good reason
237
		if ( strpos( $u_agent, 'MSIE' ) !== false && strpos( $u_agent, 'Opera' ) === false ) {
238
			$bname = 'Internet Explorer';
239
			$ub = 'MSIE';
240
		} else {
241
			foreach ( $agent_options as $agent_key => $agent_name ) {
242
				if ( strpos( $u_agent, $agent_key ) !== false ) {
243
					$bname = $agent_name;
244
					$ub = $agent_key;
245
					break;
246
				}
247
			}
248
		}
249
250
		// finally get the correct version number
251
		$known = array( 'Version', $ub, 'other' );
252
		$pattern = '#(?<browser>' . join( '|', $known ) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
253
		preg_match_all( $pattern, $u_agent, $matches ); // get the matching numbers
254
255
		// see how many we have
256
		$i = count($matches['browser']);
257
		if ( $i != 1 ) {
258
			//we will have two since we are not using 'other' argument yet
259
			//see if version is before or after the name
260
			if ( strripos( $u_agent, 'Version' ) < strripos( $u_agent, $ub ) ) {
261
				$version = $matches['version'][0];
262
			} else {
263
				$version = $matches['version'][1];
264
			}
265
		} else {
266
			$version = $matches['version'][0];
267
		}
268
269
		// check if we have a number
270
		if ( $version == '' ) {
271
			$version = '?';
272
		}
273
274
		return $bname .' '. $version .' / '. $platform;
275
	}
276
277
	public static function convert_entry_to_content( $values, $atts, array &$content ) {
278
279
		if ( $atts['plain_text'] ) {
280
			$bg_color_alt = $row_style = '';
281
		} else {
282
			$default_settings = apply_filters( 'frm_show_entry_styles', array(
283
				'border_color' => 'dddddd',
284
				'bg_color'     => 'f7f7f7',
285
				'text_color'   => '444444',
286
				'font_size'    => '12px',
287
				'border_width' => '1px',
288
				'alt_bg_color' => 'ffffff',
289
			) );
290
291
			// merge defaults, global settings, and shortcode options
292
			foreach ( $default_settings as $key => $setting ) {
293
				if ( $atts[ $key ] != '' ) {
294
					continue;
295
				}
296
297
				$atts[ $key ] = $setting;
298
				unset( $key, $setting );
299
			}
300
301
			unset($default_settings);
302
303
			$content[] = '<table cellspacing="0" style="font-size:'. $atts['font_size'] .';line-height:135%; border-bottom:'. $atts['border_width'] . ' solid #' . $atts['border_color'] . ';"><tbody>' . "\r\n";
304
			$atts['bg_color'] = ' style="background-color:#'. $atts['bg_color'] .';"';
305
			$bg_color_alt = ' style="background-color:#'. $atts['alt_bg_color'] .';"';
306
			$row_style = 'style="text-align:' . ( $atts['direction'] == 'rtl' ? 'right' : 'left' ) .';color:#'. $atts['text_color'] . ';padding:7px 9px;border-top:' . $atts['border_width'] .' solid #' . $atts['border_color'] . '"';
307
		}
308
309
		$odd = true;
310
		foreach ( $values as $id => $value ) {
311
			if ( $atts['plain_text'] ) {
312
				if ( 'rtl' == $atts['direction'] ) {
313
					$content[] = $value['val'] . ' :'. $value['label'] ."\r\n";
314
				} else {
315
					$content[] = $value['label'] . ': '. $value['val'] ."\r\n";
316
				}
317
				continue;
318
			}
319
320
			if ( $atts['default_email'] && is_numeric($id) ) {
321
				$content[] = '[if ' . $id . ']<tr style="[frm-alt-color]">';
322
			} else {
323
				$content[] = '<tr' . ( $odd ? $atts['bg_color'] : $bg_color_alt ) . '>';
324
			}
325
326
			$value['val'] = str_replace( "\r\n", '<br/>', $value['val'] );
327
			if ( 'rtl' == $atts['direction'] ) {
328
				$content[] = '<td ' . $row_style . '>' . $value['val'] . '</td><th ' . $row_style . '>' . $value['label'] . '</th>';
329
			} else {
330
				$content[] = '<th ' . $row_style . '>' . $value['label'] . '</th><td '. $row_style . '>' . $value['val'] . '</td>';
331
			}
332
			$content[] = '</tr>' . "\r\n";
333
334
			if ( $atts['default_email'] && is_numeric( $id ) ) {
335
				$content[] = '[/if ' . $id . ']';
336
			}
337
			$odd = $odd ? false : true;
338
		}
339
340
		if ( ! $atts['plain_text'] ) {
341
			$content[] = '</tbody></table>';
342
		}
343
	}
344
}
345