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

421 lines 16.0 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 = "SELECT * FROM {$wpdb->weforms_entries} as entry
114 LEFT JOIN {$wpdb->weforms_entrymeta} AS meta ON entry.id = meta.weforms_entry_id
115 WHERE entry.id = {$this->id}";
116
117 $results = $wpdb->get_results( $query );
118
119 if ( $results ) {
120 $first_row = reset( $results );
121
122 $this->form_id = (int) $first_row->form_id;
123 $this->user_id = (int) $first_row->user_id;
124 $this->ip_address = long2ip( $first_row->user_ip );
125 $this->device = $first_row->user_device;
126 $this->referer = $first_row->referer;
127 $this->created = $first_row->created_at;
128
129 $this->fields = $this->form->get_field_values();
130 $this->raw_fields = $this->fields;
131
132 foreach ( $results as $result ) {
133 if ( array_key_exists( $result->meta_key, $this->fields ) ) {
134 $field = $this->fields[ $result->meta_key ];
135 $value = $result->meta_value;
136
137 $this->raw_fields[ $result->meta_key ]['value'] = $value;
138
139 if ( $field['type'] == 'textarea_field' ) {
140 $value = weforms_format_text( $value );
141 } elseif ( $field['type'] == 'name_field' ) {
142 $value = implode( ' ', explode( WeForms::$field_separator, $value ) );
143 } elseif ( in_array( $field['type'], [ 'dropdown_field', 'radio_field' ] ) ) {
144 if ( isset( $field['options'] ) && $field['options'] ) {
145 if ( isset( $field['options'][ $value ] ) ) {
146 $value = $field['options'][ $value ];
147 }
148 }
149 } elseif ( in_array( $field['type'], [ 'multiple_select', 'checkbox_field' ] ) ) {
150 $value = explode( WeForms::$field_separator, $value );
151 $temp_value = $value;
152
153 if ( is_array( $value ) && $value ) {
154 $new_array = [];
155
156 foreach ( $value as $option_key ) {
157 if ( is_array( $field['options'] ) && array_key_exists( $option_key, $field['options'] ) ) {
158 $new_array[] = $field['options'][ $option_key ];
159 } else {
160 $new_array[] = $option_key;
161 }
162 }
163
164 $value = $new_array;
165 }
166 } elseif ( in_array( $field['type'], [ 'image_upload', 'file_upload' ] ) ) {
167 $file_field = '';
168 $value = maybe_unserialize( $value );
169
170 if ( is_array( $value ) && $value ) {
171 foreach ( $value as $attachment_id ) {
172 if ( $field['type'] == 'image_upload' ) {
173 $thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' );
174 } else {
175 $thumb = get_post_field( 'post_title', $attachment_id );
176 }
177
178 $full_size = wp_get_attachment_url( $attachment_id );
179
180 $file_field .= sprintf( '<a href="%s" target="_blank">%s</a> ', $full_size, $thumb );
181 }
182 }
183
184 $value = $file_field;
185 } elseif ( $field['type'] == 'google_map' ) {
186 list( $lat, $long ) = explode( ',', $value );
187
188 $value = [
189 'lat' => $lat,
190 'long' => $long,
191 ];
192 } elseif ( $field['type'] == 'multiple_product' ) {
193 $field_value = unserialize( $value );
194
195 $serialized_value = [];
196
197 if ( is_array( $field_value ) ) {
198 foreach ( $field_value as $key => $sfv ) {
199 if ( is_array( $sfv ) ) {
200 $v = [];
201
202 foreach ( $sfv as $key => $sv ) {
203 $sv = str_replace( [ '_', '-' ], ' ', $key ) . ': ' . $sv;
204 $sv = ucwords( $sv );
205 $v[] = $sv;
206 }
207
208 $serialized_value[] = implode( '<br> ', $v );
209 }
210 }
211
212 $value = implode( '<br> <br> ', $serialized_value );
213 }
214 } elseif ( $field['type'] == 'checkbox_grid' ) {
215 $entry_value = unserialize( $value );
216
217 if ( $entry_value ) {
218 $return = '';
219 $check = '';
220
221 if ( !$grid_css_added ) {
222 $return = $grid_css;
223 $grid_css_added = true;
224 }
225
226 $new_val = [];
227
228 foreach ( $entry_value as $key => $option_value ) {
229 $new_val[ $key ] = $option_value;
230 }
231
232 if ( $field['grid_rows'] && count( $field['grid_rows'] ) > 0 && $field['grid_columns'] && count( $field['grid_columns'] ) > 0 ) {
233 $return .= '<div class="wpufTable">
234 <div class="wpufTableHeading">
235 <div class="wpufTableRow">
236 <div class="wpufTableHead">&nbsp;</div>';
237
238 foreach ( $field['grid_columns'] as $column ) {
239 $return .= '<div class="wpufTableHead">' . $column . '</div>';
240 }
241
242 $return .= '</div>
243 </div>
244 <div class="wpufTableBody">';
245
246 foreach ( $field['grid_rows'] as $row_key => $row_value ) {
247 $return .= '<div class="wpufTableRow">
248 <div class="wpufTableHead">' . $row_value . '</div>';
249
250 foreach ( $field['grid_columns'] as $column_key => $column_value ) {
251 if ( isset( $new_val[ $row_key ] ) ) {
252 $check = ( in_array( $column_value, $new_val[ $row_key ] ) ) ? 'checked ' : '';
253 }
254
255 $return .= '<div class="wpufTableCell">
256 <label class="wpuf-radio-inline">
257 <input
258 name="' . $field['name'] . '[' . $row_key . '][]"
259 class="wpuf_' . $field['name'] . '_' . $this->form_id . '"
260 type="checkbox"
261 value="' . esc_attr( $column_value ) . '"'
262 . $check . 'disabled
263 />
264 </label>
265 </div>';
266 }
267
268 $return .= '</div>';
269 }
270
271 $return .= '</div>
272 </div>';
273 }
274
275 $value = $return;
276 }
277 } elseif ( $field['type'] == 'multiple_choice_grid' ) {
278 $entry_value = unserialize( $value );
279
280 if ( $entry_value ) {
281 $return = '';
282 $check = '';
283
284 if ( !$grid_css_added ) {
285 $return = $grid_css;
286 $grid_css_added = true;
287 }
288
289 $new_val = [];
290
291 foreach ( $entry_value as $key => $option_value ) {
292 $new_val[ $key ] = $option_value;
293 }
294
295 if ( $field['grid_rows'] && count( $field['grid_rows'] ) > 0 && $field['grid_columns'] && count( $field['grid_columns'] ) > 0 ) {
296 $return .= '<div class="wpufTable">
297 <div class="wpufTableHeading">
298 <div class="wpufTableRow">
299 <div class="wpufTableHead">&nbsp;</div>';
300
301 foreach ( $field['grid_columns'] as $column ) {
302 $return .= '<div class="wpufTableHead">' . $column . '</div>';
303 }
304
305 $return .= '</div>
306 </div>
307 <div class="wpufTableBody">';
308
309 foreach ( $field['grid_rows'] as $row_key => $row_value ) {
310 $return .= '<div class="wpufTableRow">
311 <div class="wpufTableHead">' . $row_value . '</div>';
312
313 foreach ( $field['grid_columns'] as $column_key => $column_value ) {
314 if ( isset( $new_val[ $row_key ] ) ) {
315 $check = ( $new_val[ $row_key ] == $column_value ) ? 'checked ' : '';
316 }
317
318 $return .= '<div class="wpufTableCell">
319 <label class="wpuf-radio-inline">
320 <input
321 name="' . $field['name'] . '[' . $row_key . ']"
322 class="wpuf_' . $field['name'] . '_' . $this->form_id . '"
323 type="radio"
324 value="' . esc_attr( $column_value ) . '"'
325 . $check . 'disabled
326 />
327 </label>
328 </div>';
329 }
330
331 $return .= '</div>';
332 }
333
334 $return .= '</div>
335 </div>';
336 }
337
338 $value = $return;
339 }
340 } elseif ( $field['type'] == 'address_field' || is_serialized( $value ) ) {
341 $field_value = unserialize( $value );
342
343 $serialized_value = [];
344
345 if ( is_array( $field_value ) ) {
346 foreach ( $field_value as $key => $sfv ) {
347 $sfv = str_replace( [ '_', '-' ], ' ', $key ) . ': ' . $sfv;
348 $sfv = ucwords( $sfv );
349 $serialized_value[] = $sfv;
350 }
351
352 $value = implode( '<br> ', $serialized_value );
353 }
354 } elseif ( $field['type'] == 'signature_field' ) {
355 $url = content_url() . $value;
356 $value = $url;
357
358 if ( isset( $_REQUEST['action'] ) != 'weforms_pdf_download' ) {
359 $value = sprintf( '<img src="%s">', $url );
360 $value .= sprintf( '<a style="margin-left: -200px" href="%s">Download</a>', $url );
361 }
362 }
363
364 $this->fields[ $result->meta_key ]['value'] = apply_filters( 'weforms_entry_meta_field', $value, $field );
365 }
366 }
367 }
368 }
369
370 /**
371 * Get entry fields
372 *
373 * @return array
374 */
375 public function get_fields() {
376 return $this->fields;
377 }
378
379 /**
380 * Get entry fields
381 *
382 * @return array
383 */
384 public function get_raw_fields() {
385 return $this->raw_fields;
386 }
387
388 /**
389 * Get entry metadata
390 *
391 * @return array
392 */
393 public function get_metadata() {
394 return [
395 'id' => $this->id,
396 'form_id' => $this->form_id,
397 'form_title' => $this->form->get_name(),
398 'user' => $this->user_id ? get_user_by( 'id', $this->user_id )->display_name : false,
399 'ip_address' => $this->ip_address,
400 'device' => $this->device,
401 'referer' => $this->referer,
402 'created' => date_i18n( 'F j, Y g:i a', strtotime( $this->created ) ),
403 ];
404 }
405
406 /**
407 * Get entry metadata
408 *
409 * @return array
410 */
411 public function get_payment_data() {
412 global $wpdb;
413
414 if ( !class_exists( 'WeForms_Payment' ) ) {
415 return;
416 }
417
418 return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}weforms_payments WHERE entry_id = {$this->id} " );
419 }
420 }
421