PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.0.4
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.0.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 / functions.php

functions.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.0.4, at includes/functions.php

640 lines 17.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Get a single form
5 *
6 * @since 1.0.3
7 *
8 * @param int $form_id
9 *
10 * @return false|WP_Post
11 */
12 function weform_get_form( $form_id ) {
13 $form = get_post( $form_id );
14
15 if ( ! $form ) {
16 return false;
17 }
18
19 if ( $form->post_type != 'wpuf_contact_form' ) {
20 return false;
21 }
22
23 return $form;
24 }
25
26 /**
27 * Get contact form templates
28 *
29 * @return array
30 */
31 function weforms_get_form_templates() {
32 require_once WEFORMS_INCLUDES . '/admin/form-templates/contact-form.php';
33 require_once WEFORMS_INCLUDES . '/admin/form-templates/support-form.php';
34 require_once WEFORMS_INCLUDES . '/admin/form-templates/event-registration-form.php';
35
36 $integrations = array(
37 'WPUF_Contact_Form_Template_Contact' => new WPUF_Contact_Form_Template_Contact(),
38 'WPUF_Contact_Form_Template_Support' => new WPUF_Contact_Form_Template_Support(),
39 'WPUF_Contact_Form_Template_Event_Registration' => new WPUF_Contact_Form_Template_Event_Registration(),
40 );
41
42 return apply_filters( 'weforms_form_templates', $integrations );
43 }
44
45 /**
46 * Get entries by a form_id
47 *
48 * @param int $form_id
49 * @param array $args
50 *
51 * @return Object
52 */
53 function weforms_get_form_entries( $form_id, $args = array() ) {
54 global $wpdb;
55
56 $defaults = array(
57 'number' => 10,
58 'offset' => 0,
59 'orderby' => 'created_at',
60 'status' => 'publish',
61 'order' => 'DESC',
62 );
63
64 $r = wp_parse_args( $args, $defaults );
65
66 $query = 'SELECT id, form_id, user_id, INET_NTOA( user_ip ) as ip_address, created_at
67 FROM ' . $wpdb->weforms_entries .
68 ' WHERE form_id = ' . $form_id . ' AND status = \'' . $r['status'] . '\''.
69 ' ORDER BY ' . $r['orderby'] . ' ' . $r['order'] .
70 ' LIMIT ' . $r['offset'] . ', ' . $r['number'];
71
72 $results = $wpdb->get_results( $query );
73
74 return $results;
75 }
76
77 /**
78 * Get an entry by id
79 *
80 * @param int $entry_id
81 *
82 * @return Object
83 */
84 function weforms_get_entry( $entry_id ) {
85 global $wpdb;
86
87 $cache_key = 'weforms-entry-' . $entry_id;
88 $entry = wp_cache_get( $cache_key, 'weforms' );
89
90 if ( false === $entry ) {
91 $query = 'SELECT id, form_id, user_id, user_device, referer, INET_NTOA( user_ip ) as ip_address, created_at
92 FROM ' . $wpdb->weforms_entries . '
93 WHERE id = %d';
94
95 $entry = $wpdb->get_row( $wpdb->prepare( $query, $entry_id ) );
96 wp_cache_set( $cache_key, $entry );
97 }
98
99 return $entry;
100 }
101
102 /**
103 * Insert a new entry
104 *
105 * @param array $args
106 * @param array $fields
107 *
108 * @return WP_Error|integer
109 */
110 function weforms_insert_entry( $args, $fields = array() ) {
111 global $wpdb;
112
113 $browser = weforms_get_browser();
114
115 $defaults = array(
116 'form_id' => 0,
117 'user_id' => get_current_user_id(),
118 'user_ip' => ip2long( wpuf_get_client_ip() ),
119 'user_device' => $browser['name'] . '/' . $browser['platform'],
120 'referer' => $_SERVER['HTTP_REFERER'],
121 'created_at' => current_time( 'mysql' )
122 );
123
124 $r = wp_parse_args( $args, $defaults );
125
126 if ( !$r['form_id'] ) {
127 return new WP_Error( 'no-form-id', __( 'No form ID was found.', 'weforms' ) );
128 }
129
130 if ( !$fields ) {
131 return new WP_Error( 'no-fields', __( 'No form fields were found.', 'weforms' ) );
132 }
133
134 $success = $wpdb->insert( $wpdb->weforms_entries, $r );
135
136 if ( is_wp_error( $success ) || !$success ) {
137 return new WP_Error( 'could-not-create', __( 'Could not create an entry', 'weforms' ) );
138 }
139
140 $entry_id = $wpdb->insert_id;
141
142 foreach ($fields as $key => $value) {
143 weforms_add_entry_meta( $entry_id, $key, $value );
144 }
145
146 return $entry_id;
147 }
148
149 /**
150 * Change an entry status
151 *
152 * @param int $entry_id
153 * @param string $status
154 *
155 * @return int|boolean
156 */
157 function weforms_change_entry_status( $entry_id, $status ) {
158 global $wpdb;
159
160 return $wpdb->update( $wpdb->weforms_entries,
161 array( 'status' => $status ),
162 array( 'id' => $entry_id ),
163 array( '%s' ),
164 array( '%d' )
165 );
166 }
167
168 /**
169 * Delete an entry
170 *
171 * @param int $entry_id
172 *
173 * @return int|boolean
174 */
175 function weforms_delete_entry( $entry_id ) {
176 global $wpdb;
177
178 return $wpdb->delete( $wpdb->weforms_entries, array( 'id' => $entry_id ), array( '%d' ) );
179 }
180
181 /**
182 * Add meta data field to an entry.
183 *
184 * @param int $entry_id Entry ID.
185 * @param string $meta_key Metadata name.
186 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
187 * @param bool $unique Optional. Whether the same key should not be added.
188 * Default false.
189 * @return int|false Meta ID on success, false on failure.
190 */
191 function weforms_add_entry_meta( $entry_id, $meta_key, $meta_value, $unique = false ) {
192 return add_metadata( 'weforms_entry', $entry_id, $meta_key, $meta_value, $unique);
193 }
194
195 /**
196 * Retrieve entry meta field for a entry.
197 *
198 * @param int $entry_id Entry ID.
199 * @param string $key Optional. The meta key to retrieve. By default, returns
200 * data for all keys. Default empty.
201 * @param bool $single Optional. Whether to return a single value. Default false.
202 * @return mixed Will be an array if $single is false. Will be value of meta data
203 * field if $single is true.
204 */
205 function weforms_get_entry_meta( $entry_id, $key = '', $single = false ) {
206 return get_metadata( 'weforms_entry', $entry_id, $key, $single );
207 }
208
209 /**
210 * Update entry meta field based on entry ID.
211 *
212 * Use the $prev_value parameter to differentiate between meta fields with the
213 * same key and entry ID.
214 *
215 * If the meta field for the entry does not exist, it will be added.
216 *
217 * @param int $entry_id entry ID.
218 * @param string $meta_key Metadata key.
219 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
220 * @param mixed $prev_value Optional. Previous value to check before removing.
221 * Default empty.
222 * @return int|bool Meta ID if the key didn't exist, true on successful update,
223 * false on failure.
224 */
225 function weforms_update_entry_meta( $entry_id, $meta_key, $meta_value, $prev_value = '' ) {
226 return update_metadata( 'weforms_entry', $entry_id, $meta_key, $meta_value, $prev_value);
227 }
228
229 /**
230 * Delete everything from entry meta matching meta key.
231 *
232 * @param string $entry_meta_key Key to search for when deleting.
233 * @return bool Whether the entry meta key was deleted from the database.
234 */
235 function weforms_delete_entry_meta_by_key( $meta_key ) {
236 return delete_metadata( 'weforms_entry', null, $meta_key, '', true );
237 }
238
239 /**
240 * Retrieve entry meta fields, based on entry ID.
241 *
242 * The entry meta fields are retrieved from the cache where possible,
243 * so the function is optimized to be called more than once.
244 *
245 * @since 1.2.0
246 *
247 * @param int $entry_id Optional.
248 * @return array entry meta for the given entry.
249 */
250 function weforms_get_entry_custom( $entry_id = 0 ) {
251 $entry_id = absint( $entry_id );
252
253 return weforms_get_entry_meta( $entry_id );
254 }
255
256 /**
257 * Retrieve meta field names for a entry.
258 *
259 * If there are no meta fields, then nothing (null) will be returned.
260 *
261 * @param int $entry_id Optional.
262 * @return array|void Array of the keys, if retrieved.
263 */
264 function weforms_get_entry_custom_keys( $entry_id = 0 ) {
265 $custom = weforms_get_entry_custom( $entry_id );
266
267 if ( !is_array( $custom ) ) {
268 return false;
269 }
270
271 if ( $keys = array_keys($custom) ) {
272 return $keys;
273 }
274 }
275
276 /**
277 * Get the number of entries count on a form
278 *
279 * @param int $form_id
280 *
281 * @return int
282 */
283 function weforms_count_form_entries( $form_id, $status = 'publish' ) {
284 global $wpdb;
285
286 return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT count(id) FROM ' . $wpdb->weforms_entries . ' WHERE form_id = %d AND status = %s', $form_id, $status ) );
287 }
288
289 /**
290 * Get table column heads for a form
291 *
292 * For now, return only text type fields
293 *
294 * @param int $form_id
295 *
296 * @return array
297 */
298 function weforms_get_entry_columns( $form_id, $limit = 6 ) {
299 $fields = wpuf_get_form_fields( $form_id );
300 $columns = array();
301
302 // filter by input types
303 if ( $limit ) {
304
305 $fields = array_filter( $fields, function($item) {
306 return in_array( $item['input_type'], array( 'text', 'name', 'select', 'radio', 'email', 'url' ) );
307 } );
308 }
309
310 if ( $fields ) {
311 foreach ($fields as $field) {
312 $columns[ $field['name'] ] = $field['label'];
313 }
314 }
315
316 // if passed 0/false, return all collumns
317 if ( ! $limit ) {
318 return $columns;
319 }
320
321 return array_slice( $columns, 0, 6 ); // max 6 columns
322 }
323
324 /**
325 * Get fields from a form by it's meta key
326 *
327 * @param int $form_id
328 *
329 * @return array
330 */
331 function weforms_get_form_field_labels( $form_id ) {
332 $fields = wpuf_get_form_fields( $form_id );
333 $exclude = array( 'step_start', 'section_break', 'recaptcha', 'shortcode', 'action_hook' );
334
335 if ( ! $fields ) {
336 return false;
337 }
338
339 $data = array();
340 foreach ($fields as $field) {
341 if ( empty( $field['name'] ) ) {
342 continue;
343 }
344
345 // exclude the fields
346 if ( in_array( $field['input_type'], $exclude ) ) {
347 continue;
348 }
349
350 $data[ $field['name'] ] = array(
351 'label' => $field['label'],
352 'type' => $field['input_type']
353 );
354 }
355
356 return $data;
357 }
358
359 /**
360 * Get data from an entry
361 *
362 * @param int $entry_id
363 * @param int $form_id
364 *
365 * @return false|array
366 */
367 function weforms_get_entry_data( $entry_id ) {
368 $data = array();
369 $entry = weforms_get_entry( $entry_id );
370 $fields = weforms_get_form_field_labels( $entry->form_id );
371
372 if ( ! $fields ) {
373 return false;
374 }
375
376 foreach ($fields as $meta_key => $field ) {
377 $value = weforms_get_entry_meta( $entry_id, $meta_key, true );
378
379 if ( $field['type'] == 'textarea' ) {
380
381 $data[ $meta_key ] = weforms_format_text( $value );
382
383 } elseif ( $field['type'] == 'name' ) {
384
385 $data[ $meta_key ] = implode( ' ', explode( WPUF_Render_Form::$separator, $value ) );
386
387 } elseif ( in_array( $field['type'], array( 'image_upload', 'file_upload' ) ) ) {
388
389 $data[ $meta_key ] = '';
390
391 if ( is_array( $value ) && $value ) {
392
393 foreach ($value as $attachment_id) {
394
395 if ( $field['type'] == 'image_upload' ) {
396 $thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' );
397 } else {
398 $thumb = get_post_field( 'post_title', $attachment_id );
399 }
400
401 $full_size = wp_get_attachment_url( $attachment_id );
402
403 $data[ $meta_key ] .= sprintf( '<a href="%s" target="_blank">%s</a> ', $full_size, $thumb );
404 }
405 }
406
407 } elseif ( in_array( $field['type'], array( 'checkbox', 'multiselect' ) ) ) {
408
409 $data[ $meta_key ] = explode( WPUF_Render_Form::$separator, $value );
410
411 } elseif ( $field['type'] == 'map' ) {
412
413 list( $lat, $long ) = explode( ',', $value );
414
415 $data[ $meta_key ] = array( 'lat' => $lat, 'long' => $long );
416
417 } else {
418 $data[ $meta_key ] = $value;
419 }
420 }
421
422 return array(
423 'fields' => $fields,
424 'data' => $data
425 );
426 }
427
428 /**
429 * Format a text and apply WP function callbacks
430 *
431 * @param string $content
432 *
433 * @return string
434 */
435 function weforms_format_text( $content ) {
436 $content = wptexturize( $content );
437 $content = convert_smilies( $content );
438 $content = wpautop( $content );
439 $content = make_clickable( $content );
440
441 return $content;
442 }
443
444 /**
445 * Get User Agent browser and OS type
446 *
447 * @return array
448 */
449 function weforms_get_browser() {
450 $u_agent = $_SERVER['HTTP_USER_AGENT'];
451 $bname = 'Unknown';
452 $platform = 'Unknown';
453 $version = "";
454
455 // first get the platform
456 if (preg_match('/linux/i', $u_agent)) {
457 $platform = 'Linux';
458 } elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
459 $platform = 'MAC OS';
460 } elseif (preg_match('/windows|win32/i', $u_agent)) {
461 $platform = 'Windows';
462 }
463
464 // next get the name of the useragent yes seperately and for good reason
465 if ( preg_match('/MSIE/i',$u_agent) && !preg_match( '/Opera/i',$u_agent ) ) {
466 $bname = 'Internet Explorer';
467 $ub = "MSIE";
468 } elseif(preg_match('/Trident/i',$u_agent)) {
469 // this condition is for IE11
470 $bname = 'Internet Explorer';
471 $ub = "rv";
472 } elseif(preg_match('/Firefox/i',$u_agent)) {
473 $bname = 'Mozilla Firefox';
474 $ub = "Firefox";
475 } elseif(preg_match('/Chrome/i',$u_agent)) {
476 $bname = 'Google Chrome';
477 $ub = "Chrome";
478 } elseif(preg_match('/Safari/i',$u_agent)) {
479 $bname = 'Apple Safari';
480 $ub = "Safari";
481 } elseif(preg_match('/Opera/i',$u_agent)) {
482 $bname = 'Opera';
483 $ub = "Opera";
484 } elseif(preg_match('/Netscape/i',$u_agent)) {
485 $bname = 'Netscape';
486 $ub = "Netscape";
487 }
488
489 // finally get the correct version number
490 // Added "|:"
491 $known = array('Version', $ub, 'other');
492 $pattern = '#(?<browser>' . join('|', $known) .
493 ')[/|: ]+(?<version>[0-9.|a-zA-Z.]*)#';
494 if (!preg_match_all($pattern, $u_agent, $matches)) {
495 // we have no matching number just continue
496 }
497
498 // see how many we have
499 $i = count($matches['browser']);
500
501 if ($i != 1) {
502 //we will have two since we are not using 'other' argument yet
503 //see if version is before or after the name
504 if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
505 $version= $matches['version'][0];
506 } else {
507 $version= $matches['version'][1];
508 }
509 } else {
510 $version= $matches['version'][0];
511 }
512
513 // check if we have a number
514 if ( $version == null || $version == '' ) {
515 $version = '';
516 }
517
518 return array(
519 'userAgent' => $u_agent,
520 'name' => $bname,
521 'version' => $version,
522 'platform' => $platform,
523 'pattern' => $pattern
524 );
525 }
526
527 /**
528 * Get form notification merge tags
529 *
530 * @since 1.0
531 *
532 * @return array
533 */
534 function weforms_get_merge_tags() {
535 $tags = array(
536 'form' => array(
537 'title' => __( 'Form', 'weforms' ),
538 'tags' => array(
539 'entry_id' => __( 'Entry ID', 'weforms' ),
540 'form_id' => __( 'Form ID', 'weforms' ),
541 'form_name' => __( 'Form Name', 'weforms' )
542 )
543 ),
544 'system' => array(
545 'title' => __( 'System', 'weforms' ),
546 'tags' => array(
547 'admin_email' => __( 'Site Administrator Email', 'weforms' ),
548 'date' => __( 'Date', 'weforms' ),
549 'site_name' => __( 'Site Title', 'weforms' ),
550 'site_url' => __( 'Site URL', 'weforms' ),
551 'page_title' => __( 'Embedded Page Title', 'weforms' ),
552 )
553 ),
554 'user' => array(
555 'title' => __( 'User', 'weforms' ),
556 'tags' => array(
557 'ip_address' => __( 'IP Address', 'weforms' ),
558 'user_id' => __( 'User ID', 'weforms' ),
559 'first_name' => __( 'First Name', 'weforms' ),
560 'last_name' => __( 'Last Name', 'weforms' ),
561 'display_name' => __( 'Display Name', 'weforms' ),
562 'user_email' => __( 'Email', 'weforms' ),
563 )
564 ),
565 'urls' => array(
566 'title' => __( 'URL\'s', 'weforms' ),
567 'tags' => array(
568 'url_page' => __( 'Embeded Page URL', 'weforms' ),
569 'url_referer' => __( 'Referer URL', 'weforms' ),
570 'url_login' => __( 'Login URL', 'weforms' ),
571 'url_logout' => __( 'Logout URL', 'weforms' ),
572 'url_register' => __( 'Register URL', 'weforms' ),
573 'url_lost_password' => __( 'Lost Password URL', 'weforms' ),
574 )
575 ),
576 );
577
578 return apply_filters( 'wpuf_cf_merge_tags', $tags );
579 }
580
581 /**
582 * Record a form view count
583 *
584 * @param int $form_id
585 *
586 * @return void
587 */
588 function weforms_track_form_view( $form_id ) {
589 // don't track administrators
590 if ( current_user_can( 'administrator' ) ) {
591 return;
592 }
593
594 // ability to turn this off if someone doesn't like this tracking
595 $is_enabled = apply_filters( 'weforms_track_form_view', true );
596
597 if ( !$is_enabled ) {
598 return;
599 }
600
601 // increase the count
602 $meta_key = '_weforms_view_count';
603 $number = (int) get_post_meta( $form_id, $meta_key, true );
604
605 update_post_meta( $form_id, $meta_key, ( $number + 1 ) );
606 }
607
608 /**
609 * Get form view count of a form
610 *
611 * @param int $form_id
612 *
613 * @return int
614 */
615 function weforms_get_form_views( $form_id ) {
616 return (int) get_post_meta( $form_id, '_weforms_view_count', true );
617 }
618
619 /**
620 * Get the weForms global settings
621 *
622 * @param string $key
623 * @param mixed $default
624 *
625 * @return mixed
626 */
627 function weforms_get_settings( $key = '', $default = '' ) {
628 $settings = get_option( 'weforms_settings', array() );
629
630 if ( empty( $key ) ) {
631 return $settings;
632 }
633
634 if ( isset( $settings[ $key ] ) ) {
635 return $settings[ $key ];
636 }
637
638 return $default;
639 }
640