PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 2.05.09
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v2.05.09
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmEntryValues.php

FrmEntryValues.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 2.05.09, at classes/models/FrmEntryValues.php

298 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @since 2.04
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.04
45 *
46 * @var string
47 */
48 protected $source = '';
49
50 /**
51 * FrmEntryValues constructor
52 *
53 * @since 2.04
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_source( $atts );
67 $this->init_include_fields( $atts );
68 $this->init_exclude_fields( $atts );
69 $this->init_fields();
70 $this->init_field_values();
71 $this->init_user_info();
72 }
73
74 /**
75 * Set the entry property
76 *
77 * @since 2.04
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.04
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.04
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 }
115 }
116
117 $this->include_fields = $this->prepare_array_property( 'include_fields', $atts );
118 }
119
120 /**
121 * Set the exclude_fields property
122 *
123 * @since 2.04
124 *
125 * @param array $atts
126 */
127 protected function init_exclude_fields( $atts ) {
128 $this->exclude_fields = $this->prepare_array_property( 'exclude_fields', $atts );
129 }
130
131 /**
132 * Initialize the source property
133 *
134 * @since 2.04
135 *
136 * @param array $atts
137 */
138 protected function init_source( $atts ) {
139 if ( isset( $atts['source'] ) && is_string( $atts['source'] ) && $atts['source'] !== '' ) {
140 $this->source = (string) $atts['source'];
141 } else {
142 $this->source = 'general';
143 }
144 }
145
146 /**
147 * Prepare an array property value, such as include_fields and exclude_fields
148 *
149 * @since 2.04
150 *
151 * @param string $index
152 * @param array $atts
153 *
154 * @return array
155 */
156 private function prepare_array_property( $index, $atts ) {
157 if ( isset( $atts[ $index ] ) && ! empty( $atts[ $index ] ) ) {
158
159 if ( is_array( $atts[ $index ] ) ) {
160 $property = $atts[ $index ];
161 } else {
162 $property = explode( ',', $atts[ $index ] );
163 }
164 } else {
165 $property = array();
166 }
167
168 return $property;
169 }
170
171 /**
172 * Set the fields property
173 *
174 * @since 2.04
175 */
176 protected function init_fields() {
177 $this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
178 }
179
180 /**
181 * Set the field_values property
182 *
183 * @since 2.04
184 */
185 protected function init_field_values() {
186 foreach ( $this->fields as $field ) {
187 if ( $this->is_field_included( $field ) ) {
188 $this->add_field_values( $field );
189 }
190 }
191 }
192
193 /**
194 * Get the field_values property
195 *
196 * @since 2.04
197 *
198 * @return array
199 */
200 public function get_field_values() {
201 return $this->field_values;
202 }
203
204 /**
205 * Set the user_info property
206 *
207 * @since 2.04
208 */
209 protected function init_user_info() {
210 if ( isset( $this->entry->description ) ) {
211 $entry_description = (array) maybe_unserialize( $this->entry->description );
212 } else {
213 $entry_description = array(
214 'browser' => '',
215 'referrer' => '',
216 );
217 }
218
219 $ip = array(
220 'label' => __( 'IP Address', 'formidable' ),
221 'value' => $this->entry->ip,
222 );
223
224 $browser = array(
225 'label' => __( 'User-Agent (Browser/OS)', 'formidable' ),
226 'value' => FrmEntriesHelper::get_browser( $entry_description['browser'] ),
227 );
228
229 $referrer = array(
230 'label' => __( 'Referrer', 'formidable' ),
231 'value' => $entry_description['referrer'],
232 );
233
234 $this->user_info = array(
235 'ip' => $ip,
236 'browser' => $browser,
237 'referrer' => $referrer,
238 );
239 }
240
241 /**
242 * Get the user_info property
243 *
244 * @since 2.04
245 *
246 * @return array
247 */
248 public function get_user_info() {
249 return $this->user_info;
250 }
251
252 /**
253 * Check if a field should be included in the values
254 *
255 * @since 2.04
256 *
257 * @param stdClass $field
258 *
259 * @return bool
260 */
261 protected function is_field_included( $field ) {
262 if ( ! empty( $this->include_fields ) ) {
263 $is_included = $this->is_field_in_array( $field, $this->include_fields );
264 } else if ( ! empty( $this->exclude_fields ) ) {
265 $is_included = ! $this->is_field_in_array( $field, $this->exclude_fields );
266 } else {
267 $is_included = true;
268 }
269
270 return $is_included;
271 }
272
273 /**
274 * Check if a field is in the include fields or exclude fields array
275 *
276 * @since 2.04
277 *
278 * @param stdClass $field
279 * @param array $array
280 *
281 * @return bool
282 */
283 protected function is_field_in_array( $field, $array ) {
284 return in_array( $field->id, $array ) || in_array( $field->field_key, $array );
285 }
286
287 /**
288 * Add a field's values to the field_values property
289 *
290 * @since 2.04
291 *
292 * @param stdClass $field
293 */
294 protected function add_field_values( $field ) {
295 $this->field_values[ $field->id ] = new FrmFieldValue( $field, $this->entry, array( 'source' => $this->source ) );
296 }
297 }
298