← All changes
|
classes/controllers/FrmEntriesAJAXSubmitController.php
+38
-35
6.30
→
6.6
View file →
| @@ -14,10 +14,9 @@ | ||
| 14 | 14 | * |
| 15 | 15 | * @return void |
| 16 | 16 | */ |
| 17 | 17 | public static function ajax_create() { |
| 18 | - // This is called before we exit early to cover the conflict in Pro as well. | |
| 19 | - self::fix_woocommerce_conflict(); | |
| 18 | + self::fix_woocommerce_conflict(); // This is called before we exit early to cover the conflict in Pro as well. | |
| 20 | 19 | |
| 21 | 20 | if ( is_callable( 'FrmProEntriesController::ajax_create' ) ) { |
| 22 | 21 | // Let Pro handle AJAX Submit if it's available. |
| 23 | 22 | // This is because Pro requires additional code to support other Pro features. |
| @@ -40,9 +39,8 @@ | ||
| 40 | 39 | 'pass' => false, |
| 41 | 40 | ); |
| 42 | 41 | |
| 43 | 42 | $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); |
| 44 | - | |
| 45 | 43 | if ( ! $form_id ) { |
| 46 | 44 | echo json_encode( $response ); |
| 47 | 45 | wp_die(); |
| 48 | 46 | } |
| @@ -47,15 +45,15 @@ | ||
| 47 | 45 | wp_die(); |
| 48 | 46 | } |
| 49 | 47 | |
| 50 | 48 | $form = FrmForm::getOne( $form_id ); |
| 51 | - | |
| 52 | 49 | if ( ! $form ) { |
| 53 | 50 | echo json_encode( $response ); |
| 54 | 51 | wp_die(); |
| 55 | 52 | } |
| 56 | 53 | |
| 57 | - if ( ! FrmForm::is_ajax_on( $form ) ) { | |
| 54 | + $is_ajax_on = FrmForm::is_ajax_on( $form ); | |
| 55 | + if ( ! $is_ajax_on ) { | |
| 58 | 56 | // This continues in the Pro version as it is required for other features including in-place edit. |
| 59 | 57 | // In Lite, if AJAX submit is not on, just exit early as this function is getting called incorrectly. |
| 60 | 58 | echo json_encode( $response ); |
| 61 | 59 | wp_die(); |
| @@ -62,28 +60,9 @@ | ||
| 62 | 60 | } |
| 63 | 61 | |
| 64 | 62 | $errors = FrmEntryValidate::validate( wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 65 | 63 | |
| 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 { | |
| 64 | + if ( ! $errors ) { | |
| 86 | 65 | global $frm_vars; |
| 87 | 66 | $frm_vars['ajax'] = true; |
| 88 | 67 | $frm_vars['css_loaded'] = true; |
| 89 | 68 | |
| @@ -96,15 +75,34 @@ | ||
| 96 | 75 | // Trigger the footer scripts if there is a form to show. |
| 97 | 76 | if ( ! empty( $frm_vars['forms_loaded'] ) ) { |
| 98 | 77 | ob_start(); |
| 99 | 78 | self::print_ajax_scripts(); |
| 100 | - $response['content'] .= ob_get_clean(); | |
| 79 | + $response['content'] .= ob_get_contents(); | |
| 80 | + ob_end_clean(); | |
| 101 | 81 | |
| 102 | 82 | // Mark the end of added footer content. |
| 103 | 83 | $response['content'] .= '<span class="frm_end_ajax_' . $form->id . '"></span>'; |
| 104 | 84 | } |
| 105 | - }//end if | |
| 85 | + } else { | |
| 86 | + $obj = array(); | |
| 87 | + foreach ( $errors as $field => $error ) { | |
| 88 | + $field_id = str_replace( 'field', '', $field ); | |
| 89 | + $error = self::maybe_modify_ajax_error( $error, $field_id, $form, $errors ); | |
| 90 | + $obj[ $field_id ] = $error; | |
| 91 | + } | |
| 106 | 92 | |
| 93 | + $response['errors'] = $obj; | |
| 94 | + $invalid_msg = FrmFormsHelper::get_invalid_error_message( array( 'form' => $form ) ); | |
| 95 | + $response['error_message'] = FrmFormsHelper::get_success_message( | |
| 96 | + array( | |
| 97 | + 'message' => $invalid_msg, | |
| 98 | + 'form' => $form, | |
| 99 | + 'entry_id' => 0, | |
| 100 | + 'class' => FrmFormsHelper::form_error_class(), | |
| 101 | + ) | |
| 102 | + ); | |
| 103 | + } | |
| 104 | + | |
| 107 | 105 | $response = self::check_for_failed_form_submission( $response, $form->id ); |
| 108 | 106 | |
| 109 | 107 | echo json_encode( $response ); |
| 110 | 108 | wp_die(); |
| @@ -119,9 +117,9 @@ | ||
| 119 | 117 | */ |
| 120 | 118 | private static function fix_woocommerce_conflict() { |
| 121 | 119 | add_action( |
| 122 | 120 | 'wp_print_footer_scripts', |
| 123 | - function () { | |
| 121 | + function() { | |
| 124 | 122 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 125 | 123 | require_once ABSPATH . 'wp-admin/includes/screen.php'; |
| 126 | 124 | } |
| 127 | 125 | |
| @@ -160,17 +158,23 @@ | ||
| 160 | 158 | * @param string $error |
| 161 | 159 | * @param string $field_id |
| 162 | 160 | * @param stdClass $form the form being submitted (not necessarily the field's form when embedded/repeated). |
| 163 | 161 | * @param array $errors all errors that were caught in this form submission, passed into the frm_before_replace_shortcodes filter for reference. |
| 164 | - * | |
| 165 | 162 | * @return string |
| 166 | 163 | */ |
| 167 | 164 | private static function maybe_modify_ajax_error( $error, $field_id, $form, $errors ) { |
| 168 | - if ( ! is_numeric( $field_id ) ) { | |
| 165 | + if ( false !== strpos( $field_id, '-' ) ) { | |
| 166 | + // repeated fields look like field_id-repeater_id-iteration, so pull the first value for the field id. | |
| 167 | + list( $use_field_id ) = explode( '-', $field_id ); | |
| 168 | + } else { | |
| 169 | + $use_field_id = $field_id; | |
| 170 | + } | |
| 171 | + | |
| 172 | + if ( ! is_numeric( $use_field_id ) ) { | |
| 169 | 173 | return $error; |
| 170 | 174 | } |
| 171 | 175 | |
| 172 | - $use_field = FrmField::getOne( $field_id ); | |
| 176 | + $use_field = FrmField::getOne( $use_field_id ); | |
| 173 | 177 | |
| 174 | 178 | if ( ! $use_field ) { |
| 175 | 179 | return $error; |
| 176 | 180 | } |
| @@ -179,9 +183,9 @@ | ||
| 179 | 183 | $error_body = FrmFieldsController::pull_custom_error_body_from_custom_html( $form, $use_field, $errors ); |
| 180 | 184 | |
| 181 | 185 | if ( false !== $error_body ) { |
| 182 | 186 | $error = str_replace( '[error]', $error, $error_body ); |
| 183 | - $error = str_replace( '[key]', $use_field['field_key'], $error ); | |
| 187 | + $error = str_replace( '[key]', $field_id, $error ); | |
| 184 | 188 | } |
| 185 | 189 | |
| 186 | 190 | return $error; |
| 187 | 191 | } |
| @@ -192,16 +196,15 @@ | ||
| 192 | 196 | * |
| 193 | 197 | * @since 6.2 |
| 194 | 198 | * |
| 195 | 199 | * @param array $response |
| 196 | - * @param int|string $form_id | |
| 197 | - * | |
| 200 | + * @param string|int $form_id | |
| 198 | 201 | * @return array |
| 199 | 202 | */ |
| 200 | 203 | private static function check_for_failed_form_submission( $response, $form_id ) { |
| 201 | 204 | $frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form_id ) ); |
| 202 | 205 | |
| 203 | - if ( str_contains( $response['content'], $frm_settings->failed_msg ) ) { | |
| 206 | + if ( false !== strpos( $response['content'], $frm_settings->failed_msg ) ) { | |
| 204 | 207 | $response['errors']['failed'] = $frm_settings->failed_msg; |
| 205 | 208 | $response['content'] = ''; |
| 206 | 209 | } |
| 207 | 210 | |