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 +41 -38 6.3.1 → 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,12 +119,18 @@
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 }
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();
125 133 },
126 134 1
127 135 );
128 136 }
@@ -152,23 +160,17 @@
152 160 * @param string $error
153 161 * @param string $field_id
154 162 * @param stdClass $form the form being submitted (not necessarily the field's form when embedded/repeated).
155 163 * @param array $errors all errors that were caught in this form submission, passed into the frm_before_replace_shortcodes filter for reference.
164 + *
156 165 * @return string
157 166 */
158 167 private static function maybe_modify_ajax_error( $error, $field_id, $form, $errors ) {
159 - if ( false !== strpos( $field_id, '-' ) ) {
160 - // repeated fields look like field_id-repeater_id-iteration, so pull the first value for the field id.
161 - list( $use_field_id ) = explode( '-', $field_id );
162 - } else {
163 - $use_field_id = $field_id;
164 - }
165 -
166 - if ( ! is_numeric( $use_field_id ) ) {
168 + if ( ! is_numeric( $field_id ) ) {
167 169 return $error;
168 170 }
169 171
170 - $use_field = FrmField::getOne( $use_field_id );
172 + $use_field = FrmField::getOne( $field_id );
171 173
172 174 if ( ! $use_field ) {
173 175 return $error;
174 176 }
@@ -177,9 +179,9 @@
177 179 $error_body = FrmFieldsController::pull_custom_error_body_from_custom_html( $form, $use_field, $errors );
178 180
179 181 if ( false !== $error_body ) {
180 182 $error = str_replace( '[error]', $error, $error_body );
181 - $error = str_replace( '[key]', $field_id, $error );
183 + $error = str_replace( '[key]', $use_field['field_key'], $error );
182 184 }
183 185
184 186 return $error;
185 187 }
@@ -190,15 +192,16 @@
190 192 *
191 193 * @since 6.2
192 194 *
193 195 * @param array $response
194 - * @param string|int $form_id
196 + * @param int|string $form_id
197 + *
195 198 * @return array
196 199 */
197 200 private static function check_for_failed_form_submission( $response, $form_id ) {
198 201 $frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form_id ) );
199 202
200 - if ( false !== strpos( $response['content'], $frm_settings->failed_msg ) ) {
203 + if ( str_contains( $response['content'], $frm_settings->failed_msg ) ) {
201 204 $response['errors']['failed'] = $frm_settings->failed_msg;
202 205 $response['content'] = '';
203 206 }
204 207