PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26.1
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 6.26.1, at classes/models/FrmEntryValues.php

362 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 2.04
8 */
9 class FrmEntryValues {
10
11 /**
12 * @var stdClass|null
13 */
14 protected $entry;
15
16 /**
17 * @var int
18 */
19 protected $form_id;
20
21 /**
22 * @var array
23 */
24 protected $fields = array();
25
26 /**
27 * @var FrmFieldValue[]
28 */
29 protected $field_values = array();
30
31 /**
32 * @var array
33 */
34 protected $user_info = array();
35
36 /**
37 * @var array
38 */
39 protected $include_fields = array();
40
41 /**
42 * @var array
43 */
44 protected $exclude_fields = array();
45
46 /**
47 * FrmEntryValues constructor
48 *
49 * @since 2.04
50 *
51 * @param int|string $entry_id
52 * @param array $atts
53 */
54 public function __construct( $entry_id, $atts = array() ) {
55 if ( isset( $atts['entry'] ) && is_object( $atts['entry'] ) && ! empty( $atts['entry']->metas ) ) {
56 $this->entry = $atts['entry'];
57 } else {
58 $this->init_entry( $entry_id );
59 }
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 }
72
73 /**
74 * Set the entry property
75 *
76 * @since 2.04
77 *
78 * @param int|string $entry_id
79 *
80 * @return void
81 */
82 protected function init_entry( $entry_id ) {
83 $this->entry = FrmEntry::getOne( $entry_id, true );
84 }
85
86 /**
87 * Gets entry property.
88 *
89 * @since 5.0.16
90 *
91 * @return stdClass
92 */
93 public function get_entry() {
94 return $this->entry;
95 }
96
97 /**
98 * Set the form_id property
99 *
100 * @since 2.04
101 *
102 * @return void
103 */
104 protected function init_form_id() {
105 $this->form_id = (int) $this->entry->form_id;
106 }
107
108 /**
109 * Set the include_fields property
110 *
111 * @since 2.04
112 *
113 * @param array $atts
114 *
115 * @return void
116 */
117 protected function init_include_fields( $atts ) {
118
119 // For reverse compatibility with the fields parameter.
120 if ( empty( $atts['include_fields'] ) && ! empty( $atts['fields'] ) ) {
121 if ( ! is_array( $atts['fields'] ) ) {
122 $atts['include_fields'] = $atts['fields'];
123 } else {
124 $atts['include_fields'] = '';
125
126 foreach ( $atts['fields'] as $included_field ) {
127 $atts['include_fields'] .= $included_field->id . ',';
128 }
129
130 $atts['include_fields'] = rtrim( $atts['include_fields'], ',' );
131 }
132 }
133
134 $this->include_fields = $this->prepare_array_property( 'include_fields', $atts );
135
136 /**
137 * Allows modifying the IDs of include_fields used in the entry values.
138 *
139 * @since 5.0.04
140 *
141 * @param array $field_ids The list of field IDs.
142 * @param array $atts The arguments. See {@see FrmEntriesController::show_entry_shortcode()}.
143 */
144 $this->include_fields = apply_filters( 'frm_entry_values_include_fields', $this->include_fields, $atts );
145 }
146
147 /**
148 * Set the exclude_fields property
149 *
150 * @since 2.04
151 *
152 * @param array $atts
153 *
154 * @return void
155 */
156 protected function init_exclude_fields( $atts ) {
157 $this->exclude_fields = $this->prepare_array_property( 'exclude_fields', $atts );
158
159 /**
160 * Allows modifying the IDs of exclude_fields used in the entry values.
161 *
162 * @since 5.0.04
163 *
164 * @param array $field_ids The list of field IDs.
165 * @param array $atts The arguments. See {@see FrmEntriesController::show_entry_shortcode()}.
166 */
167 $this->exclude_fields = apply_filters( 'frm_entry_values_exclude_fields', $this->exclude_fields, $atts );
168 }
169
170 /**
171 * Prepare an array property value, such as include_fields and exclude_fields
172 *
173 * @since 2.04
174 *
175 * @param string $index
176 * @param array $atts
177 *
178 * @return array
179 */
180 private function prepare_array_property( $index, $atts ) {
181 if ( ! empty( $atts[ $index ] ) ) {
182
183 if ( is_array( $atts[ $index ] ) ) {
184 $property = $atts[ $index ];
185 } else {
186 $property = explode( ',', $atts[ $index ] );
187 }
188 } else {
189 $property = array();
190 }
191
192 return $property;
193 }
194
195 /**
196 * Set the fields property
197 *
198 * @since 2.04
199 *
200 * @return void
201 */
202 protected function init_fields() {
203 $this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
204
205 /**
206 * Allows modifying the list of all field in the form that is used in the entry values.
207 *
208 * @since 5.0.04
209 *
210 * @param array $fields The list of fields.
211 * @param array $args The arguments. Contains `form_id`, `entry`.
212 */
213 $this->fields = apply_filters(
214 'frm_entry_values_fields',
215 $this->fields,
216 array(
217 'form_id' => $this->form_id,
218 'entry' => $this->entry,
219 )
220 );
221 }
222
223 /**
224 * Set the field_values property
225 *
226 * @since 2.04
227 *
228 * @return void
229 */
230 protected function init_field_values() {
231 foreach ( $this->fields as $field ) {
232 if ( $this->is_field_included( $field ) ) {
233 $this->add_field_values( $field );
234 }
235 }
236 }
237
238 /**
239 * Get the field_values property
240 *
241 * @since 2.04
242 *
243 * @return array
244 */
245 public function get_field_values() {
246 return $this->field_values;
247 }
248
249 /**
250 * Set the user_info property
251 *
252 * @since 2.04
253 *
254 * @return void
255 */
256 protected function init_user_info() {
257 if ( isset( $this->entry->description ) ) {
258 $entry_description = (array) $this->entry->description;
259 } else {
260 $entry_description = array(
261 'browser' => '',
262 'referrer' => '',
263 );
264 }
265
266 $ip = array(
267 'label' => __( 'IP Address', 'formidable' ),
268 'value' => $this->entry->ip,
269 );
270 $browser = array(
271 'label' => __( 'User-Agent (Browser/OS)', 'formidable' ),
272 'value' => isset( $entry_description['browser'] ) ? FrmEntriesHelper::get_browser( $entry_description['browser'] ) : '',
273 );
274 $referrer = array(
275 'label' => __( 'Referrer', 'formidable' ),
276 'value' => $entry_description['referrer'] ?? '',
277 );
278
279 /**
280 * Allow the referrer to be modified.
281 *
282 * @since 5.5.1
283 *
284 * @param array $referrer
285 * @param array $entry_description
286 * @param object $entry
287 */
288 $referrer = apply_filters( 'frm_user_info_referrer', $referrer, $entry_description, $this->entry );
289
290 $this->user_info = array(
291 'ip' => $ip,
292 'browser' => $browser,
293 'referrer' => $referrer,
294 );
295 }
296
297 /**
298 * Get the user_info property
299 *
300 * @since 2.04
301 *
302 * @return array
303 */
304 public function get_user_info() {
305 return $this->user_info;
306 }
307
308 /**
309 * Check if a field should be included in the values
310 *
311 * @since 2.04
312 *
313 * @param stdClass $field
314 *
315 * @return bool
316 */
317 protected function is_field_included( $field ) {
318 $is_included = true;
319
320 if ( ! empty( $this->include_fields ) ) {
321 $is_included = $this->is_field_in_array( $field, $this->include_fields );
322 }
323
324 if ( ! empty( $this->exclude_fields ) ) {
325 $is_excluded = $this->is_field_in_array( $field, $this->exclude_fields );
326
327 if ( $is_excluded ) {
328 $is_included = false;
329 }
330 }
331
332 return $is_included;
333 }
334
335 /**
336 * Check if a field is in the include fields or exclude fields array
337 *
338 * @since 2.04
339 *
340 * @param stdClass $field
341 * @param array $array
342 *
343 * @return bool
344 */
345 protected function is_field_in_array( $field, $array ) {
346 return in_array( $field->id, $array ) || in_array( (string) $field->field_key, $array, true );
347 }
348
349 /**
350 * Add a field's values to the field_values property
351 *
352 * @since 2.04
353 *
354 * @param stdClass $field
355 *
356 * @return void
357 */
358 protected function add_field_values( $field ) {
359 $this->field_values[ $field->id ] = new FrmFieldValue( $field, $this->entry );
360 }
361 }
362