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 / class-ajax.php

class-ajax.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.0.0-beta.3, at includes/class-ajax.php

531 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The ajax handler class
5 */
6 class WeForms_Ajax {
7
8 public function __construct() {
9
10 // backend requests
11 add_action( 'wp_ajax_weforms_form_list', array( $this, 'get_contact_forms' ) );
12 add_action( 'wp_ajax_weforms_form_names', array( $this, 'get_contact_form_names' ) );
13 add_action( 'wp_ajax_weforms_form_create', array( $this, 'create_form' ) );
14 add_action( 'wp_ajax_weforms_form_delete', array( $this, 'delete_form' ) );
15 add_action( 'wp_ajax_weforms_form_delete_bulk', array( $this, 'delete_form_bulk' ) );
16 add_action( 'wp_ajax_weforms_form_duplicate', array( $this, 'duplicate_form' ) );
17
18 // form settings
19 add_action( 'wp_ajax_weforms_save_settings', array( $this, 'save_settings' ) );
20 add_action( 'wp_ajax_weforms_get_settings', array( $this, 'get_settings' ) );
21
22 // import
23 add_action( 'wp_ajax_weforms_import_form', array( $this, 'import_form' ) );
24
25 // form editing
26 add_action( 'wp_ajax_weforms_get_form', array( $this, 'get_form' ) );
27
28 // entries
29 add_action( 'wp_ajax_weforms_form_entries', array( $this, 'get_entries' ) );
30 add_action( 'wp_ajax_weforms_form_entry_details', array( $this, 'get_entry_detail' ) );
31 add_action( 'wp_ajax_weforms_form_entry_trash', array( $this, 'trash_entry' ) );
32 add_action( 'wp_ajax_weforms_form_entry_trash_bulk', array( $this, 'bulk_delete_entry' ) );
33
34 // frontend requests
35 add_action( 'wp_ajax_wpuf_submit_contact', array( $this, 'handle_frontend_submission' ) );
36 add_action( 'wp_ajax_nopriv_wpuf_submit_contact', array( $this, 'handle_frontend_submission' ) );
37 }
38
39 /**
40 * Administrator validation
41 *
42 * @return void
43 */
44 public function check_admin() {
45 if ( !current_user_can( 'administrator' ) ) {
46 wp_send_json_error( __( 'You do not have sufficient permission.', 'weforms' ) );
47 }
48 }
49
50 /**
51 * Get a form to edit
52 *
53 * @return void
54 */
55 public function get_form() {
56
57 $this->check_admin();
58
59 $form_id = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : 0;
60
61 $data = array(
62 'post' => get_post( $form_id ),
63 'form_fields' => wpuf_get_form_fields( $form_id ),
64 'settings' => wpuf_get_form_settings( $form_id ),
65 'notifications' => wpuf_get_form_notifications( $form_id ),
66 'integrations' => wpuf_get_form_integrations( $form_id )
67 );
68
69 wp_send_json_success( $data );
70 }
71
72 /**
73 * Get all contact forms
74 *
75 * @return void
76 */
77 public function get_contact_forms() {
78 check_ajax_referer( 'weforms' );
79
80 $this->check_admin();
81
82 $args = array(
83 'post_type' => 'wpuf_contact_form',
84 'posts_per_page' => 10,
85 'paged' => isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1,
86 'order' => 'DESC',
87 'orderby' => 'post_date'
88 );
89
90 $forms = new WP_Query( $args );
91 $contact_forms = $forms->get_posts();
92
93 array_map( function($form) {
94 $form->entries = weforms_count_form_entries( $form->ID );
95 $form->views = weforms_get_form_views( $form->ID );
96 }, $contact_forms);
97
98 // var_dump( $forms->get_posts() );
99 // var_dump( $forms );
100 $response = array(
101 'forms' => $contact_forms,
102 'total' => (int) $forms->found_posts,
103 'pages' => (int) $forms->max_num_pages
104 );
105
106 wp_send_json_success( $response );
107 }
108
109 /**
110 * Get the names of contact forms for generating dropdown
111 *
112 * @return void
113 */
114 public function get_contact_form_names() {
115 check_ajax_referer( 'weforms' );
116
117 $this->check_admin();
118
119 $args = array(
120 'post_type' => 'wpuf_contact_form',
121 'posts_per_page' => -1,
122 'post_status' => 'publish',
123 'order' => 'ASC',
124 'orderby' => 'post_title'
125 );
126
127 $response = array();
128 $forms = new WP_Query( $args );
129 $contact_forms = $forms->get_posts();
130
131 foreach ($contact_forms as $form) {
132 $response[] = array(
133 'id' => $form->ID,
134 'title' => $form->post_title . ' (#' . $form->ID . ')'
135 );
136 }
137
138 wp_send_json_success( $response );
139 }
140
141 /**
142 * Create a form
143 *
144 * @return void
145 */
146 public function create_form() {
147 check_ajax_referer( 'weforms' );
148
149 $this->check_admin();
150
151 $form_name = isset( $_POST['form_name'] ) ? sanitize_text_field( $_POST['form_name'] ) : '';
152
153 if ( empty( $form_name ) ) {
154 wp_send_json_error( __( 'Please provide a form name', 'weforms' ) );
155 }
156
157 $post_id = wp_insert_post( array(
158 'post_title' => $form_name,
159 'post_type' => 'wpuf_contact_form',
160 'post_status' => 'publish'
161 ) );
162
163 if ( is_wp_error( $post_id )) {
164 wp_send_json_error( $post_id->get_error_message() );
165 }
166
167 wp_send_json_success( array(
168 'form_id' => $post_id,
169 'form_name' => $form_name
170 ) );
171 }
172
173 /**
174 * Delete a form
175 *
176 * @return void
177 */
178 public function delete_form() {
179 check_ajax_referer( 'weforms' );
180
181 $this->check_admin();
182
183 $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0;
184
185 if ( ! $form_id ) {
186 wp_send_json_error( __( 'No form id provided!', 'weforms' ) );
187 }
188
189 wpuf_delete_form( $form_id, true );
190 wp_send_json_success();
191 }
192
193 public function delete_form_bulk() {
194 check_ajax_referer( 'weforms' );
195
196 $this->check_admin();
197
198 $form_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array();
199
200 if ( ! $form_ids ) {
201 wp_send_json_error( __( 'No form ids provided!', 'weforms' ) );
202 }
203
204 foreach ($form_ids as $form_id) {
205 wpuf_delete_form( $form_id, true );
206 }
207
208 wp_send_json_success();
209 }
210
211 /**
212 * Duplicate a form
213 *
214 * @return voiud
215 */
216 public function duplicate_form() {
217 check_ajax_referer( 'weforms' );
218
219 $this->check_admin();
220
221 $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0;
222
223 $duplicate_id = wpuf_duplicate_form( $form_id );
224 $form = get_post( $duplicate_id );
225
226 $form->entires = 0;
227 $form->views = 0;
228
229 wp_send_json_success( $form );
230 }
231
232 /**
233 * Save the weForms settings
234 *
235 * @return void
236 */
237 public function save_settings() {
238 check_ajax_referer( 'weforms' );
239
240 $this->check_admin();
241
242 $settings = (array) json_decode( wp_unslash( $_POST['settings'] ) );
243
244 update_option( 'weforms_settings', $settings );
245
246 do_action( 'weforms_save_settings', $settings );
247
248 wp_send_json_success( $settings );
249 }
250
251 /**
252 * Get the weForms Settings
253 *
254 * @return void
255 */
256 public function get_settings() {
257 check_ajax_referer( 'weforms' );
258
259 $this->check_admin();
260
261 $settings = get_option( 'weforms_settings', array() );
262
263 wp_send_json_success( $settings );
264 }
265
266 /**
267 * Get all entries
268 *
269 * @return void
270 */
271 public function get_entries() {
272 check_ajax_referer( 'weforms' );
273
274 $this->check_admin();
275
276 $form_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
277 $current_page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1;
278 $per_page = 20;
279 $offset = ( $current_page - 1 ) * $per_page;
280
281 if ( ! $form_id ) {
282 wp_send_json_error( __( 'No form id provided!', 'weforms' ) );
283 }
284
285 $entries = weforms_get_form_entries( $form_id, array(
286 'number' => $per_page,
287 'offset' => $offset
288 ) );
289
290 $columns = weforms_get_entry_columns( $form_id );
291 $total_entries = weforms_count_form_entries( $form_id );
292
293 array_map( function( $entry ) use ($columns) {
294 $entry_id = $entry->id;
295 $entry->fields = array();
296
297 foreach ($columns as $meta_key => $label) {
298 $value = weforms_get_entry_meta( $entry_id, $meta_key, true );
299 $entry->fields[$meta_key] = str_replace( WPUF_Render_Form::$separator, ' ', $value );
300 }
301 }, $entries );
302
303 $response = array(
304 'columns' => $columns,
305 'entries' => $entries,
306 'form_title' => get_post_field( 'post_title', $form_id ),
307 'pagination' => array(
308 'total' => $total_entries,
309 'per_page' => $per_page,
310 'pages' => ceil( $total_entries / $per_page ),
311 'current' => $current_page
312 )
313 );
314
315 wp_send_json_success( $response );
316 }
317
318 /**
319 * Get an entry details
320 *
321 * @return void
322 */
323 public function get_entry_detail() {
324 check_ajax_referer( 'weforms' );
325
326 $this->check_admin();
327
328 $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0;
329 $entry = weforms_get_entry( $entry_id );
330
331 if ( !$entry ) {
332 wp_send_json_error( __( 'No such entry found!', 'weforms' ) );
333 }
334
335 $info = array(
336 'form_title' => get_post_field( 'post_title', $entry->form_id ),
337 'created' => date_i18n( 'F j, Y g:i a', strtotime( $entry->created_at ) ),
338 'ip' => $entry->ip_address,
339 'user' => $entry->user_id ? get_user_by( 'id', $entry->user_id )->display_name : false,
340 'referer' => $entry->referer,
341 'device' => $entry->user_device
342 );
343
344 $data = weforms_get_entry_data( $entry_id );
345
346 if ( false === $data ) {
347 wp_send_json_error( __( 'No form fields found!', 'weforms' ) );
348 }
349
350 $response = array(
351 'form_fields' => $data['fields'],
352 'meta_data' => $data['data'],
353 'info' => $info
354 );
355
356 wp_send_json_success( $response );
357 }
358
359 /**
360 * Trash an entry
361 *
362 * @return void
363 */
364 public function trash_entry() {
365 check_ajax_referer( 'weforms' );
366
367 $this->check_admin();
368
369 $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0;
370
371 weforms_change_entry_status( $entry_id, 'trash' );
372 wp_send_json_success();
373 }
374
375 /**
376 * Bulk trash entries
377 *
378 * @return void
379 */
380 public function bulk_delete_entry() {
381 check_ajax_referer( 'weforms' );
382
383 $this->check_admin();
384
385 $entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array();
386
387 if ( ! $entry_ids ) {
388 wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) );
389 }
390
391 foreach ($entry_ids as $entry_id) {
392 weforms_change_entry_status( $entry_id, 'trash' );
393 }
394
395 wp_send_json_success();
396 }
397
398 /**
399 * Handle the frontend submission
400 *
401 * @return void
402 */
403 public function handle_frontend_submission() {
404 check_ajax_referer( 'wpuf_form_add' );
405
406 $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0;
407 $page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0;
408
409 $form_vars = WPUF_Render_Form::get_input_fields( $form_id );
410 $form_settings = wpuf_get_form_settings( $form_id );
411
412 list( $post_vars, $taxonomy_vars, $meta_vars ) = $form_vars;
413
414 if ( !$meta_vars ) {
415 wp_send_json( array(
416 'success' => false,
417 'error' => __( 'No form field was found.', 'weforms' ),
418 ) );
419 }
420
421 // process the form fields
422 $entry_fields = $this->prepare_entry_fields( $meta_vars );
423
424 $entry_id = weforms_insert_entry( array(
425 'form_id' => $form_id
426 ), $entry_fields );
427
428 if ( is_wp_error( $entry_id ) ) {
429 wp_send_json( array(
430 'success' => false,
431 'error' => $entry_id->get_error_message(),
432 ) );
433 }
434
435 // redirect URL
436 $show_message = false;
437 $redirect_to = false;
438
439 if ( $form_settings['redirect_to'] == 'page' ) {
440 $redirect_to = get_permalink( $form_settings['page_id'] );
441 } elseif ( $form_settings['redirect_to'] == 'url' ) {
442 $redirect_to = $form_settings['url'];
443 } elseif ( $form_settings['redirect_to'] == 'same' ) {
444 $show_message = true;
445 } else {
446 $redirect_to = get_permalink( $post_id );
447 }
448
449 // Fire a hook for integration
450 do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings );
451
452 // send the response
453 $response = array(
454 'success' => true,
455 'redirect_to' => $redirect_to,
456 'show_message' => $show_message,
457 'message' => $form_settings['message']
458 );
459
460 $notification = new WeForms_Notification( array(
461 'form_id' => $form_id,
462 'page_id' => $page_id,
463 'entry_id' => $entry_id
464 ) );
465
466 $notification->send_notifications();
467
468 // wpuf_clear_buffer();
469 wp_send_json( $response );
470 }
471
472 /**
473 * Prepare meta fields by it's types
474 *
475 * @param array $form_fields
476 *
477 * @return array
478 */
479 public function prepare_entry_fields( $form_fields ) {
480 $entry_fields = array();
481
482 list( $meta_key_value, $multi_repeated, $files ) = WPUF_Frontend_Form_Post::prepare_meta_fields( $form_fields );
483
484 // var_dump( $meta_key_value, $multi_repeated, $files );
485 if ( $meta_key_value ) {
486 foreach ($meta_key_value as $key => $value) {
487 $entry_fields[ $key ] = $value;
488 }
489 }
490
491 return $entry_fields;
492 }
493
494 /**
495 * Import a form from a JSON file
496 *
497 * @return void
498 */
499 public function import_form() {
500 check_ajax_referer( 'weforms' );
501
502 $this->check_admin();
503
504 $the_file = isset( $_FILES['importFile'] ) ? $_FILES['importFile'] : false;
505
506 if ( ! $the_file ) {
507 wp_send_json_error( __( 'No file found to import.', 'weforms' ) );
508 }
509
510 $file_ext = pathinfo( $the_file['name'], PATHINFO_EXTENSION );
511
512 if ( ! class_exists( 'WPUF_Admin_Tools' ) ) {
513 require_once WPUF_ROOT . '/admin/class-tools.php';
514 }
515
516 if ( ( $file_ext == 'json' ) && ( $the_file['size'] < 500000 ) ) {
517
518 $status = WPUF_Admin_Tools::import_json_file( $the_file['tmp_name'] );
519
520 if ( $status ) {
521 wp_send_json_success( __( 'The forms have been imported successfully!', 'weforms' ) );
522 } else {
523 wp_send_json_error( __( 'Something went wrong importing the file.', 'weforms' ) );
524 }
525
526 } else {
527 wp_send_json_error( __( 'Invalid file or file size too big.', 'weforms' ) );
528 }
529 }
530
531 }