| @@ -1,1229 +1,1170 @@ | ||
| 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', [ $this, 'get_contact_forms' ] ); | |
| 12 | - add_action( 'wp_ajax_weforms_get_users', [ $this, 'get_all_users' ] ); | |
| 13 | - add_action( 'wp_ajax_weforms_form_names', [ $this, 'get_contact_form_names' ] ); | |
| 14 | - add_action( 'wp_ajax_weforms_form_create', [ $this, 'create_form' ] ); | |
| 15 | - add_action( 'wp_ajax_weforms_form_delete', [ $this, 'delete_form' ] ); | |
| 16 | - add_action( 'wp_ajax_weforms_form_delete_bulk', [ $this, 'delete_form_bulk' ] ); | |
| 17 | - add_action( 'wp_ajax_weforms_form_duplicate', [ $this, 'duplicate_form' ] ); | |
| 18 | - | |
| 19 | - // read logs | |
| 20 | - add_action( 'wp_ajax_weforms_read_logs', [ $this, 'get_logs' ] ); | |
| 21 | - add_action( 'wp_ajax_weforms_delete_logs', [ $this, 'delete_logs' ] ); | |
| 22 | - | |
| 23 | - // create from a template | |
| 24 | - add_filter( 'wp_ajax_weforms_contact_form_template', [ $this, 'create_form_from_template' ] ); | |
| 25 | - | |
| 26 | - // form settings | |
| 27 | - add_action( 'wp_ajax_weforms_save_settings', [ $this, 'save_settings' ] ); | |
| 28 | - add_action( 'wp_ajax_weforms_get_settings', [ $this, 'get_settings' ] ); | |
| 29 | - | |
| 30 | - // import | |
| 31 | - add_action( 'wp_ajax_weforms_import_form', [ $this, 'import_form' ] ); | |
| 32 | - | |
| 33 | - // form editing | |
| 34 | - add_action( 'wp_ajax_weforms_get_form', [ $this, 'get_form' ] ); | |
| 35 | - add_action( 'wp_ajax_wpuf_form_builder_save_form', [ $this, 'save_form' ] ); | |
| 36 | - | |
| 37 | - // entries | |
| 38 | - add_action( 'wp_ajax_weforms_form_entries', [ $this, 'get_entries' ] ); | |
| 39 | - | |
| 40 | - add_action( 'wp_ajax_weforms_form_entry_details', [ $this, 'get_entry_detail' ] ); | |
| 41 | - add_action( 'wp_ajax_weforms_form_entry_trash', [ $this, 'trash_entry' ] ); | |
| 42 | - add_action( 'wp_ajax_weforms_form_entry_delete', [ $this, 'delete_entry' ] ); | |
| 43 | - add_action( 'wp_ajax_weforms_form_entry_restore', [ $this, 'restore_entry' ] ); | |
| 44 | - | |
| 45 | - add_action( 'wp_ajax_weforms_form_entry_trash_bulk', [ $this, 'bulk_delete_entry' ] ); | |
| 46 | - add_action( 'wp_ajax_weforms_form_entry_restore_bulk', [ $this, 'bulk_restore_entry' ] ); | |
| 47 | - | |
| 48 | - // frontend requests | |
| 49 | - add_action( 'wp_ajax_weforms_frontend_submit', [ $this, 'handle_frontend_submission' ] ); | |
| 50 | - add_action( 'wp_ajax_nopriv_weforms_frontend_submit', [ $this, 'handle_frontend_submission' ] ); | |
| 51 | - } | |
| 52 | - | |
| 53 | - /** | |
| 54 | - * Administrator validation | |
| 55 | - * | |
| 56 | - * @return void | |
| 57 | - */ | |
| 58 | - public function check_admin() { | |
| 59 | - if ( !current_user_can( weforms_form_access_capability() ) ) { | |
| 60 | - wp_send_json_error( __( 'You do not have sufficient permission.', 'weforms' ) ); | |
| 61 | - } | |
| 62 | - } | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * Get a form to edit | |
| 66 | - * | |
| 67 | - * @return void | |
| 68 | - */ | |
| 69 | - public function get_form() { | |
| 70 | - $this->check_admin(); | |
| 71 | - | |
| 72 | - $form_id = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : 0; | |
| 73 | - | |
| 74 | - $form = weforms()->form->get( $form_id ); | |
| 75 | - | |
| 76 | - $data = [ | |
| 77 | - 'post' => $form->data, | |
| 78 | - 'form_fields' => $form->get_fields(), | |
| 79 | - 'settings' => $form->get_settings(), | |
| 80 | - 'notifications' => $form->get_notifications(), | |
| 81 | - 'integrations' => $form->get_integrations(), | |
| 82 | - ]; | |
| 83 | - | |
| 84 | - wp_send_json_success( $data ); | |
| 85 | - } | |
| 86 | - | |
| 87 | - /** | |
| 88 | - * Save the form | |
| 89 | - * | |
| 90 | - * @return void | |
| 91 | - */ | |
| 92 | - public function save_form() { | |
| 93 | - $post_data = wp_unslash( $_POST ); | |
| 94 | - if ( isset( $post_data['form_data'] ) ) { | |
| 95 | - parse_str( sanitize_text_field( wp_unslash( $post_data['form_data'] ) ), $form_data ); | |
| 96 | - } | |
| 97 | - | |
| 98 | - if ( !wp_verify_nonce( $form_data['wpuf_form_builder_nonce'], 'wpuf_form_builder_save_form' ) ) { | |
| 99 | - wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); | |
| 100 | - } | |
| 101 | - | |
| 102 | - if ( empty( $form_data['wpuf_form_id'] ) ) { | |
| 103 | - wp_send_json_error( __( 'Invalid form id', 'weforms' ) ); | |
| 104 | - } | |
| 105 | - | |
| 106 | - $form_fields = isset( $post_data['form_fields'] ) ? $post_data['form_fields'] : ''; | |
| 107 | - $notifications = isset( $post_data['notifications'] ) ? $post_data['notifications']: ''; | |
| 108 | - $settings = array(); | |
| 109 | - $integrations = array(); | |
| 110 | - | |
| 111 | - if ( isset( $post_data['settings'] ) ) { | |
| 112 | - $settings = (array) json_decode( $post_data['settings'] ); | |
| 113 | - } else { | |
| 114 | - $settings = isset( $form_data['wpuf_settings'] ) ? $form_data['wpuf_settings'] : []; | |
| 115 | - } | |
| 116 | - | |
| 117 | - if ( isset( $post_data['integrations'] ) ) { | |
| 118 | - $integrations = (array) json_decode( $post_data['integrations'] ); | |
| 119 | - } | |
| 120 | - | |
| 121 | - // $form_fields = wp_unslash( $form_fields ); | |
| 122 | - // $notifications = wp_unslash( $notifications ); | |
| 123 | - | |
| 124 | - $form_fields = json_decode( $form_fields, true ); | |
| 125 | - $notifications = json_decode( $notifications, true ); | |
| 126 | - $data = [ | |
| 127 | - 'form_id' => absint( $form_data['wpuf_form_id'] ), | |
| 128 | - 'post_title' => $form_data['post_title'], | |
| 129 | - 'form_fields' => $form_fields, | |
| 130 | - 'form_settings' => $settings, | |
| 131 | - 'form_settings_key' => isset( $form_data['form_settings_key'] ) ? $form_data['form_settings_key'] : '', | |
| 132 | - 'notifications' => $notifications, | |
| 133 | - 'integrations' => $integrations, | |
| 134 | - ]; | |
| 135 | - | |
| 136 | - $form_fields = weforms()->form->save( $data ); | |
| 137 | - | |
| 138 | - do_action( 'weforms_update_form', $form_data['wpuf_form_id'], $form_fields, $settings ); | |
| 139 | - | |
| 140 | - wp_send_json_success( [ 'form_fields' => $form_fields ] ); | |
| 141 | - } | |
| 142 | - | |
| 143 | - /** | |
| 144 | - * Get all contact forms | |
| 145 | - * | |
| 146 | - * @return void | |
| 147 | - */ | |
| 148 | - public function get_contact_forms() { | |
| 149 | - check_ajax_referer( 'weforms' ); | |
| 150 | - | |
| 151 | - $this->check_admin(); | |
| 152 | - | |
| 153 | - $args = [ | |
| 154 | - 'posts_per_page' => isset( $_POST['posts_per_page'] ) ? intval( $_POST['posts_per_page'] ) : 10, | |
| 155 | - 'paged' => isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1, | |
| 156 | - 'order' => 'DESC', | |
| 157 | - 'orderby' => 'post_date', | |
| 158 | - ]; | |
| 159 | - | |
| 160 | - $args = apply_filters( 'weforms_ajax_get_contact_forms_args', $args ); | |
| 161 | - | |
| 162 | - $contact_forms = weforms()->form->get_forms( $args ); | |
| 163 | - | |
| 164 | - array_map( | |
| 165 | - function ( $form ) { | |
| 166 | - $form->entries = $form->num_form_entries(); | |
| 167 | - $form->settings = $form->get_settings(); | |
| 168 | - $form->views = $form->num_form_views(); | |
| 169 | - $form->payments = $form->num_form_payments(); | |
| 170 | - $form->author = $form->get_form_author_details(); | |
| 171 | - }, $contact_forms['forms'] | |
| 172 | - ); | |
| 173 | - | |
| 174 | - $contact_forms = $this->filter_contact_forms( $contact_forms ); | |
| 175 | - $contact_forms = apply_filters( 'weforms_ajax_get_contact_forms', $contact_forms ); | |
| 176 | - | |
| 177 | - wp_send_json_success( $contact_forms ); | |
| 178 | - } | |
| 179 | - | |
| 180 | - /** | |
| 181 | - * Get all users | |
| 182 | - * | |
| 183 | - * @return array | |
| 184 | - */ | |
| 185 | - public function get_all_users() { | |
| 186 | - check_ajax_referer( 'weforms' ); | |
| 187 | - | |
| 188 | - $this->check_admin(); | |
| 189 | - | |
| 190 | - $users_meta = []; | |
| 191 | - $users = get_users( [ 'fields' => [ 'ID' ] ] ); | |
| 192 | - | |
| 193 | - foreach ( $users as $user ) { | |
| 194 | - $users_meta[] = [ | |
| 195 | - 'id' => $user->ID, | |
| 196 | - 'data' => get_user_meta( $user->ID ), | |
| 197 | - ]; | |
| 198 | - } | |
| 199 | - | |
| 200 | - wp_send_json_success( $users_meta ); | |
| 201 | - } | |
| 202 | - | |
| 203 | - /** | |
| 204 | - * Filter | |
| 205 | - * | |
| 206 | - * @return void | |
| 207 | - */ | |
| 208 | - public function filter_contact_forms( &$contact_forms ) { | |
| 209 | - if ( isset( $_REQUEST['filter'] ) && $_REQUEST['filter'] == 'entries' ) { | |
| 210 | - foreach ( $contact_forms['forms'] as $key => &$form ) { | |
| 211 | - if ( isset( $form->entries ) && !$form->entries ) { | |
| 212 | - unset( $contact_forms['forms'][ $key ] ); | |
| 213 | - } | |
| 214 | - } | |
| 215 | - | |
| 216 | - $contact_forms['meta']['total'] = count( $contact_forms['forms'] ); | |
| 217 | - } | |
| 218 | - | |
| 219 | - return $contact_forms; | |
| 220 | - } | |
| 221 | - | |
| 222 | - /** | |
| 223 | - * Get the names of contact forms for generating dropdown | |
| 224 | - * | |
| 225 | - * @return void | |
| 226 | - */ | |
| 227 | - public function get_contact_form_names() { | |
| 228 | - check_ajax_referer( 'weforms' ); | |
| 229 | - | |
| 230 | - $this->check_admin(); | |
| 231 | - | |
| 232 | - $contact_forms = weforms()->form->all(); | |
| 233 | - $response = []; | |
| 234 | - | |
| 235 | - foreach ( $contact_forms['forms'] as $form ) { | |
| 236 | - $response[] = [ | |
| 237 | - 'id' => $form->get_id(), | |
| 238 | - 'title' => $form->get_name() . ' (#' . $form->get_id() . ')', | |
| 239 | - ]; | |
| 240 | - } | |
| 241 | - | |
| 242 | - wp_send_json_success( $response ); | |
| 243 | - } | |
| 244 | - | |
| 245 | - /** | |
| 246 | - * Create a form | |
| 247 | - * | |
| 248 | - * @return void | |
| 249 | - */ | |
| 250 | - public function create_form() { | |
| 251 | - check_ajax_referer( 'weforms' ); | |
| 252 | - | |
| 253 | - $this->check_admin(); | |
| 254 | - | |
| 255 | - $form_name = isset( $_POST['form_name'] ) ? sanitize_text_field( wp_unslash( $_POST['form_name'] ) ) : ''; | |
| 256 | - | |
| 257 | - if ( empty( $form_name ) ) { | |
| 258 | - wp_send_json_error( __( 'Please provide a form name', 'weforms' ) ); | |
| 259 | - } | |
| 260 | - | |
| 261 | - $form_id = weforms()->form->create( $form_name ); | |
| 262 | - | |
| 263 | - if ( is_wp_error( $form_id ) ) { | |
| 264 | - wp_send_json_error( $form_id->get_error_message() ); | |
| 265 | - } | |
| 266 | - | |
| 267 | - wp_send_json_success( [ | |
| 268 | - 'form_id' => $form_id, | |
| 269 | - 'form_name' => $form_name, | |
| 270 | - ] ); | |
| 271 | - } | |
| 272 | - | |
| 273 | - /** | |
| 274 | - * Delete a form | |
| 275 | - * | |
| 276 | - * @return void | |
| 277 | - */ | |
| 278 | - public function delete_form() { | |
| 279 | - check_ajax_referer( 'weforms' ); | |
| 280 | - | |
| 281 | - $this->check_admin(); | |
| 282 | - | |
| 283 | - $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; | |
| 284 | - | |
| 285 | - if ( !$form_id ) { | |
| 286 | - wp_send_json_error( __( 'No form id provided!', 'weforms' ) ); | |
| 287 | - } | |
| 288 | - | |
| 289 | - weforms()->form->delete( $form_id ); | |
| 290 | - | |
| 291 | - wp_send_json_success(); | |
| 292 | - } | |
| 293 | - | |
| 294 | - public function delete_form_bulk() { | |
| 295 | - check_ajax_referer( 'weforms' ); | |
| 296 | - | |
| 297 | - $this->check_admin(); | |
| 298 | - | |
| 299 | - $form_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : []; | |
| 300 | - | |
| 301 | - if ( !$form_ids ) { | |
| 302 | - wp_send_json_error( __( 'No form ids provided!', 'weforms' ) ); | |
| 303 | - } | |
| 304 | - | |
| 305 | - foreach ( $form_ids as $form_id ) { | |
| 306 | - weforms()->form->delete( $form_id ); | |
| 307 | - } | |
| 308 | - | |
| 309 | - wp_send_json_success(); | |
| 310 | - } | |
| 311 | - | |
| 312 | - /** | |
| 313 | - * Duplicate a form | |
| 314 | - * | |
| 315 | - * @return voiud | |
| 316 | - */ | |
| 317 | - public function duplicate_form() { | |
| 318 | - check_ajax_referer( 'weforms' ); | |
| 319 | - | |
| 320 | - $this->check_admin(); | |
| 321 | - | |
| 322 | - $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; | |
| 323 | - | |
| 324 | - $form = weforms()->form->duplicate( $form_id ); | |
| 325 | - $form->settings = $form->get_settings(); | |
| 326 | - | |
| 327 | - wp_send_json_success( $form ); | |
| 328 | - } | |
| 329 | - | |
| 330 | - /** | |
| 331 | - * Create form from a template | |
| 332 | - * | |
| 333 | - * @return void | |
| 334 | - */ | |
| 335 | - public function create_form_from_template() { | |
| 336 | - check_ajax_referer( 'weforms' ); | |
| 337 | - | |
| 338 | - $template = isset( $_REQUEST['template'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['template'] ) ) : ''; | |
| 339 | - | |
| 340 | - $form_id = weforms()->templates->create( $template ); | |
| 341 | - | |
| 342 | - if ( is_wp_error( $form_id ) ) { | |
| 343 | - wp_send_json_error( __( 'Could not create the form', 'weforms' ) ); | |
| 344 | - } | |
| 345 | - | |
| 346 | - wp_send_json_success( [ | |
| 347 | - 'id' => $form_id, | |
| 348 | - ] ); | |
| 349 | - } | |
| 350 | - | |
| 351 | - /** | |
| 352 | - * Save the weForms settings | |
| 353 | - * | |
| 354 | - * @return void | |
| 355 | - */ | |
| 356 | - public function save_settings() { | |
| 357 | - check_ajax_referer( 'weforms' ); | |
| 358 | - $this->check_admin(); | |
| 359 | - | |
| 360 | - $requires_wpuf_update = false; | |
| 361 | - $wpuf_update_array = array(); | |
| 362 | - $settings = isset( $_POST['settings'] ) ? (array) json_decode( wp_unslash( $_POST['settings'] ) ) : []; | |
| 363 | - update_option( 'weforms_settings', $settings ); | |
| 364 | - | |
| 365 | - // wpuf settings sync | |
| 366 | - if ( isset( $settings['gmap_api'] ) ) { | |
| 367 | - $requires_wpuf_update = true; | |
| 368 | - $wpuf_update_array['gmap_api_key'] = $settings['gmap_api']; | |
| 369 | - } | |
| 370 | - | |
| 371 | - if ( isset( $settings['recaptcha'] ) ) { | |
| 372 | - $requires_wpuf_update = true; | |
| 373 | - $wpuf_update_array['recaptcha_public'] = $settings['recaptcha']->key; | |
| 374 | - $wpuf_update_array['recaptcha_private'] = $settings['recaptcha']->secret; | |
| 375 | - $wpuf_update_array['recaptcha_type'] = $settings['recaptcha']->type; | |
| 376 | - } | |
| 377 | - | |
| 378 | - if ( isset( $settings['no_conflict'] ) ) { | |
| 379 | - $requires_wpuf_update = true; | |
| 380 | - $wpuf_update_array['no_conflict'] = $settings['no_conflict']; | |
| 381 | - } | |
| 382 | - | |
| 383 | - if ( isset( $settings['email_footer'] ) ) { | |
| 384 | - $requires_wpuf_update = true; | |
| 385 | - $wpuf_update_array['email_footer'] = $settings['email_footer']; | |
| 386 | - } | |
| 387 | - | |
| 388 | - if ( $requires_wpuf_update ) { | |
| 389 | - $wpuf_settings = get_option( 'wpuf_general', [] ); | |
| 390 | - | |
| 391 | - $wpuf_settings = array_merge( $wpuf_settings, $wpuf_update_array ); | |
| 392 | - update_option( 'wpuf_general', $wpuf_settings ); | |
| 393 | - } | |
| 394 | - | |
| 395 | - do_action( 'weforms_save_settings', $settings ); | |
| 396 | - | |
| 397 | - $settings = apply_filters( 'weforms_after_save_settings', $settings ); | |
| 398 | - wp_send_json_success( $settings ); | |
| 399 | - } | |
| 400 | - | |
| 401 | - /** | |
| 402 | - * Get the weForms Settings | |
| 403 | - * | |
| 404 | - * @return void | |
| 405 | - */ | |
| 406 | - public function get_settings() { | |
| 407 | - check_ajax_referer( 'weforms' ); | |
| 408 | - | |
| 409 | - $this->check_admin(); | |
| 410 | - | |
| 411 | - $settings = weforms_get_settings(); | |
| 412 | - // checking to prevent js error, will be removed in future | |
| 413 | - if ( !isset( $settings['credit'] ) ) { | |
| 414 | - $settings['credit'] = false; | |
| 415 | - } | |
| 416 | - | |
| 417 | - if ( !isset( $settings['permission'] ) ) { | |
| 418 | - $settings['permission'] = 'manage_options'; | |
| 419 | - } | |
| 420 | - | |
| 421 | - wp_send_json_success( $settings ); | |
| 422 | - } | |
| 423 | - | |
| 424 | - /** | |
| 425 | - * Get all entries | |
| 426 | - * | |
| 427 | - * @return void | |
| 428 | - */ | |
| 429 | - public function get_entries() { | |
| 430 | - check_ajax_referer( 'weforms' ); | |
| 431 | - | |
| 432 | - $this->check_admin(); | |
| 433 | - | |
| 434 | - $form_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; | |
| 435 | - $current_page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1; | |
| 436 | - $status = isset( $_REQUEST['status'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['status'] ) ) : 'publish'; | |
| 437 | - $per_page = 20; | |
| 438 | - $offset = ( $current_page - 1 ) * $per_page; | |
| 439 | - | |
| 440 | - if ( !$form_id ) { | |
| 441 | - wp_send_json_error( __( 'No form id provided!', 'weforms' ) ); | |
| 442 | - } | |
| 443 | - | |
| 444 | - $entries = weforms_get_form_entries( | |
| 445 | - $form_id, [ | |
| 446 | - 'number' => $per_page, | |
| 447 | - 'offset' => $offset, | |
| 448 | - 'status' => $status, | |
| 449 | - ] | |
| 450 | - ); | |
| 451 | - | |
| 452 | - $columns = weforms_get_entry_columns( $form_id ); | |
| 453 | - $total_entries = weforms_count_form_entries( $form_id, $status ); | |
| 454 | - | |
| 455 | - array_map( | |
| 456 | - function ( $entry ) use ( $columns ) { | |
| 457 | - $entry_id = $entry->id; | |
| 458 | - $entry->fields = []; | |
| 459 | - | |
| 460 | - foreach ( $columns as $meta_key => $label ) { | |
| 461 | - if ( empty( $meta_key ) ) { | |
| 462 | - continue; | |
| 463 | - } | |
| 464 | - $value = weforms_get_entry_meta( $entry_id, $meta_key, true ); | |
| 465 | - $entry->fields[ $meta_key ] = str_replace( WeForms::$field_separator, ' ', $value ); | |
| 466 | - } | |
| 467 | - }, $entries | |
| 468 | - ); | |
| 469 | - | |
| 470 | - $entries = apply_filters( 'weforms_get_entries', $entries, $form_id ); | |
| 471 | - | |
| 472 | - $response = [ | |
| 473 | - 'columns' => $columns, | |
| 474 | - 'entries' => $entries, | |
| 475 | - 'form_title' => get_post_field( 'post_title', $form_id ), | |
| 476 | - 'pagination' => [ | |
| 477 | - 'total' => $total_entries, | |
| 478 | - 'per_page' => $per_page, | |
| 479 | - 'pages' => ceil( $total_entries / $per_page ), | |
| 480 | - 'current' => $current_page, | |
| 481 | - ], | |
| 482 | - 'meta' => [ | |
| 483 | - 'total' => weforms_count_form_entries( $form_id ), | |
| 484 | - 'totalTrash' => weforms_count_form_entries( $form_id, 'trash' ), | |
| 485 | - ], | |
| 486 | - ]; | |
| 487 | - | |
| 488 | - wp_send_json_success( $response ); | |
| 489 | - } | |
| 490 | - | |
| 491 | - /** | |
| 492 | - * Get an entry details | |
| 493 | - * | |
| 494 | - * @return void | |
| 495 | - */ | |
| 496 | - public function get_entry_detail() { | |
| 497 | - check_ajax_referer( 'weforms' ); | |
| 498 | - | |
| 499 | - $this->check_admin(); | |
| 500 | - | |
| 501 | - $form_id = isset( $_REQUEST['form_id'] ) ? intval( $_REQUEST['form_id'] ) : 0; | |
| 502 | - $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; | |
| 503 | - | |
| 504 | - $form = weforms()->form->get( $form_id ); | |
| 505 | - $form_settings = $form->get_settings(); | |
| 506 | - $entry = $form->entries()->get( $entry_id ); | |
| 507 | - $fields = $entry->get_fields(); | |
| 508 | - $metadata = $entry->get_metadata(); | |
| 509 | - $payment = $entry->get_payment_data(); | |
| 510 | - | |
| 511 | - if ( isset( $payment->payment_data ) && is_serialized( $payment->payment_data ) ) { | |
| 512 | - $payment->payment_data = unserialize( $payment->payment_data ); | |
| 513 | - } | |
| 514 | - | |
| 515 | - if ( false === $fields ) { | |
| 516 | - wp_send_json_error( __( 'No form fields found!', 'weforms' ) ); | |
| 517 | - } | |
| 518 | - | |
| 519 | - if ( sizeof( $fields ) < 1 ) { | |
| 520 | - $fields[] = [ | |
| 521 | - 'label' => __( 'No form fields found!', 'weforms' ), | |
| 522 | - ]; | |
| 523 | - } | |
| 524 | - | |
| 525 | - $has_empty = false; | |
| 526 | - $answers = []; | |
| 527 | - $respondentPoints = isset( $form_settings['total_points'] ) ? floatval( $form_settings['total_points'] ) : 0; | |
| 528 | - | |
| 529 | - foreach ( $fields as $key => $field ) { | |
| 530 | - if ( $form_settings['quiz_form'] == 'yes' ) { | |
| 531 | - $selectedAnswers = isset( $field['selected_answers'] ) ? $field['selected_answers'] : ''; | |
| 532 | - $givenAnswer = isset( $field['value'] ) ? $field['value'] : ''; | |
| 533 | - $options = isset( $field['options'] ) ? $field['options'] : ''; | |
| 534 | - $template = $field['template']; | |
| 535 | - $fieldPoints = isset( $field['points'] ) ? floatval( $field['points'] ) : 0; | |
| 536 | - | |
| 537 | - if ( $template == 'radio_field' || $template == 'dropdown_field' ) { | |
| 538 | - $answers[$field['name']] = true; | |
| 539 | - | |
| 540 | - if ( empty( $givenAnswer ) ) { | |
| 541 | - $answers[$field['name']] = false; | |
| 542 | - $respondentPoints -= $fieldPoints; | |
| 543 | - } else { | |
| 544 | - foreach ( $options as $key => $value ) { | |
| 545 | - if ( $givenAnswer == $value ) { | |
| 546 | - if ( $key != $selectedAnswers ) { | |
| 547 | - $answers[$field['name']] = false; | |
| 548 | - $respondentPoints -= $fieldPoints; | |
| 549 | - } | |
| 550 | - } | |
| 551 | - } | |
| 552 | - } | |
| 553 | - } elseif ( $template == 'checkbox_field' || $template == 'multiple_select' ) { | |
| 554 | - $answers[$field['name']] = true; | |
| 555 | - $userAnswer = []; | |
| 556 | - | |
| 557 | - foreach ( $options as $key => $value ) { | |
| 558 | - foreach ( $givenAnswer as $answer ) { | |
| 559 | - if ( $value == $answer ) { | |
| 560 | - $userAnswer[] = $key; | |
| 561 | - } | |
| 562 | - } | |
| 563 | - } | |
| 564 | - | |
| 565 | - $userAnswer = implode( '|', $userAnswer ); | |
| 566 | - $rightAnswers = implode( '|', $selectedAnswers ); | |
| 567 | - | |
| 568 | - if ( $userAnswer != $rightAnswers || empty( $userAnswer ) ) { | |
| 569 | - $answers[$field['name']] = false; | |
| 570 | - $respondentPoints -= $fieldPoints; | |
| 571 | - } | |
| 572 | - } | |
| 573 | - } elseif ( empty( $field['value'] ) ) { | |
| 574 | - $has_empty = true; | |
| 575 | - break; | |
| 576 | - } | |
| 577 | - } | |
| 578 | - | |
| 579 | - $response = [ | |
| 580 | - 'form_fields' => $fields, | |
| 581 | - 'form_settings' => $form_settings, | |
| 582 | - 'meta_data' => $metadata, | |
| 583 | - 'payment_data' => $payment, | |
| 584 | - 'has_empty' => $has_empty, | |
| 585 | - 'respondent_points' => $respondentPoints, | |
| 586 | - 'answers' => $answers, | |
| 587 | - ]; | |
| 588 | - | |
| 589 | - wp_send_json_success( $response ); | |
| 590 | - } | |
| 591 | - | |
| 592 | - /** | |
| 593 | - * Trash an entry | |
| 594 | - * | |
| 595 | - * @return void | |
| 596 | - */ | |
| 597 | - public function trash_entry() { | |
| 598 | - check_ajax_referer( 'weforms' ); | |
| 599 | - | |
| 600 | - $this->check_admin(); | |
| 601 | - | |
| 602 | - $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; | |
| 603 | - | |
| 604 | - weforms_change_entry_status( $entry_id, 'trash' ); | |
| 605 | - wp_send_json_success(); | |
| 606 | - } | |
| 607 | - | |
| 608 | - /** | |
| 609 | - * Trash an entry | |
| 610 | - * | |
| 611 | - * @return void | |
| 612 | - */ | |
| 613 | - public function delete_entry() { | |
| 614 | - check_ajax_referer( 'weforms' ); | |
| 615 | - | |
| 616 | - $this->check_admin(); | |
| 617 | - | |
| 618 | - $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; | |
| 619 | - | |
| 620 | - weforms_delete_entry( $entry_id ); | |
| 621 | - | |
| 622 | - wp_send_json_success(); | |
| 623 | - } | |
| 624 | - | |
| 625 | - /** | |
| 626 | - * Restore Entry | |
| 627 | - * | |
| 628 | - * @return void | |
| 629 | - */ | |
| 630 | - public function restore_entry() { | |
| 631 | - check_ajax_referer( 'weforms' ); | |
| 632 | - | |
| 633 | - $this->check_admin(); | |
| 634 | - | |
| 635 | - $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; | |
| 636 | - | |
| 637 | - weforms_change_entry_status( $entry_id, 'publish' ); | |
| 638 | - wp_send_json_success(); | |
| 639 | - } | |
| 640 | - | |
| 641 | - /** | |
| 642 | - * Bulk trash entries | |
| 643 | - * | |
| 644 | - * @return void | |
| 645 | - */ | |
| 646 | - public function bulk_delete_entry() { | |
| 647 | - check_ajax_referer( 'weforms' ); | |
| 648 | - | |
| 649 | - $this->check_admin(); | |
| 650 | - | |
| 651 | - $entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array(); | |
| 652 | - $permanent = isset( $_POST['permanent'] ) && ( sanitize_text_field( wp_unslash ( $_POST['permanent'] ) ) ) ? true : false; | |
| 653 | - | |
| 654 | - if ( !$entry_ids ) { | |
| 655 | - wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) ); | |
| 656 | - } | |
| 657 | - | |
| 658 | - foreach ( $entry_ids as $entry_id ) { | |
| 659 | - if ( $permanent ) { | |
| 660 | - weforms_delete_entry( $entry_id ); | |
| 661 | - } else { | |
| 662 | - weforms_change_entry_status( $entry_id, 'trash' ); | |
| 663 | - } | |
| 664 | - } | |
| 665 | - | |
| 666 | - wp_send_json_success(); | |
| 667 | - } | |
| 668 | - | |
| 669 | - /** | |
| 670 | - * Bulk trash entries | |
| 671 | - * | |
| 672 | - * @return void | |
| 673 | - */ | |
| 674 | - public function bulk_restore_entry() { | |
| 675 | - check_ajax_referer( 'weforms' ); | |
| 676 | - | |
| 677 | - $this->check_admin(); | |
| 678 | - | |
| 679 | - $entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : []; | |
| 680 | - | |
| 681 | - if ( !$entry_ids ) { | |
| 682 | - wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) ); | |
| 683 | - } | |
| 684 | - | |
| 685 | - foreach ( $entry_ids as $entry_id ) { | |
| 686 | - weforms_change_entry_status( $entry_id, 'publish' ); | |
| 687 | - } | |
| 688 | - | |
| 689 | - wp_send_json_success(); | |
| 690 | - } | |
| 691 | - | |
| 692 | - /** | |
| 693 | - * Handle the frontend submission | |
| 694 | - * | |
| 695 | - * @return void | |
| 696 | - */ | |
| 697 | - public function handle_frontend_submission() { | |
| 698 | - check_ajax_referer( 'wpuf_form_add' ); | |
| 699 | - | |
| 700 | - $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; | |
| 701 | - $page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0; | |
| 702 | - | |
| 703 | - $form = weforms()->form->get( $form_id ); | |
| 704 | - $form_settings = $form->get_settings(); | |
| 705 | - $form_fields = $form->get_fields(); | |
| 706 | - $entry_fields = $form->prepare_entries(); | |
| 707 | - $form_entries = weforms_get_form_entries( $form_id, [ 'number' => '', 'offset' => '' ] ); | |
| 708 | - | |
| 709 | - if ( $form_fields && count( $form_entries ) && count( $entry_fields ) ) { | |
| 710 | - foreach ( $entry_fields as $field_key => $field_value ) { | |
| 711 | - $duplicate_check = false; | |
| 712 | - $field_label = 'This'; | |
| 713 | - | |
| 714 | - foreach ( $form_fields as $form_field ) { | |
| 715 | - if ( in_array( $form_field['template'], [ 'text_field', 'website_url', 'numeric_text_field', 'email_address' ] ) && $form_field['name'] == $field_key && isset( $form_field['duplicate'] ) && 'no' == $form_field['duplicate'] ) { | |
| 716 | - $duplicate_check = true; | |
| 717 | - $field_label = $form_field['label']; | |
| 718 | - } | |
| 719 | - } | |
| 720 | - | |
| 721 | - if ( $duplicate_check ) { | |
| 722 | - foreach ( $form_entries as $entry ) { | |
| 723 | - $existing = weforms_get_entry_meta( $entry->id, $field_key, true ); | |
| 724 | - | |
| 725 | - if ( $existing && $field_value == $existing ) { | |
| 726 | - wp_send_json( [ | |
| 727 | - 'success' => false, | |
| 728 | - 'error' => sprintf( __( '"%s" field requires a unique entry and "%s" has already been used.', 'weforms' ), $field_label, $field_value ), | |
| 729 | - ] ); | |
| 730 | - } | |
| 731 | - } | |
| 732 | - } | |
| 733 | - } | |
| 734 | - } | |
| 735 | - | |
| 736 | - if ( !$form_fields ) { | |
| 737 | - wp_send_json( [ | |
| 738 | - 'success' => false, | |
| 739 | - 'error' => __( 'No form field was found.', 'weforms' ), | |
| 740 | - ] ); | |
| 741 | - } | |
| 742 | - | |
| 743 | - if ( $form->has_field( 'recaptcha' ) ) { | |
| 744 | - $settings = weforms_get_settings( 'recaptcha' ); | |
| 745 | - $type = isset( $settings->type ) ? $settings->type : ''; | |
| 746 | - $secret = isset( $settings->secret ) ? $settings->secret : ''; | |
| 747 | - if( $type == 'v3' ) { | |
| 748 | - $this->validate_reCaptchav3( $secret ); | |
| 749 | - } else { | |
| 750 | - $this->validate_reCaptcha(); | |
| 751 | - } | |
| 752 | - } | |
| 753 | - | |
| 754 | - // vaidate submission | |
| 755 | - $this->validate_submission( $entry_fields, $form, $form_settings, $form_fields ); | |
| 756 | - | |
| 757 | - $entry_fields = apply_filters( 'weforms_before_entry_submission', $entry_fields, $form, $form_settings, $form_fields ); | |
| 758 | - | |
| 759 | - $entry_id = weforms_insert_entry( [ | |
| 760 | - 'form_id' => $form_id, | |
| 761 | - ], $entry_fields ); | |
| 762 | - | |
| 763 | - if ( is_wp_error( $entry_id ) ) { | |
| 764 | - wp_send_json( [ | |
| 765 | - 'success' => false, | |
| 766 | - 'error' => $entry_id->get_error_message(), | |
| 767 | - ] ); | |
| 768 | - } | |
| 769 | - | |
| 770 | - // redirect URL | |
| 771 | - $show_message = false; | |
| 772 | - $redirect_to = false; | |
| 773 | - | |
| 774 | - if ( $form_settings['redirect_to'] == 'page' ) { | |
| 775 | - $redirect_to = get_permalink( $form_settings['page_id'] ); | |
| 776 | - } elseif ( $form_settings['redirect_to'] == 'url' ) { | |
| 777 | - $redirect_to = $form_settings['url']; | |
| 778 | - } elseif ( $form_settings['redirect_to'] == 'same' ) { | |
| 779 | - $show_message = true; | |
| 780 | - } else { | |
| 781 | - $show_message = true; | |
| 782 | - } | |
| 783 | - | |
| 784 | - // Fire a hook for integration | |
| 785 | - do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings ); | |
| 786 | - | |
| 787 | - $field_search = $field_replace = []; | |
| 788 | - | |
| 789 | - foreach ( $form_fields as $r_field ) { | |
| 790 | - $field_search[] = '{' . $r_field['name'] . '}'; | |
| 791 | - | |
| 792 | - if ( $r_field['template'] == 'name_field' ) { | |
| 793 | - $field_replace[] = implode( ' ', explode( '|', $entry_fields[ $r_field['name'] ] ) ); | |
| 794 | - } else if ( $r_field['template'] == 'address_field' ) { | |
| 795 | - $field_replace[] = implode( ', ', $entry_fields[ $r_field['name'] ] ); | |
| 796 | - } else { | |
| 797 | - $field_replace[] = isset( $entry_fields[ $r_field['name'] ] ) ? $entry_fields[ $r_field['name'] ] : ''; | |
| 798 | - } | |
| 799 | - } | |
| 800 | - $message = str_replace( $field_search, $field_replace, $form_settings['message'] ); | |
| 801 | - | |
| 802 | - // send the response | |
| 803 | - $response = apply_filters( 'weforms_entry_submission_response', [ | |
| 804 | - 'success' => true, | |
| 805 | - 'redirect_to' => $redirect_to, | |
| 806 | - 'show_message' => $show_message, | |
| 807 | - 'message' => $message, | |
| 808 | - 'data' => $_POST, | |
| 809 | - 'form_id' => $form_id, | |
| 810 | - 'entry_id' => $entry_id, | |
| 811 | - ] ); | |
| 812 | - | |
| 813 | - $notification = new WeForms_Notification( [ | |
| 814 | - 'form_id' => $form_id, | |
| 815 | - 'page_id' => $page_id, | |
| 816 | - 'entry_id' => $entry_id, | |
| 817 | - ] ); | |
| 818 | - | |
| 819 | - $notification->send_notifications(); | |
| 820 | - | |
| 821 | - weforms_clear_buffer(); | |
| 822 | - wp_send_json( $response ); | |
| 823 | - } | |
| 824 | - | |
| 825 | - function validate_reCaptchav3( $secret ) { | |
| 826 | - check_ajax_referer( 'wpuf_form_add' ); | |
| 827 | - | |
| 828 | - $post_data = wp_unslash($_POST); | |
| 829 | - $token = $post_data['g-recaptcha-response']; | |
| 830 | - $action = $post_data['g-action']; | |
| 831 | - $google_captcha_url = esc_url( 'https://www.google.com/recaptcha/api/siteverify' ); | |
| 832 | - | |
| 833 | - $response = wp_remote_post( $google_captcha_url, | |
| 834 | - array( | |
| 835 | - 'method' => 'POST', | |
| 836 | - 'body' => array( | |
| 837 | - 'secret' => $secret, | |
| 838 | - 'response' => $token | |
| 839 | - ) | |
| 840 | - ) | |
| 841 | - ); | |
| 842 | - | |
| 843 | - | |
| 844 | - if ( is_wp_error( $response ) ) { | |
| 845 | - wp_send_json( [ | |
| 846 | - 'success' => false, | |
| 847 | - 'error' => __( 'reCAPTCHA validation failed', 'weforms' ), | |
| 848 | - ] ); | |
| 849 | - } else { | |
| 850 | - $api_response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 851 | - if( $api_response["success"] == '1' && $api_response["action"] == $action ) { | |
| 852 | - return true; | |
| 853 | - } else { | |
| 854 | - wp_send_json( [ | |
| 855 | - 'success' => false, | |
| 856 | - 'error' => __( 'reCAPTCHA validation failed', 'weforms' ), | |
| 857 | - ] ); | |
| 858 | - } | |
| 859 | - } | |
| 860 | - } | |
| 861 | - /** | |
| 862 | - * reCaptcha Validation | |
| 863 | - * | |
| 864 | - * @return void | |
| 865 | - */ | |
| 866 | - function validate_reCaptcha() { | |
| 867 | - check_ajax_referer( 'wpuf_form_add' ); | |
| 868 | - if ( class_exists( 'WPUF_ReCaptcha' ) ) { | |
| 869 | - $recaptcha_class = 'WPUF_ReCaptcha'; | |
| 870 | - } else { | |
| 871 | - if ( !function_exists( 'recaptcha_get_html' ) ) { | |
| 872 | - require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib.php'; | |
| 873 | - } | |
| 874 | - | |
| 875 | - require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib_noCaptcha.php'; | |
| 876 | - $recaptcha_class = 'Weforms_ReCaptcha'; | |
| 877 | - } | |
| 878 | - | |
| 879 | - $invisible = isset( $_POST['g-recaptcha-response'] ) ? false : true; | |
| 880 | - | |
| 881 | - $recaptcha_settings = weforms_get_settings( 'recaptcha' ); | |
| 882 | - $secret = isset( $recaptcha_settings->secret ) ? $recaptcha_settings->secret : ''; | |
| 883 | - | |
| 884 | - if ( ! $invisible ) { | |
| 885 | - $response = null; | |
| 886 | - $reCaptcha = new $recaptcha_class( $secret ); | |
| 887 | - $remote_ADDR = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; | |
| 888 | - $recaptcha_response = isset( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ) : ''; | |
| 889 | - $resp = $reCaptcha->verifyResponse( | |
| 890 | - $remote_ADDR, | |
| 891 | - $recaptcha_response | |
| 892 | - ); | |
| 893 | - | |
| 894 | - if ( !$resp->success ) { | |
| 895 | - wp_send_json( [ | |
| 896 | - 'success' => false, | |
| 897 | - 'error' => __( 'reCAPTCHA validation failed', 'weforms' ), | |
| 898 | - ] ); | |
| 899 | - } | |
| 900 | - | |
| 901 | - } else { | |
| 902 | - | |
| 903 | - $recap_challenge = isset( $_POST['recaptcha_challenge_field'] ) ? sanitize_text_field( wp_unslash( $_POST['recaptcha_challenge_field'] ) ) : ''; | |
| 904 | - $recap_response = isset( $_POST['recaptcha_response_field'] ) ? sanitize_text_field( wp_unslash( $_POST['recaptcha_response_field'] ) ) : ''; | |
| 905 | - $resp = recaptcha_check_answer( $secret, $remote_ADDR, $recap_challenge, $recap_response ); | |
| 906 | - | |
| 907 | - if ( !$resp->is_valid ) { | |
| 908 | - ob_clean(); | |
| 909 | - | |
| 910 | - wp_send_json( [ | |
| 911 | - 'success' => false, | |
| 912 | - 'error' => __( 'reCAPTCHA validation failed', 'weforms' ), | |
| 913 | - ] ); | |
| 914 | - } | |
| 915 | - } | |
| 916 | - } | |
| 917 | - | |
| 918 | - /** | |
| 919 | - * Validate submission | |
| 920 | - * | |
| 921 | - * @param array $entry_fields | |
| 922 | - * @param object $form | |
| 923 | - * @param array $form_settings | |
| 924 | - * @param array $form_fields | |
| 925 | - * | |
| 926 | - * @return bool|json | |
| 927 | - */ | |
| 928 | - public function validate_submission( $entry_fields, $form, $form_settings, $form_fields ) { | |
| 929 | - foreach ( $form_fields as $key => $field ) { | |
| 930 | - | |
| 931 | - //skip custom html field as it is not saved | |
| 932 | - if ( 'custom_html' == $field['name'] ) { | |
| 933 | - continue; | |
| 934 | - } | |
| 935 | - | |
| 936 | - //skip recaptcha field as it is not saved | |
| 937 | - if ( 'recaptcha' == $field['name'] ) { | |
| 938 | - continue; | |
| 939 | - } | |
| 940 | - | |
| 941 | - $value = $entry_fields[ $field['name'] ]; | |
| 942 | - | |
| 943 | - if ( 'single_product' === $field['template'] ) { | |
| 944 | - if ( !$value ) { | |
| 945 | - $value = []; | |
| 946 | - } | |
| 947 | - | |
| 948 | - $value['price'] = isset( $value['price'] ) ? floatval( $value['price'] ) : 0; | |
| 949 | - $value['quantity'] = isset( $value['quantity'] ) ? floatval( $value['quantity'] ) : 0; | |
| 950 | - $quantity = isset( $field['quantity'] ) ? $field['quantity'] : []; | |
| 951 | - $price = isset( $field['price'] ) ? $field['price'] : []; | |
| 952 | - | |
| 953 | - if ( isset( $price['is_flexible'] ) && $price['is_flexible'] ) { | |
| 954 | - $min = isset( $price['min'] ) ? floatval( $price['min'] ) : 0; | |
| 955 | - $max = isset( $price['max'] ) ? floatval( $price['max'] ) : 0; | |
| 956 | - | |
| 957 | - if ( $value['price'] < $min ) { | |
| 958 | - wp_send_json( [ | |
| 959 | - 'success' => false, | |
| 960 | - 'error' => __( sprintf( | |
| 961 | - '%s price must be equal or greater than %s', | |
| 962 | - $field['weforms'], | |
| 963 | - $min ), | |
| 964 | - 'weforms' ), | |
| 965 | - ] ); | |
| 966 | - } | |
| 967 | - | |
| 968 | - if ( $max && $value['price'] > $max ) { | |
| 969 | - wp_send_json( [ | |
| 970 | - 'success' => false, | |
| 971 | - 'error' => __( sprintf( | |
| 972 | - '%s price must be equal or less than %s', | |
| 973 | - $field['weforms'], | |
| 974 | - $max ), | |
| 975 | - 'weforms' ), | |
| 976 | - ] ); | |
| 977 | - } | |
| 978 | - } | |
| 979 | - | |
| 980 | - if ( isset( $quantity['status'] ) && $quantity['status'] ) { | |
| 981 | - $min = isset( $quantity['min'] ) ? floatval( $quantity['min'] ) : 0; | |
| 982 | - $max = isset( $quantity['max'] ) ? floatval( $quantity['max'] ) : 0; | |
| 983 | - | |
| 984 | - if ( $value['quantity'] < $min ) { | |
| 985 | - wp_send_json( [ | |
| 986 | - 'success' => false, | |
| 987 | - 'error' => __( sprintf( | |
| 988 | - '%s quantity must be equal or greater than %s', | |
| 989 | - $field['weforms'], | |
| 990 | - $min ), | |
| 991 | - 'weforms' ), | |
| 992 | - ] ); | |
| 993 | - } | |
| 994 | - | |
| 995 | - if ( $max && $value['quantity'] > $max ) { | |
| 996 | - wp_send_json( [ | |
| 997 | - 'success' => false, | |
| 998 | - 'error' => __( sprintf( | |
| 999 | - '%s quantity must be equal or less than %s', | |
| 1000 | - $field['weforms'], | |
| 1001 | - $max ), | |
| 1002 | - 'weforms' ), | |
| 1003 | - ] ); | |
| 1004 | - } | |
| 1005 | - } | |
| 1006 | - } | |
| 1007 | - } | |
| 1008 | - } | |
| 1009 | - | |
| 1010 | - public static function prepare_meta_fields( $meta_vars ) { | |
| 1011 | - check_ajax_referer( 'wpuf_form_add' ); | |
| 1012 | - // loop through custom fields | |
| 1013 | - // skip files, put in a key => value paired array for later executation | |
| 1014 | - // process repeatable fields separately | |
| 1015 | - // if the input is array type, implode with separator in a field | |
| 1016 | - $files = []; | |
| 1017 | - $meta_key_value = []; | |
| 1018 | - $multi_repeated = []; // multi repeated fields will in sotre duplicated meta key | |
| 1019 | - | |
| 1020 | - foreach ( $meta_vars as $key => $value ) { | |
| 1021 | - switch ( $value['template'] ) { | |
| 1022 | - | |
| 1023 | - // put files in a separate array, we'll process it later | |
| 1024 | - case 'file_upload': | |
| 1025 | - case 'image_upload': | |
| 1026 | - $files[] = [ | |
| 1027 | - 'name' => $value['name'], | |
| 1028 | - 'value' => isset( $_POST['wpuf_files'][ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST['wpuf_files'][ $value['name'] ] ) ) : array(), | |
| 1029 | - 'count' => $value['count'] | |
| 1030 | - ]; | |
| 1031 | - break; | |
| 1032 | - | |
| 1033 | - case 'repeat_field': | |
| 1034 | - // if it is a multi column repeat field | |
| 1035 | - if ( isset( $value['multiple'] ) && $value['multiple'] == 'true' ) { | |
| 1036 | - | |
| 1037 | - // if there's any items in the array, process it | |
| 1038 | - if ( isset( $_POST[ $value['name'] ] ) ) { | |
| 1039 | - $ref_arr = []; | |
| 1040 | - $cols = count( $value['columns'] ); | |
| 1041 | - $first = array_shift( array_values( sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) ) ); // first element | |
| 1042 | - $rows = count( $first ); | |
| 1043 | - | |
| 1044 | - // loop through columns | |
| 1045 | - for ( $i = 0; $i < $rows; $i++ ) { | |
| 1046 | - | |
| 1047 | - // loop through the rows and store in a temp array | |
| 1048 | - $temp = []; | |
| 1049 | - for ( $j = 0; $j < $cols; $j++ ) { | |
| 1050 | - $temp[] = isset( $_POST[ $value['name'] ][ $j ][ $i ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ][ $j ][ $i ] ) ) : ''; | |
| 1051 | - } | |
| 1052 | - | |
| 1053 | - // store all fields in a row with WeForms::$field_separator separated | |
| 1054 | - $ref_arr[] = implode( WeForms::$field_separator, $temp ); | |
| 1055 | - } | |
| 1056 | - | |
| 1057 | - // now, if we found anything in $ref_arr, store to $multi_repeated | |
| 1058 | - if ( $ref_arr ) { | |
| 1059 | - $multi_repeated[ $value['name'] ] = array_slice( $ref_arr, 0, $rows ); | |
| 1060 | - } | |
| 1061 | - } | |
| 1062 | - } else { | |
| 1063 | - $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) ); | |
| 1064 | - } | |
| 1065 | - | |
| 1066 | - break; | |
| 1067 | - | |
| 1068 | - case 'address_field': | |
| 1069 | - if ( isset( $_POST[ $value['name'] ] ) && is_array( $_POST[ $value['name'] ] ) ) { | |
| 1070 | - $post_value = sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ); | |
| 1071 | - foreach ( $post_value as $address_field => $field_value ) { | |
| 1072 | - $meta_key_value[ $value['name'] ][ $address_field ] = sanitize_text_field( $field_value ); | |
| 1073 | - } | |
| 1074 | - } | |
| 1075 | - | |
| 1076 | - break; | |
| 1077 | - | |
| 1078 | - case 'text_field': | |
| 1079 | - case 'email_address': | |
| 1080 | - case 'numeric_text_field': | |
| 1081 | - case 'date_field': | |
| 1082 | - $meta_key_value[ $value['name'] ] = isset( $_POST[ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) : ''; | |
| 1083 | - | |
| 1084 | - break; | |
| 1085 | - | |
| 1086 | - case 'textarea_field': | |
| 1087 | - $meta_key_value[ $value['name'] ] = isset( $_POST[ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) : ''; | |
| 1088 | - | |
| 1089 | - break; | |
| 1090 | - | |
| 1091 | - case 'dropdown_field': | |
| 1092 | - case 'radio_field': | |
| 1093 | - $val = sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ); | |
| 1094 | - $meta_key_value[ $value['name'] ] = isset( $value['options'][ $val ] ) ? $value['options'][ $val ] : ''; | |
| 1095 | - break; | |
| 1096 | - | |
| 1097 | - case 'multiple_select': | |
| 1098 | - case 'checkbox_field': | |
| 1099 | - $val = ( is_array( $_POST[ $value['name'] ] ) && sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) : array(); | |
| 1100 | - $meta_key_value[ $value['name'] ] = $val; | |
| 1101 | - | |
| 1102 | - if ( $val ) { | |
| 1103 | - $new_val = []; | |
| 1104 | - | |
| 1105 | - foreach ( $val as $option_key ) { | |
| 1106 | - $new_val[] = isset( $value['options'][ $option_key ] ) ? $value['options'][ $option_key ] : ''; | |
| 1107 | - } | |
| 1108 | - | |
| 1109 | - $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, $new_val ); | |
| 1110 | - } | |
| 1111 | - break; | |
| 1112 | - | |
| 1113 | - default: | |
| 1114 | - // if it's an array, implode with this->separator | |
| 1115 | - if ( is_array( $_POST[ $value['name'] ] ) ) { | |
| 1116 | - $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) ); | |
| 1117 | - } else { | |
| 1118 | - $meta_key_value[ $value['name'] ] = isset( $_POST[ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ): ''; | |
| 1119 | - } | |
| 1120 | - | |
| 1121 | - break; | |
| 1122 | - } | |
| 1123 | - } //end foreach | |
| 1124 | - | |
| 1125 | - return [ $meta_key_value, $multi_repeated, $files ]; | |
| 1126 | - } | |
| 1127 | - | |
| 1128 | - /** | |
| 1129 | - * Import a form from a JSON file | |
| 1130 | - * | |
| 1131 | - * @return void | |
| 1132 | - */ | |
| 1133 | - public function import_form() { | |
| 1134 | - check_ajax_referer( 'weforms' ); | |
| 1135 | - | |
| 1136 | - $this->check_admin(); | |
| 1137 | - | |
| 1138 | - $the_file = isset( $_FILES['importFile'] ) ? $_FILES['importFile'] : false; | |
| 1139 | - | |
| 1140 | - if ( !$the_file ) { | |
| 1141 | - wp_send_json_error( __( 'No file found to import.', 'weforms' ) ); | |
| 1142 | - } | |
| 1143 | - | |
| 1144 | - $file_ext = pathinfo( $the_file['name'], PATHINFO_EXTENSION ); | |
| 1145 | - | |
| 1146 | - if ( !class_exists( 'WeForms_Admin_Tools' ) ) { | |
| 1147 | - require_once __DIR__ . '/admin/class-admin-tools.php'; | |
| 1148 | - } | |
| 1149 | - | |
| 1150 | - if ( ( $file_ext == 'json' ) && ( $the_file['size'] < 500000 ) ) { | |
| 1151 | - $status = WeForms_Admin_Tools::import_json_file( $the_file['tmp_name'] ); | |
| 1152 | - | |
| 1153 | - if ( $status ) { | |
| 1154 | - wp_send_json_success( __( 'The forms have been imported successfully!', 'weforms' ) ); | |
| 1155 | - } else { | |
| 1156 | - wp_send_json_error( __( 'Something went wrong importing the file.', 'weforms' ) ); | |
| 1157 | - } | |
| 1158 | - } else { | |
| 1159 | - wp_send_json_error( __( 'Invalid file or file size too big.', 'weforms' ) ); | |
| 1160 | - } | |
| 1161 | - } | |
| 1162 | - | |
| 1163 | - /** | |
| 1164 | - * Read Log file | |
| 1165 | - * | |
| 1166 | - * @return json | |
| 1167 | - **/ | |
| 1168 | - public function get_logs() { | |
| 1169 | - check_ajax_referer( 'weforms' ); | |
| 1170 | - | |
| 1171 | - $this->check_admin(); | |
| 1172 | - | |
| 1173 | - $file = weforms_log_file_path(); | |
| 1174 | - | |
| 1175 | - if ( !file_exists( $file ) ) { | |
| 1176 | - return; | |
| 1177 | - } | |
| 1178 | - | |
| 1179 | - $data = file_get_contents( $file ); | |
| 1180 | - $data = explode( "\n", $data ); | |
| 1181 | - $data = array_reverse( $data ); | |
| 1182 | - $data = array_filter( $data ); | |
| 1183 | - | |
| 1184 | - if ( empty( $data ) ) { | |
| 1185 | - return; | |
| 1186 | - } | |
| 1187 | - | |
| 1188 | - $logs = []; | |
| 1189 | - | |
| 1190 | - foreach ( $data as $key => $row ) { | |
| 1191 | - preg_match( '/\[(?<time>.+?)\]\[(?<type>.+?)\](?<message>.+)/im', $row, $log ); | |
| 1192 | - | |
| 1193 | - if ( empty( $log['message'] ) ) { | |
| 1194 | - $log = []; | |
| 1195 | - $log['message'] = !empty( $log['message'] ) ? $log['message'] : $row; | |
| 1196 | - } | |
| 1197 | - | |
| 1198 | - if ( !empty( $log['time'] ) ) { | |
| 1199 | - $human_time = human_time_diff( strtotime( $log['time'] ), current_time( 'timestamp' ) ); | |
| 1200 | - $log['time'] = $human_time ? $human_time . ' ' . __( 'ago', 'weforms' ) : $log['time']; | |
| 1201 | - } | |
| 1202 | - | |
| 1203 | - $logs[] = $log; | |
| 1204 | - } | |
| 1205 | - | |
| 1206 | - wp_send_json_success( $logs ); | |
| 1207 | - } | |
| 1208 | - | |
| 1209 | - /** | |
| 1210 | - * Read Log file | |
| 1211 | - * | |
| 1212 | - * @return json | |
| 1213 | - **/ | |
| 1214 | - public function delete_logs() { | |
| 1215 | - check_ajax_referer( 'weforms' ); | |
| 1216 | - | |
| 1217 | - $this->check_admin(); | |
| 1218 | - | |
| 1219 | - $file = weforms_log_file_path(); | |
| 1220 | - | |
| 1221 | - if ( !file_exists( $file ) ) { | |
| 1222 | - return; | |
| 1223 | - } | |
| 1224 | - | |
| 1225 | - @unlink( $file ); | |
| 1226 | - | |
| 1227 | - wp_send_json_success(); | |
| 1228 | - } | |
| 1229 | -} | |
| 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 | + // read logs | |
| 19 | + add_action( 'wp_ajax_weforms_read_logs', array( $this, 'get_logs' ) ); | |
| 20 | + add_action( 'wp_ajax_weforms_delete_logs', array( $this, 'delete_logs' ) ); | |
| 21 | + | |
| 22 | + // create from a template | |
| 23 | + add_filter( 'wp_ajax_weforms_contact_form_template', array( $this, 'create_form_from_template' ) ); | |
| 24 | + | |
| 25 | + // form settings | |
| 26 | + add_action( 'wp_ajax_weforms_save_settings', array( $this, 'save_settings' ) ); | |
| 27 | + add_action( 'wp_ajax_weforms_get_settings', array( $this, 'get_settings' ) ); | |
| 28 | + | |
| 29 | + // import | |
| 30 | + add_action( 'wp_ajax_weforms_import_form', array( $this, 'import_form' ) ); | |
| 31 | + | |
| 32 | + // form editing | |
| 33 | + add_action( 'wp_ajax_weforms_get_form', array( $this, 'get_form' ) ); | |
| 34 | + add_action( 'wp_ajax_wpuf_form_builder_save_form', array( $this, 'save_form' ) ); | |
| 35 | + | |
| 36 | + // entries | |
| 37 | + add_action( 'wp_ajax_weforms_form_entries', array( $this, 'get_entries' ) ); | |
| 38 | + | |
| 39 | + add_action( 'wp_ajax_weforms_form_entry_details', array( $this, 'get_entry_detail' ) ); | |
| 40 | + add_action( 'wp_ajax_weforms_form_entry_trash', array( $this, 'trash_entry' ) ); | |
| 41 | + add_action( 'wp_ajax_weforms_form_entry_delete', array( $this, 'delete_entry' ) ); | |
| 42 | + add_action( 'wp_ajax_weforms_form_entry_restore', array( $this, 'restore_entry' ) ); | |
| 43 | + | |
| 44 | + add_action( 'wp_ajax_weforms_form_entry_trash_bulk', array( $this, 'bulk_delete_entry' ) ); | |
| 45 | + add_action( 'wp_ajax_weforms_form_entry_restore_bulk', array( $this, 'bulk_restore_entry' ) ); | |
| 46 | + | |
| 47 | + // frontend requests | |
| 48 | + add_action( 'wp_ajax_weforms_frontend_submit', array( $this, 'handle_frontend_submission' ) ); | |
| 49 | + add_action( 'wp_ajax_nopriv_weforms_frontend_submit', array( $this, 'handle_frontend_submission' ) ); | |
| 50 | + } | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * Administrator validation | |
| 54 | + * | |
| 55 | + * @return void | |
| 56 | + */ | |
| 57 | + public function check_admin() { | |
| 58 | + if ( ! current_user_can( weforms_form_access_capability() ) ) { | |
| 59 | + wp_send_json_error( __( 'You do not have sufficient permission.', 'weforms' ) ); | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Get a form to edit | |
| 65 | + * | |
| 66 | + * @return void | |
| 67 | + */ | |
| 68 | + public function get_form() { | |
| 69 | + | |
| 70 | + $this->check_admin(); | |
| 71 | + | |
| 72 | + $form_id = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : 0; | |
| 73 | + | |
| 74 | + $form = weforms()->form->get( $form_id ); | |
| 75 | + | |
| 76 | + $data = array( | |
| 77 | + 'post' => $form->data, | |
| 78 | + 'form_fields' => $form->get_fields(), | |
| 79 | + 'settings' => $form->get_settings(), | |
| 80 | + 'notifications' => $form->get_notifications(), | |
| 81 | + 'integrations' => $form->get_integrations() | |
| 82 | + ); | |
| 83 | + | |
| 84 | + wp_send_json_success( $data ); | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Save the form | |
| 89 | + * | |
| 90 | + * @return void | |
| 91 | + */ | |
| 92 | + public function save_form() { | |
| 93 | + parse_str( $_POST['form_data'], $form_data ); | |
| 94 | + | |
| 95 | + if ( ! wp_verify_nonce( $form_data['wpuf_form_builder_nonce'], 'wpuf_form_builder_save_form' ) ) { | |
| 96 | + wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); | |
| 97 | + } | |
| 98 | + | |
| 99 | + if ( empty( $form_data['wpuf_form_id'] ) ) { | |
| 100 | + wp_send_json_error( __( 'Invalid form id', 'weforms' ) ); | |
| 101 | + } | |
| 102 | + | |
| 103 | + $form_fields = isset( $_POST['form_fields'] ) ? $_POST['form_fields'] : ''; | |
| 104 | + $notifications = isset( $_POST['notifications'] ) ? $_POST['notifications'] : ''; | |
| 105 | + $settings = array(); | |
| 106 | + $integrations = array(); | |
| 107 | + | |
| 108 | + if ( isset( $_POST['settings'] ) ) { | |
| 109 | + $settings = (array) json_decode( wp_unslash( $_POST['settings'] ) ); | |
| 110 | + } else { | |
| 111 | + $settings = isset( $form_data['wpuf_settings'] ) ? $form_data['wpuf_settings'] : array(); | |
| 112 | + } | |
| 113 | + | |
| 114 | + if ( isset( $_POST['integrations'] ) ) { | |
| 115 | + $integrations = (array) json_decode( wp_unslash( $_POST['integrations'] ) ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + $form_fields = wp_unslash( $form_fields ); | |
| 119 | + $notifications = wp_unslash( $notifications ); | |
| 120 | + | |
| 121 | + $form_fields = json_decode( $form_fields, true ); | |
| 122 | + $notifications = json_decode( $notifications, true ); | |
| 123 | + | |
| 124 | + $data = array( | |
| 125 | + 'form_id' => absint( $form_data['wpuf_form_id'] ), | |
| 126 | + 'post_title' => sanitize_text_field( $form_data['post_title'] ), | |
| 127 | + 'form_fields' => $form_fields, | |
| 128 | + 'form_settings' => $settings, | |
| 129 | + 'form_settings_key' => isset( $form_data['form_settings_key'] ) ? $form_data['form_settings_key'] : '', | |
| 130 | + 'notifications' => $notifications, | |
| 131 | + 'integrations' => $integrations | |
| 132 | + ); | |
| 133 | + | |
| 134 | + $form_fields = weforms()->form->save( $data ); | |
| 135 | + | |
| 136 | + wp_send_json_success( array( 'form_fields' => $form_fields ) ); | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * Get all contact forms | |
| 141 | + * | |
| 142 | + * @return void | |
| 143 | + */ | |
| 144 | + public function get_contact_forms() { | |
| 145 | + check_ajax_referer( 'weforms' ); | |
| 146 | + | |
| 147 | + $this->check_admin(); | |
| 148 | + | |
| 149 | + $args = array( | |
| 150 | + 'posts_per_page' => isset( $_POST['posts_per_page'] ) ? intval( $_POST['posts_per_page'] ) : 10, | |
| 151 | + 'paged' => isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1, | |
| 152 | + 'order' => 'DESC', | |
| 153 | + 'orderby' => 'post_date' | |
| 154 | + ); | |
| 155 | + | |
| 156 | + $args = apply_filters( 'weforms_ajax_get_contact_forms_args', $args ); | |
| 157 | + | |
| 158 | + $contact_forms = weforms()->form->get_forms( $args ); | |
| 159 | + | |
| 160 | + array_map( | |
| 161 | + function( $form ) { | |
| 162 | + $form->entries = $form->num_form_entries(); | |
| 163 | + $form->views = $form->num_form_views(); | |
| 164 | + $form->payments = $form->num_form_payments(); | |
| 165 | + }, $contact_forms['forms'] | |
| 166 | + ); | |
| 167 | + | |
| 168 | + $contact_forms = $this->filter_contact_forms( $contact_forms ); | |
| 169 | + $contact_forms = apply_filters( 'weforms_ajax_get_contact_forms', $contact_forms ); | |
| 170 | + | |
| 171 | + wp_send_json_success( $contact_forms ); | |
| 172 | + } | |
| 173 | + | |
| 174 | + /** | |
| 175 | + * Filter | |
| 176 | + * | |
| 177 | + * @return void | |
| 178 | + */ | |
| 179 | + public function filter_contact_forms( &$contact_forms ) { | |
| 180 | + | |
| 181 | + if ( isset( $_REQUEST['filter'] ) && $_REQUEST['filter'] == 'entries' ) { | |
| 182 | + foreach ( $contact_forms['forms'] as $key => &$form ) { | |
| 183 | + if ( isset( $form->entries ) && ! $form->entries ) { | |
| 184 | + unset( $contact_forms['forms'][ $key ] ); | |
| 185 | + } | |
| 186 | + } | |
| 187 | + | |
| 188 | + $contact_forms['meta']['total'] = count( $contact_forms['forms'] ); | |
| 189 | + } | |
| 190 | + | |
| 191 | + return $contact_forms; | |
| 192 | + } | |
| 193 | + | |
| 194 | + /** | |
| 195 | + * Get the names of contact forms for generating dropdown | |
| 196 | + * | |
| 197 | + * @return void | |
| 198 | + */ | |
| 199 | + public function get_contact_form_names() { | |
| 200 | + check_ajax_referer( 'weforms' ); | |
| 201 | + | |
| 202 | + $this->check_admin(); | |
| 203 | + | |
| 204 | + $contact_forms = weforms()->form->all(); | |
| 205 | + $response = array(); | |
| 206 | + | |
| 207 | + foreach ( $contact_forms['forms'] as $form ) { | |
| 208 | + $response[] = array( | |
| 209 | + 'id' => $form->get_id(), | |
| 210 | + 'title' => $form->get_name() . ' (#' . $form->get_id() . ')' | |
| 211 | + ); | |
| 212 | + } | |
| 213 | + | |
| 214 | + wp_send_json_success( $response ); | |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * Create a form | |
| 219 | + * | |
| 220 | + * @return void | |
| 221 | + */ | |
| 222 | + public function create_form() { | |
| 223 | + check_ajax_referer( 'weforms' ); | |
| 224 | + | |
| 225 | + $this->check_admin(); | |
| 226 | + | |
| 227 | + $form_name = isset( $_POST['form_name'] ) ? sanitize_text_field( $_POST['form_name'] ) : ''; | |
| 228 | + | |
| 229 | + if ( empty( $form_name ) ) { | |
| 230 | + wp_send_json_error( __( 'Please provide a form name', 'weforms' ) ); | |
| 231 | + } | |
| 232 | + | |
| 233 | + $form_id = weforms()->form->create( $form_name ); | |
| 234 | + | |
| 235 | + if ( is_wp_error( $form_id ) ) { | |
| 236 | + wp_send_json_error( $form_id->get_error_message() ); | |
| 237 | + } | |
| 238 | + | |
| 239 | + wp_send_json_success( array( | |
| 240 | + 'form_id' => $form_id, | |
| 241 | + 'form_name' => $form_name | |
| 242 | + ) ); | |
| 243 | + } | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * Delete a form | |
| 247 | + * | |
| 248 | + * @return void | |
| 249 | + */ | |
| 250 | + public function delete_form() { | |
| 251 | + check_ajax_referer( 'weforms' ); | |
| 252 | + | |
| 253 | + $this->check_admin(); | |
| 254 | + | |
| 255 | + $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; | |
| 256 | + | |
| 257 | + if ( ! $form_id ) { | |
| 258 | + wp_send_json_error( __( 'No form id provided!', 'weforms' ) ); | |
| 259 | + } | |
| 260 | + | |
| 261 | + weforms()->form->delete( $form_id ); | |
| 262 | + | |
| 263 | + wp_send_json_success(); | |
| 264 | + } | |
| 265 | + | |
| 266 | + public function delete_form_bulk() { | |
| 267 | + check_ajax_referer( 'weforms' ); | |
| 268 | + | |
| 269 | + $this->check_admin(); | |
| 270 | + | |
| 271 | + $form_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array(); | |
| 272 | + | |
| 273 | + if ( ! $form_ids ) { | |
| 274 | + wp_send_json_error( __( 'No form ids provided!', 'weforms' ) ); | |
| 275 | + } | |
| 276 | + | |
| 277 | + foreach ( $form_ids as $form_id ) { | |
| 278 | + weforms()->form->delete( $form_id ); | |
| 279 | + } | |
| 280 | + | |
| 281 | + wp_send_json_success(); | |
| 282 | + } | |
| 283 | + | |
| 284 | + /** | |
| 285 | + * Duplicate a form | |
| 286 | + * | |
| 287 | + * @return voiud | |
| 288 | + */ | |
| 289 | + public function duplicate_form() { | |
| 290 | + check_ajax_referer( 'weforms' ); | |
| 291 | + | |
| 292 | + $this->check_admin(); | |
| 293 | + | |
| 294 | + $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; | |
| 295 | + | |
| 296 | + $form = weforms()->form->duplicate( $form_id ); | |
| 297 | + | |
| 298 | + wp_send_json_success( $form ); | |
| 299 | + } | |
| 300 | + | |
| 301 | + /** | |
| 302 | + * Create form from a template | |
| 303 | + * | |
| 304 | + * @return void | |
| 305 | + */ | |
| 306 | + public function create_form_from_template() { | |
| 307 | + check_ajax_referer( 'weforms' ); | |
| 308 | + | |
| 309 | + $template = isset( $_REQUEST['template'] ) ? sanitize_text_field( $_REQUEST['template'] ) : ''; | |
| 310 | + | |
| 311 | + $form_id = weforms()->templates->create( $template ); | |
| 312 | + | |
| 313 | + if ( is_wp_error( $form_id ) ) { | |
| 314 | + wp_send_json_error( __( 'Could not create the form', 'weforms' ) ); | |
| 315 | + } | |
| 316 | + | |
| 317 | + wp_send_json_success( array( | |
| 318 | + 'id' => $form_id | |
| 319 | + ) ); | |
| 320 | + } | |
| 321 | + | |
| 322 | + /** | |
| 323 | + * Save the weForms settings | |
| 324 | + * | |
| 325 | + * @return void | |
| 326 | + */ | |
| 327 | + public function save_settings() { | |
| 328 | + check_ajax_referer( 'weforms' ); | |
| 329 | + | |
| 330 | + $this->check_admin(); | |
| 331 | + | |
| 332 | + $requires_wpuf_update = false; | |
| 333 | + $wpuf_update_array = array(); | |
| 334 | + $settings = (array) json_decode( wp_unslash( $_POST['settings'] ) ); | |
| 335 | + | |
| 336 | + update_option( 'weforms_settings', $settings ); | |
| 337 | + | |
| 338 | + // wpuf settings sync | |
| 339 | + if ( isset( $settings['gmap_api'] ) ) { | |
| 340 | + $requires_wpuf_update = true; | |
| 341 | + $wpuf_update_array['gmap_api_key'] = $settings['gmap_api']; | |
| 342 | + } | |
| 343 | + | |
| 344 | + if ( isset( $settings['recaptcha'] ) ) { | |
| 345 | + $requires_wpuf_update = true; | |
| 346 | + $wpuf_update_array['recaptcha_public'] = $settings['recaptcha']->key; | |
| 347 | + $wpuf_update_array['recaptcha_private'] = $settings['recaptcha']->secret; | |
| 348 | + } | |
| 349 | + | |
| 350 | + if ( isset( $settings['no_conflict'] ) ) { | |
| 351 | + $requires_wpuf_update = true; | |
| 352 | + $wpuf_update_array['no_conflict'] = $settings['no_conflict']; | |
| 353 | + } | |
| 354 | + | |
| 355 | + if ( $requires_wpuf_update ) { | |
| 356 | + $wpuf_settings = get_option( 'wpuf_general', array() ); | |
| 357 | + | |
| 358 | + $wpuf_settings = array_merge( $wpuf_settings, $wpuf_update_array ); | |
| 359 | + update_option( 'wpuf_general', $wpuf_settings ); | |
| 360 | + } | |
| 361 | + | |
| 362 | + do_action( 'weforms_save_settings', $settings ); | |
| 363 | + | |
| 364 | + $settings = apply_filters( 'weforms_after_save_settings', $settings ); | |
| 365 | + | |
| 366 | + wp_send_json_success( $settings ); | |
| 367 | + } | |
| 368 | + | |
| 369 | + /** | |
| 370 | + * Get the weForms Settings | |
| 371 | + * | |
| 372 | + * @return void | |
| 373 | + */ | |
| 374 | + public function get_settings() { | |
| 375 | + check_ajax_referer( 'weforms' ); | |
| 376 | + | |
| 377 | + $this->check_admin(); | |
| 378 | + | |
| 379 | + $settings = weforms_get_settings(); | |
| 380 | + | |
| 381 | + // checking to prevent js error, will be removed in future | |
| 382 | + if ( ! isset( $settings['credit'] ) ) { | |
| 383 | + $settings['credit'] = false; | |
| 384 | + } | |
| 385 | + | |
| 386 | + if ( ! isset( $settings['permission'] ) ) { | |
| 387 | + $settings['permission'] = 'manage_options'; | |
| 388 | + } | |
| 389 | + | |
| 390 | + wp_send_json_success( $settings ); | |
| 391 | + } | |
| 392 | + | |
| 393 | + /** | |
| 394 | + * Get all entries | |
| 395 | + * | |
| 396 | + * @return void | |
| 397 | + */ | |
| 398 | + public function get_entries() { | |
| 399 | + check_ajax_referer( 'weforms' ); | |
| 400 | + | |
| 401 | + $this->check_admin(); | |
| 402 | + | |
| 403 | + $form_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; | |
| 404 | + $current_page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1; | |
| 405 | + $status = isset( $_REQUEST['status'] ) ? $_REQUEST['status'] : 'publish'; | |
| 406 | + $per_page = 20; | |
| 407 | + $offset = ( $current_page - 1 ) * $per_page; | |
| 408 | + | |
| 409 | + if ( ! $form_id ) { | |
| 410 | + wp_send_json_error( __( 'No form id provided!', 'weforms' ) ); | |
| 411 | + } | |
| 412 | + | |
| 413 | + $entries = weforms_get_form_entries( | |
| 414 | + $form_id, array( | |
| 415 | + 'number' => $per_page, | |
| 416 | + 'offset' => $offset, | |
| 417 | + 'status' => $status, | |
| 418 | + ) | |
| 419 | + ); | |
| 420 | + | |
| 421 | + $columns = weforms_get_entry_columns( $form_id ); | |
| 422 | + $total_entries = weforms_count_form_entries( $form_id, $status ); | |
| 423 | + | |
| 424 | + array_map( | |
| 425 | + function( $entry ) use ( $columns ) { | |
| 426 | + $entry_id = $entry->id; | |
| 427 | + $entry->fields = array(); | |
| 428 | + | |
| 429 | + foreach ( $columns as $meta_key => $label ) { | |
| 430 | + $value = weforms_get_entry_meta( $entry_id, $meta_key, true ); | |
| 431 | + $entry->fields[ $meta_key ] = str_replace( WeForms::$field_separator, ' ', $value ); | |
| 432 | + } | |
| 433 | + }, $entries | |
| 434 | + ); | |
| 435 | + | |
| 436 | + $entries = apply_filters( 'weforms_get_entries', $entries, $form_id ); | |
| 437 | + | |
| 438 | + $response = array( | |
| 439 | + 'columns' => $columns, | |
| 440 | + 'entries' => $entries, | |
| 441 | + 'form_title' => get_post_field( 'post_title', $form_id ), | |
| 442 | + 'pagination' => array( | |
| 443 | + 'total' => $total_entries, | |
| 444 | + 'per_page' => $per_page, | |
| 445 | + 'pages' => ceil( $total_entries / $per_page ), | |
| 446 | + 'current' => $current_page | |
| 447 | + ), | |
| 448 | + 'meta' => array( | |
| 449 | + 'total' => weforms_count_form_entries( $form_id ), | |
| 450 | + 'totalTrash' => weforms_count_form_entries( $form_id, 'trash' ), | |
| 451 | + ), | |
| 452 | + ); | |
| 453 | + | |
| 454 | + wp_send_json_success( $response ); | |
| 455 | + } | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + /** | |
| 460 | + * Get an entry details | |
| 461 | + * | |
| 462 | + * @return void | |
| 463 | + */ | |
| 464 | + public function get_entry_detail() { | |
| 465 | + check_ajax_referer( 'weforms' ); | |
| 466 | + | |
| 467 | + $this->check_admin(); | |
| 468 | + | |
| 469 | + $form_id = isset( $_REQUEST['form_id'] ) ? intval( $_REQUEST['form_id'] ) : 0; | |
| 470 | + $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; | |
| 471 | + | |
| 472 | + $form = weforms()->form->get( $form_id ); | |
| 473 | + $form_settings = $form->get_settings(); | |
| 474 | + $entry = $form->entries()->get( $entry_id ); | |
| 475 | + $fields = $entry->get_fields(); | |
| 476 | + $metadata = $entry->get_metadata(); | |
| 477 | + $payment = $entry->get_payment_data(); | |
| 478 | + | |
| 479 | + if ( isset( $payment->payment_data ) && is_serialized( $payment->payment_data ) ) { | |
| 480 | + $payment->payment_data = unserialize( $payment->payment_data ); | |
| 481 | + } | |
| 482 | + | |
| 483 | + if ( false === $fields ) { | |
| 484 | + wp_send_json_error( __( 'No form fields found!', 'weforms' ) ); | |
| 485 | + } | |
| 486 | + | |
| 487 | + if ( sizeof( $fields ) < 1 ) { | |
| 488 | + $fields[] = array( | |
| 489 | + 'label' => __( 'No form fields found!', 'weforms' ) | |
| 490 | + ); | |
| 491 | + } | |
| 492 | + | |
| 493 | + $has_empty = false; | |
| 494 | + $answers = array(); | |
| 495 | + $respondentPoints = isset($form_settings['total_points']) ? floatval( $form_settings['total_points'] ) : 0 ; | |
| 496 | + | |
| 497 | + foreach ( $fields as $key => $field ) { | |
| 498 | + | |
| 499 | + if ( $form_settings['quiz_form'] == 'yes' ) { | |
| 500 | + $selectedAnswers = isset($field['selected_answers']) ? $field['selected_answers'] : ''; | |
| 501 | + $givenAnswer = isset($field['value']) ? $field['value'] : ''; | |
| 502 | + $options = isset($field['options']) ? $field['options'] : ''; | |
| 503 | + $template = $field['template']; | |
| 504 | + $fieldPoints = isset($field['points']) ? floatval( $field['points'] ) : 0; | |
| 505 | + | |
| 506 | + if ( $template == 'radio_field' || $template == 'dropdown_field' ) { | |
| 507 | + $answers[$field['name']] = true; | |
| 508 | + | |
| 509 | + if ( empty($givenAnswer) ) { | |
| 510 | + $answers[$field['name']] = false; | |
| 511 | + $respondentPoints -= $fieldPoints; | |
| 512 | + }else { | |
| 513 | + foreach ($options as $key => $value) { | |
| 514 | + if ( $givenAnswer == $value ) { | |
| 515 | + if ( $key != $selectedAnswers ) { | |
| 516 | + $answers[$field['name']] = false; | |
| 517 | + $respondentPoints -= $fieldPoints; | |
| 518 | + } | |
| 519 | + } | |
| 520 | + } | |
| 521 | + } | |
| 522 | + } elseif ( $template == 'checkbox_field' || $template == 'multiple_select' ) { | |
| 523 | + $answers[$field['name']] = true; | |
| 524 | + $userAnswer = []; | |
| 525 | + | |
| 526 | + foreach ($options as $key => $value) { | |
| 527 | + foreach ($givenAnswer as $answer) { | |
| 528 | + if ($value == $answer) { | |
| 529 | + $userAnswer[] = $key; | |
| 530 | + } | |
| 531 | + } | |
| 532 | + } | |
| 533 | + | |
| 534 | + $userAnswer = implode('|', $userAnswer); | |
| 535 | + $rightAnswers = implode('|', $selectedAnswers); | |
| 536 | + | |
| 537 | + if ( $userAnswer != $rightAnswers || empty($userAnswer) ) { | |
| 538 | + $answers[$field['name']] = false; | |
| 539 | + $respondentPoints -= $fieldPoints; | |
| 540 | + } | |
| 541 | + } | |
| 542 | + } elseif ( empty( $field['value'] ) ) { | |
| 543 | + $has_empty = true; | |
| 544 | + break; | |
| 545 | + } | |
| 546 | + | |
| 547 | + } | |
| 548 | + | |
| 549 | + $response = array( | |
| 550 | + 'form_fields' => $fields, | |
| 551 | + 'form_settings' => $form_settings, | |
| 552 | + 'meta_data' => $metadata, | |
| 553 | + 'payment_data' => $payment, | |
| 554 | + 'has_empty' => $has_empty, | |
| 555 | + 'respondent_points' => $respondentPoints, | |
| 556 | + 'answers' => $answers, | |
| 557 | + ); | |
| 558 | + | |
| 559 | + wp_send_json_success( $response ); | |
| 560 | + } | |
| 561 | + | |
| 562 | + /** | |
| 563 | + * Trash an entry | |
| 564 | + * | |
| 565 | + * @return void | |
| 566 | + */ | |
| 567 | + public function trash_entry() { | |
| 568 | + check_ajax_referer( 'weforms' ); | |
| 569 | + | |
| 570 | + $this->check_admin(); | |
| 571 | + | |
| 572 | + $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; | |
| 573 | + | |
| 574 | + weforms_change_entry_status( $entry_id, 'trash' ); | |
| 575 | + wp_send_json_success(); | |
| 576 | + } | |
| 577 | + | |
| 578 | + /** | |
| 579 | + * Trash an entry | |
| 580 | + * | |
| 581 | + * @return void | |
| 582 | + */ | |
| 583 | + public function delete_entry() { | |
| 584 | + check_ajax_referer( 'weforms' ); | |
| 585 | + | |
| 586 | + $this->check_admin(); | |
| 587 | + | |
| 588 | + $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; | |
| 589 | + | |
| 590 | + weforms_delete_entry( $entry_id ); | |
| 591 | + | |
| 592 | + wp_send_json_success(); | |
| 593 | + } | |
| 594 | + | |
| 595 | + /** | |
| 596 | + * Restore Entry | |
| 597 | + * | |
| 598 | + * @return void | |
| 599 | + */ | |
| 600 | + public function restore_entry() { | |
| 601 | + check_ajax_referer( 'weforms' ); | |
| 602 | + | |
| 603 | + $this->check_admin(); | |
| 604 | + | |
| 605 | + $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0; | |
| 606 | + | |
| 607 | + weforms_change_entry_status( $entry_id, 'publish' ); | |
| 608 | + wp_send_json_success(); | |
| 609 | + } | |
| 610 | + | |
| 611 | + /** | |
| 612 | + * Bulk trash entries | |
| 613 | + * | |
| 614 | + * @return void | |
| 615 | + */ | |
| 616 | + public function bulk_delete_entry() { | |
| 617 | + check_ajax_referer( 'weforms' ); | |
| 618 | + | |
| 619 | + $this->check_admin(); | |
| 620 | + | |
| 621 | + $entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array(); | |
| 622 | + $permanent = isset( $_POST['permanent'] ) && ( $_POST['permanent'] ) ? true : false; | |
| 623 | + | |
| 624 | + if ( ! $entry_ids ) { | |
| 625 | + wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) ); | |
| 626 | + } | |
| 627 | + | |
| 628 | + foreach ( $entry_ids as $entry_id ) { | |
| 629 | + if ( $permanent ) { | |
| 630 | + weforms_delete_entry( $entry_id ); | |
| 631 | + } else { | |
| 632 | + weforms_change_entry_status( $entry_id, 'trash' ); | |
| 633 | + } | |
| 634 | + } | |
| 635 | + | |
| 636 | + wp_send_json_success(); | |
| 637 | + } | |
| 638 | + | |
| 639 | + /** | |
| 640 | + * Bulk trash entries | |
| 641 | + * | |
| 642 | + * @return void | |
| 643 | + */ | |
| 644 | + public function bulk_restore_entry() { | |
| 645 | + check_ajax_referer( 'weforms' ); | |
| 646 | + | |
| 647 | + $this->check_admin(); | |
| 648 | + | |
| 649 | + $entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array(); | |
| 650 | + | |
| 651 | + if ( ! $entry_ids ) { | |
| 652 | + wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) ); | |
| 653 | + } | |
| 654 | + | |
| 655 | + foreach ( $entry_ids as $entry_id ) { | |
| 656 | + weforms_change_entry_status( $entry_id, 'publish' ); | |
| 657 | + } | |
| 658 | + | |
| 659 | + wp_send_json_success(); | |
| 660 | + } | |
| 661 | + | |
| 662 | + /** | |
| 663 | + * Handle the frontend submission | |
| 664 | + * | |
| 665 | + * @return void | |
| 666 | + */ | |
| 667 | + public function handle_frontend_submission() { | |
| 668 | + check_ajax_referer( 'wpuf_form_add' ); | |
| 669 | + | |
| 670 | + $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; | |
| 671 | + $page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0; | |
| 672 | + | |
| 673 | + $form = weforms()->form->get( $form_id ); | |
| 674 | + $form_settings = $form->get_settings(); | |
| 675 | + $form_fields = $form->get_fields(); | |
| 676 | + $entry_fields = $form->prepare_entries(); | |
| 677 | + $form_entries = weforms_get_form_entries( $form_id, array( 'number' => '', 'offset' => '' ) ); | |
| 678 | + | |
| 679 | + if ( $form_fields && count( $form_entries ) && count( $entry_fields ) ) { | |
| 680 | + | |
| 681 | + foreach ( $entry_fields as $field_key => $field_value ) { | |
| 682 | + $duplicate_check = false; | |
| 683 | + $field_label = 'This'; | |
| 684 | + | |
| 685 | + foreach ( $form_fields as $form_field ) { | |
| 686 | + 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'] ) { | |
| 687 | + $duplicate_check = true; | |
| 688 | + $field_label = $form_field['label']; | |
| 689 | + } | |
| 690 | + } | |
| 691 | + | |
| 692 | + if ( $duplicate_check ) { | |
| 693 | + | |
| 694 | + foreach ( $form_entries as $entry ) { | |
| 695 | + $existing = weforms_get_entry_meta( $entry->id, $field_key, true ); | |
| 696 | + | |
| 697 | + if ( $existing && $field_value == $existing ) { | |
| 698 | + wp_send_json( array( | |
| 699 | + 'success' => false, | |
| 700 | + 'error' => sprintf( __( '"%s" field requires a unique entry and "%s" has already been used.', 'weforms' ), $field_label, $field_value ) | |
| 701 | + ) ); | |
| 702 | + } | |
| 703 | + } | |
| 704 | + } | |
| 705 | + } | |
| 706 | + } | |
| 707 | + | |
| 708 | + if ( ! $form_fields ) { | |
| 709 | + wp_send_json( array( | |
| 710 | + 'success' => false, | |
| 711 | + 'error' => __( 'No form field was found.', 'weforms' ), | |
| 712 | + ) ); | |
| 713 | + } | |
| 714 | + | |
| 715 | + if ( $form->has_field( 'recaptcha' ) ) { | |
| 716 | + $this->validate_reCaptcha(); | |
| 717 | + } | |
| 718 | + | |
| 719 | + // vaidate submission | |
| 720 | + $this->validate_submission( $entry_fields, $form, $form_settings, $form_fields ); | |
| 721 | + | |
| 722 | + $entry_fields = apply_filters( 'weforms_before_entry_submission', $entry_fields, $form, $form_settings, $form_fields ); | |
| 723 | + | |
| 724 | + $entry_id = weforms_insert_entry( array( | |
| 725 | + 'form_id' => $form_id | |
| 726 | + ), $entry_fields ); | |
| 727 | + | |
| 728 | + if ( is_wp_error( $entry_id ) ) { | |
| 729 | + wp_send_json( array( | |
| 730 | + 'success' => false, | |
| 731 | + 'error' => $entry_id->get_error_message(), | |
| 732 | + ) ); | |
| 733 | + } | |
| 734 | + | |
| 735 | + // redirect URL | |
| 736 | + $show_message = false; | |
| 737 | + $redirect_to = false; | |
| 738 | + | |
| 739 | + if ( $form_settings['redirect_to'] == 'page' ) { | |
| 740 | + $redirect_to = get_permalink( $form_settings['page_id'] ); | |
| 741 | + } elseif ( $form_settings['redirect_to'] == 'url' ) { | |
| 742 | + $redirect_to = $form_settings['url']; | |
| 743 | + } elseif ( $form_settings['redirect_to'] == 'same' ) { | |
| 744 | + $show_message = true; | |
| 745 | + } else { | |
| 746 | + $redirect_to = get_permalink( $post_id ); | |
| 747 | + } | |
| 748 | + | |
| 749 | + // Fire a hook for integration | |
| 750 | + do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings ); | |
| 751 | + | |
| 752 | + $field_search = $field_replace = array(); | |
| 753 | + | |
| 754 | + foreach ( $form_fields as $r_field ) { | |
| 755 | + $field_search[] = '{'.$r_field['name'].'}'; | |
| 756 | + if ( $r_field['template'] == 'name_field' ) { | |
| 757 | + $field_replace[] = implode( ' ' , explode( '|', $entry_fields[$r_field['name']] )); | |
| 758 | + } else { | |
| 759 | + $field_replace[] = $entry_fields[$r_field['name']]; | |
| 760 | + } | |
| 761 | + } | |
| 762 | + $message = str_replace( $field_search, $field_replace, $form_settings['message'] ); | |
| 763 | + | |
| 764 | + // send the response | |
| 765 | + $response = apply_filters( 'weforms_entry_submission_response', array( | |
| 766 | + 'success' => true, | |
| 767 | + 'redirect_to' => $redirect_to, | |
| 768 | + 'show_message' => $show_message, | |
| 769 | + 'message' => $message, | |
| 770 | + 'data' => $_POST, | |
| 771 | + 'form_id' => $form_id, | |
| 772 | + 'entry_id' => $entry_id, | |
| 773 | + ) ); | |
| 774 | + | |
| 775 | + $notification = new WeForms_Notification( array( | |
| 776 | + 'form_id' => $form_id, | |
| 777 | + 'page_id' => $page_id, | |
| 778 | + 'entry_id' => $entry_id | |
| 779 | + ) ); | |
| 780 | + | |
| 781 | + $notification->send_notifications(); | |
| 782 | + | |
| 783 | + weforms_clear_buffer(); | |
| 784 | + wp_send_json( $response ); | |
| 785 | + } | |
| 786 | + | |
| 787 | + /** | |
| 788 | + * reCaptcha Validation | |
| 789 | + * | |
| 790 | + * @return void | |
| 791 | + */ | |
| 792 | + function validate_reCaptcha() { | |
| 793 | + | |
| 794 | + if ( class_exists( 'WPUF_ReCaptcha' ) ) { | |
| 795 | + $recaptcha_class = 'WPUF_Recaptcha'; | |
| 796 | + } else { | |
| 797 | + require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib.php'; | |
| 798 | + require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib_noCaptcha.php'; | |
| 799 | + $recaptcha_class = 'Weforms_ReCaptcha'; | |
| 800 | + } | |
| 801 | + | |
| 802 | + $invisible = isset( $_POST['g-recaptcha-response'] ) ? false : true; | |
| 803 | + | |
| 804 | + $recaptcha_settings = weforms_get_settings( 'recaptcha' ); | |
| 805 | + $secret = isset( $recaptcha_settings->secret ) ? $recaptcha_settings->secret : ''; | |
| 806 | + | |
| 807 | + if ( ! $invisible ) { | |
| 808 | + | |
| 809 | + $response = null; | |
| 810 | + $reCaptcha = new $recaptcha_class( $secret ); | |
| 811 | + | |
| 812 | + $resp = $reCaptcha->verifyResponse( | |
| 813 | + $_SERVER['REMOTE_ADDR'], | |
| 814 | + $_POST['g-recaptcha-response'] | |
| 815 | + ); | |
| 816 | + | |
| 817 | + if ( ! $resp->success ) { | |
| 818 | + wp_send_json( array( | |
| 819 | + 'success' => false, | |
| 820 | + 'error' => __( 'reCAPTCHA validation failed', 'weforms' ), | |
| 821 | + ) ); | |
| 822 | + } | |
| 823 | + } else { | |
| 824 | + | |
| 825 | + $recap_challenge = isset( $_POST['recaptcha_challenge_field'] ) ? $_POST['recaptcha_challenge_field'] : ''; | |
| 826 | + $recap_response = isset( $_POST['recaptcha_response_field'] ) ? $_POST['recaptcha_response_field'] : ''; | |
| 827 | + | |
| 828 | + $resp = recaptcha_check_answer( $secret, $_SERVER['REMOTE_ADDR'], $recap_challenge, $recap_response ); | |
| 829 | + | |
| 830 | + if ( ! $resp->is_valid ) { | |
| 831 | + | |
| 832 | + ob_clean(); | |
| 833 | + | |
| 834 | + wp_send_json( array( | |
| 835 | + 'success' => false, | |
| 836 | + 'error' => __( 'reCAPTCHA validation failed', 'weforms' ), | |
| 837 | + ) ); | |
| 838 | + } | |
| 839 | + } | |
| 840 | + | |
| 841 | + } | |
| 842 | + | |
| 843 | + /** | |
| 844 | + * Validate submission | |
| 845 | + * | |
| 846 | + * @param array $entry_fields | |
| 847 | + * @param object $form | |
| 848 | + * @param array $form_settings | |
| 849 | + * @param array $form_fields | |
| 850 | + * | |
| 851 | + * @return bool|json | |
| 852 | + */ | |
| 853 | + function validate_submission( $entry_fields, $form, $form_settings, $form_fields ) { | |
| 854 | + | |
| 855 | + foreach ( $form_fields as $key => $field ) { | |
| 856 | + | |
| 857 | + //skip custom html field as it is not saved | |
| 858 | + if ( 'custom_html' == $field['name'] ) | |
| 859 | + continue; | |
| 860 | + | |
| 861 | + $value = $entry_fields[ $field['name'] ]; | |
| 862 | + | |
| 863 | + // if ( isset( $field['required'] ) && $field['required'] && empty( $value ) ) { | |
| 864 | + // wp_send_json( array( | |
| 865 | + // 'success' => false, | |
| 866 | + // 'error' => __( sprintf( '%s field is required', $field['label'] ), 'weforms' ), | |
| 867 | + // ) ); | |
| 868 | + // } | |
| 869 | + if ( 'single_product' === $field['template'] ) { | |
| 870 | + | |
| 871 | + if ( ! $value ) { | |
| 872 | + $value = array(); | |
| 873 | + } | |
| 874 | + | |
| 875 | + $value['price'] = isset( $value['price'] ) ? floatval( $value['price'] ) : 0; | |
| 876 | + $value['quantity'] = isset( $value['quantity'] ) ? floatval( $value['quantity'] ) : 0; | |
| 877 | + $quantity = isset( $field['quantity'] ) ? $field['quantity'] : array(); | |
| 878 | + $price = isset( $field['price'] ) ? $field['price'] : array(); | |
| 879 | + | |
| 880 | + if ( isset( $price['is_flexible'] ) && $price['is_flexible'] ) { | |
| 881 | + | |
| 882 | + $min = isset( $price['min'] ) ? floatval( $price['min'] ) : 0; | |
| 883 | + $max = isset( $price['max'] ) ? floatval( $price['max'] ) : 0; | |
| 884 | + | |
| 885 | + if ( $value['price'] < $min ) { | |
| 886 | + | |
| 887 | + wp_send_json( array( | |
| 888 | + 'success' => false, | |
| 889 | + 'error' => __( sprintf( | |
| 890 | + '%s price must be equal or greater than %s', | |
| 891 | + $field['weforms'], | |
| 892 | + $min), | |
| 893 | + 'weforms' ), | |
| 894 | + ) ); | |
| 895 | + } | |
| 896 | + | |
| 897 | + if ( $max && $value['price'] > $max ) { | |
| 898 | + | |
| 899 | + wp_send_json( array( | |
| 900 | + 'success' => false, | |
| 901 | + 'error' => __( sprintf( | |
| 902 | + '%s price must be equal or less than %s', | |
| 903 | + $field['weforms'], | |
| 904 | + $max), | |
| 905 | + 'weforms' ), | |
| 906 | + ) ); | |
| 907 | + } | |
| 908 | + } | |
| 909 | + | |
| 910 | + if ( isset( $quantity['status'] ) && $quantity['status'] ) { | |
| 911 | + | |
| 912 | + $min = isset( $quantity['min'] ) ? floatval( $quantity['min'] ) : 0; | |
| 913 | + $max = isset( $quantity['max'] ) ? floatval( $quantity['max'] ) : 0; | |
| 914 | + | |
| 915 | + if ( $value['quantity'] < $min ) { | |
| 916 | + | |
| 917 | + wp_send_json( array( | |
| 918 | + 'success' => false, | |
| 919 | + 'error' => __( sprintf( | |
| 920 | + '%s quantity must be equal or greater than %s', | |
| 921 | + $field['weforms'], | |
| 922 | + $min), | |
| 923 | + 'weforms' ), | |
| 924 | + ) ); | |
| 925 | + } | |
| 926 | + | |
| 927 | + if ( $max && $value['quantity'] > $max ) { | |
| 928 | + | |
| 929 | + wp_send_json( array( | |
| 930 | + 'success' => false, | |
| 931 | + 'error' => __( sprintf( | |
| 932 | + '%s quantity must be equal or less than %s', | |
| 933 | + $field['weforms'], | |
| 934 | + $max), | |
| 935 | + 'weforms' ), | |
| 936 | + ) ); | |
| 937 | + } | |
| 938 | + } | |
| 939 | + } | |
| 940 | + } | |
| 941 | + } | |
| 942 | + | |
| 943 | + public static function prepare_meta_fields( $meta_vars ) { | |
| 944 | + // loop through custom fields | |
| 945 | + // skip files, put in a key => value paired array for later executation | |
| 946 | + // process repeatable fields separately | |
| 947 | + // if the input is array type, implode with separator in a field | |
| 948 | + $files = array(); | |
| 949 | + $meta_key_value = array(); | |
| 950 | + $multi_repeated = array(); // multi repeated fields will in sotre duplicated meta key | |
| 951 | + | |
| 952 | + foreach ( $meta_vars as $key => $value ) { | |
| 953 | + | |
| 954 | + switch ( $value['template'] ) { | |
| 955 | + | |
| 956 | + // put files in a separate array, we'll process it later | |
| 957 | + case 'file_upload': | |
| 958 | + case 'image_upload': | |
| 959 | + $files[] = array( | |
| 960 | + 'name' => $value['name'], | |
| 961 | + 'value' => isset( $_POST['wpuf_files'][ $value['name'] ] ) ? $_POST['wpuf_files'][ $value['name'] ] : array(), | |
| 962 | + 'count' => $value['count'] | |
| 963 | + ); | |
| 964 | + break; | |
| 965 | + | |
| 966 | + case 'repeat_field': | |
| 967 | + // if it is a multi column repeat field | |
| 968 | + if ( isset( $value['multiple'] ) && $value['multiple'] == 'true' ) { | |
| 969 | + | |
| 970 | + // if there's any items in the array, process it | |
| 971 | + if ( $_POST[ $value['name'] ] ) { | |
| 972 | + | |
| 973 | + $ref_arr = array(); | |
| 974 | + $cols = count( $value['columns'] ); | |
| 975 | + $first = array_shift( array_values( $_POST[ $value['name'] ] ) ); // first element | |
| 976 | + $rows = count( $first ); | |
| 977 | + | |
| 978 | + // loop through columns | |
| 979 | + for ( $i = 0; $i < $rows; $i++ ) { | |
| 980 | + | |
| 981 | + // loop through the rows and store in a temp array | |
| 982 | + $temp = array(); | |
| 983 | + for ( $j = 0; $j < $cols; $j++ ) { | |
| 984 | + | |
| 985 | + $temp[] = $_POST[ $value['name'] ][ $j ][ $i ]; | |
| 986 | + } | |
| 987 | + | |
| 988 | + // store all fields in a row with WeForms::$field_separator separated | |
| 989 | + $ref_arr[] = implode( WeForms::$field_separator, $temp ); | |
| 990 | + } | |
| 991 | + | |
| 992 | + // now, if we found anything in $ref_arr, store to $multi_repeated | |
| 993 | + if ( $ref_arr ) { | |
| 994 | + $multi_repeated[ $value['name'] ] = array_slice( $ref_arr, 0, $rows ); | |
| 995 | + } | |
| 996 | + } | |
| 997 | + } else { | |
| 998 | + $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, $_POST[ $value['name'] ] ); | |
| 999 | + } | |
| 1000 | + | |
| 1001 | + break; | |
| 1002 | + | |
| 1003 | + case 'address_field': | |
| 1004 | + if ( isset( $_POST[ $value['name'] ] ) && is_array( $_POST[ $value['name'] ] ) ) { | |
| 1005 | + foreach ( $_POST[ $value['name'] ] as $address_field => $field_value ) { | |
| 1006 | + $meta_key_value[ $value['name'] ][ $address_field ] = sanitize_text_field( $field_value ); | |
| 1007 | + } | |
| 1008 | + } | |
| 1009 | + | |
| 1010 | + break; | |
| 1011 | + | |
| 1012 | + case 'text_field': | |
| 1013 | + case 'email_address': | |
| 1014 | + case 'numeric_text_field': | |
| 1015 | + case 'date_field': | |
| 1016 | + $meta_key_value[ $value['name'] ] = sanitize_text_field( trim( $_POST[ $value['name'] ] ) ); | |
| 1017 | + | |
| 1018 | + break; | |
| 1019 | + | |
| 1020 | + case 'textarea_field': | |
| 1021 | + $meta_key_value[ $value['name'] ] = wp_kses_post( $_POST[ $value['name'] ] ); | |
| 1022 | + | |
| 1023 | + break; | |
| 1024 | + | |
| 1025 | + case 'dropdown_field': | |
| 1026 | + case 'radio_field': | |
| 1027 | + $val = $_POST[ $value['name'] ]; | |
| 1028 | + $meta_key_value[ $value['name'] ] = isset( $value['options'][ $val ] ) ? $value['options'][ $val ] : ''; | |
| 1029 | + break; | |
| 1030 | + | |
| 1031 | + case 'multiple_select': | |
| 1032 | + case 'checkbox_field': | |
| 1033 | + $val = ( is_array( $_POST[ $value['name'] ] ) && $_POST[ $value['name'] ] ) ? $_POST[ $value['name'] ] : array(); | |
| 1034 | + $meta_key_value[ $value['name'] ] = $val; | |
| 1035 | + | |
| 1036 | + if ( $val ) { | |
| 1037 | + $new_val = array(); | |
| 1038 | + | |
| 1039 | + foreach ( $val as $option_key ) { | |
| 1040 | + $new_val[] = isset( $value['options'][ $option_key ] ) ? $value['options'][ $option_key ] : ''; | |
| 1041 | + } | |
| 1042 | + | |
| 1043 | + $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, $new_val ); | |
| 1044 | + } | |
| 1045 | + break; | |
| 1046 | + | |
| 1047 | + default: | |
| 1048 | + // if it's an array, implode with this->separator | |
| 1049 | + if ( is_array( $_POST[ $value['name'] ] ) ) { | |
| 1050 | + | |
| 1051 | + $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, $_POST[ $value['name'] ] ); | |
| 1052 | + | |
| 1053 | + } else { | |
| 1054 | + $meta_key_value[ $value['name'] ] = trim( $_POST[ $value['name'] ] ); | |
| 1055 | + } | |
| 1056 | + | |
| 1057 | + break; | |
| 1058 | + } | |
| 1059 | + } //end foreach | |
| 1060 | + | |
| 1061 | + return array( $meta_key_value, $multi_repeated, $files ); | |
| 1062 | + } | |
| 1063 | + | |
| 1064 | + /** | |
| 1065 | + * Import a form from a JSON file | |
| 1066 | + * | |
| 1067 | + * @return void | |
| 1068 | + */ | |
| 1069 | + public function import_form() { | |
| 1070 | + check_ajax_referer( 'weforms' ); | |
| 1071 | + | |
| 1072 | + $this->check_admin(); | |
| 1073 | + | |
| 1074 | + $the_file = isset( $_FILES['importFile'] ) ? $_FILES['importFile'] : false; | |
| 1075 | + | |
| 1076 | + if ( ! $the_file ) { | |
| 1077 | + wp_send_json_error( __( 'No file found to import.', 'weforms' ) ); | |
| 1078 | + } | |
| 1079 | + | |
| 1080 | + $file_ext = pathinfo( $the_file['name'], PATHINFO_EXTENSION ); | |
| 1081 | + | |
| 1082 | + if ( ! class_exists( 'WeForms_Admin_Tools' ) ) { | |
| 1083 | + require_once dirname( __FILE__ ) . '/admin/class-admin-tools.php'; | |
| 1084 | + } | |
| 1085 | + | |
| 1086 | + if ( ( $file_ext == 'json' ) && ( $the_file['size'] < 500000 ) ) { | |
| 1087 | + | |
| 1088 | + $status = WeForms_Admin_Tools::import_json_file( $the_file['tmp_name'] ); | |
| 1089 | + | |
| 1090 | + if ( $status ) { | |
| 1091 | + wp_send_json_success( __( 'The forms have been imported successfully!', 'weforms' ) ); | |
| 1092 | + } else { | |
| 1093 | + wp_send_json_error( __( 'Something went wrong importing the file.', 'weforms' ) ); | |
| 1094 | + } | |
| 1095 | + } else { | |
| 1096 | + wp_send_json_error( __( 'Invalid file or file size too big.', 'weforms' ) ); | |
| 1097 | + } | |
| 1098 | + } | |
| 1099 | + | |
| 1100 | + /** | |
| 1101 | + * Read Log file | |
| 1102 | + * | |
| 1103 | + * @return json | |
| 1104 | + **/ | |
| 1105 | + function get_logs() { | |
| 1106 | + | |
| 1107 | + check_ajax_referer( 'weforms' ); | |
| 1108 | + | |
| 1109 | + $this->check_admin(); | |
| 1110 | + | |
| 1111 | + $file = weforms_log_file_path(); | |
| 1112 | + | |
| 1113 | + if ( ! file_exists( $file ) ) { | |
| 1114 | + return; | |
| 1115 | + } | |
| 1116 | + | |
| 1117 | + $data = file_get_contents( $file ); | |
| 1118 | + $data = explode( "\n", $data ); | |
| 1119 | + $data = array_reverse( $data ); | |
| 1120 | + $data = array_filter( $data ); | |
| 1121 | + | |
| 1122 | + if ( empty( $data ) ) { | |
| 1123 | + return; | |
| 1124 | + } | |
| 1125 | + | |
| 1126 | + $logs = array(); | |
| 1127 | + | |
| 1128 | + foreach ( $data as $key => $row ) { | |
| 1129 | + | |
| 1130 | + preg_match( '/\[(?<time>.+?)\]\[(?<type>.+?)\](?<message>.+)/im', $row, $log ); | |
| 1131 | + | |
| 1132 | + if ( empty( $log['message'] ) ) { | |
| 1133 | + $log = array(); | |
| 1134 | + $log['message'] = ! empty( $log['message'] ) ? $log['message'] : $row; | |
| 1135 | + } | |
| 1136 | + | |
| 1137 | + if ( ! empty( $log['time'] ) ) { | |
| 1138 | + $human_time = human_time_diff( strtotime( $log['time'] ) , current_time( 'timestamp' ) ); | |
| 1139 | + $log['time'] = $human_time ? $human_time . ' ' . __( 'ago', 'weforms' ) : $log['time']; | |
| 1140 | + } | |
| 1141 | + | |
| 1142 | + $logs[] = $log; | |
| 1143 | + } | |
| 1144 | + | |
| 1145 | + wp_send_json_success( $logs ); | |
| 1146 | + } | |
| 1147 | + | |
| 1148 | + /** | |
| 1149 | + * Read Log file | |
| 1150 | + * | |
| 1151 | + * @return json | |
| 1152 | + **/ | |
| 1153 | + function delete_logs() { | |
| 1154 | + | |
| 1155 | + check_ajax_referer( 'weforms' ); | |
| 1156 | + | |
| 1157 | + $this->check_admin(); | |
| 1158 | + | |
| 1159 | + $file = weforms_log_file_path(); | |
| 1160 | + | |
| 1161 | + if ( ! file_exists( $file ) ) { | |
| 1162 | + return; | |
| 1163 | + } | |
| 1164 | + | |
| 1165 | + @unlink( $file ); | |
| 1166 | + | |
| 1167 | + wp_send_json_success(); | |
| 1168 | + } | |
| 1169 | + | |
| 1170 | +} | |