PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.6.25
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.6.25
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.6.25, at includes/class-form-entry.php

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