| 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 |
// create from a template |
| 19 |
add_filter( 'wp_ajax_weforms_contact_form_template', array( $this, 'create_form_from_template' ) ); |
| 20 |
|
| 21 |
// form settings |
| 22 |
add_action( 'wp_ajax_weforms_save_settings', array( $this, 'save_settings' ) ); |
| 23 |
add_action( 'wp_ajax_weforms_get_settings', array( $this, 'get_settings' ) ); |
| 24 |
|
| 25 |
// import |
| 26 |
add_action( 'wp_ajax_weforms_import_form', array( $this, 'import_form' ) ); |
| 27 |
|
| 28 |
// form editing |
| 29 |
add_action( 'wp_ajax_weforms_get_form', array( $this, 'get_form' ) ); |
| 30 |
add_action( 'wp_ajax_wpuf_form_builder_save_form', array( $this, 'save_form' ) ); |
| 31 |
|
| 32 |
// entries |
| 33 |
add_action( 'wp_ajax_weforms_form_entries', array( $this, 'get_entries' ) ); |
| 34 |
|
| 35 |
add_action( 'wp_ajax_weforms_form_entry_details', array( $this, 'get_entry_detail' ) ); |
| 36 |
add_action( 'wp_ajax_weforms_form_entry_trash', array( $this, 'trash_entry' ) ); |
| 37 |
add_action( 'wp_ajax_weforms_form_entry_trash_bulk', array( $this, 'bulk_delete_entry' ) ); |
| 38 |
|
| 39 |
// frontend requests |
| 40 |
add_action( 'wp_ajax_weforms_frontend_submit', array( $this, 'handle_frontend_submission' ) ); |
| 41 |
add_action( 'wp_ajax_nopriv_weforms_frontend_submit', array( $this, 'handle_frontend_submission' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Administrator validation |
| 46 |
* |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function check_admin() { |
| 50 |
if ( !current_user_can( weforms_form_access_capability() ) ) { |
| 51 |
wp_send_json_error( __( 'You do not have sufficient permission.', 'weforms' ) ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get a form to edit |
| 57 |
* |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function get_form() { |
| 61 |
|
| 62 |
$this->check_admin(); |
| 63 |
|
| 64 |
$form_id = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : 0; |
| 65 |
|
| 66 |
$form = weforms()->form->get( $form_id ); |
| 67 |
|
| 68 |
$data = array( |
| 69 |
'post' => $form->data, |
| 70 |
'form_fields' => $form->get_fields(), |
| 71 |
'settings' => $form->get_settings(), |
| 72 |
'notifications' => $form->get_notifications(), |
| 73 |
'integrations' => $form->get_integrations() |
| 74 |
); |
| 75 |
|
| 76 |
wp_send_json_success( $data ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Save the form |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function save_form() { |
| 85 |
parse_str( $_POST['form_data'], $form_data ); |
| 86 |
|
| 87 |
if ( ! wp_verify_nonce( $form_data['wpuf_form_builder_nonce'], 'wpuf_form_builder_save_form' ) ) { |
| 88 |
wp_send_json_error( __( 'Unauthorized operation', 'wpuf' ) ); |
| 89 |
} |
| 90 |
|
| 91 |
if ( empty( $form_data['wpuf_form_id'] ) ) { |
| 92 |
wp_send_json_error( __( 'Invalid form id', 'wpuf' ) ); |
| 93 |
} |
| 94 |
|
| 95 |
$form_fields = isset( $_POST['form_fields'] ) ? $_POST['form_fields'] : ''; |
| 96 |
$notifications = isset( $_POST['notifications'] ) ? $_POST['notifications'] : ''; |
| 97 |
$settings = array(); |
| 98 |
$integrations = array(); |
| 99 |
|
| 100 |
if ( isset( $_POST['settings'] ) ) { |
| 101 |
$settings = (array) json_decode( wp_unslash( $_POST['settings'] ) ); |
| 102 |
} else { |
| 103 |
$settings = isset( $form_data['wpuf_settings'] ) ? $form_data['wpuf_settings'] : array(); |
| 104 |
} |
| 105 |
|
| 106 |
if ( isset( $_POST['integrations'] ) ) { |
| 107 |
$integrations = (array) json_decode( wp_unslash( $_POST['integrations'] ) ); |
| 108 |
} |
| 109 |
|
| 110 |
$form_fields = wp_unslash( $form_fields ); |
| 111 |
$notifications = wp_unslash( $notifications ); |
| 112 |
|
| 113 |
$form_fields = json_decode( $form_fields, true ); |
| 114 |
$notifications = json_decode( $notifications, true ); |
| 115 |
|
| 116 |
$data = array( |
| 117 |
'form_id' => absint( $form_data['wpuf_form_id'] ), |
| 118 |
'post_title' => sanitize_text_field( $form_data['post_title'] ), |
| 119 |
'form_fields' => $form_fields, |
| 120 |
'form_settings' => $settings, |
| 121 |
'form_settings_key' => isset( $form_data['form_settings_key'] ) ? $form_data['form_settings_key'] : '', |
| 122 |
'notifications' => $notifications, |
| 123 |
'integrations' => $integrations |
| 124 |
); |
| 125 |
|
| 126 |
$form_fields = weforms()->form->save( $data ); |
| 127 |
|
| 128 |
wp_send_json_success( array( 'form_fields' => $form_fields ) ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get all contact forms |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
public function get_contact_forms() { |
| 137 |
check_ajax_referer( 'weforms' ); |
| 138 |
|
| 139 |
$this->check_admin(); |
| 140 |
|
| 141 |
$args = array( |
| 142 |
'posts_per_page' => 10, |
| 143 |
'paged' => isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1, |
| 144 |
'order' => 'DESC', |
| 145 |
'orderby' => 'post_date' |
| 146 |
); |
| 147 |
|
| 148 |
$contact_forms = weforms()->form->get_forms( $args ); |
| 149 |
|
| 150 |
array_map( function($form) { |
| 151 |
$form->entries = $form->num_form_entries(); |
| 152 |
$form->views = $form->num_form_views(); |
| 153 |
$form->payments = $form->num_form_payments(); |
| 154 |
}, $contact_forms['forms'] ); |
| 155 |
|
| 156 |
$contact_forms = apply_filters( 'weforms_ajax_get_contact_forms', $contact_forms ); |
| 157 |
|
| 158 |
wp_send_json_success( $contact_forms ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get the names of contact forms for generating dropdown |
| 163 |
* |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
public function get_contact_form_names() { |
| 167 |
check_ajax_referer( 'weforms' ); |
| 168 |
|
| 169 |
$this->check_admin(); |
| 170 |
|
| 171 |
$contact_forms = weforms()->form->all(); |
| 172 |
$response = array(); |
| 173 |
|
| 174 |
foreach ($contact_forms['forms'] as $form) { |
| 175 |
$response[] = array( |
| 176 |
'id' => $form->get_id(), |
| 177 |
'title' => $form->get_name() . ' (#' . $form->get_id() . ')' |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
wp_send_json_success( $response ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Create a form |
| 186 |
* |
| 187 |
* @return void |
| 188 |
*/ |
| 189 |
public function create_form() { |
| 190 |
check_ajax_referer( 'weforms' ); |
| 191 |
|
| 192 |
$this->check_admin(); |
| 193 |
|
| 194 |
$form_name = isset( $_POST['form_name'] ) ? sanitize_text_field( $_POST['form_name'] ) : ''; |
| 195 |
|
| 196 |
if ( empty( $form_name ) ) { |
| 197 |
wp_send_json_error( __( 'Please provide a form name', 'weforms' ) ); |
| 198 |
} |
| 199 |
|
| 200 |
$form_id = weforms()->form->create( $form_name ); |
| 201 |
|
| 202 |
if ( is_wp_error( $form_id )) { |
| 203 |
wp_send_json_error( $form_id->get_error_message() ); |
| 204 |
} |
| 205 |
|
| 206 |
wp_send_json_success( array( |
| 207 |
'form_id' => $form_id, |
| 208 |
'form_name' => $form_name |
| 209 |
) ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Delete a form |
| 214 |
* |
| 215 |
* @return void |
| 216 |
*/ |
| 217 |
public function delete_form() { |
| 218 |
check_ajax_referer( 'weforms' ); |
| 219 |
|
| 220 |
$this->check_admin(); |
| 221 |
|
| 222 |
$form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; |
| 223 |
|
| 224 |
if ( ! $form_id ) { |
| 225 |
wp_send_json_error( __( 'No form id provided!', 'weforms' ) ); |
| 226 |
} |
| 227 |
|
| 228 |
weforms()->form->delete( $form_id ); |
| 229 |
|
| 230 |
wp_send_json_success(); |
| 231 |
} |
| 232 |
|
| 233 |
public function delete_form_bulk() { |
| 234 |
check_ajax_referer( 'weforms' ); |
| 235 |
|
| 236 |
$this->check_admin(); |
| 237 |
|
| 238 |
$form_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array(); |
| 239 |
|
| 240 |
if ( ! $form_ids ) { |
| 241 |
wp_send_json_error( __( 'No form ids provided!', 'weforms' ) ); |
| 242 |
} |
| 243 |
|
| 244 |
foreach ($form_ids as $form_id) { |
| 245 |
weforms()->form->delete( $form_id ); |
| 246 |
} |
| 247 |
|
| 248 |
wp_send_json_success(); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Duplicate a form |
| 253 |
* |
| 254 |
* @return voiud |
| 255 |
*/ |
| 256 |
public function duplicate_form() { |
| 257 |
check_ajax_referer( 'weforms' ); |
| 258 |
|
| 259 |
$this->check_admin(); |
| 260 |
|
| 261 |
$form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; |
| 262 |
|
| 263 |
$form = weforms()->form->duplicate( $form_id ); |
| 264 |
|
| 265 |
wp_send_json_success( $form ); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Create form from a template |
| 270 |
* |
| 271 |
* @return void |
| 272 |
*/ |
| 273 |
public function create_form_from_template() { |
| 274 |
check_ajax_referer( 'weforms' ); |
| 275 |
|
| 276 |
$template = isset( $_REQUEST['template'] ) ? sanitize_text_field( $_REQUEST['template'] ) : ''; |
| 277 |
|
| 278 |
$form_id = weforms()->templates->create( $template ); |
| 279 |
|
| 280 |
if ( is_wp_error( $form_id ) ) { |
| 281 |
wp_send_json_error( __( 'Could not create the form', 'weforms' ) ); |
| 282 |
} |
| 283 |
|
| 284 |
wp_send_json_success( array( |
| 285 |
'id' => $form_id |
| 286 |
) ); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Save the weForms settings |
| 291 |
* |
| 292 |
* @return void |
| 293 |
*/ |
| 294 |
public function save_settings() { |
| 295 |
check_ajax_referer( 'weforms' ); |
| 296 |
|
| 297 |
$this->check_admin(); |
| 298 |
|
| 299 |
$requires_wpuf_update = false; |
| 300 |
$wpuf_update_array = array(); |
| 301 |
$settings = (array) json_decode( wp_unslash( $_POST['settings'] ) ); |
| 302 |
|
| 303 |
update_option( 'weforms_settings', $settings ); |
| 304 |
|
| 305 |
// wpuf settings sync |
| 306 |
if ( isset( $settings['gmap_api'] ) ) { |
| 307 |
$requires_wpuf_update = true; |
| 308 |
$wpuf_update_array['gmap_api_key'] = $settings['gmap_api']; |
| 309 |
} |
| 310 |
|
| 311 |
if ( isset( $settings['recaptcha'] ) ) { |
| 312 |
$requires_wpuf_update = true; |
| 313 |
$wpuf_update_array['recaptcha_public'] = $settings['recaptcha']->key; |
| 314 |
$wpuf_update_array['recaptcha_private'] = $settings['recaptcha']->secret; |
| 315 |
} |
| 316 |
|
| 317 |
if ( $requires_wpuf_update ) { |
| 318 |
$wpuf_settings = get_option( 'wpuf_general', array() ); |
| 319 |
|
| 320 |
$wpuf_settings = array_merge( $wpuf_settings, $wpuf_update_array ); |
| 321 |
update_option( 'wpuf_general', $wpuf_settings ); |
| 322 |
} |
| 323 |
|
| 324 |
do_action( 'weforms_save_settings', $settings ); |
| 325 |
|
| 326 |
wp_send_json_success( $settings ); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Get the weForms Settings |
| 331 |
* |
| 332 |
* @return void |
| 333 |
*/ |
| 334 |
public function get_settings() { |
| 335 |
check_ajax_referer( 'weforms' ); |
| 336 |
|
| 337 |
$this->check_admin(); |
| 338 |
|
| 339 |
$settings = weforms_get_settings(); |
| 340 |
|
| 341 |
// checking to prevent js error, will be removed in future |
| 342 |
if ( ! isset( $settings['credit'] ) ) { |
| 343 |
$settings['credit'] = false; |
| 344 |
} |
| 345 |
|
| 346 |
if ( ! isset( $settings['permission'] ) ) { |
| 347 |
$settings['permission'] = 'manage_options'; |
| 348 |
} |
| 349 |
|
| 350 |
wp_send_json_success( $settings ); |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Get all entries |
| 355 |
* |
| 356 |
* @return void |
| 357 |
*/ |
| 358 |
public function get_entries() { |
| 359 |
check_ajax_referer( 'weforms' ); |
| 360 |
|
| 361 |
$this->check_admin(); |
| 362 |
|
| 363 |
$form_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; |
| 364 |
$current_page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1; |
| 365 |
$per_page = 20; |
| 366 |
$offset = ( $current_page - 1 ) * $per_page; |
| 367 |
|
| 368 |
if ( ! $form_id ) { |
| 369 |
wp_send_json_error( __( 'No form id provided!', 'weforms' ) ); |
| 370 |
} |
| 371 |
|
| 372 |
$entries = weforms_get_form_entries( $form_id, array( |
| 373 |
'number' => $per_page, |
| 374 |
'offset' => $offset |
| 375 |
) ); |
| 376 |
|
| 377 |
$columns = weforms_get_entry_columns( $form_id ); |
| 378 |
$total_entries = weforms_count_form_entries( $form_id ); |
| 379 |
|
| 380 |
array_map( function( $entry ) use ($columns) { |
| 381 |
$entry_id = $entry->id; |
| 382 |
$entry->fields = array(); |
| 383 |
|
| 384 |
foreach ($columns as $meta_key => $label) { |
| 385 |
$value = weforms_get_entry_meta( $entry_id, $meta_key, true ); |
| 386 |
$entry->fields[$meta_key] = str_replace( WeForms::$field_separator, ' ', $value ); |
| 387 |
} |
| 388 |
}, $entries ); |
| 389 |
|
| 390 |
$entries = apply_filters('weforms_get_entries', $entries, $form_id ); |
| 391 |
|
| 392 |
$response = array( |
| 393 |
'columns' => $columns, |
| 394 |
'entries' => $entries, |
| 395 |
'form_title' => get_post_field( 'post_title', $form_id ), |
| 396 |
'pagination' => array( |
| 397 |
'total' => $total_entries, |
| 398 |
'per_page' => $per_page, |
| 399 |
'pages' => ceil( $total_entries / $per_page ), |
| 400 |
'current' => $current_page |
| 401 |
) |
| 402 |
); |
| 403 |
|
| 404 |
wp_send_json_success( $response ); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
/** |
| 410 |
* Get an entry details |
| 411 |
* |
| 412 |
* @return void |
| 413 |
*/ |
| 414 |
public function get_entry_detail() { |
| 415 |
check_ajax_referer( 'weforms' ); |
| 416 |
|
| 417 |
$this->check_admin(); |
| 418 |
|
| 419 |
$form_id = isset( $_REQUEST['form_id'] ) ? intval( $_REQUEST['form_id'] ) : 0; |
| 420 |
$entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; |
| 421 |
|
| 422 |
$form = weforms()->form->get( $form_id ); |
| 423 |
$entry = $form->entries()->get( $entry_id ); |
| 424 |
$fields = $entry->get_fields(); |
| 425 |
$metadata = $entry->get_metadata(); |
| 426 |
$payment = $entry->get_payment_data(); |
| 427 |
|
| 428 |
if ( isset($payment->payment_data) && is_serialized( $payment->payment_data ) ) { |
| 429 |
$payment->payment_data = unserialize( $payment->payment_data ); |
| 430 |
} |
| 431 |
|
| 432 |
if ( false === $fields ) { |
| 433 |
wp_send_json_error( __( 'No form fields found!', 'weforms' ) ); |
| 434 |
} |
| 435 |
|
| 436 |
$response = array( |
| 437 |
'form_fields' => $fields, |
| 438 |
'meta_data' => $metadata, |
| 439 |
'payment_data' => $payment, |
| 440 |
); |
| 441 |
|
| 442 |
wp_send_json_success( $response ); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Trash an entry |
| 447 |
* |
| 448 |
* @return void |
| 449 |
*/ |
| 450 |
public function trash_entry() { |
| 451 |
check_ajax_referer( 'weforms' ); |
| 452 |
|
| 453 |
$this->check_admin(); |
| 454 |
|
| 455 |
$entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; |
| 456 |
|
| 457 |
weforms_change_entry_status( $entry_id, 'trash' ); |
| 458 |
wp_send_json_success(); |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Bulk trash entries |
| 463 |
* |
| 464 |
* @return void |
| 465 |
*/ |
| 466 |
public function bulk_delete_entry() { |
| 467 |
check_ajax_referer( 'weforms' ); |
| 468 |
|
| 469 |
$this->check_admin(); |
| 470 |
|
| 471 |
$entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array(); |
| 472 |
|
| 473 |
if ( ! $entry_ids ) { |
| 474 |
wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) ); |
| 475 |
} |
| 476 |
|
| 477 |
foreach ($entry_ids as $entry_id) { |
| 478 |
weforms_change_entry_status( $entry_id, 'trash' ); |
| 479 |
} |
| 480 |
|
| 481 |
wp_send_json_success(); |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* Handle the frontend submission |
| 486 |
* |
| 487 |
* @return void |
| 488 |
*/ |
| 489 |
public function handle_frontend_submission() { |
| 490 |
check_ajax_referer( 'wpuf_form_add' ); |
| 491 |
|
| 492 |
$form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; |
| 493 |
$page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0; |
| 494 |
|
| 495 |
$form = weforms()->form->get( $form_id ); |
| 496 |
$form_settings = $form->get_settings(); |
| 497 |
$form_fields = $form->get_fields(); |
| 498 |
$entry_fields = $form->prepare_entries(); |
| 499 |
$form_entries = weforms_get_form_entries( $form_id, array( 'number' => '', 'offset' => '' ) ); |
| 500 |
|
| 501 |
if ( $form_fields && count( $form_entries ) && count( $entry_fields ) ) { |
| 502 |
|
| 503 |
foreach ( $entry_fields as $field_key => $field_value ) { |
| 504 |
$duplicate_check = false; |
| 505 |
$field_label = 'This'; |
| 506 |
foreach ( $form_fields as $form_field ) { |
| 507 |
if ( in_array( $form_field['template'], array( 'text_field', 'website_url', 'numeric_text_field', 'email_address' ) ) && $form_field['name'] == $field_key && isset( $form_field['duplicate'] ) && 'no' == $form_field['duplicate'] ) { |
| 508 |
$duplicate_check = true; |
| 509 |
$field_label = $form_field['label']; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
if ( $duplicate_check ) { |
| 514 |
foreach ( $form_entries as $entry ) { |
| 515 |
$existing = weforms_get_entry_meta( $entry->id, $field_key, true ); |
| 516 |
if ( $existing && $field_value == $existing ) { |
| 517 |
wp_send_json( array( |
| 518 |
'success' => false, |
| 519 |
'error' => sprintf( __( '"%s" field requires a unique entry and "%s" has already been used.', 'weforms' ), $field_label, $field_value ) |
| 520 |
) ); |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
if ( !$form_fields ) { |
| 528 |
wp_send_json( array( |
| 529 |
'success' => false, |
| 530 |
'error' => __( 'No form field was found.', 'weforms' ), |
| 531 |
) ); |
| 532 |
} |
| 533 |
|
| 534 |
if ( $form->has_field( 'recaptcha' ) ) { |
| 535 |
$this->validate_reCaptcha(); |
| 536 |
} |
| 537 |
|
| 538 |
// vaidate submission |
| 539 |
$this->validate_submission( $entry_fields, $form, $form_settings, $form_fields ); |
| 540 |
|
| 541 |
$entry_fields = apply_filters( 'weforms_before_entry_submission', $entry_fields, $form, $form_settings, $form_fields); |
| 542 |
|
| 543 |
$entry_id = weforms_insert_entry( array( |
| 544 |
'form_id' => $form_id |
| 545 |
), $entry_fields ); |
| 546 |
|
| 547 |
if ( is_wp_error( $entry_id ) ) { |
| 548 |
wp_send_json( array( |
| 549 |
'success' => false, |
| 550 |
'error' => $entry_id->get_error_message(), |
| 551 |
) ); |
| 552 |
} |
| 553 |
|
| 554 |
// redirect URL |
| 555 |
$show_message = false; |
| 556 |
$redirect_to = false; |
| 557 |
|
| 558 |
if ( $form_settings['redirect_to'] == 'page' ) { |
| 559 |
$redirect_to = get_permalink( $form_settings['page_id'] ); |
| 560 |
} elseif ( $form_settings['redirect_to'] == 'url' ) { |
| 561 |
$redirect_to = $form_settings['url']; |
| 562 |
} elseif ( $form_settings['redirect_to'] == 'same' ) { |
| 563 |
$show_message = true; |
| 564 |
} else { |
| 565 |
$redirect_to = get_permalink( $post_id ); |
| 566 |
} |
| 567 |
|
| 568 |
// Fire a hook for integration |
| 569 |
do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings ); |
| 570 |
|
| 571 |
// send the response |
| 572 |
$response = apply_filters( 'weforms_entry_submission_response', array( |
| 573 |
'success' => true, |
| 574 |
'redirect_to' => $redirect_to, |
| 575 |
'show_message' => $show_message, |
| 576 |
'message' => $form_settings['message'], |
| 577 |
'data' => $_POST, |
| 578 |
'form_id' => $form_id, |
| 579 |
'entry_id' => $entry_id, |
| 580 |
) ); |
| 581 |
|
| 582 |
$notification = new WeForms_Notification( array( |
| 583 |
'form_id' => $form_id, |
| 584 |
'page_id' => $page_id, |
| 585 |
'entry_id' => $entry_id |
| 586 |
) ); |
| 587 |
|
| 588 |
$notification->send_notifications(); |
| 589 |
|
| 590 |
weforms_clear_buffer(); |
| 591 |
wp_send_json( $response ); |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* reCaptcha Validation |
| 596 |
* |
| 597 |
* @return void |
| 598 |
*/ |
| 599 |
function validate_reCaptcha() { |
| 600 |
|
| 601 |
// add reCaptcha library if not found |
| 602 |
if ( !function_exists( 'recaptcha_get_html' ) ) { |
| 603 |
require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib.php'; |
| 604 |
require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib_noCaptcha.php'; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
$invisible = isset( $_POST["g-recaptcha-response"] ) ? false : true; |
| 609 |
|
| 610 |
$recaptcha_settings = weforms_get_settings( 'recaptcha' ); |
| 611 |
$secret = isset( $recaptcha_settings->secret ) ? $recaptcha_settings->secret : ''; |
| 612 |
|
| 613 |
if ( ! $invisible ) { |
| 614 |
|
| 615 |
$response = null; |
| 616 |
$reCaptcha = new ReCaptcha($secret); |
| 617 |
|
| 618 |
$resp = $reCaptcha->verifyResponse( |
| 619 |
$_SERVER["REMOTE_ADDR"], |
| 620 |
$_POST["g-recaptcha-response"] |
| 621 |
); |
| 622 |
|
| 623 |
if ( !$resp->success ) { |
| 624 |
wp_send_json( array( |
| 625 |
'success' => false, |
| 626 |
'error' => __( 'reCAPTCHA validation failed', 'wpuf' ), |
| 627 |
) ); |
| 628 |
} |
| 629 |
|
| 630 |
} else { |
| 631 |
|
| 632 |
$recap_challenge = isset( $_POST['recaptcha_challenge_field'] ) ? $_POST['recaptcha_challenge_field'] : ''; |
| 633 |
$recap_response = isset( $_POST['recaptcha_response_field'] ) ? $_POST['recaptcha_response_field'] : ''; |
| 634 |
|
| 635 |
$resp = recaptcha_check_answer( $secret, $_SERVER["REMOTE_ADDR"], $recap_challenge, $recap_response ); |
| 636 |
|
| 637 |
if ( !$resp->is_valid ) { |
| 638 |
|
| 639 |
ob_clean(); |
| 640 |
|
| 641 |
wp_send_json( array( |
| 642 |
'success' => false, |
| 643 |
'error' => __( 'reCAPTCHA validation failed', 'wpuf' ), |
| 644 |
) ); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
} |
| 649 |
|
| 650 |
/** |
| 651 |
* Validate submission |
| 652 |
* |
| 653 |
* @param array $entry_fields |
| 654 |
* @param object $form |
| 655 |
* @param array $form_settings |
| 656 |
* @param array $form_fields |
| 657 |
* |
| 658 |
* @return bool|json |
| 659 |
*/ |
| 660 |
function validate_submission( $entry_fields, $form, $form_settings, $form_fields ) { |
| 661 |
|
| 662 |
foreach ( $form_fields as $key => $field ) { |
| 663 |
|
| 664 |
$value = $entry_fields[$field['name']]; |
| 665 |
|
| 666 |
// if ( isset( $field['required'] ) && $field['required'] && empty( $value ) ) { |
| 667 |
|
| 668 |
// wp_send_json( array( |
| 669 |
// 'success' => false, |
| 670 |
// 'error' => __( sprintf( '%s field is required', $field['label'] ), 'weforms' ), |
| 671 |
// ) ); |
| 672 |
// } |
| 673 |
|
| 674 |
if ( 'single_product' === $field['template'] ) { |
| 675 |
|
| 676 |
if ( ! $value ) { |
| 677 |
$value = array(); |
| 678 |
} |
| 679 |
|
| 680 |
$value['price'] = isset( $value['price'] ) ? floatval( $value['price'] ) : 0; |
| 681 |
$value['quantity'] = isset( $value['quantity'] ) ? floatval( $value['quantity'] ) : 0; |
| 682 |
$quantity = isset( $field['quantity'] ) ? $field['quantity'] : array(); |
| 683 |
$price = isset( $field['price'] ) ? $field['price'] : array(); |
| 684 |
|
| 685 |
|
| 686 |
if ( isset($price['is_flexible']) && $price['is_flexible'] ) { |
| 687 |
|
| 688 |
$min = isset( $price['min'] ) ? floatval( $price['min'] ) : 0; |
| 689 |
$max = isset( $price['max'] ) ? floatval( $price['max'] ) : 0; |
| 690 |
|
| 691 |
if ( $value['price'] < $min ) { |
| 692 |
|
| 693 |
wp_send_json( array( |
| 694 |
'success' => false, |
| 695 |
'error' => __( sprintf( |
| 696 |
'%s price must be equal or greater than %s', |
| 697 |
$field['label'], |
| 698 |
$min), |
| 699 |
'weforms' ), |
| 700 |
) ); |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
if ( $max && $value['price'] > $max ) { |
| 705 |
|
| 706 |
wp_send_json( array( |
| 707 |
'success' => false, |
| 708 |
'error' => __( sprintf( |
| 709 |
'%s price must be equal or less than %s', |
| 710 |
$field['label'], |
| 711 |
$max), |
| 712 |
'weforms' ), |
| 713 |
) ); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
if ( isset($quantity['status']) && $quantity['status'] ) { |
| 718 |
|
| 719 |
$min = isset( $quantity['min'] ) ? floatval( $quantity['min'] ) : 0; |
| 720 |
$max = isset( $quantity['max'] ) ? floatval( $quantity['max'] ) : 0; |
| 721 |
|
| 722 |
if ( $value['quantity'] < $min ) { |
| 723 |
|
| 724 |
wp_send_json( array( |
| 725 |
'success' => false, |
| 726 |
'error' => __( sprintf( |
| 727 |
'%s quantity must be equal or greater than %s', |
| 728 |
$field['label'], |
| 729 |
$min), |
| 730 |
'weforms' ), |
| 731 |
) ); |
| 732 |
} |
| 733 |
|
| 734 |
if ( $max && $value['quantity'] > $max ) { |
| 735 |
|
| 736 |
wp_send_json( array( |
| 737 |
'success' => false, |
| 738 |
'error' => __( sprintf( |
| 739 |
'%s quantity must be equal or less than %s', |
| 740 |
$field['label'], |
| 741 |
$max), |
| 742 |
'weforms' ), |
| 743 |
) ); |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
} |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
public static function prepare_meta_fields( $meta_vars ) { |
| 752 |
// loop through custom fields |
| 753 |
// skip files, put in a key => value paired array for later executation |
| 754 |
// process repeatable fields separately |
| 755 |
// if the input is array type, implode with separator in a field |
| 756 |
|
| 757 |
$files = array(); |
| 758 |
$meta_key_value = array(); |
| 759 |
$multi_repeated = array(); //multi repeated fields will in sotre duplicated meta key |
| 760 |
|
| 761 |
foreach ($meta_vars as $key => $value) { |
| 762 |
|
| 763 |
switch ( $value['template'] ) { |
| 764 |
|
| 765 |
// put files in a separate array, we'll process it later |
| 766 |
case 'file_upload': |
| 767 |
case 'image_upload': |
| 768 |
|
| 769 |
$files[] = array( |
| 770 |
'name' => $value['name'], |
| 771 |
'value' => isset( $_POST['wpuf_files'][$value['name']] ) ? $_POST['wpuf_files'][$value['name']] : array(), |
| 772 |
'count' => $value['count'] |
| 773 |
); |
| 774 |
break; |
| 775 |
|
| 776 |
case 'repeat_field': |
| 777 |
|
| 778 |
// if it is a multi column repeat field |
| 779 |
if ( isset( $value['multiple'] ) && $value['multiple'] == 'true' ) { |
| 780 |
|
| 781 |
// if there's any items in the array, process it |
| 782 |
if ( $_POST[$value['name']] ) { |
| 783 |
|
| 784 |
$ref_arr = array(); |
| 785 |
$cols = count( $value['columns'] ); |
| 786 |
$first = array_shift( array_values( $_POST[$value['name']] ) ); //first element |
| 787 |
$rows = count( $first ); |
| 788 |
|
| 789 |
// loop through columns |
| 790 |
for ($i = 0; $i < $rows; $i++) { |
| 791 |
|
| 792 |
// loop through the rows and store in a temp array |
| 793 |
$temp = array(); |
| 794 |
for ($j = 0; $j < $cols; $j++) { |
| 795 |
|
| 796 |
$temp[] = $_POST[$value['name']][$j][$i]; |
| 797 |
} |
| 798 |
|
| 799 |
// store all fields in a row with WeForms::$field_separator separated |
| 800 |
$ref_arr[] = implode( WeForms::$field_separator, $temp ); |
| 801 |
} |
| 802 |
|
| 803 |
// now, if we found anything in $ref_arr, store to $multi_repeated |
| 804 |
if ( $ref_arr ) { |
| 805 |
$multi_repeated[$value['name']] = array_slice( $ref_arr, 0, $rows ); |
| 806 |
} |
| 807 |
} |
| 808 |
} else { |
| 809 |
$meta_key_value[$value['name']] = implode( WeForms::$field_separator, $_POST[$value['name']] ); |
| 810 |
} |
| 811 |
|
| 812 |
break; |
| 813 |
|
| 814 |
case 'address_field': |
| 815 |
|
| 816 |
if ( isset( $_POST[ $value['name'] ] ) && is_array( $_POST[ $value['name'] ] ) ) { |
| 817 |
foreach ( $_POST[ $value['name'] ] as $address_field => $field_value ) { |
| 818 |
$meta_key_value[ $value['name'] ][ $address_field ] = sanitize_text_field( $field_value ); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
break; |
| 823 |
|
| 824 |
case 'text_field': |
| 825 |
case 'email_address': |
| 826 |
case 'numeric_text_field': |
| 827 |
case 'date_field': |
| 828 |
|
| 829 |
$meta_key_value[$value['name']] = sanitize_text_field( trim( $_POST[$value['name']] ) ); |
| 830 |
|
| 831 |
break; |
| 832 |
|
| 833 |
case 'textarea_field': |
| 834 |
|
| 835 |
$meta_key_value[$value['name']] = wp_kses_post( $_POST[$value['name']] ); |
| 836 |
|
| 837 |
break; |
| 838 |
|
| 839 |
case 'dropdown_field': |
| 840 |
case 'radio_field': |
| 841 |
|
| 842 |
$val = $_POST[$value['name']]; |
| 843 |
$meta_key_value[$value['name']] = isset( $value['options'][$val] ) ? $value['options'][$val] : ''; |
| 844 |
break; |
| 845 |
|
| 846 |
case 'multiple_select': |
| 847 |
case 'checkbox_field': |
| 848 |
|
| 849 |
$val = ( is_array( $_POST[$value['name']] ) && $_POST[$value['name']] ) ? $_POST[$value['name']] : array(); |
| 850 |
$meta_key_value[$value['name']] = $val; |
| 851 |
|
| 852 |
if ( $val ) { |
| 853 |
$new_val = array(); |
| 854 |
|
| 855 |
foreach ($val as $option_key) { |
| 856 |
$new_val[] = isset( $value['options'][$option_key] ) ? $value['options'][$option_key] : ''; |
| 857 |
} |
| 858 |
|
| 859 |
$meta_key_value[$value['name']] = implode( WeForms::$field_separator, $new_val ); |
| 860 |
} |
| 861 |
break; |
| 862 |
|
| 863 |
default: |
| 864 |
// if it's an array, implode with this->separator |
| 865 |
if ( is_array( $_POST[$value['name']] ) ) { |
| 866 |
|
| 867 |
$meta_key_value[$value['name']] = implode( WeForms::$field_separator, $_POST[$value['name']] ); |
| 868 |
|
| 869 |
} else { |
| 870 |
$meta_key_value[$value['name']] = trim( $_POST[$value['name']] ); |
| 871 |
} |
| 872 |
|
| 873 |
break; |
| 874 |
} |
| 875 |
|
| 876 |
} //end foreach |
| 877 |
|
| 878 |
return array($meta_key_value, $multi_repeated, $files); |
| 879 |
} |
| 880 |
|
| 881 |
/** |
| 882 |
* Import a form from a JSON file |
| 883 |
* |
| 884 |
* @return void |
| 885 |
*/ |
| 886 |
public function import_form() { |
| 887 |
check_ajax_referer( 'weforms' ); |
| 888 |
|
| 889 |
$this->check_admin(); |
| 890 |
|
| 891 |
$the_file = isset( $_FILES['importFile'] ) ? $_FILES['importFile'] : false; |
| 892 |
|
| 893 |
if ( ! $the_file ) { |
| 894 |
wp_send_json_error( __( 'No file found to import.', 'weforms' ) ); |
| 895 |
} |
| 896 |
|
| 897 |
$file_ext = pathinfo( $the_file['name'], PATHINFO_EXTENSION ); |
| 898 |
|
| 899 |
if ( ! class_exists( 'WeForms_Admin_Tools' ) ) { |
| 900 |
require_once dirname( __FILE__ ) . '/admin/class-admin-tools.php'; |
| 901 |
} |
| 902 |
|
| 903 |
if ( ( $file_ext == 'json' ) && ( $the_file['size'] < 500000 ) ) { |
| 904 |
|
| 905 |
$status = WeForms_Admin_Tools::import_json_file( $the_file['tmp_name'] ); |
| 906 |
|
| 907 |
if ( $status ) { |
| 908 |
wp_send_json_success( __( 'The forms have been imported successfully!', 'weforms' ) ); |
| 909 |
} else { |
| 910 |
wp_send_json_error( __( 'Something went wrong importing the file.', 'weforms' ) ); |
| 911 |
} |
| 912 |
|
| 913 |
} else { |
| 914 |
wp_send_json_error( __( 'Invalid file or file size too big.', 'weforms' ) ); |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
} |