PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.4
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Frontend / Form / FrontendFormHandler.php

FrontendFormHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 1.4, at includes/Frontend/Form/FrontendFormHandler.php

346 lines 15.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Frontend\Form;
4
5 use WP_Error;
6 use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper;
7 use BitCode\BitForm\Frontend\Form\FrontendFormManager;
8 use BitCode\BitForm\Core\Integration\IntegrationHandler;
9 use BitCode\BitForm\Core\Form\Validator\FormFieldValidator;
10
11 final class FrontendFormHandler
12 {
13 public function __construct()
14 {
15 add_action('wp_enqueue_scripts', array($this, 'loadAssets'));
16 add_shortcode('bitform', array($this, 'handleFrontendRenderRequest'));
17 }
18
19 private function validPassowordResetToken($token, $userID, $formId)
20 {
21 $existResetInteg = (new IntegrationHandler($formId))->getAllIntegration('wp_user_auth', 'wp_auth',1);
22 if (!is_wp_error($existResetInteg) && count($existResetInteg) > 0) {
23 if ($existResetInteg[0]->integration_name === 'reset') {
24 $user = get_userdata($userID);
25 if ($user) {
26 $validKey = check_password_reset_key($token, $user->user_login);
27 if (is_wp_error($validKey)) {
28 echo "<div id='bf-resp' style='display:grid;justify-content:center;color:#860000;'>This password reset token is invalid.</div>";
29 exit();
30 }
31 } else {
32 echo "<div id='bf-resp' style='display:grid;justify-content:center;color:#860000;'>Invalid User!!</div>";
33 exit();
34 }
35 }
36 }
37 }
38
39 public function handleFrontendRenderRequest($atts)
40 {
41 static $shortCodeCounter = 0;
42 $shortCodeCounter += 1;
43 $atts = shortcode_atts(
44 array('id' => 0),
45 $atts
46 );
47 $formID = intval($atts['id']);
48
49 if (!$formID) {
50 return __('Form ID cannot be empty', 'bitform');
51 }
52 static $formsInpage = array();
53 $FrontendFormManager = new FrontendFormManager($formID, $shortCodeCounter);
54 if (!$FrontendFormManager->isExist()) {
55 return sprintf(__('#%s no. Form doesn\'t exists', 'bitform'), $formID);
56 }
57 if (!empty($_GET['token']) && !empty($_GET['id'])) {
58 $this->validPassowordResetToken($_GET['token'], $_GET['id'], $formID);
59 }
60
61 $isBufferStarted = false;
62 $reqField = $_SERVER['QUERY_STRING'];
63 $previousValue = [];
64 $errorMessages = [];
65 if (!empty($reqField)) {
66 foreach (explode('&', $reqField) as $keyValue) {
67 list($field, $value) = explode('=', $keyValue);
68
69 if ('' == trim($value)) {
70 continue;
71 }
72
73 $previousValue[$field][] = sanitize_text_field(urldecode($value));
74 }
75 }
76 $FormIdentifier = esc_js($FrontendFormManager->getFormIdentifier());
77 $nonce = $FrontendFormManager->getFormToken();
78 $file = count($FrontendFormManager->getUploadFields()) > 0 ? $FrontendFormManager->getUploadFields() : false;
79 if ($FrontendFormManager->isSubmitted()) {
80 $submitResp = $FrontendFormManager->handleSubmission();
81 if (!is_wp_error($submitResp) && isset($submitResp['redirectPage'])) {
82 if (!headers_sent()) {
83 wp_safe_redirect($submitResp['redirectPage']);
84 exit;
85 } else {
86 $isBufferStarted = true;
87 echo "<script type='text/javascript'>window.onload = function() {window.location.replace('" . $submitResp['redirectPage'] . "')}</script>";
88 }
89 } elseif (is_wp_error($submitResp)) {
90 $errors = $submitResp->get_error_message();
91 if (\is_string($errors)) {
92 if (!$isBufferStarted) {
93 $isBufferStarted = true;
94 ob_start();
95 }
96 echo "<div id='bf-resp' style='display:grid;justify-content:center'>" . wp_kses_post($errors) . "</div>";
97 } elseif (isset($errors['$form'])) {
98 if (!$isBufferStarted) {
99 $isBufferStarted = true;
100 ob_start();
101 }
102 foreach ($errors['$form'] as $error) {
103 echo "<div id='bf-resp' style='display:grid;justify-content:center'>" . wp_kses_post($error) . "</div>";
104 }
105 unset($errors['$form']);
106 $errorMessages = $errors;
107 }
108 if (!empty($errors)) {
109 $errorMessages = $errors;
110 }
111 $previousValue = $_POST;
112 $_POST = [];
113 }
114 }
115 if ($FrontendFormManager->checkStatus()) {
116 $FrontendFormManager->setViewCount();
117
118 if (isset($_REQUEST) && count($previousValue) > 0) {
119 $formContent = $FrontendFormManager->getFormContentWithValue($previousValue);
120 $fields = $formContent->fields;
121 $layout = $formContent->layout;
122 $buttons = !empty($formContent->buttons) ? $formContent->buttons : '';
123 $additional = $formContent->additional;
124 } else {
125 $formContent = $FrontendFormManager->getFormContent();
126 $fields = $formContent->fields;
127 $layout = $formContent->layout;
128 $buttons = !empty($formContent->buttons) ? $formContent->buttons : '';
129 $additional = $formContent->additional;
130 }
131
132 $captchaV3Settings = $FrontendFormManager->getCaptchaV3Settings();
133 if ($FrontendFormManager->getCaptchaSettings() || $captchaV3Settings) {
134 $integrationHandler = new IntegrationHandler(0);
135 $allFormIntegrations = $integrationHandler->getAllIntegration('app');
136 if (!is_wp_error($allFormIntegrations)) {
137 foreach ($allFormIntegrations as $integration) {
138 if (
139 $FrontendFormManager->getCaptchaSettings()
140 && !is_null($integration->integration_type)
141 && $integration->integration_type === 'gReCaptcha'
142 ) {
143 $integrationDetails = json_decode($integration->integration_details);
144 $integrationDetails->id = $integration->id;
145 $reCAPTCHA = $integrationDetails;
146 $reCAPTCHAVersion = 'v2';
147 }
148
149 if ($captchaV3Settings) {
150 if (!is_null($integration->integration_type) && $integration->integration_type === 'gReCaptchaV3') {
151 $integrationDetails = json_decode($integration->integration_details);
152 $integrationDetails->id = $integration->id;
153 $reCAPTCHA = $integrationDetails;
154 $reCAPTCHAVersion = 'v3';
155 }
156 }
157 }
158 }
159 }
160 wp_enqueue_script('bitforms-frontend-script');
161 if ($file) {
162 wp_enqueue_script('bitforms-frontend-file');
163 }
164
165 if ($captchaV3Settings && !empty($reCAPTCHA->siteKey)) {
166 wp_enqueue_script('bitform-recaptchav3', "https://www.google.com/recaptcha/api.js?render={$reCAPTCHA->siteKey}");
167 }
168 $fieldsKey = $FrontendFormManager->getFieldsKey();
169 $workFlowreturnedOnUserInput = null;
170 if (!empty($formContent->workFlowExist)) {
171 $workFlowRunHelper = new WorkFlowRunHelper($formID);
172 if (!empty($formContent->workFlowExist->onload)) {
173 $workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad(
174 'create',
175 $fields
176 );
177 if (!empty($workFlowreturnedOnLoad['fields'])) {
178 $fields = $workFlowreturnedOnLoad['fields'];
179 }
180 }
181 if (!empty($formContent->workFlowExist->oninput)) {
182 $workFlowreturnedOnUserInput = $workFlowRunHelper->executeOnUserInput(
183 'create',
184 $fields
185 );
186 }
187 }
188
189 if (!wp_style_is('bitform-style' . $formID)) {
190 $this->loadAssets(true, $formID, $file);
191 }
192
193 if ($captchaV3Settings && !empty($captchaV3Settings->hideReCaptcha)) {
194 echo '<style>.grecaptcha-badge { visibility: hidden; }</style>';
195 }
196
197 $FrontendFormManager->honeypotTrap();
198
199 $bitFormFrontArr = array(
200 'ajaxURL' => admin_url('admin-ajax.php'),
201 'nonce' => $nonce,
202 'version' => BITFORMS_VERSION,
203 // 'contentID' => $FormIdentifier,
204 'layout' => $layout,
205 'fields' => $fields,
206 'buttons' => $buttons,
207 'fieldsKey' => $fieldsKey,
208 'file' => $file,
209 'formId' => $formID,
210 'GCLID' => $FrontendFormManager->isGCLIDEnabled(),
211 'assetUrl' => BITFORMS_ASSET_URI,
212 'gRecaptchaSiteKey' => !empty($reCAPTCHA->siteKey) ? $reCAPTCHA->siteKey : null,
213 'gRecaptchaVersion' => !empty($reCAPTCHAVersion) ? $reCAPTCHAVersion : null,
214 'conditional' => !empty($workFlowreturnedOnUserInput['conditional']) ? $workFlowreturnedOnUserInput['conditional'] : false,
215 'fieldToCheck' => !empty($workFlowreturnedOnUserInput['fieldToCheck']) ? $workFlowreturnedOnUserInput['fieldToCheck'] : false,
216 'fieldToChange' => !empty($workFlowreturnedOnUserInput['fieldToChange']) ? $workFlowreturnedOnUserInput['fieldToChange'] : false
217 );
218
219 $paymentKeys = $FrontendFormManager->checkPaymentFields();
220 if (count($paymentKeys)) {
221 $bitFormFrontArr['paymentKeys'] = $paymentKeys;
222 }
223
224 if (isset($additional->enabled->validateFocusLost)) {
225 $bitFormFrontArr['validateFocusLost'] = true;
226 }
227
228 $bitFormsFront = apply_filters(
229 'bitforms_localized_script',
230 $bitFormFrontArr
231 );
232
233 $fields = \json_encode($fields);
234 $layout = \json_encode($layout);
235 $buttons = \json_encode($buttons);
236 if (!\in_array($formID, $formsInpage)) {
237 wp_localize_script('bitforms-frontend-script', $FormIdentifier, $bitFormsFront);
238 } else {
239 $formsInpage[] = $formID;
240 }
241 $formSpecificScripts = "if(typeof window._bitforms_front==='function'){_bitforms_front('$FormIdentifier')}else{document.addEventListener('DOMContentLoaded',(event)=>{ window._bitforms_front && _bitforms_front('$FormIdentifier')})}";
242 wp_add_inline_script('bitforms-frontend-script', $formSpecificScripts, 'after');
243 $html = $FrontendFormManager->formView($fields, $file, $errorMessages);
244 if (!$isBufferStarted) {
245 ob_start();
246 } ?>
247 <div id=<?php echo esc_attr($FormIdentifier); ?>><?php echo trim($html); ?>
248 </div>
249 <?php
250 return ob_get_clean();
251 }
252 }
253
254 public function loadAssets($recheck = false, $formID = 0, $isFileExists = false)
255 {
256 global $post;
257 if (!empty($post->post_content)) {
258 \preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $post->post_content, $shortCode);
259 } else {
260 $isSCExists = false;
261 if (isset($post->ID) && \is_int($post->ID)) {
262 global $wpdb;
263 $pMetadata = $wpdb->get_results("SELECT meta_value FROM `" . $wpdb->postmeta . "` WHERE `post_id`={$post->ID} AND meta_value LIKE '%[bitform id=%' LIMIT 1");
264 if ($pMetadata && !empty($pMetadata[0]->meta_value)) {
265 $isSCExists = true;
266 \preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $pMetadata[0]->meta_value, $shortCode);
267 }
268 }
269 if (!$isSCExists) {
270 $shortCode[0] = null;
271 $shortCode[1] = null;
272 $shortCode[2] = [];
273 $shortCode[3] = null;
274 }
275 }
276 if ($formID !== 0) {
277 $shortCode[2][] = $formID;
278 }
279 if (count($shortCode) === 4 && count($shortCode[2]) > 0) {
280 wp_enqueue_style(
281 'bitforms-style-frontend',
282 BITFORMS_ASSET_URI . '/css/components.css',
283 array(),
284 BITFORMS_VERSION,
285 'all'
286 );
287 foreach ($shortCode[2] as $formID) {
288 if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $formID . '.css')) {
289 $styleModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $formID . '.css');
290 wp_enqueue_style(
291 'bitform-style' . $formID,
292 BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-' . $formID . '.css',
293 array(),
294 $styleModifyTime,
295 'all'
296 );
297 }
298 if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-layout-' . $formID . '.css')) {
299 $layoutModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-layout-' . $formID . '.css');
300 wp_enqueue_style(
301 'bitform-layout-style' . $formID,
302 BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-layout-' . $formID . '.css',
303 array(),
304 $layoutModifyTime,
305 'all'
306 );
307 }
308 }
309
310 if (!$recheck) {
311 wp_register_script(
312 'bitforms-frontend-script',
313 BITFORMS_ASSET_FRNT_JS_URI . '/bitformsFrontend.js',
314 array(),
315 BITFORMS_VERSION,
316 true
317 );
318 wp_register_script(
319 'bitforms-frontend-file',
320 BITFORMS_ASSET_FRNT_JS_URI . '/bitforms-file.js',
321 array(),
322 BITFORMS_VERSION,
323 true
324 );
325 } else {
326 wp_enqueue_script(
327 'bitforms-frontend-script',
328 BITFORMS_ASSET_FRNT_JS_URI . '/bitformsFrontend.js',
329 array(),
330 BITFORMS_VERSION,
331 true
332 );
333 if ($isFileExists) {
334 wp_enqueue_script(
335 'bitforms-frontend-file',
336 BITFORMS_ASSET_JS_URI . '/bitforms-file.js',
337 array(),
338 BITFORMS_VERSION,
339 true
340 );
341 }
342 }
343 }
344 }
345 }
346