| 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 |
$requires_wpuf_update = false; |
| 243 |
$wpuf_update_array = array(); |
| 244 |
$settings = (array) json_decode( wp_unslash( $_POST['settings'] ) ); |
| 245 |
|
| 246 |
update_option( 'weforms_settings', $settings ); |
| 247 |
|
| 248 |
// wpuf settings sync |
| 249 |
if ( isset( $settings['gmap_api'] ) ) { |
| 250 |
$requires_wpuf_update = true; |
| 251 |
$wpuf_update_array['gmap_api_key'] = $settings['gmap_api']; |
| 252 |
} |
| 253 |
|
| 254 |
if ( isset( $settings['recaptcha'] ) ) { |
| 255 |
$requires_wpuf_update = true; |
| 256 |
$wpuf_update_array['recaptcha_public'] = $settings['recaptcha']->key; |
| 257 |
$wpuf_update_array['recaptcha_private'] = $settings['recaptcha']->secret; |
| 258 |
} |
| 259 |
|
| 260 |
if ( $requires_wpuf_update ) { |
| 261 |
$wpuf_settings = get_option( 'wpuf_general', array() ); |
| 262 |
|
| 263 |
$wpuf_settings = array_merge( $wpuf_settings, $wpuf_update_array ); |
| 264 |
update_option( 'wpuf_general', $wpuf_settings ); |
| 265 |
} |
| 266 |
|
| 267 |
do_action( 'weforms_save_settings', $settings ); |
| 268 |
|
| 269 |
wp_send_json_success( $settings ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Get the weForms Settings |
| 274 |
* |
| 275 |
* @return void |
| 276 |
*/ |
| 277 |
public function get_settings() { |
| 278 |
check_ajax_referer( 'weforms' ); |
| 279 |
|
| 280 |
$this->check_admin(); |
| 281 |
|
| 282 |
$settings = get_option( 'weforms_settings', array() ); |
| 283 |
|
| 284 |
wp_send_json_success( $settings ); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Get all entries |
| 289 |
* |
| 290 |
* @return void |
| 291 |
*/ |
| 292 |
public function get_entries() { |
| 293 |
check_ajax_referer( 'weforms' ); |
| 294 |
|
| 295 |
$this->check_admin(); |
| 296 |
|
| 297 |
$form_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; |
| 298 |
$current_page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1; |
| 299 |
$per_page = 20; |
| 300 |
$offset = ( $current_page - 1 ) * $per_page; |
| 301 |
|
| 302 |
if ( ! $form_id ) { |
| 303 |
wp_send_json_error( __( 'No form id provided!', 'weforms' ) ); |
| 304 |
} |
| 305 |
|
| 306 |
$entries = weforms_get_form_entries( $form_id, array( |
| 307 |
'number' => $per_page, |
| 308 |
'offset' => $offset |
| 309 |
) ); |
| 310 |
|
| 311 |
$columns = weforms_get_entry_columns( $form_id ); |
| 312 |
$total_entries = weforms_count_form_entries( $form_id ); |
| 313 |
|
| 314 |
array_map( function( $entry ) use ($columns) { |
| 315 |
$entry_id = $entry->id; |
| 316 |
$entry->fields = array(); |
| 317 |
|
| 318 |
foreach ($columns as $meta_key => $label) { |
| 319 |
$value = weforms_get_entry_meta( $entry_id, $meta_key, true ); |
| 320 |
$entry->fields[$meta_key] = str_replace( WPUF_Render_Form::$separator, ' ', $value ); |
| 321 |
} |
| 322 |
}, $entries ); |
| 323 |
|
| 324 |
$response = array( |
| 325 |
'columns' => $columns, |
| 326 |
'entries' => $entries, |
| 327 |
'form_title' => get_post_field( 'post_title', $form_id ), |
| 328 |
'pagination' => array( |
| 329 |
'total' => $total_entries, |
| 330 |
'per_page' => $per_page, |
| 331 |
'pages' => ceil( $total_entries / $per_page ), |
| 332 |
'current' => $current_page |
| 333 |
) |
| 334 |
); |
| 335 |
|
| 336 |
wp_send_json_success( $response ); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Get an entry details |
| 341 |
* |
| 342 |
* @return void |
| 343 |
*/ |
| 344 |
public function get_entry_detail() { |
| 345 |
check_ajax_referer( 'weforms' ); |
| 346 |
|
| 347 |
$this->check_admin(); |
| 348 |
|
| 349 |
$entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; |
| 350 |
$entry = weforms_get_entry( $entry_id ); |
| 351 |
|
| 352 |
if ( !$entry ) { |
| 353 |
wp_send_json_error( __( 'No such entry found!', 'weforms' ) ); |
| 354 |
} |
| 355 |
|
| 356 |
$info = array( |
| 357 |
'form_title' => get_post_field( 'post_title', $entry->form_id ), |
| 358 |
'created' => date_i18n( 'F j, Y g:i a', strtotime( $entry->created_at ) ), |
| 359 |
'ip' => $entry->ip_address, |
| 360 |
'user' => $entry->user_id ? get_user_by( 'id', $entry->user_id )->display_name : false, |
| 361 |
'referer' => $entry->referer, |
| 362 |
'device' => $entry->user_device |
| 363 |
); |
| 364 |
|
| 365 |
$data = weforms_get_entry_data( $entry_id ); |
| 366 |
|
| 367 |
if ( false === $data ) { |
| 368 |
wp_send_json_error( __( 'No form fields found!', 'weforms' ) ); |
| 369 |
} |
| 370 |
|
| 371 |
$response = array( |
| 372 |
'form_fields' => $data['fields'], |
| 373 |
'meta_data' => $data['data'], |
| 374 |
'info' => $info |
| 375 |
); |
| 376 |
|
| 377 |
wp_send_json_success( $response ); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Trash an entry |
| 382 |
* |
| 383 |
* @return void |
| 384 |
*/ |
| 385 |
public function trash_entry() { |
| 386 |
check_ajax_referer( 'weforms' ); |
| 387 |
|
| 388 |
$this->check_admin(); |
| 389 |
|
| 390 |
$entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; |
| 391 |
|
| 392 |
weforms_change_entry_status( $entry_id, 'trash' ); |
| 393 |
wp_send_json_success(); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Bulk trash entries |
| 398 |
* |
| 399 |
* @return void |
| 400 |
*/ |
| 401 |
public function bulk_delete_entry() { |
| 402 |
check_ajax_referer( 'weforms' ); |
| 403 |
|
| 404 |
$this->check_admin(); |
| 405 |
|
| 406 |
$entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array(); |
| 407 |
|
| 408 |
if ( ! $entry_ids ) { |
| 409 |
wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) ); |
| 410 |
} |
| 411 |
|
| 412 |
foreach ($entry_ids as $entry_id) { |
| 413 |
weforms_change_entry_status( $entry_id, 'trash' ); |
| 414 |
} |
| 415 |
|
| 416 |
wp_send_json_success(); |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Handle the frontend submission |
| 421 |
* |
| 422 |
* @return void |
| 423 |
*/ |
| 424 |
public function handle_frontend_submission() { |
| 425 |
check_ajax_referer( 'wpuf_form_add' ); |
| 426 |
|
| 427 |
$form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; |
| 428 |
$page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0; |
| 429 |
|
| 430 |
$form_vars = WPUF_Render_Form::get_input_fields( $form_id ); |
| 431 |
$form_settings = wpuf_get_form_settings( $form_id ); |
| 432 |
|
| 433 |
list( $post_vars, $taxonomy_vars, $meta_vars ) = $form_vars; |
| 434 |
|
| 435 |
if ( !$meta_vars ) { |
| 436 |
wp_send_json( array( |
| 437 |
'success' => false, |
| 438 |
'error' => __( 'No form field was found.', 'weforms' ), |
| 439 |
) ); |
| 440 |
} |
| 441 |
|
| 442 |
// process the form fields |
| 443 |
$entry_fields = $this->prepare_entry_fields( $meta_vars ); |
| 444 |
|
| 445 |
$entry_id = weforms_insert_entry( array( |
| 446 |
'form_id' => $form_id |
| 447 |
), $entry_fields ); |
| 448 |
|
| 449 |
if ( is_wp_error( $entry_id ) ) { |
| 450 |
wp_send_json( array( |
| 451 |
'success' => false, |
| 452 |
'error' => $entry_id->get_error_message(), |
| 453 |
) ); |
| 454 |
} |
| 455 |
|
| 456 |
// redirect URL |
| 457 |
$show_message = false; |
| 458 |
$redirect_to = false; |
| 459 |
|
| 460 |
if ( $form_settings['redirect_to'] == 'page' ) { |
| 461 |
$redirect_to = get_permalink( $form_settings['page_id'] ); |
| 462 |
} elseif ( $form_settings['redirect_to'] == 'url' ) { |
| 463 |
$redirect_to = $form_settings['url']; |
| 464 |
} elseif ( $form_settings['redirect_to'] == 'same' ) { |
| 465 |
$show_message = true; |
| 466 |
} else { |
| 467 |
$redirect_to = get_permalink( $post_id ); |
| 468 |
} |
| 469 |
|
| 470 |
// Fire a hook for integration |
| 471 |
do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings ); |
| 472 |
|
| 473 |
// send the response |
| 474 |
$response = array( |
| 475 |
'success' => true, |
| 476 |
'redirect_to' => $redirect_to, |
| 477 |
'show_message' => $show_message, |
| 478 |
'message' => $form_settings['message'] |
| 479 |
); |
| 480 |
|
| 481 |
$notification = new WeForms_Notification( array( |
| 482 |
'form_id' => $form_id, |
| 483 |
'page_id' => $page_id, |
| 484 |
'entry_id' => $entry_id |
| 485 |
) ); |
| 486 |
|
| 487 |
$notification->send_notifications(); |
| 488 |
|
| 489 |
// wpuf_clear_buffer(); |
| 490 |
wp_send_json( $response ); |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* Prepare meta fields by it's types |
| 495 |
* |
| 496 |
* @param array $form_fields |
| 497 |
* |
| 498 |
* @return array |
| 499 |
*/ |
| 500 |
public function prepare_entry_fields( $form_fields ) { |
| 501 |
$entry_fields = array(); |
| 502 |
|
| 503 |
list( $meta_key_value, $multi_repeated, $files ) = WPUF_Frontend_Form_Post::prepare_meta_fields( $form_fields ); |
| 504 |
|
| 505 |
// var_dump( $meta_key_value, $multi_repeated, $files ); |
| 506 |
if ( $meta_key_value ) { |
| 507 |
foreach ($meta_key_value as $key => $value) { |
| 508 |
$entry_fields[ $key ] = $value; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
if ( $files ) { |
| 513 |
foreach ( $files as $file_input ) { |
| 514 |
$entry_fields[ $file_input['name'] ] = $file_input['value']; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
return $entry_fields; |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Import a form from a JSON file |
| 523 |
* |
| 524 |
* @return void |
| 525 |
*/ |
| 526 |
public function import_form() { |
| 527 |
check_ajax_referer( 'weforms' ); |
| 528 |
|
| 529 |
$this->check_admin(); |
| 530 |
|
| 531 |
$the_file = isset( $_FILES['importFile'] ) ? $_FILES['importFile'] : false; |
| 532 |
|
| 533 |
if ( ! $the_file ) { |
| 534 |
wp_send_json_error( __( 'No file found to import.', 'weforms' ) ); |
| 535 |
} |
| 536 |
|
| 537 |
$file_ext = pathinfo( $the_file['name'], PATHINFO_EXTENSION ); |
| 538 |
|
| 539 |
if ( ! class_exists( 'WPUF_Admin_Tools' ) ) { |
| 540 |
require_once WPUF_ROOT . '/admin/class-tools.php'; |
| 541 |
} |
| 542 |
|
| 543 |
if ( ( $file_ext == 'json' ) && ( $the_file['size'] < 500000 ) ) { |
| 544 |
|
| 545 |
$status = WPUF_Admin_Tools::import_json_file( $the_file['tmp_name'] ); |
| 546 |
|
| 547 |
if ( $status ) { |
| 548 |
wp_send_json_success( __( 'The forms have been imported successfully!', 'weforms' ) ); |
| 549 |
} else { |
| 550 |
wp_send_json_error( __( 'Something went wrong importing the file.', 'weforms' ) ); |
| 551 |
} |
| 552 |
|
| 553 |
} else { |
| 554 |
wp_send_json_error( __( 'Invalid file or file size too big.', 'weforms' ) ); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
} |