| @@ -109,10 +109,8 @@ | ||
| 109 | 109 | $integrations = array(); |
| 110 | 110 | |
| 111 | 111 | if ( isset( $post_data['settings'] ) ) { |
| 112 | 112 | $settings = (array) json_decode( $post_data['settings'] ); |
| 113 | - $settings['message'] = sanitize_text_field( $settings['message'] ); | |
| 114 | - error_log(print_r($settings, true)); | |
| 115 | 113 | } else { |
| 116 | 114 | $settings = isset( $form_data['wpuf_settings'] ) ? $form_data['wpuf_settings'] : []; |
| 117 | 115 | } |
| 118 | 116 | |
| @@ -119,8 +117,11 @@ | ||
| 119 | 117 | if ( isset( $post_data['integrations'] ) ) { |
| 120 | 118 | $integrations = (array) json_decode( $post_data['integrations'] ); |
| 121 | 119 | } |
| 122 | 120 | |
| 121 | + // $form_fields = wp_unslash( $form_fields ); | |
| 122 | + // $notifications = wp_unslash( $notifications ); | |
| 123 | + | |
| 123 | 124 | $form_fields = json_decode( $form_fields, true ); |
| 124 | 125 | $notifications = json_decode( $notifications, true ); |
| 125 | 126 | $data = [ |
| 126 | 127 | 'form_id' => absint( $form_data['wpuf_form_id'] ), |
| @@ -133,14 +134,8 @@ | ||
| 133 | 134 | ]; |
| 134 | 135 | |
| 135 | 136 | $form_fields = weforms()->form->save( $data ); |
| 136 | 137 | |
| 137 | - // Update Old Entry meta_key if changed | |
| 138 | - $form_id = $form_data['wpuf_form_id']; | |
| 139 | - $form = weforms()->form->get( $form_id ); | |
| 140 | - | |
| 141 | - $form->maybe_update_entries( $form_fields ); | |
| 142 | - | |
| 143 | 138 | do_action( 'weforms_update_form', $form_data['wpuf_form_id'], $form_fields, $settings ); |
| 144 | 139 | |
| 145 | 140 | wp_send_json_success( [ 'form_fields' => $form_fields ] ); |
| 146 | 141 | } |
| @@ -529,9 +524,9 @@ | ||
| 529 | 524 | |
| 530 | 525 | $has_empty = false; |
| 531 | 526 | $answers = []; |
| 532 | 527 | $respondentPoints = isset( $form_settings['total_points'] ) ? floatval( $form_settings['total_points'] ) : 0; |
| 533 | - $fields_formatted = array(); | |
| 528 | + | |
| 534 | 529 | foreach ( $fields as $key => $field ) { |
| 535 | 530 | if ( $form_settings['quiz_form'] == 'yes' ) { |
| 536 | 531 | $selectedAnswers = isset( $field['selected_answers'] ) ? $field['selected_answers'] : ''; |
| 537 | 532 | $givenAnswer = isset( $field['value'] ) ? $field['value'] : ''; |
| @@ -540,8 +535,9 @@ | ||
| 540 | 535 | $fieldPoints = isset( $field['points'] ) ? floatval( $field['points'] ) : 0; |
| 541 | 536 | |
| 542 | 537 | if ( $template == 'radio_field' || $template == 'dropdown_field' ) { |
| 543 | 538 | $answers[$field['name']] = true; |
| 539 | + | |
| 544 | 540 | if ( empty( $givenAnswer ) ) { |
| 545 | 541 | $answers[$field['name']] = false; |
| 546 | 542 | $respondentPoints -= $fieldPoints; |
| 547 | 543 | } else { |
| @@ -575,16 +571,14 @@ | ||
| 575 | 571 | } |
| 576 | 572 | } |
| 577 | 573 | } elseif ( empty( $field['value'] ) ) { |
| 578 | 574 | $has_empty = true; |
| 579 | - continue; | |
| 580 | - } else { | |
| 581 | - $field = WeForms_Form_Entry_Manager::format_entry_value( $field ); | |
| 582 | - array_push( $fields_formatted, $field ); | |
| 575 | + break; | |
| 583 | 576 | } |
| 584 | 577 | } |
| 578 | + | |
| 585 | 579 | $response = [ |
| 586 | - 'form_fields' => $fields_formatted, | |
| 580 | + 'form_fields' => $fields, | |
| 587 | 581 | 'form_settings' => $form_settings, |
| 588 | 582 | 'meta_data' => $metadata, |
| 589 | 583 | 'payment_data' => $payment, |
| 590 | 584 | 'has_empty' => $has_empty, |
| @@ -591,9 +585,8 @@ | ||
| 591 | 585 | 'respondent_points' => $respondentPoints, |
| 592 | 586 | 'answers' => $answers, |
| 593 | 587 | ]; |
| 594 | 588 | |
| 595 | - | |
| 596 | 589 | wp_send_json_success( $response ); |
| 597 | 590 | } |
| 598 | 591 | |
| 599 | 592 | /** |
| @@ -702,8 +695,9 @@ | ||
| 702 | 695 | * @return void |
| 703 | 696 | */ |
| 704 | 697 | public function handle_frontend_submission() { |
| 705 | 698 | check_ajax_referer( 'wpuf_form_add' ); |
| 699 | + | |
| 706 | 700 | $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0; |
| 707 | 701 | $page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0; |
| 708 | 702 | |
| 709 | 703 | $form = weforms()->form->get( $form_id ); |
| @@ -761,40 +755,23 @@ | ||
| 761 | 755 | $this->validate_submission( $entry_fields, $form, $form_settings, $form_fields ); |
| 762 | 756 | |
| 763 | 757 | $entry_fields = apply_filters( 'weforms_before_entry_submission', $entry_fields, $form, $form_settings, $form_fields ); |
| 764 | 758 | |
| 765 | - //check for entry_fields for a return error | |
| 766 | - if ( is_wp_error( $entry_fields ) ) { | |
| 759 | + $entry_id = weforms_insert_entry( [ | |
| 760 | + 'form_id' => $form_id, | |
| 761 | + ], $entry_fields ); | |
| 762 | + | |
| 763 | + if ( is_wp_error( $entry_id ) ) { | |
| 767 | 764 | wp_send_json( [ |
| 768 | 765 | 'success' => false, |
| 769 | - 'error' => $entry_fields->get_error_message(), | |
| 766 | + 'error' => $entry_id->get_error_message(), | |
| 770 | 767 | ] ); |
| 771 | - } else { | |
| 772 | - $entry_id = 1; | |
| 773 | - $global_settings = weforms_get_settings(); | |
| 774 | - if ( empty( $form_settings['after_submission'] ) ) { | |
| 775 | - $entry_id = weforms_insert_entry( [ | |
| 776 | - 'form_id' => $form_id, | |
| 777 | - ], $entry_fields ); | |
| 778 | - if ( is_wp_error( $entry_id ) ) { | |
| 779 | - wp_send_json( [ | |
| 780 | - 'success' => false, | |
| 781 | - 'error' => $entry_id->get_error_message(), | |
| 782 | - ] ); | |
| 783 | - } | |
| 784 | - // Fire a hook for integration | |
| 785 | - do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings ); | |
| 786 | - $notification = new WeForms_Notification( [ | |
| 787 | - 'form_id' => $form_id, | |
| 788 | - 'page_id' => $page_id, | |
| 789 | - 'entry_id' => $entry_id, | |
| 790 | - ] ); | |
| 791 | - $notification->send_notifications(); | |
| 792 | - } | |
| 793 | 768 | } |
| 769 | + | |
| 794 | 770 | // redirect URL |
| 795 | 771 | $show_message = false; |
| 796 | - $redirect_to = false; | |
| 772 | + $redirect_to = false; | |
| 773 | + | |
| 797 | 774 | if ( $form_settings['redirect_to'] == 'page' ) { |
| 798 | 775 | $redirect_to = get_permalink( $form_settings['page_id'] ); |
| 799 | 776 | } elseif ( $form_settings['redirect_to'] == 'url' ) { |
| 800 | 777 | $redirect_to = $form_settings['url']; |
| @@ -802,11 +779,17 @@ | ||
| 802 | 779 | $show_message = true; |
| 803 | 780 | } else { |
| 804 | 781 | $show_message = true; |
| 805 | 782 | } |
| 783 | + | |
| 784 | + // Fire a hook for integration | |
| 785 | + do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings ); | |
| 786 | + | |
| 806 | 787 | $field_search = $field_replace = []; |
| 788 | + | |
| 807 | 789 | foreach ( $form_fields as $r_field ) { |
| 808 | 790 | $field_search[] = '{' . $r_field['name'] . '}'; |
| 791 | + | |
| 809 | 792 | if ( $r_field['template'] == 'name_field' ) { |
| 810 | 793 | $field_replace[] = implode( ' ', explode( '|', $entry_fields[ $r_field['name'] ] ) ); |
| 811 | 794 | } else if ( $r_field['template'] == 'address_field' ) { |
| 812 | 795 | $field_replace[] = implode( ', ', $entry_fields[ $r_field['name'] ] ); |
| @@ -814,60 +797,68 @@ | ||
| 814 | 797 | $field_replace[] = isset( $entry_fields[ $r_field['name'] ] ) ? $entry_fields[ $r_field['name'] ] : ''; |
| 815 | 798 | } |
| 816 | 799 | } |
| 817 | 800 | $message = str_replace( $field_search, $field_replace, $form_settings['message'] ); |
| 801 | + | |
| 818 | 802 | // send the response |
| 819 | 803 | $response = apply_filters( 'weforms_entry_submission_response', [ |
| 820 | - 'success' => true, | |
| 821 | - 'redirect_to' => $redirect_to, | |
| 804 | + 'success' => true, | |
| 805 | + 'redirect_to' => $redirect_to, | |
| 822 | 806 | 'show_message' => $show_message, |
| 823 | - 'message' => $message, | |
| 824 | - 'data' => $_POST, | |
| 825 | - 'form_id' => $form_id, | |
| 826 | - 'entry_id' => $entry_id, | |
| 827 | - 'entry_fields' =>$entry_fields, | |
| 807 | + 'message' => $message, | |
| 808 | + 'data' => $_POST, | |
| 809 | + 'form_id' => $form_id, | |
| 810 | + 'entry_id' => $entry_id, | |
| 828 | 811 | ] ); |
| 829 | 812 | |
| 813 | + $notification = new WeForms_Notification( [ | |
| 814 | + 'form_id' => $form_id, | |
| 815 | + 'page_id' => $page_id, | |
| 816 | + 'entry_id' => $entry_id, | |
| 817 | + ] ); | |
| 818 | + | |
| 819 | + $notification->send_notifications(); | |
| 820 | + | |
| 830 | 821 | weforms_clear_buffer(); |
| 831 | 822 | wp_send_json( $response ); |
| 832 | - } | |
| 823 | + } | |
| 833 | 824 | |
| 834 | - function validate_reCaptchav3( $secret ) { | |
| 835 | - check_ajax_referer( 'wpuf_form_add' ); | |
| 825 | + function validate_reCaptchav3( $secret ) { | |
| 826 | + check_ajax_referer( 'wpuf_form_add' ); | |
| 836 | 827 | |
| 837 | - $post_data = wp_unslash($_POST); | |
| 838 | - $token = $post_data['g-recaptcha-response']; | |
| 839 | - $action = $post_data['g-action']; | |
| 840 | - $google_captcha_url = esc_url( 'https://www.google.com/recaptcha/api/siteverify' ); | |
| 828 | + $post_data = wp_unslash($_POST); | |
| 829 | + $token = $post_data['g-recaptcha-response']; | |
| 830 | + $action = $post_data['g-action']; | |
| 831 | + $google_captcha_url = esc_url( 'https://www.google.com/recaptcha/api/siteverify' ); | |
| 841 | 832 | |
| 842 | - $response = wp_remote_post( $google_captcha_url, | |
| 843 | - array( | |
| 844 | - 'method' => 'POST', | |
| 845 | - 'body' => array( | |
| 846 | - 'secret' => $secret, | |
| 847 | - 'response' => $token | |
| 848 | - ) | |
| 833 | + $response = wp_remote_post( $google_captcha_url, | |
| 834 | + array( | |
| 835 | + 'method' => 'POST', | |
| 836 | + 'body' => array( | |
| 837 | + 'secret' => $secret, | |
| 838 | + 'response' => $token | |
| 849 | 839 | ) |
| 850 | - ); | |
| 840 | + ) | |
| 841 | + ); | |
| 851 | 842 | |
| 852 | 843 | |
| 853 | - if ( is_wp_error( $response ) ) { | |
| 844 | + if ( is_wp_error( $response ) ) { | |
| 845 | + wp_send_json( [ | |
| 846 | + 'success' => false, | |
| 847 | + 'error' => __( 'reCAPTCHA validation failed', 'weforms' ), | |
| 848 | + ] ); | |
| 849 | + } else { | |
| 850 | + $api_response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 851 | + if( $api_response["success"] == '1' && $api_response["action"] == $action ) { | |
| 852 | + return true; | |
| 853 | + } else { | |
| 854 | 854 | wp_send_json( [ |
| 855 | 855 | 'success' => false, |
| 856 | 856 | 'error' => __( 'reCAPTCHA validation failed', 'weforms' ), |
| 857 | 857 | ] ); |
| 858 | - } else { | |
| 859 | - $api_response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 860 | - if( $api_response["success"] == '1' && $api_response["action"] == $action ) { | |
| 861 | - return true; | |
| 862 | - } else { | |
| 863 | - wp_send_json( [ | |
| 864 | - 'success' => false, | |
| 865 | - 'error' => __( 'reCAPTCHA validation failed', 'weforms' ), | |
| 866 | - ] ); | |
| 867 | - } | |
| 868 | 858 | } |
| 869 | 859 | } |
| 860 | + } | |
| 870 | 861 | /** |
| 871 | 862 | * reCaptcha Validation |
| 872 | 863 | * |
| 873 | 864 | * @return void |