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

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

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