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

187 lines 5.7 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 if ( is_callable( 'FrmProEntriesController::ajax_create' ) ) {
19 // Let Pro handle AJAX Submit if it's available.
20 // This is because Pro requires additional code to support other Pro features.
21 return;
22 }
23
24 if ( ! FrmAppHelper::doing_ajax() ) {
25 // Normally, this function would be triggered with the wp_ajax hook, but we need it fired sooner.
26 return;
27 }
28
29 if ( 'frm_entries_create' !== FrmAppHelper::get_post_param( 'action', '', 'sanitize_title' ) ) {
30 // Not a Formidable AJAX create request so exit early.
31 return;
32 }
33
34 $response = array(
35 'errors' => array(),
36 'content' => '',
37 'pass' => false,
38 );
39
40 $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
41 if ( ! $form_id ) {
42 echo json_encode( $response );
43 wp_die();
44 }
45
46 $form = FrmForm::getOne( $form_id );
47 if ( ! $form ) {
48 echo json_encode( $response );
49 wp_die();
50 }
51
52 $is_ajax_on = FrmForm::is_ajax_on( $form );
53 if ( ! $is_ajax_on ) {
54 // This continues in the Pro version as it is required for other features including in-place edit.
55 // In Lite, if AJAX submit is not on, just exit early as this function is getting called incorrectly.
56 echo json_encode( $response );
57 wp_die();
58 }
59
60 $errors = FrmEntryValidate::validate( wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
61
62 if ( ! $errors ) {
63 global $frm_vars;
64 $frm_vars['ajax'] = true;
65 $frm_vars['css_loaded'] = true;
66
67 FrmEntriesController::process_entry( '', true );
68
69 $title = FrmFormState::get_from_request( 'title', 'auto' );
70 $description = FrmFormState::get_from_request( 'description', 'auto' );
71 $response['content'] .= FrmFormsController::show_form( $form->id, '', $title, $description );
72
73 // Trigger the footer scripts if there is a form to show.
74 if ( ! empty( $frm_vars['forms_loaded'] ) ) {
75 ob_start();
76 self::print_ajax_scripts();
77 $response['content'] .= ob_get_contents();
78 ob_end_clean();
79
80 // Mark the end of added footer content.
81 $response['content'] .= '<span class="frm_end_ajax_' . $form->id . '"></span>';
82 }
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 }
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
103 $response = self::check_for_failed_form_submission( $response, $form->id );
104
105 echo json_encode( $response );
106 wp_die();
107 }
108
109 /**
110 * Load CAPTCHA script after AJAX submit so subsequent form CAPTCHAs don't break.
111 *
112 * @since 6.2
113 *
114 * @return void
115 */
116 private static function print_ajax_scripts() {
117 global $wp_scripts;
118 $keep_scripts = array( 'captcha-api' );
119 $keep_scripts = apply_filters( 'frm_ajax_load_scripts', $keep_scripts );
120 $registered_scripts = (array) $wp_scripts->registered;
121 $registered_scripts = array_diff( array_keys( $registered_scripts ), $keep_scripts );
122 $wp_scripts->done = array_merge( $wp_scripts->done, $registered_scripts );
123 wp_print_footer_scripts();
124 }
125
126 /**
127 * If a field has custom HTML for errors, apply it around the message.
128 *
129 * @since 6.2
130 *
131 * @param string $error
132 * @param string $field_id
133 * @param stdClass $form the form being submitted (not necessarily the field's form when embedded/repeated).
134 * @param array $errors all errors that were caught in this form submission, passed into the frm_before_replace_shortcodes filter for reference.
135 * @return string
136 */
137 private static function maybe_modify_ajax_error( $error, $field_id, $form, $errors ) {
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 ) ) {
146 return $error;
147 }
148
149 $use_field = FrmField::getOne( $use_field_id );
150
151 if ( ! $use_field ) {
152 return $error;
153 }
154
155 $use_field = FrmFieldsHelper::setup_edit_vars( $use_field );
156 $error_body = FrmFieldsController::pull_custom_error_body_from_custom_html( $form, $use_field, $errors );
157
158 if ( false !== $error_body ) {
159 $error = str_replace( '[error]', $error, $error_body );
160 $error = str_replace( '[key]', $field_id, $error );
161 }
162
163 return $error;
164 }
165
166 /**
167 * Confirm that the result of calling FrmFormsController::show_form added the failed message for a duplicate entry to the HTML.
168 * If it did, move the message to the errors key instead of returning the content.
169 *
170 * @since 6.2
171 *
172 * @param array $response
173 * @param string|int $form_id
174 * @return array
175 */
176 private static function check_for_failed_form_submission( $response, $form_id ) {
177 $frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form_id ) );
178
179 if ( false !== strpos( $response['content'], $frm_settings->failed_msg ) ) {
180 $response['errors']['failed'] = $frm_settings->failed_msg;
181 $response['content'] = '';
182 }
183
184 return $response;
185 }
186 }
187