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

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