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