PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.2.0
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.2.0
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / class-form-entry.php

class-form-entry.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.2.0, at includes/class-form-entry.php

460 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Single Form Entry Class
5 *
6 * @since 1.1.0
7 */
8 class WeForms_Form_Entry {
9
10 /**
11 * The form object
12 *
13 * @var \WeForms_Form
14 */
15 private $form;
16
17 /**
18 * Entry id
19 *
20 * @var integer
21 */
22 public $id = 0;
23
24 /**
25 * The form id
26 *
27 * @var integer
28 */
29 public $form_id = 0;
30
31 /**
32 * The user id
33 *
34 * @var integer
35 */
36 public $user_id = 0;
37
38 /**
39 * IP Address
40 *
41 * @var string
42 */
43 public $ip_address = '127.0.0.1';
44
45 /**
46 * The device of the user
47 *
48 * @var string
49 */
50 public $device = '';
51
52 /**
53 * Referer URL
54 *
55 * @var string
56 */
57 public $referer = '';
58
59 /**
60 * Entry creation date
61 *
62 * @var string
63 */
64 public $created = '0000-00-00 00:00:00';
65
66 /**
67 * Form fields
68 *
69 * @var array
70 */
71 public $fields = array();
72
73 /**
74 * Form fields raw data
75 *
76 * @var array
77 */
78 public $raw_fields = array();
79
80 /**
81 * The constructor
82 *
83 * @param integer $entry_id
84 * @param \WeForms_Form $form
85 */
86 function __construct( $entry_id, $form ) {
87 $this->id = $entry_id;
88 $this->form = $form;
89
90 $this->populate_entry_data();
91 }
92
93 /**
94 *
95 * Populate the class with data
96 *
97 * @TODO: Abstract this
98 *
99 * @return void
100 */
101 public function populate_entry_data() {
102 global $wpdb;
103
104 // return if we populated the already, ensures single db call
105 if ( $this->form_id ) {
106 return;
107 }
108
109 $grid_css_added = false;
110 $grid_css = '<style>.wpufTable {display: table; width: 100%; } .wpufTableRow {display: table-row; } .wpufTableRow:nth-child(even) {background-color: #f5f5f5; } .wpufTableHeading {background-color: #eee; display: table-header-group; font-weight: bold; } .wpufTableCell, .wpufTableHead {border: none; display: table-cell; padding: 3px 10px; } .wpufTableFoot {background-color: #eee; display: table-footer-group; font-weight: bold; } .wpufTableBody {display: table-row-group; }</style>';
111
112 $values = array();
113
114 $query = "SELECT * FROM {$wpdb->weforms_entries} as entry
115 LEFT JOIN {$wpdb->weforms_entrymeta} AS meta ON entry.id = meta.weforms_entry_id
116 WHERE entry.id = {$this->id}";
117
118 $results = $wpdb->get_results( $query );
119
120 if ( $results ) {
121 $first_row = reset( $results );
122
123 $this->form_id = (int) $first_row->form_id;
124 $this->user_id = (int) $first_row->user_id;
125 $this->ip_address = long2ip( $first_row->user_ip );
126 $this->device = $first_row->user_device;
127 $this->referer = $first_row->referer;
128 $this->created = $first_row->created_at;
129
130 $this->fields = $this->form->get_field_values();
131 $this->raw_fields = $this->fields;
132
133 foreach ($results as $result) {
134
135 if ( array_key_exists( $result->meta_key, $this->fields ) ) {
136
137 $field = $this->fields[ $result->meta_key ];
138 $value = $result->meta_value;
139
140 $this->raw_fields[ $result->meta_key ]['value'] = $value;
141
142 if ( $field['type'] == 'textarea_field' ) {
143
144 $value = weforms_format_text( $value );
145
146 } elseif ( $field['type'] == 'name_field' ) {
147
148 $value = implode( ' ', explode( WeForms::$field_separator, $value ) );
149
150 } elseif ( in_array( $field['type'], array( 'dropdown_field', 'radio_field' ) ) ) {
151
152 if ( isset( $field['options'] ) && $field['options'] ) {
153
154 if ( isset( $field['options'][ $value ] ) ) {
155 $value = $field['options'][ $value ];
156 }
157 }
158
159 } elseif ( in_array( $field['type'], array( 'multiple_select', 'checkbox_field' ) ) ) {
160 $value = explode( WeForms::$field_separator, $value );
161 $temp_value = $value;
162
163 if ( is_array( $value ) && $value ) {
164
165 $new_array = array();
166
167 foreach ( $value as $option_key ) {
168 if ( is_array( $field['options'] ) && array_key_exists( $option_key, $field['options'] ) ) {
169 $new_array[] = $field['options'][ $option_key ];
170 } else {
171 $new_array[] = $option_key;
172 }
173 }
174
175 $value = $new_array;
176 }
177
178
179 } elseif ( in_array( $field['type'], array( 'image_upload', 'file_upload' ) ) ) {
180
181 $file_field = '';
182 $value = maybe_unserialize( $value );
183
184 if ( is_array( $value ) && $value ) {
185
186 foreach ($value as $attachment_id) {
187
188 if ( $field['type'] == 'image_upload' ) {
189 $thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' );
190 } else {
191 $thumb = get_post_field( 'post_title', $attachment_id );
192 }
193
194 $full_size = wp_get_attachment_url( $attachment_id );
195
196 $file_field .= sprintf( '<a href="%s" target="_blank">%s</a> ', $full_size, $thumb );
197 }
198 }
199
200 $value = $file_field;
201
202 } elseif ( $field['type'] == 'google_map' ) {
203 list( $lat, $long ) = explode( ',', $value );
204
205 $value = array( 'lat' => $lat, 'long' => $long );
206
207 } elseif ( $field['type'] == 'multiple_product' ) {
208
209 $field_value = unserialize( $value );
210
211 $serialized_value = array();
212
213 if ( is_array( $field_value ) ) {
214
215 foreach ( $field_value as $key => $sfv ) {
216
217 if ( is_array( $sfv ) ) {
218
219 $v = array();
220
221 foreach ($sfv as $key => $sv) {
222
223 $sv = str_replace( array( "_", "-" ), " ", $key) . ': ' . $sv;
224 $sv = ucwords( $sv );
225 $v[] = $sv;
226 }
227
228 $serialized_value[] = implode( "<br> ", $v );
229
230 }
231 }
232
233 $value = implode( "<br> <br> ", $serialized_value );
234 }
235 }
236 elseif ( $field['type'] == 'checkbox_grid' ) {
237
238 $entry_value = unserialize( $value );
239
240 if ( $entry_value ) {
241
242
243 $return = '';
244 $check = '';
245
246 if ( ! $grid_css_added ) {
247 $return = $grid_css;
248 $grid_css_added = true;
249 }
250
251 $new_val = array();
252
253 foreach ($entry_value as $key => $option_value ) {
254 $new_val[$key] = $option_value;
255 }
256
257 if ( $field['grid_rows'] && count( $field['grid_rows'] ) > 0 && $field['grid_columns'] && count( $field['grid_columns'] ) > 0 ) {
258
259 $return .= '<div class="wpufTable">
260 <div class="wpufTableHeading">
261 <div class="wpufTableRow">
262 <div class="wpufTableHead">&nbsp;</div>';
263
264 foreach ( $field['grid_columns'] as $column ) {
265 $return .= '<div class="wpufTableHead">' . $column . "</div>";
266 }
267
268 $return .= '</div>
269 </div>
270 <div class="wpufTableBody">';
271
272 foreach ( $field['grid_rows'] as $row_key => $row_value ) {
273
274 $return .= '<div class="wpufTableRow">
275 <div class="wpufTableHead">' . $row_value . '</div>';
276
277 foreach ( $field['grid_columns'] as $column_key => $column_value ) {
278 if ( isset( $new_val[$row_key] ) ) {
279 $check = ( in_array( $column_value, $new_val[$row_key] ) ) ? 'checked ' : '';
280 }
281
282 $return .= '<div class="wpufTableCell">
283 <label class="wpuf-radio-inline">
284 <input
285 name="' . $field['name'] . '[' . $row_key . '][]"
286 class="wpuf_'. $field['name'] . '_' . $form_id . '"
287 type="checkbox"
288 value="' . esc_attr( $column_value ) . '"'
289 . $check . 'disabled
290 />
291 </label>
292 </div>';
293
294 }
295
296 $return .= '</div>';
297
298 }
299
300 $return .= '</div>
301 </div>';
302
303 }
304
305 $value = $return;
306 }
307 }
308
309 elseif ( $field['type'] == 'multiple_choice_grid' ) {
310
311 $entry_value = unserialize( $value );
312
313 if ( $entry_value ) {
314
315 $return = '';
316 $check = '';
317
318 if ( ! $grid_css_added ) {
319 $return = $grid_css;
320 $grid_css_added = true;
321 }
322
323 $new_val = array();
324
325 foreach ($entry_value as $key => $option_value ) {
326 $new_val[$key] = $option_value;
327 }
328
329 if ( $field['grid_rows'] && count( $field['grid_rows'] ) > 0 && $field['grid_columns'] && count( $field['grid_columns'] ) > 0 ) {
330
331 $return .= '<div class="wpufTable">
332 <div class="wpufTableHeading">
333 <div class="wpufTableRow">
334 <div class="wpufTableHead">&nbsp;</div>';
335
336 foreach ( $field['grid_columns'] as $column ) {
337 $return .= '<div class="wpufTableHead">' . $column . "</div>";
338 }
339
340 $return .= '</div>
341 </div>
342 <div class="wpufTableBody">';
343
344 foreach ( $field['grid_rows'] as $row_key => $row_value ) {
345
346 $return .= '<div class="wpufTableRow">
347 <div class="wpufTableHead">' . $row_value . '</div>';
348
349 foreach ( $field['grid_columns'] as $column_key => $column_value ) {
350 if ( isset( $new_val[$row_key] ) ) {
351 $check = ( $new_val[$row_key] == $column_value ) ? 'checked ' : '';
352 }
353
354 $return .= '<div class="wpufTableCell">
355 <label class="wpuf-radio-inline">
356 <input
357 name="' . $field['name'] . '[' . $row_key . ']"
358 class="wpuf_'. $field['name'] . '_' . $form_id . '"
359 type="radio"
360 value="' . esc_attr( $column_value ) . '"'
361 . $check . 'disabled
362 />
363 </label>
364 </div>';
365
366 }
367
368 $return .= '</div>';
369
370 }
371
372 $return .= '</div>
373 </div>';
374
375 }
376
377 $value = $return;
378 }
379
380 }
381
382 elseif ( $field['type'] == 'address_field' || is_serialized( $value ) ) {
383
384 $field_value = unserialize( $value );
385
386 $serialized_value = array();
387
388 if ( is_array( $field_value ) ) {
389
390 foreach ( $field_value as $key => $sfv ) {
391
392 $sfv = str_replace( array( "_", "-" ), " ", $key) . ': ' . $sfv;
393 $sfv = ucwords( $sfv );
394 $serialized_value[] = $sfv;
395 }
396
397 $value = implode( "<br> ", $serialized_value );
398 }
399 }
400
401 $this->fields[ $result->meta_key ]['value'] = apply_filters( 'weforms_entry_meta_field', $value, $field );
402 }
403 }
404 }
405 }
406
407
408 /**
409 * Get entry fields
410 *
411 * @return array
412 */
413 public function get_fields() {
414 return $this->fields;
415 }
416
417 /**
418 * Get entry fields
419 *
420 * @return array
421 */
422 public function get_raw_fields() {
423 return $this->raw_fields;
424 }
425
426 /**
427 * Get entry metadata
428 *
429 * @return array
430 */
431 public function get_metadata() {
432 return array(
433 'id' => $this->id,
434 'form_id' => $this->form_id,
435 'form_title' => $this->form->get_name(),
436 'user' => $this->user_id ? get_user_by( 'id', $this->user_id )->display_name : false,
437 'ip_address' => $this->ip_address,
438 'device' => $this->device,
439 'referer' => $this->referer,
440 'created' => date_i18n( 'F j, Y g:i a', strtotime( $this->created ) )
441 );
442 }
443
444 /**
445 * Get entry metadata
446 *
447 * @return array
448 */
449 public function get_payment_data() {
450 global $wpdb;
451
452 if ( ! class_exists( 'WeForms_Payment' ) ) {
453 return;
454 }
455
456 return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}weforms_payments WHERE entry_id = {$this->id} " );
457 }
458
459 }
460