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