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

288 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @since 2.04
5 */
6 class FrmEntryValues {
7
8 /**
9 * @var stdClass
10 */
11 protected $entry = null;
12
13 /**
14 * @var int
15 */
16 protected $form_id;
17
18 /**
19 * @var array
20 */
21 protected $fields = array();
22
23 /**
24 * @var FrmFieldValue[]
25 */
26 protected $field_values = array();
27
28 /**
29 * @var array
30 */
31 protected $user_info = array();
32
33 /**
34 * @var array
35 */
36 protected $include_fields = array();
37
38 /**
39 * @var array
40 */
41 protected $exclude_fields = array();
42
43 /**
44 * FrmEntryValues constructor
45 *
46 * @since 2.04
47 *
48 * @param int|string $entry_id
49 * @param array $atts
50 */
51 public function __construct( $entry_id, $atts = array() ) {
52 if ( isset( $atts['entry'] ) && is_object( $atts['entry'] ) && ! empty( $atts['entry']->metas ) ) {
53 $this->entry = $atts['entry'];
54 } else {
55 $this->init_entry( $entry_id );
56 }
57
58 if ( $this->entry === null || $this->entry === false ) {
59 return;
60 }
61
62 $this->init_form_id();
63 $this->init_include_fields( $atts );
64 $this->init_exclude_fields( $atts );
65 $this->init_fields();
66 $this->init_field_values();
67 $this->init_user_info();
68 }
69
70 /**
71 * Set the entry property
72 *
73 * @since 2.04
74 *
75 * @param int|string $entry_id
76 */
77 protected function init_entry( $entry_id ) {
78 $this->entry = FrmEntry::getOne( $entry_id, true );
79 }
80
81 /**
82 * Set the form_id property
83 *
84 * @since 2.04
85 */
86 protected function init_form_id() {
87 $this->form_id = (int) $this->entry->form_id;
88 }
89
90 /**
91 * Set the include_fields property
92 *
93 * @since 2.04
94 *
95 * @param array $atts
96 */
97 protected function init_include_fields( $atts ) {
98
99 // For reverse compatibility with the fields parameter
100 if ( ! isset( $atts['include_fields'] ) || empty( $atts['include_fields'] ) ) {
101
102 if ( isset( $atts['fields'] ) && ! empty( $atts['fields'] ) ) {
103
104 if ( ! is_array( $atts['fields'] ) ) {
105 $atts['include_fields'] = $atts['fields'];
106 } else {
107 $atts['include_fields'] = '';
108
109 foreach ( $atts['fields'] as $included_field ) {
110 $atts['include_fields'] .= $included_field->id . ',';
111 }
112
113 $atts['include_fields'] = rtrim( $atts['include_fields'], ',' );
114 }
115 }
116 }
117
118 $this->include_fields = $this->prepare_array_property( 'include_fields', $atts );
119 }
120
121 /**
122 * Set the exclude_fields property
123 *
124 * @since 2.04
125 *
126 * @param array $atts
127 */
128 protected function init_exclude_fields( $atts ) {
129 $this->exclude_fields = $this->prepare_array_property( 'exclude_fields', $atts );
130 }
131
132 /**
133 * Prepare an array property value, such as include_fields and exclude_fields
134 *
135 * @since 2.04
136 *
137 * @param string $index
138 * @param array $atts
139 *
140 * @return array
141 */
142 private function prepare_array_property( $index, $atts ) {
143 if ( isset( $atts[ $index ] ) && ! empty( $atts[ $index ] ) ) {
144
145 if ( is_array( $atts[ $index ] ) ) {
146 $property = $atts[ $index ];
147 } else {
148 $property = explode( ',', $atts[ $index ] );
149 }
150 } else {
151 $property = array();
152 }
153
154 return $property;
155 }
156
157 /**
158 * Set the fields property
159 *
160 * @since 2.04
161 */
162 protected function init_fields() {
163 $this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
164 }
165
166 /**
167 * Set the field_values property
168 *
169 * @since 2.04
170 */
171 protected function init_field_values() {
172 foreach ( $this->fields as $field ) {
173 if ( $this->is_field_included( $field ) ) {
174 $this->add_field_values( $field );
175 }
176 }
177 }
178
179 /**
180 * Get the field_values property
181 *
182 * @since 2.04
183 *
184 * @return array
185 */
186 public function get_field_values() {
187 return $this->field_values;
188 }
189
190 /**
191 * Set the user_info property
192 *
193 * @since 2.04
194 */
195 protected function init_user_info() {
196 if ( isset( $this->entry->description ) ) {
197 $entry_description = (array) $this->entry->description;
198 } else {
199 $entry_description = array(
200 'browser' => '',
201 'referrer' => '',
202 );
203 }
204
205 $ip = array(
206 'label' => __( 'IP Address', 'formidable' ),
207 'value' => $this->entry->ip,
208 );
209
210 $browser = array(
211 'label' => __( 'User-Agent (Browser/OS)', 'formidable' ),
212 'value' => FrmEntriesHelper::get_browser( $entry_description['browser'] ),
213 );
214
215 $referrer = array(
216 'label' => __( 'Referrer', 'formidable' ),
217 'value' => $entry_description['referrer'],
218 );
219
220 $this->user_info = array(
221 'ip' => $ip,
222 'browser' => $browser,
223 'referrer' => $referrer,
224 );
225 }
226
227 /**
228 * Get the user_info property
229 *
230 * @since 2.04
231 *
232 * @return array
233 */
234 public function get_user_info() {
235 return $this->user_info;
236 }
237
238 /**
239 * Check if a field should be included in the values
240 *
241 * @since 2.04
242 *
243 * @param stdClass $field
244 *
245 * @return bool
246 */
247 protected function is_field_included( $field ) {
248 $is_included = true;
249 if ( ! empty( $this->include_fields ) ) {
250 $is_included = $this->is_field_in_array( $field, $this->include_fields );
251 }
252
253 if ( ! empty( $this->exclude_fields ) ) {
254 $is_excluded = $this->is_field_in_array( $field, $this->exclude_fields );
255 if ( $is_excluded ) {
256 $is_included = false;
257 }
258 }
259
260 return $is_included;
261 }
262
263 /**
264 * Check if a field is in the include fields or exclude fields array
265 *
266 * @since 2.04
267 *
268 * @param stdClass $field
269 * @param array $array
270 *
271 * @return bool
272 */
273 protected function is_field_in_array( $field, $array ) {
274 return in_array( $field->id, $array ) || in_array( (string) $field->field_key, $array, true );
275 }
276
277 /**
278 * Add a field's values to the field_values property
279 *
280 * @since 2.04
281 *
282 * @param stdClass $field
283 */
284 protected function add_field_values( $field ) {
285 $this->field_values[ $field->id ] = new FrmFieldValue( $field, $this->entry );
286 }
287 }
288