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

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