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