← All changes
|
classes/controllers/FrmEntriesAJAXSubmitController.php
+24
-27
6.32
→
6.13
View file →
| @@ -40,9 +40,8 @@ | ||
| 40 | 40 | 'pass' => false, |
| 41 | 41 | ); |
| 42 | 42 | |
| 43 | 43 | $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); |
| 44 | - | |
| 45 | 44 | if ( ! $form_id ) { |
| 46 | 45 | echo json_encode( $response ); |
| 47 | 46 | wp_die(); |
| 48 | 47 | } |
| @@ -47,15 +46,15 @@ | ||
| 47 | 46 | wp_die(); |
| 48 | 47 | } |
| 49 | 48 | |
| 50 | 49 | $form = FrmForm::getOne( $form_id ); |
| 51 | - | |
| 52 | 50 | if ( ! $form ) { |
| 53 | 51 | echo json_encode( $response ); |
| 54 | 52 | wp_die(); |
| 55 | 53 | } |
| 56 | 54 | |
| 57 | - if ( ! FrmForm::is_ajax_on( $form ) ) { | |
| 55 | + $is_ajax_on = FrmForm::is_ajax_on( $form ); | |
| 56 | + if ( ! $is_ajax_on ) { | |
| 58 | 57 | // This continues in the Pro version as it is required for other features including in-place edit. |
| 59 | 58 | // In Lite, if AJAX submit is not on, just exit early as this function is getting called incorrectly. |
| 60 | 59 | echo json_encode( $response ); |
| 61 | 60 | wp_die(); |
| @@ -62,28 +61,9 @@ | ||
| 62 | 61 | } |
| 63 | 62 | |
| 64 | 63 | $errors = FrmEntryValidate::validate( wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 65 | 64 | |
| 66 | - if ( $errors ) { | |
| 67 | - $obj = array(); | |
| 68 | - | |
| 69 | - foreach ( $errors as $field => $error ) { | |
| 70 | - $field_id = str_replace( 'field', '', $field ); | |
| 71 | - $error = self::maybe_modify_ajax_error( $error, $field_id, $form, $errors ); | |
| 72 | - $obj[ $field_id ] = $error; | |
| 73 | - } | |
| 74 | - | |
| 75 | - $response['errors'] = $obj; | |
| 76 | - $invalid_msg = FrmFormsHelper::get_invalid_error_message( array( 'form' => $form ) ); | |
| 77 | - $response['error_message'] = FrmFormsHelper::get_success_message( | |
| 78 | - array( | |
| 79 | - 'message' => $invalid_msg, | |
| 80 | - 'form' => $form, | |
| 81 | - 'entry_id' => 0, | |
| 82 | - 'class' => FrmFormsHelper::form_error_class(), | |
| 83 | - ) | |
| 84 | - ); | |
| 85 | - } else { | |
| 65 | + if ( ! $errors ) { | |
| 86 | 66 | global $frm_vars; |
| 87 | 67 | $frm_vars['ajax'] = true; |
| 88 | 68 | $frm_vars['css_loaded'] = true; |
| 89 | 69 | |
| @@ -96,13 +76,32 @@ | ||
| 96 | 76 | // Trigger the footer scripts if there is a form to show. |
| 97 | 77 | if ( ! empty( $frm_vars['forms_loaded'] ) ) { |
| 98 | 78 | ob_start(); |
| 99 | 79 | self::print_ajax_scripts(); |
| 100 | - $response['content'] .= ob_get_clean(); | |
| 80 | + $response['content'] .= ob_get_contents(); | |
| 81 | + ob_end_clean(); | |
| 101 | 82 | |
| 102 | 83 | // Mark the end of added footer content. |
| 103 | 84 | $response['content'] .= '<span class="frm_end_ajax_' . $form->id . '"></span>'; |
| 104 | 85 | } |
| 86 | + } else { | |
| 87 | + $obj = array(); | |
| 88 | + foreach ( $errors as $field => $error ) { | |
| 89 | + $field_id = str_replace( 'field', '', $field ); | |
| 90 | + $error = self::maybe_modify_ajax_error( $error, $field_id, $form, $errors ); | |
| 91 | + $obj[ $field_id ] = $error; | |
| 92 | + } | |
| 93 | + | |
| 94 | + $response['errors'] = $obj; | |
| 95 | + $invalid_msg = FrmFormsHelper::get_invalid_error_message( array( 'form' => $form ) ); | |
| 96 | + $response['error_message'] = FrmFormsHelper::get_success_message( | |
| 97 | + array( | |
| 98 | + 'message' => $invalid_msg, | |
| 99 | + 'form' => $form, | |
| 100 | + 'entry_id' => 0, | |
| 101 | + 'class' => FrmFormsHelper::form_error_class(), | |
| 102 | + ) | |
| 103 | + ); | |
| 105 | 104 | }//end if |
| 106 | 105 | |
| 107 | 106 | $response = self::check_for_failed_form_submission( $response, $form->id ); |
| 108 | 107 | |
| @@ -160,9 +159,8 @@ | ||
| 160 | 159 | * @param string $error |
| 161 | 160 | * @param string $field_id |
| 162 | 161 | * @param stdClass $form the form being submitted (not necessarily the field's form when embedded/repeated). |
| 163 | 162 | * @param array $errors all errors that were caught in this form submission, passed into the frm_before_replace_shortcodes filter for reference. |
| 164 | - * | |
| 165 | 163 | * @return string |
| 166 | 164 | */ |
| 167 | 165 | private static function maybe_modify_ajax_error( $error, $field_id, $form, $errors ) { |
| 168 | 166 | if ( ! is_numeric( $field_id ) ) { |
| @@ -193,15 +191,14 @@ | ||
| 193 | 191 | * @since 6.2 |
| 194 | 192 | * |
| 195 | 193 | * @param array $response |
| 196 | 194 | * @param int|string $form_id |
| 197 | - * | |
| 198 | 195 | * @return array |
| 199 | 196 | */ |
| 200 | 197 | private static function check_for_failed_form_submission( $response, $form_id ) { |
| 201 | 198 | $frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form_id ) ); |
| 202 | 199 | |
| 203 | - if ( str_contains( $response['content'], $frm_settings->failed_msg ) ) { | |
| 200 | + if ( false !== strpos( $response['content'], $frm_settings->failed_msg ) ) { | |
| 204 | 201 | $response['errors']['failed'] = $frm_settings->failed_msg; |
| 205 | 202 | $response['content'] = ''; |
| 206 | 203 | } |
| 207 | 204 | |