PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.0.0-beta.3
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.0.0-beta.3
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.0-beta.3, at includes/functions.php

576 lines 15.6 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
310 if ( ! $fields ) {
311 return false;
312 }
313
314 $data = array();
315 foreach ($fields as $field) {
316 if ( empty( $field['name'] ) ) {
317 continue;
318 }
319
320 $data[ $field['name'] ] = array(
321 'label' => $field['label'],
322 'type' => $field['input_type']
323 );
324 }
325
326 return $data;
327 }
328
329 /**
330 * Get data from an entry
331 *
332 * @param int $entry_id
333 * @param int $form_id
334 *
335 * @return false|array
336 */
337 function weforms_get_entry_data( $entry_id ) {
338 $data = array();
339 $entry = weforms_get_entry( $entry_id );
340 $fields = weforms_get_form_field_labels( $entry->form_id );
341
342 if ( ! $fields ) {
343 return false;
344 }
345
346 foreach ($fields as $meta_key => $field ) {
347 $value = weforms_get_entry_meta( $entry_id, $meta_key, true );
348
349 if ( $field['type'] == 'textarea' ) {
350 $data[ $meta_key ] = weforms_format_text( $value );
351 } elseif( $field['type'] == 'name' ) {
352 $data[ $meta_key ] = implode( ' ', explode( WPUF_Render_Form::$separator, $value ) );
353 } else {
354 $data[ $meta_key ] = $value;
355 }
356 }
357
358 return array(
359 'fields' => $fields,
360 'data' => $data
361 );
362 }
363
364 /**
365 * Format a text and apply WP function callbacks
366 *
367 * @param string $content
368 *
369 * @return string
370 */
371 function weforms_format_text( $content ) {
372 $content = wptexturize( $content );
373 $content = convert_smilies( $content );
374 $content = wpautop( $content );
375 $content = make_clickable( $content );
376
377 return $content;
378 }
379
380 /**
381 * Get User Agent browser and OS type
382 *
383 * @return array
384 */
385 function weforms_get_browser() {
386 $u_agent = $_SERVER['HTTP_USER_AGENT'];
387 $bname = 'Unknown';
388 $platform = 'Unknown';
389 $version = "";
390
391 // first get the platform
392 if (preg_match('/linux/i', $u_agent)) {
393 $platform = 'Linux';
394 } elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
395 $platform = 'MAC OS';
396 } elseif (preg_match('/windows|win32/i', $u_agent)) {
397 $platform = 'Windows';
398 }
399
400 // next get the name of the useragent yes seperately and for good reason
401 if ( preg_match('/MSIE/i',$u_agent) && !preg_match( '/Opera/i',$u_agent ) ) {
402 $bname = 'Internet Explorer';
403 $ub = "MSIE";
404 } elseif(preg_match('/Trident/i',$u_agent)) {
405 // this condition is for IE11
406 $bname = 'Internet Explorer';
407 $ub = "rv";
408 } elseif(preg_match('/Firefox/i',$u_agent)) {
409 $bname = 'Mozilla Firefox';
410 $ub = "Firefox";
411 } elseif(preg_match('/Chrome/i',$u_agent)) {
412 $bname = 'Google Chrome';
413 $ub = "Chrome";
414 } elseif(preg_match('/Safari/i',$u_agent)) {
415 $bname = 'Apple Safari';
416 $ub = "Safari";
417 } elseif(preg_match('/Opera/i',$u_agent)) {
418 $bname = 'Opera';
419 $ub = "Opera";
420 } elseif(preg_match('/Netscape/i',$u_agent)) {
421 $bname = 'Netscape';
422 $ub = "Netscape";
423 }
424
425 // finally get the correct version number
426 // Added "|:"
427 $known = array('Version', $ub, 'other');
428 $pattern = '#(?<browser>' . join('|', $known) .
429 ')[/|: ]+(?<version>[0-9.|a-zA-Z.]*)#';
430 if (!preg_match_all($pattern, $u_agent, $matches)) {
431 // we have no matching number just continue
432 }
433
434 // see how many we have
435 $i = count($matches['browser']);
436
437 if ($i != 1) {
438 //we will have two since we are not using 'other' argument yet
439 //see if version is before or after the name
440 if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
441 $version= $matches['version'][0];
442 } else {
443 $version= $matches['version'][1];
444 }
445 } else {
446 $version= $matches['version'][0];
447 }
448
449 // check if we have a number
450 if ( $version == null || $version == '' ) {
451 $version = '';
452 }
453
454 return array(
455 'userAgent' => $u_agent,
456 'name' => $bname,
457 'version' => $version,
458 'platform' => $platform,
459 'pattern' => $pattern
460 );
461 }
462
463 /**
464 * Get form notification merge tags
465 *
466 * @since 1.0
467 *
468 * @return array
469 */
470 function weforms_get_merge_tags() {
471 $tags = array(
472 'form' => array(
473 'title' => __( 'Form', 'weforms' ),
474 'tags' => array(
475 'entry_id' => __( 'Entry ID', 'weforms' ),
476 'form_id' => __( 'Form ID', 'weforms' ),
477 'form_name' => __( 'Form Name', 'weforms' )
478 )
479 ),
480 'system' => array(
481 'title' => __( 'System', 'weforms' ),
482 'tags' => array(
483 'admin_email' => __( 'Site Administrator Email', 'weforms' ),
484 'date' => __( 'Date', 'weforms' ),
485 'site_name' => __( 'Site Title', 'weforms' ),
486 'site_url' => __( 'Site URL', 'weforms' ),
487 'page_title' => __( 'Embedded Page Title', 'weforms' ),
488 )
489 ),
490 'user' => array(
491 'title' => __( 'User', 'weforms' ),
492 'tags' => array(
493 'ip_address' => __( 'IP Address', 'weforms' ),
494 'user_id' => __( 'User ID', 'weforms' ),
495 'first_name' => __( 'First Name', 'weforms' ),
496 'last_name' => __( 'Last Name', 'weforms' ),
497 'display_name' => __( 'Display Name', 'weforms' ),
498 'user_email' => __( 'Email', 'weforms' ),
499 )
500 ),
501 'urls' => array(
502 'title' => __( 'URL\'s', 'weforms' ),
503 'tags' => array(
504 'url_page' => __( 'Embeded Page URL', 'weforms' ),
505 'url_referer' => __( 'Referer URL', 'weforms' ),
506 'url_login' => __( 'Login URL', 'weforms' ),
507 'url_logout' => __( 'Logout URL', 'weforms' ),
508 'url_register' => __( 'Register URL', 'weforms' ),
509 'url_lost_password' => __( 'Lost Password URL', 'weforms' ),
510 )
511 ),
512 );
513
514 return apply_filters( 'wpuf_cf_merge_tags', $tags );
515 }
516
517 /**
518 * Record a form view count
519 *
520 * @param int $form_id
521 *
522 * @return void
523 */
524 function weforms_track_form_view( $form_id ) {
525 // don't track administrators
526 if ( current_user_can( 'administrator' ) ) {
527 return;
528 }
529
530 // ability to turn this off if someone doesn't like this tracking
531 $is_enabled = apply_filters( 'weforms_track_form_view', true );
532
533 if ( !$is_enabled ) {
534 return;
535 }
536
537 // increase the count
538 $meta_key = '_weforms_view_count';
539 $number = (int) get_post_meta( $form_id, $meta_key, true );
540
541 update_post_meta( $form_id, $meta_key, ( $number + 1 ) );
542 }
543
544 /**
545 * Get form view count of a form
546 *
547 * @param int $form_id
548 *
549 * @return int
550 */
551 function weforms_get_form_views( $form_id ) {
552 return (int) get_post_meta( $form_id, '_weforms_view_count', true );
553 }
554
555 /**
556 * Get the weForms global settings
557 *
558 * @param string $key
559 * @param mixed $default
560 *
561 * @return mixed
562 */
563 function weforms_get_settings( $key = '', $default = '' ) {
564 $settings = get_option( 'weforms_settings', array() );
565
566 if ( empty( $key ) ) {
567 return $settings;
568 }
569
570 if ( isset( $settings[ $key ] ) ) {
571 return $settings[ $key ];
572 }
573
574 return $default;
575 }
576