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

359 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 * @return stdClass
91 */
92 public function get_entry() {
93 return $this->entry;
94 }
95
96 /**
97 * Set the form_id property
98 *
99 * @since 2.04
100 *
101 * @return void
102 */
103 protected function init_form_id() {
104 $this->form_id = (int) $this->entry->form_id;
105 }
106
107 /**
108 * Set the include_fields property
109 *
110 * @since 2.04
111 *
112 * @param array $atts
113 *
114 * @return void
115 */
116 protected function init_include_fields( $atts ) {
117
118 // For reverse compatibility with the fields parameter.
119 if ( empty( $atts['include_fields'] ) && ! empty( $atts['fields'] ) ) {
120 if ( ! is_array( $atts['fields'] ) ) {
121 $atts['include_fields'] = $atts['fields'];
122 } else {
123 $atts['include_fields'] = '';
124
125 foreach ( $atts['fields'] as $included_field ) {
126 $atts['include_fields'] .= $included_field->id . ',';
127 }
128
129 $atts['include_fields'] = rtrim( $atts['include_fields'], ',' );
130 }
131 }
132
133 $this->include_fields = $this->prepare_array_property( 'include_fields', $atts );
134
135 /**
136 * Allows modifying the IDs of include_fields used in the entry values.
137 *
138 * @since 5.0.04
139 *
140 * @param array $field_ids The list of field IDs.
141 * @param array $atts The arguments. See {@see FrmEntriesController::show_entry_shortcode()}.
142 */
143 $this->include_fields = apply_filters( 'frm_entry_values_include_fields', $this->include_fields, $atts );
144 }
145
146 /**
147 * Set the exclude_fields property
148 *
149 * @since 2.04
150 *
151 * @param array $atts
152 *
153 * @return void
154 */
155 protected function init_exclude_fields( $atts ) {
156 $this->exclude_fields = $this->prepare_array_property( 'exclude_fields', $atts );
157
158 /**
159 * Allows modifying the IDs of exclude_fields used in the entry values.
160 *
161 * @since 5.0.04
162 *
163 * @param array $field_ids The list of field IDs.
164 * @param array $atts The arguments. See {@see FrmEntriesController::show_entry_shortcode()}.
165 */
166 $this->exclude_fields = apply_filters( 'frm_entry_values_exclude_fields', $this->exclude_fields, $atts );
167 }
168
169 /**
170 * Prepare an array property value, such as include_fields and exclude_fields
171 *
172 * @since 2.04
173 *
174 * @param string $index
175 * @param array $atts
176 *
177 * @return array
178 */
179 private function prepare_array_property( $index, $atts ) {
180 if ( ! empty( $atts[ $index ] ) ) {
181
182 if ( is_array( $atts[ $index ] ) ) {
183 $property = $atts[ $index ];
184 } else {
185 $property = explode( ',', $atts[ $index ] );
186 }
187 } else {
188 $property = array();
189 }
190
191 return $property;
192 }
193
194 /**
195 * Set the fields property
196 *
197 * @since 2.04
198 *
199 * @return void
200 */
201 protected function init_fields() {
202 $this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
203
204 /**
205 * Allows modifying the list of all field in the form that is used in the entry values.
206 *
207 * @since 5.0.04
208 *
209 * @param array $fields The list of fields.
210 * @param array $args The arguments. Contains `form_id`, `entry`.
211 */
212 $this->fields = apply_filters(
213 'frm_entry_values_fields',
214 $this->fields,
215 array(
216 'form_id' => $this->form_id,
217 'entry' => $this->entry,
218 )
219 );
220 }
221
222 /**
223 * Set the field_values property
224 *
225 * @since 2.04
226 *
227 * @return void
228 */
229 protected function init_field_values() {
230 foreach ( $this->fields as $field ) {
231 if ( $this->is_field_included( $field ) ) {
232 $this->add_field_values( $field );
233 }
234 }
235 }
236
237 /**
238 * Get the field_values property
239 *
240 * @since 2.04
241 *
242 * @return array
243 */
244 public function get_field_values() {
245 return $this->field_values;
246 }
247
248 /**
249 * Set the user_info property
250 *
251 * @since 2.04
252 *
253 * @return void
254 */
255 protected function init_user_info() {
256 if ( isset( $this->entry->description ) ) {
257 $entry_description = (array) $this->entry->description;
258 } else {
259 $entry_description = array(
260 'browser' => '',
261 'referrer' => '',
262 );
263 }
264
265 $ip = array(
266 'label' => __( 'IP Address', 'formidable' ),
267 'value' => $this->entry->ip,
268 );
269 $browser = array(
270 'label' => __( 'User-Agent (Browser/OS)', 'formidable' ),
271 'value' => isset( $entry_description['browser'] ) ? FrmEntriesHelper::get_browser( $entry_description['browser'] ) : '',
272 );
273 $referrer = array(
274 'label' => __( 'Referrer', 'formidable' ),
275 'value' => isset( $entry_description['referrer'] ) ? $entry_description['referrer'] : '',
276 );
277
278 /**
279 * Allow the referrer to be modified.
280 *
281 * @since 5.5.1
282 *
283 * @param array $referrer
284 * @param array $entry_description
285 * @param object $entry
286 */
287 $referrer = apply_filters( 'frm_user_info_referrer', $referrer, $entry_description, $this->entry );
288
289 $this->user_info = array(
290 'ip' => $ip,
291 'browser' => $browser,
292 'referrer' => $referrer,
293 );
294 }
295
296 /**
297 * Get the user_info property
298 *
299 * @since 2.04
300 *
301 * @return array
302 */
303 public function get_user_info() {
304 return $this->user_info;
305 }
306
307 /**
308 * Check if a field should be included in the values
309 *
310 * @since 2.04
311 *
312 * @param stdClass $field
313 *
314 * @return bool
315 */
316 protected function is_field_included( $field ) {
317 $is_included = true;
318 if ( ! empty( $this->include_fields ) ) {
319 $is_included = $this->is_field_in_array( $field, $this->include_fields );
320 }
321
322 if ( ! empty( $this->exclude_fields ) ) {
323 $is_excluded = $this->is_field_in_array( $field, $this->exclude_fields );
324 if ( $is_excluded ) {
325 $is_included = false;
326 }
327 }
328
329 return $is_included;
330 }
331
332 /**
333 * Check if a field is in the include fields or exclude fields array
334 *
335 * @since 2.04
336 *
337 * @param stdClass $field
338 * @param array $array
339 *
340 * @return bool
341 */
342 protected function is_field_in_array( $field, $array ) {
343 return in_array( $field->id, $array ) || in_array( (string) $field->field_key, $array, true );
344 }
345
346 /**
347 * Add a field's values to the field_values property
348 *
349 * @since 2.04
350 *
351 * @param stdClass $field
352 *
353 * @return void
354 */
355 protected function add_field_values( $field ) {
356 $this->field_values[ $field->id ] = new FrmFieldValue( $field, $this->entry );
357 }
358 }
359