PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.5.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.5.2
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.5.2, at includes/Frontend/Form/FrontendFormHandler.php

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