| 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 |
// checking to prevent js error, will be removed in future |
| 285 |
if ( ! isset( $settings['credit'] ) ) { |
| 286 |
$settings['credit'] = false; |
| 287 |
} |
| 288 |
|
| 289 |
wp_send_json_success( $settings ); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Get all entries |
| 294 |
* |
| 295 |
* @return void |
| 296 |
*/ |
| 297 |
public function get_entries() { |
| 298 |
check_ajax_referer( 'weforms' ); |
| 299 |
|
| 300 |
$this->check_admin(); |
| 301 |
|
| 302 |
$form_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; |
| 303 |
$current_page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1; |
| 304 |
$per_page = 20; |
| 305 |
$offset = ( $current_page - 1 ) * $per_page; |
| 306 |
|
| 307 |
if ( ! $form_id ) { |
| 308 |
wp_send_json_error( __( 'No form id provided!', 'weforms' ) ); |
| 309 |
} |
| 310 |
|
| 311 |
$entries = weforms_get_form_entries( $form_id, array( |
| 312 |
'number' => $per_page, |
| 313 |
'offset' => $offset |
| 314 |
) ); |
| 315 |
|
| 316 |
$columns = weforms_get_entry_columns( $form_id ); |
| 317 |
$total_entries = weforms_count_form_entries( $form_id ); |
| 318 |
|
| 319 |
array_map( function( $entry ) use ($columns) { |
| 320 |
$entry_id = $entry->id; |
| 321 |
$entry->fields = array(); |
| 322 |
|
| 323 |
foreach ($columns as $meta_key => $label) { |
| 324 |
$value = weforms_get_entry_meta( $entry_id, $meta_key, true ); |
| 325 |
$entry->fields[$meta_key] = str_replace( WPUF_Render_Form::$separator, ' ', $value ); |
| 326 |
} |
| 327 |
}, $entries ); |
| 328 |
|
| 329 |
$response = array( |
| 330 |
'columns' => $columns, |
| 331 |
'entries' => $entries, |
| 332 |
'form_title' => get_post_field( 'post_title', $form_id ), |
| 333 |
'pagination' => array( |
| 334 |
'total' => $total_entries, |
| 335 |
'per_page' => $per_page, |
| 336 |
'pages' => ceil( $total_entries / $per_page ), |
| 337 |
'current' => $current_page |
| 338 |
) |
| 339 |
); |
| 340 |
|
| 341 |
wp_send_json_success( $response ); |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Get an entry details |
| 346 |
* |
| 347 |
* @return void |
| 348 |
*/ |
| 349 |
public function get_entry_detail() { |
| 350 |
check_ajax_referer( 'weforms' ); |
| 351 |
|
| 352 |
$this->check_admin(); |
| 353 |
|
| 354 |
$entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; |
| 355 |
$entry = weforms_get_entry( $entry_id ); |
| 356 |
|
| 357 |
if ( !$entry ) { |
| 358 |
wp_send_json_error( __( 'No such entry found!', 'weforms' ) ); |
| 359 |
} |
| 360 |
|
| 361 |
$info = array( |
| 362 |
'form_title' => get_post_field( 'post_title', $entry->form_id ), |
| 363 |
'created' => date_i18n( 'F j, Y g:i a', strtotime( $entry->created_at ) ), |
| 364 |
'ip' => $entry->ip_address, |
| 365 |
'user' => $entry->user_id ? get_user_by( 'id', $entry->user_id )->display_name : false, |
| 366 |
'referer' => $entry->referer, |
| 367 |
'device' => $entry->user_device |
| 368 |
); |
| 369 |
|
| 370 |
$data = weforms_get_entry_data( $entry_id ); |
| 371 |
|
| 372 |
if ( false === $data ) { |
| 373 |
wp_send_json_error( __( 'No form fields found!', 'weforms' ) ); |
| 374 |
} |
| 375 |
|
| 376 |
$response = array( |
| 377 |
'form_fields' => $data['fields'], |
| 378 |
'meta_data' => $data['data'], |
| 379 |
'info' => $info |
| 380 |
); |
| 381 |
|
| 382 |
wp_send_json_success( $response ); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Trash an entry |
| 387 |
* |
| 388 |
* @return void |
| 389 |
*/ |
| 390 |
public function trash_entry() { |
| 391 |
check_ajax_referer( 'weforms' ); |
| 392 |
|
| 393 |
$this->check_admin(); |
| 394 |
|
| 395 |
$entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; |
| 396 |
|
| 397 |
weforms_change_entry_status( $entry_id, 'trash' ); |
| 398 |
wp_send_json_success(); |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Bulk trash entries |
| 403 |
* |
| 404 |
* @return void |
| 405 |
*/ |
| 406 |
public function bulk_delete_entry() { |
| 407 |
check_ajax_referer( 'weforms' ); |
| 408 |
|
| 409 |
$this->check_admin(); |
| 410 |
|
| 411 |
$entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array(); |
| 412 |
|
| 413 |
if ( ! $entry_ids ) { |
| 414 |
wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) ); |
| 415 |
} |
| 416 |
|
| 417 |
foreach ($entry_ids as $entry_id) { |
| 418 |
weforms_change_entry_status( $entry_id, 'trash' ); |
| 419 |
} |
| 420 |
|
| 421 |
wp_send_json_success(); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Handle the frontend submission |
| 426 |
* |
| 427 |
* @return void |
| 428 |
*/ |
| 429 |
public function handle_frontend_submission() { |
| 430 |
check_ajax_referer( 'wpuf_form_add' ); |
| 431 |
|
| 432 |
$form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; |
| 433 |
$page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0; |
| 434 |
|
| 435 |
$form_vars = WPUF_Render_Form::get_input_fields( $form_id ); |
| 436 |
$form_settings = wpuf_get_form_settings( $form_id ); |
| 437 |
|
| 438 |
list( $post_vars, $taxonomy_vars, $meta_vars ) = $form_vars; |
| 439 |
|
| 440 |
if ( !$meta_vars ) { |
| 441 |
wp_send_json( array( |
| 442 |
'success' => false, |
| 443 |
'error' => __( 'No form field was found.', 'weforms' ), |
| 444 |
) ); |
| 445 |
} |
| 446 |
|
| 447 |
// process the form fields |
| 448 |
$entry_fields = $this->prepare_entry_fields( $meta_vars ); |
| 449 |
|
| 450 |
$entry_id = weforms_insert_entry( array( |
| 451 |
'form_id' => $form_id |
| 452 |
), $entry_fields ); |
| 453 |
|
| 454 |
if ( is_wp_error( $entry_id ) ) { |
| 455 |
wp_send_json( array( |
| 456 |
'success' => false, |
| 457 |
'error' => $entry_id->get_error_message(), |
| 458 |
) ); |
| 459 |
} |
| 460 |
|
| 461 |
// redirect URL |
| 462 |
$show_message = false; |
| 463 |
$redirect_to = false; |
| 464 |
|
| 465 |
if ( $form_settings['redirect_to'] == 'page' ) { |
| 466 |
$redirect_to = get_permalink( $form_settings['page_id'] ); |
| 467 |
} elseif ( $form_settings['redirect_to'] == 'url' ) { |
| 468 |
$redirect_to = $form_settings['url']; |
| 469 |
} elseif ( $form_settings['redirect_to'] == 'same' ) { |
| 470 |
$show_message = true; |
| 471 |
} else { |
| 472 |
$redirect_to = get_permalink( $post_id ); |
| 473 |
} |
| 474 |
|
| 475 |
// Fire a hook for integration |
| 476 |
do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings ); |
| 477 |
|
| 478 |
// send the response |
| 479 |
$response = array( |
| 480 |
'success' => true, |
| 481 |
'redirect_to' => $redirect_to, |
| 482 |
'show_message' => $show_message, |
| 483 |
'message' => $form_settings['message'] |
| 484 |
); |
| 485 |
|
| 486 |
$notification = new WeForms_Notification( array( |
| 487 |
'form_id' => $form_id, |
| 488 |
'page_id' => $page_id, |
| 489 |
'entry_id' => $entry_id |
| 490 |
) ); |
| 491 |
|
| 492 |
$notification->send_notifications(); |
| 493 |
|
| 494 |
wpuf_clear_buffer(); |
| 495 |
wp_send_json( $response ); |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Prepare meta fields by it's types |
| 500 |
* |
| 501 |
* @param array $form_fields |
| 502 |
* |
| 503 |
* @return array |
| 504 |
*/ |
| 505 |
public function prepare_entry_fields( $form_fields ) { |
| 506 |
$entry_fields = array(); |
| 507 |
|
| 508 |
list( $meta_key_value, $multi_repeated, $files ) = self::prepare_meta_fields( $form_fields ); |
| 509 |
|
| 510 |
// var_dump( $meta_key_value, $multi_repeated, $files ); |
| 511 |
if ( $meta_key_value ) { |
| 512 |
foreach ($meta_key_value as $key => $value) { |
| 513 |
$entry_fields[ $key ] = $value; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
if ( $files ) { |
| 518 |
foreach ( $files as $file_input ) { |
| 519 |
$entry_fields[ $file_input['name'] ] = $file_input['value']; |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
return $entry_fields; |
| 524 |
} |
| 525 |
|
| 526 |
public static function prepare_meta_fields( $meta_vars ) { |
| 527 |
// loop through custom fields |
| 528 |
// skip files, put in a key => value paired array for later executation |
| 529 |
// process repeatable fields separately |
| 530 |
// if the input is array type, implode with separator in a field |
| 531 |
|
| 532 |
$files = array(); |
| 533 |
$meta_key_value = array(); |
| 534 |
$multi_repeated = array(); //multi repeated fields will in sotre duplicated meta key |
| 535 |
|
| 536 |
foreach ($meta_vars as $key => $value) { |
| 537 |
|
| 538 |
switch ( $value['input_type'] ) { |
| 539 |
|
| 540 |
// put files in a separate array, we'll process it later |
| 541 |
case 'file_upload': |
| 542 |
case 'image_upload': |
| 543 |
|
| 544 |
$files[] = array( |
| 545 |
'name' => $value['name'], |
| 546 |
'value' => isset( $_POST['wpuf_files'][$value['name']] ) ? $_POST['wpuf_files'][$value['name']] : array(), |
| 547 |
'count' => $value['count'] |
| 548 |
); |
| 549 |
break; |
| 550 |
|
| 551 |
case 'repeat': |
| 552 |
|
| 553 |
// if it is a multi column repeat field |
| 554 |
if ( isset( $value['multiple'] ) && $value['multiple'] == 'true' ) { |
| 555 |
|
| 556 |
// if there's any items in the array, process it |
| 557 |
if ( $_POST[$value['name']] ) { |
| 558 |
|
| 559 |
$ref_arr = array(); |
| 560 |
$cols = count( $value['columns'] ); |
| 561 |
$first = array_shift( array_values( $_POST[$value['name']] ) ); //first element |
| 562 |
$rows = count( $first ); |
| 563 |
|
| 564 |
// loop through columns |
| 565 |
for ($i = 0; $i < $rows; $i++) { |
| 566 |
|
| 567 |
// loop through the rows and store in a temp array |
| 568 |
$temp = array(); |
| 569 |
for ($j = 0; $j < $cols; $j++) { |
| 570 |
|
| 571 |
$temp[] = $_POST[$value['name']][$j][$i]; |
| 572 |
} |
| 573 |
|
| 574 |
// store all fields in a row with self::$separator separated |
| 575 |
$ref_arr[] = implode( self::$separator, $temp ); |
| 576 |
} |
| 577 |
|
| 578 |
// now, if we found anything in $ref_arr, store to $multi_repeated |
| 579 |
if ( $ref_arr ) { |
| 580 |
$multi_repeated[$value['name']] = array_slice( $ref_arr, 0, $rows ); |
| 581 |
} |
| 582 |
} |
| 583 |
} else { |
| 584 |
$meta_key_value[$value['name']] = implode( self::$separator, $_POST[$value['name']] ); |
| 585 |
} |
| 586 |
|
| 587 |
break; |
| 588 |
|
| 589 |
case 'address': |
| 590 |
|
| 591 |
if ( isset( $_POST[ $value['name'] ] ) && is_array( $_POST[ $value['name'] ] ) ) { |
| 592 |
foreach ( $_POST[ $value['name'] ] as $address_field => $field_value ) { |
| 593 |
$meta_key_value[ $value['name'] ][ $address_field ] = sanitize_text_field( $field_value ); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
break; |
| 598 |
|
| 599 |
case 'text': |
| 600 |
case 'email': |
| 601 |
case 'number': |
| 602 |
case 'date': |
| 603 |
|
| 604 |
$meta_key_value[$value['name']] = sanitize_text_field( trim( $_POST[$value['name']] ) ); |
| 605 |
|
| 606 |
break; |
| 607 |
|
| 608 |
case 'textarea': |
| 609 |
|
| 610 |
$meta_key_value[$value['name']] = wp_kses_post( $_POST[$value['name']] ); |
| 611 |
|
| 612 |
break; |
| 613 |
|
| 614 |
case 'select': |
| 615 |
case 'radio': |
| 616 |
|
| 617 |
$val = $_POST[$value['name']]; |
| 618 |
$meta_key_value[$value['name']] = isset( $value['options'][$val] ) ? $value['options'][$val] : ''; |
| 619 |
break; |
| 620 |
|
| 621 |
case 'multiselect': |
| 622 |
case 'checkbox': |
| 623 |
|
| 624 |
$val = ( is_array( $_POST[$value['name']] ) && $_POST[$value['name']] ) ? $_POST[$value['name']] : array(); |
| 625 |
$meta_key_value[$value['name']] = $val; |
| 626 |
|
| 627 |
if ( $val ) { |
| 628 |
$new_val = array(); |
| 629 |
|
| 630 |
foreach ($val as $option_key) { |
| 631 |
$new_val[] = isset( $value['options'][$option_key] ) ? $value['options'][$option_key] : ''; |
| 632 |
} |
| 633 |
|
| 634 |
$meta_key_value[$value['name']] = implode( WPUF_Render_Form::$separator, $new_val ); |
| 635 |
} |
| 636 |
break; |
| 637 |
|
| 638 |
default: |
| 639 |
// if it's an array, implode with this->separator |
| 640 |
if ( is_array( $_POST[$value['name']] ) ) { |
| 641 |
|
| 642 |
if ( $value['input_type'] == 'address' ) { |
| 643 |
$meta_key_value[$value['name']] = $_POST[$value['name']]; |
| 644 |
} else { |
| 645 |
$meta_key_value[$value['name']] = implode( self::$separator, $_POST[$value['name']] ); |
| 646 |
} |
| 647 |
} else { |
| 648 |
$meta_key_value[$value['name']] = trim( $_POST[$value['name']] ); |
| 649 |
} |
| 650 |
|
| 651 |
break; |
| 652 |
} |
| 653 |
|
| 654 |
} //end foreach |
| 655 |
|
| 656 |
return array($meta_key_value, $multi_repeated, $files); |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* Import a form from a JSON file |
| 661 |
* |
| 662 |
* @return void |
| 663 |
*/ |
| 664 |
public function import_form() { |
| 665 |
check_ajax_referer( 'weforms' ); |
| 666 |
|
| 667 |
$this->check_admin(); |
| 668 |
|
| 669 |
$the_file = isset( $_FILES['importFile'] ) ? $_FILES['importFile'] : false; |
| 670 |
|
| 671 |
if ( ! $the_file ) { |
| 672 |
wp_send_json_error( __( 'No file found to import.', 'weforms' ) ); |
| 673 |
} |
| 674 |
|
| 675 |
$file_ext = pathinfo( $the_file['name'], PATHINFO_EXTENSION ); |
| 676 |
|
| 677 |
if ( ! class_exists( 'WPUF_Admin_Tools' ) ) { |
| 678 |
require_once WPUF_ROOT . '/admin/class-tools.php'; |
| 679 |
} |
| 680 |
|
| 681 |
if ( ( $file_ext == 'json' ) && ( $the_file['size'] < 500000 ) ) { |
| 682 |
|
| 683 |
$status = WPUF_Admin_Tools::import_json_file( $the_file['tmp_name'] ); |
| 684 |
|
| 685 |
if ( $status ) { |
| 686 |
wp_send_json_success( __( 'The forms have been imported successfully!', 'weforms' ) ); |
| 687 |
} else { |
| 688 |
wp_send_json_error( __( 'Something went wrong importing the file.', 'weforms' ) ); |
| 689 |
} |
| 690 |
|
| 691 |
} else { |
| 692 |
wp_send_json_error( __( 'Invalid file or file size too big.', 'weforms' ) ); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
} |