PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.0.1
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.0.1
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.1, at includes/functions.php

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