PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26.1
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
formidable / classes / controllers / FrmEntriesAJAXSubmitController.php

FrmEntriesAJAXSubmitController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.26.1, at classes/controllers/FrmEntriesAJAXSubmitController.php

214 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 6.2
8 */
9 class FrmEntriesAJAXSubmitController {
10
11 /**
12 * Create an entry when the ajax_submit option is on.
13 * If Pro is active, FrmProEntriesController::ajax_create will be used instead.
14 *
15 * @return void
16 */
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 if ( is_callable( 'FrmProEntriesController::ajax_create' ) ) {
22 // Let Pro handle AJAX Submit if it's available.
23 // This is because Pro requires additional code to support other Pro features.
24 return;
25 }
26
27 if ( ! FrmAppHelper::doing_ajax() ) {
28 // Normally, this function would be triggered with the wp_ajax hook, but we need it fired sooner.
29 return;
30 }
31
32 if ( 'frm_entries_create' !== FrmAppHelper::get_post_param( 'action', '', 'sanitize_title' ) ) {
33 // Not a Formidable AJAX create request so exit early.
34 return;
35 }
36
37 $response = array(
38 'errors' => array(),
39 'content' => '',
40 'pass' => false,
41 );
42
43 $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
44
45 if ( ! $form_id ) {
46 echo json_encode( $response );
47 wp_die();
48 }
49
50 $form = FrmForm::getOne( $form_id );
51
52 if ( ! $form ) {
53 echo json_encode( $response );
54 wp_die();
55 }
56
57 $is_ajax_on = FrmForm::is_ajax_on( $form );
58
59 if ( ! $is_ajax_on ) {
60 // This continues in the Pro version as it is required for other features including in-place edit.
61 // In Lite, if AJAX submit is not on, just exit early as this function is getting called incorrectly.
62 echo json_encode( $response );
63 wp_die();
64 }
65
66 $errors = FrmEntryValidate::validate( wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
67
68 if ( ! $errors ) {
69 global $frm_vars;
70 $frm_vars['ajax'] = true;
71 $frm_vars['css_loaded'] = true;
72
73 FrmEntriesController::process_entry( '', true );
74
75 $title = FrmFormState::get_from_request( 'title', 'auto' );
76 $description = FrmFormState::get_from_request( 'description', 'auto' );
77 $response['content'] .= FrmFormsController::show_form( $form->id, '', $title, $description );
78
79 // Trigger the footer scripts if there is a form to show.
80 if ( ! empty( $frm_vars['forms_loaded'] ) ) {
81 ob_start();
82 self::print_ajax_scripts();
83 $response['content'] .= ob_get_contents();
84 ob_end_clean();
85
86 // Mark the end of added footer content.
87 $response['content'] .= '<span class="frm_end_ajax_' . $form->id . '"></span>';
88 }
89 } else {
90 $obj = array();
91
92 foreach ( $errors as $field => $error ) {
93 $field_id = str_replace( 'field', '', $field );
94 $error = self::maybe_modify_ajax_error( $error, $field_id, $form, $errors );
95 $obj[ $field_id ] = $error;
96 }
97
98 $response['errors'] = $obj;
99 $invalid_msg = FrmFormsHelper::get_invalid_error_message( array( 'form' => $form ) );
100 $response['error_message'] = FrmFormsHelper::get_success_message(
101 array(
102 'message' => $invalid_msg,
103 'form' => $form,
104 'entry_id' => 0,
105 'class' => FrmFormsHelper::form_error_class(),
106 )
107 );
108 }//end if
109
110 $response = self::check_for_failed_form_submission( $response, $form->id );
111
112 echo json_encode( $response );
113 wp_die();
114 }
115
116 /**
117 * Prevent WooCommerce 7.6.0 from triggering a fatal error when wp_print_footer_scripts is called.
118 *
119 * @since 6.2.3
120 *
121 * @return void
122 */
123 private static function fix_woocommerce_conflict() {
124 add_action(
125 'wp_print_footer_scripts',
126 function () {
127 if ( ! function_exists( 'get_current_screen' ) ) {
128 require_once ABSPATH . 'wp-admin/includes/screen.php';
129 }
130
131 if ( ! class_exists( 'WP_Screen', false ) ) {
132 require_once ABSPATH . 'wp-admin/includes/class-wp-screen.php';
133 }
134
135 FrmAppHelper::set_current_screen_and_hook_suffix();
136 },
137 1
138 );
139 }
140
141 /**
142 * Load CAPTCHA script after AJAX submit so subsequent form CAPTCHAs don't break.
143 *
144 * @since 6.2
145 *
146 * @return void
147 */
148 private static function print_ajax_scripts() {
149 global $wp_scripts;
150 $keep_scripts = array( 'captcha-api' );
151 $keep_scripts = apply_filters( 'frm_ajax_load_scripts', $keep_scripts );
152 $registered_scripts = (array) $wp_scripts->registered;
153 $registered_scripts = array_diff( array_keys( $registered_scripts ), $keep_scripts );
154 $wp_scripts->done = array_merge( $wp_scripts->done, $registered_scripts );
155 wp_print_footer_scripts();
156 }
157
158 /**
159 * If a field has custom HTML for errors, apply it around the message.
160 *
161 * @since 6.2
162 *
163 * @param string $error
164 * @param string $field_id
165 * @param stdClass $form the form being submitted (not necessarily the field's form when embedded/repeated).
166 * @param array $errors all errors that were caught in this form submission, passed into the frm_before_replace_shortcodes filter for reference.
167 *
168 * @return string
169 */
170 private static function maybe_modify_ajax_error( $error, $field_id, $form, $errors ) {
171 if ( ! is_numeric( $field_id ) ) {
172 return $error;
173 }
174
175 $use_field = FrmField::getOne( $field_id );
176
177 if ( ! $use_field ) {
178 return $error;
179 }
180
181 $use_field = FrmFieldsHelper::setup_edit_vars( $use_field );
182 $error_body = FrmFieldsController::pull_custom_error_body_from_custom_html( $form, $use_field, $errors );
183
184 if ( false !== $error_body ) {
185 $error = str_replace( '[error]', $error, $error_body );
186 $error = str_replace( '[key]', $use_field['field_key'], $error );
187 }
188
189 return $error;
190 }
191
192 /**
193 * Confirm that the result of calling FrmFormsController::show_form added the failed message for a duplicate entry to the HTML.
194 * If it did, move the message to the errors key instead of returning the content.
195 *
196 * @since 6.2
197 *
198 * @param array $response
199 * @param int|string $form_id
200 *
201 * @return array
202 */
203 private static function check_for_failed_form_submission( $response, $form_id ) {
204 $frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form_id ) );
205
206 if ( false !== strpos( $response['content'], $frm_settings->failed_msg ) ) {
207 $response['errors']['failed'] = $frm_settings->failed_msg;
208 $response['content'] = '';
209 }
210
211 return $response;
212 }
213 }
214