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

355 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 false|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 // 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 return is_array( $atts[ $index ] ) ? $atts[ $index ] : explode( ',', $atts[ $index ] );
182 }
183
184 return array();
185 }
186
187 /**
188 * Set the fields property
189 *
190 * @since 2.04
191 *
192 * @return void
193 */
194 protected function init_fields() {
195 $this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
196
197 /**
198 * Allows modifying the list of all field in the form that is used in the entry values.
199 *
200 * @since 5.0.04
201 *
202 * @param array $fields The list of fields.
203 * @param array $args The arguments. Contains `form_id`, `entry`.
204 */
205 $this->fields = apply_filters(
206 'frm_entry_values_fields',
207 $this->fields,
208 array(
209 'form_id' => $this->form_id,
210 'entry' => $this->entry,
211 )
212 );
213 }
214
215 /**
216 * Set the field_values property
217 *
218 * @since 2.04
219 *
220 * @return void
221 */
222 protected function init_field_values() {
223 foreach ( $this->fields as $field ) {
224 if ( $this->is_field_included( $field ) ) {
225 $this->add_field_values( $field );
226 }
227 }
228 }
229
230 /**
231 * Get the field_values property
232 *
233 * @since 2.04
234 *
235 * @return array
236 */
237 public function get_field_values() {
238 return $this->field_values;
239 }
240
241 /**
242 * Set the user_info property
243 *
244 * @since 2.04
245 *
246 * @return void
247 */
248 protected function init_user_info() {
249 if ( isset( $this->entry->description ) ) {
250 $entry_description = (array) $this->entry->description;
251 } else {
252 $entry_description = array(
253 'browser' => '',
254 'referrer' => '',
255 );
256 }
257
258 $ip = array(
259 'label' => __( 'IP Address', 'formidable' ),
260 'value' => $this->entry->ip,
261 );
262 $browser = array(
263 'label' => __( 'User-Agent (Browser/OS)', 'formidable' ),
264 'value' => isset( $entry_description['browser'] ) ? FrmEntriesHelper::get_browser( $entry_description['browser'] ) : '',
265 );
266 $referrer = array(
267 'label' => __( 'Referrer', 'formidable' ),
268 'value' => $entry_description['referrer'] ?? '',
269 );
270
271 /**
272 * Allow the referrer to be modified.
273 *
274 * @since 5.5.1
275 *
276 * @param array $referrer
277 * @param array $entry_description
278 * @param object $entry
279 */
280 $referrer = apply_filters( 'frm_user_info_referrer', $referrer, $entry_description, $this->entry );
281
282 $this->user_info = array(
283 'ip' => $ip,
284 'browser' => $browser,
285 'referrer' => $referrer,
286 );
287 }
288
289 /**
290 * Get the user_info property
291 *
292 * @since 2.04
293 *
294 * @return array
295 */
296 public function get_user_info() {
297 return $this->user_info;
298 }
299
300 /**
301 * Check if a field should be included in the values
302 *
303 * @since 2.04
304 *
305 * @param stdClass $field
306 *
307 * @return bool
308 */
309 protected function is_field_included( $field ) {
310 $is_included = true;
311
312 if ( ! empty( $this->include_fields ) ) {
313 $is_included = $this->is_field_in_array( $field, $this->include_fields );
314 }
315
316 if ( ! empty( $this->exclude_fields ) ) {
317 $is_excluded = $this->is_field_in_array( $field, $this->exclude_fields );
318
319 if ( $is_excluded ) {
320 $is_included = false;
321 }
322 }
323
324 return $is_included;
325 }
326
327 /**
328 * Check if a field is in the include fields or exclude fields array
329 *
330 * @since 2.04
331 *
332 * @param stdClass $field
333 * @param array $array
334 *
335 * @return bool
336 */
337 protected function is_field_in_array( $field, $array ) {
338 // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
339 return in_array( $field->id, $array ) || in_array( (string) $field->field_key, $array, true );
340 }
341
342 /**
343 * Add a field's values to the field_values property
344 *
345 * @since 2.04
346 *
347 * @param stdClass $field
348 *
349 * @return void
350 */
351 protected function add_field_values( $field ) {
352 $this->field_values[ $field->id ] = new FrmFieldValue( $field, $this->entry );
353 }
354 }
355