Completed
Push — master ( ba1cd7...836330 )
by Jamie
02:56
created

FrmEntryValues::init_user_info()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
nc 2
nop 0
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @since 2.03.11
5
 */
6
class FrmEntryValues {
7
8
	/**
9
	 * @var stdClass
10
	 */
11
	protected $entry = null;
12
13
	/**
14
	 * @var int
15
	 */
16
	protected $form_id;
17
18
	/**
19
	 * @var array
20
	 */
21
	protected $fields = array();
22
23
	/**
24
	 * @var FrmFieldValue[]
25
	 */
26
	protected $field_values = array();
27
28
	/**
29
	 * @var array
30
	 */
31
	protected $user_info = array();
32
33
	/**
34
	 * @var array
35
	 */
36
	protected $include_fields = array();
37
38
	/**
39
	 * @var array
40
	 */
41
	protected $exclude_fields = array();
42
43
	/**
44
	 * @since 2.03.11
45
	 *
46
	 * @var string
47
	 */
48
	protected $source = '';
49
50
	/**
51
	 * FrmEntryValues constructor
52
	 *
53
	 * @since 2.03.11
54
	 *
55
	 * @param int|string $entry_id
56
	 * @param array $atts
57
	 */
58
	public function __construct( $entry_id, $atts = array() ) {
59
		$this->init_entry( $entry_id );
60
61
		if ( $this->entry === null || $this->entry === false ) {
62
			return;
63
		}
64
65
		$this->init_form_id();
66
		$this->init_include_fields( $atts );
67
		$this->init_exclude_fields( $atts );
68
		$this->init_fields();
69
		$this->init_field_values();
70
		$this->init_user_info();
71
		$this->init_source( $atts );
72
	}
73
74
	/**
75
	 * Set the entry property
76
	 *
77
	 * @since 2.03.11
78
	 *
79
	 * @param int|string $entry_id
80
	 */
81
	protected function init_entry( $entry_id ) {
82
		$this->entry = FrmEntry::getOne( $entry_id, true );
83
	}
84
85
	/**
86
	 * Set the form_id property
87
	 *
88
	 * @since 2.03.11
89
	 */
90
	protected function init_form_id() {
91
		$this->form_id = (int) $this->entry->form_id;
92
	}
93
94
	/**
95
	 * Set the include_fields property
96
	 *
97
	 * @since 2.03.11
98
	 *
99
	 * @param array $atts
100
	 */
101
	protected function init_include_fields( $atts ) {
102
103
		// For reverse compatibility with the fields parameter
104
		if ( ! isset( $atts['include_fields'] ) || empty( $atts['include_fields'] ) ) {
105
106
			if ( isset( $atts['fields'] ) && ! empty( $atts['fields'] ) ) {
107
108
				$atts['include_fields'] = '';
109
				foreach ( $atts['fields'] as $included_field ) {
110
					$atts['include_fields'] .= $included_field->id . ',';
111
				}
112
113
				$atts['include_fields'] = rtrim( $atts['include_fields'], ',' );
114
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
115
116
		}
117
118
		$this->include_fields = $this->prepare_array_property( 'include_fields', $atts );
119
	}
120
121
	/**
122
	 * Set the exclude_fields property
123
	 *
124
	 * @since 2.03.11
125
	 *
126
	 * @param array $atts
127
	 */
128
	protected function init_exclude_fields( $atts ) {
129
		$this->exclude_fields = $this->prepare_array_property( 'exclude_fields', $atts );
130
	}
131
132
	/**
133
	 * Initialize the source property
134
	 *
135
	 * @since 2.03.11
136
	 *
137
	 * @param array $atts
138
	 */
139 View Code Duplication
	protected function init_source( $atts ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
140
		if ( isset( $atts['source'] ) && is_string( $atts['source'] ) && $atts['source'] !== '' ) {
141
			$this->source = (string) $atts['source'];
142
		} else {
143
			$this->source = 'general';
144
		}
145
	}
146
147
	/**
148
	 * Prepare an array property value, such as include_fields and exclude_fields
149
	 *
150
	 * @since 2.03.11
151
	 *
152
	 * @param string $index
153
	 * @param array $atts
154
	 *
155
	 * @return array
156
	 */
157
	private function prepare_array_property( $index, $atts ) {
158
		if ( isset( $atts[ $index ] ) && ! empty( $atts[ $index ] ) ) {
159
160
			if ( is_array( $atts[ $index ] ) ) {
161
				$property = $atts[ $index ];
162
			} else {
163
				$property = explode( ',', $atts[ $index ] );
164
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
165
166
		} else {
167
			$property = array();
168
		}
169
170
		return $property;
171
	}
172
173
	/**
174
	 * Set the fields property
175
	 *
176
	 * @since 2.03.11
177
	 */
178
	protected function init_fields() {
179
		$this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
180
	}
181
182
	/**
183
	 * Set the field_values property
184
	 *
185
	 * @since 2.03.11
186
	 */
187
	protected function init_field_values() {
188
		foreach ( $this->fields as $field ) {
189
			if ( $this->is_field_included( $field ) ) {
190
				$this->add_field_values( $field );
191
			}
192
		}
193
	}
194
195
	/**
196
	 * Get the field_values property
197
	 *
198
	 * @since 2.03.11
199
	 *
200
	 * @return array
201
	 */
202
	public function get_field_values() {
203
		return $this->field_values;
204
	}
205
206
	/**
207
	 * Set the user_info property
208
	 *
209
	 * @since 2.03.11
210
	 */
211
	protected function init_user_info() {
212
		if ( isset( $this->entry->description ) ) {
213
			$entry_description = (array) maybe_unserialize( $this->entry->description );
214
		} else {
215
			$entry_description = array(
216
				'browser' => '',
217
				'referrer' => '',
218
			);
219
		}
220
221
		$ip = array(
222
			'label' => __( 'IP Address', 'formidable' ),
223
			'value' => $this->entry->ip
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
224
		);
225
226
		$browser = array(
227
			'label' => __( 'User-Agent (Browser/OS)', 'formidable' ),
228
			'value'   => FrmEntriesHelper::get_browser( $entry_description['browser'] ),
229
		);
230
231
		$referrer = array(
232
			'label' => __( 'Referrer', 'formidable' ),
233
			'value' => $entry_description['referrer']
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
234
		);
235
236
		$this->user_info = array(
237
			'ip' => $ip,
238
			'browser' => $browser,
239
			'referrer' => $referrer,
240
		);
241
	}
242
243
	/**
244
	 * Get the user_info property
245
	 *
246
	 * @since 2.03.11
247
	 *
248
	 * @return array
249
	 */
250
	public function get_user_info() {
251
		return $this->user_info;
252
	}
253
254
	/**
255
	 * Check if a field should be included in the values
256
	 *
257
	 * @since 2.03.11
258
	 *
259
	 * @param stdClass $field
260
	 *
261
	 * @return bool
262
	 */
263
	protected function is_field_included( $field ) {
264
		if ( ! empty( $this->include_fields ) ) {
265
			$is_included = $this->is_field_in_array( $field, $this->include_fields );
266
		} else if ( ! empty( $this->exclude_fields ) ) {
267
			$is_included = ! $this->is_field_in_array( $field, $this->include_fields );
268
		} else {
269
			$is_included = true;
270
		}
271
272
		return $is_included;
273
	}
274
275
	/**
276
	 * Check if a field is in the include fields or exclude fields array
277
	 *
278
	 * @since 2.03.11
279
	 *
280
	 * @param stdClass $field
281
	 * @param array $array
282
	 *
283
	 * @return bool
284
	 */
285
	protected function is_field_in_array( $field, $array ) {
286
		return in_array( $field->id, $array ) || in_array( $field->field_key, $array );
287
	}
288
289
	/**
290
	 * Add a field's values to the field_values property
291
	 *
292
	 * @since 2.03.11
293
	 *
294
	 * @param stdClass $field
295
	 */
296
	protected function add_field_values( $field ) {
297
		$this->field_values[ $field->id ] = new FrmFieldValue( $field, $this->entry, array( 'source' => $this->source ) );
298
	}
299
}