PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.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
← All changes | includes/Frontend/Form/FrontendFormHandler.php +482 -176 2.02.21.4 View file →
@@ -1,24 +1,37 @@
1 1 <?php
2 2
3 3 namespace BitCode\BitForm\Frontend\Form;
4 4
5 +use BitCode\BitForm\Admin\Form\AdminFormHandler;
5 6 use BitCode\BitForm\Admin\Form\FrontEndScriptGenerator;
7 +use BitCode\BitForm\Admin\Form\Helpers;
8 +use BitCode\BitForm\Core\Database\FormEntryMetaModel;
6 9 use BitCode\BitForm\Core\Database\FormModel;
7 10 use BitCode\BitForm\Core\Form\FormManager;
8 11 use BitCode\BitForm\Core\Integration\IntegrationHandler;
9 12 use BitCode\BitForm\Core\Util\FieldValueHandler;
13 +use BitCode\BitForm\Core\Util\FileDownloadProvider;
10 14 use BitCode\BitForm\Core\Util\FrontendHelpers;
15 +use BitCode\BitForm\Core\Util\SmartTags;
16 +use BitCode\BitForm\Core\Util\Utilities;
11 17 use BitCode\BitForm\Core\WorkFlow\WorkFlow;
18 +use BitCode\BitFormPro\Admin\FormSettings\FormAbandonment;
12 19
13 -final class FrontendFormHandler {
14 - public function __construct() {
20 +final class FrontendFormHandler
21 +{
22 + public function __construct()
23 + {
24 + // before markup load - formids [], posts [1,2]
15 25 add_action('wp_enqueue_scripts', [$this, 'loadAssets']);
26 + // markup loads - formids []
27 + add_shortcode('bitform', [$this, 'handleFrontendRenderRequest']);
28 + // after markup load - formids [1,35,3]
16 29 add_action('wp_footer', [$this, 'generateJS']);
17 - add_shortcode('bitform', [$this, 'handleFrontendRenderRequest']);
18 30 }
19 31
20 - private function validPassowordResetToken($token, $userID, $formId) {
32 + private function validPassowordResetToken($token, $userID, $formId)
33 + {
21 34 $existResetInteg = (new IntegrationHandler($formId))->getAllIntegration('wp_user_auth', 'wp_auth', 1);
22 35 if (!is_wp_error($existResetInteg) && count($existResetInteg) > 0) {
23 36 if ('reset' === $existResetInteg[0]->integration_name) {
24 37 $user = get_userdata($userID);
@@ -35,9 +48,10 @@
35 48 }
36 49 }
37 50 }
38 51
39 - private function getJSFileSrc($postId) {
52 + private function getJSFileSrc($postId)
53 + {
40 54 $formUpdateVersion = get_option('bit-form_form_update_version');
41 55 $formScriptSrc = BITFORMS_UPLOAD_BASE_URL . "/form-scripts/$postId/bitform-js-$postId.js?bfv=$formUpdateVersion";
42 56
43 57 return $formScriptSrc;
@@ -42,17 +56,20 @@
42 56
43 57 return $formScriptSrc;
44 58 }
45 59
46 - public function generateJs($formID = null) {
60 + public function generateJs($formID = null, $entryID = null, $formType = null)
61 + {
62 + // return true;
47 63 $isFormPreview = get_transient('bitform_form_preview');
48 64 if ($isFormPreview && !$formID) {
49 65 delete_transient('bitform_form_preview');
50 66 return;
51 67 }
52 - FrontendHelpers::isPageBuilder();
53 - global $isPageBuilder;
54 - if ($isPageBuilder) {
68 + $frontendScriptGenObj = new FrontEndScriptGenerator();
69 + $isPageBuilder = FrontendHelpers::checkIsPageBuilder($_SERVER);
70 + $bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds;
71 + if ($isPageBuilder || empty($bfFrontendFormIds)) {
55 72 return;
56 73 }
57 74 // for unique fields ids in the same form (e.g. multiple forms in the same page)
58 75 $allFields = [];
@@ -58,23 +75,36 @@
58 75 $allFields = [];
59 76 $formContents = [];
60 77 $contentIds = [];
61 78 $formIDs = [];
62 - $preview = false;
79 + $previewMode = 'classic';
63 80 $postId = '';
64 81
82 + $formUpdateVersion = get_option('bit-form_form_update_version');
65 83 if ($formID) {
66 84 $formIDs[] = $formID;
67 - $FrontendFormManager = new FrontendFormManager($formID, 1);
85 + $FrontendFormManager = FrontendFormManager::getInstance($formID, 1);
86 + $formInfo = $FrontendFormManager->getFormInfo();
68 87 $FormIdentifier = esc_js($FrontendFormManager->getFormIdentifier());
69 88 $formContent = $FrontendFormManager->getFormContentWithValue($this->getValuesFromQueryParams());
89 + $formContent->formId = $formID;
70 90 $formContents[] = $formContent;
71 - $fields = $this->triggerWorkflowOnLoad($formID, 1, $formContent->fields);
91 + $workFlowRunType = $entryID ? 'edit' : 'create';
92 + $fields = $formContent->fields;
93 + if ($entryID) {
94 + $fields = $this->setFieldsValue($fields, $formID, $entryID);
95 + }
96 + $fields = $this->triggerWorkflowOnLoad($formID, 1, $fields, $workFlowRunType);
72 97 array_push($contentIds, $FormIdentifier);
98 +
73 99 foreach ($fields as $fk => $field) {
74 100 $allFields[$field->typ][] = ['fk' => $fk, 'field' => $field, 'formID' => $formID, 'contentId' => $FormIdentifier];
75 101 }
76 - $preview = true;
102 + //Generate JS file for conversational form
103 + if (!empty($formInfo->conversationalSettings->enable) && $formInfo->conversationalSettings->enable) {
104 + $frontendScriptGenObj->generateJsFile([$formContent], $allFields, [$FormIdentifier], $formID, [$formID], 'conversational');
105 + }
106 + $previewMode = 'preview';
77 107 $postId = $formID;
78 108 } else {
79 109 global $post;
80 110 if (!is_object($post) && !isset($post->ID)) {
@@ -79,43 +109,54 @@
79 109 global $post;
80 110 if (!is_object($post) && !isset($post->ID)) {
81 111 return;
82 112 }
83 - FrontendHelpers::handleBfFormIdsSession();
84 - global $bfFrontendFormIds;
85 - $bfUniqFormIds = array_unique($bfFrontendFormIds);
113 + $bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds;
114 + $bfUniqFormIds = FrontendHelpers::getAllUniqFormIdsInPage();
115 + $formIDs = $bfUniqFormIds;
116 + $regenerateScriptFlag = $this->regenerateScriptChecker($bfUniqFormIds);
86 117
87 - $regenerateScriptFlag = $this->regenerateScriptChecker($bfUniqFormIds);
88 118 $postId = $post->ID;
89 119 if (!$regenerateScriptFlag) {
90 120 $regenerateScriptFlag = $this->deleteUnusedFormPageIds($postId, $bfUniqFormIds);
91 121 }
92 122 $isJsGenerating = get_option('bitforms_frontend_js_generating');
93 - if (!$regenerateScriptFlag && !$isJsGenerating) {
94 - wp_enqueue_script('bit-form-all-script-test', $this->getJSFileSrc($postId), [], '', true);
123 + if (!$regenerateScriptFlag && !$isJsGenerating && !empty($formIDs)) {
124 + wp_enqueue_script('bit-form-all-script-test', $this->getJSFileSrc($postId), [], $formUpdateVersion, true);
95 125 return;
96 126 }
97 -
98 - $formIDs = $bfUniqFormIds;
99 -
100 127 foreach ($bfFrontendFormIds as $index => $formId) {
101 128 $shortCodeCounter = $index + 1;
102 - $FrontendFormManager = new FrontendFormManager($formId, $shortCodeCounter);
129 + $FrontendFormManager = FrontendFormManager::getInstance($formId, $shortCodeCounter);
130 + $formInfo = $FrontendFormManager->getFormInfo();
103 131 $FormIdentifier = esc_js($FrontendFormManager->getFormIdentifier());
104 132 $formContent = $FrontendFormManager->getFormContentWithValue($this->getValuesFromQueryParams());
133 + $formContent->formId = $formId;
105 134 $formContents[] = $formContent;
106 135 $fields = $this->triggerWorkflowOnLoad($formId, $shortCodeCounter, $formContent->fields);
107 - array_push($contentIds, $FormIdentifier);
136 + $contentIds[] = $FormIdentifier;
137 + $formFields = []; // indivisual form fields array for conversational view
108 138 foreach ($fields as $fk => $field) {
109 - $allFields[$field->typ][] = ['fk' => $fk, 'field' => $field, 'formID' => $formId, 'contentId' => $FormIdentifier];
139 + $fieldArr = ['fk' => $fk, 'field' => $field, 'formID' => $formId, 'contentId' => $FormIdentifier];
140 + $allFields[$field->typ][] = $fieldArr;
141 + $formFields[$field->typ][] = $fieldArr;
110 142 }
143 + //Generate JS file for conversational form
144 + if (!empty($formInfo->conversationalSettings->enable) && $formInfo->conversationalSettings->enable) {
145 + $frontendScriptGenObj->generateJsFile([$formContent], $formFields, [$FormIdentifier], $formId, [$formId], 'conversational');
146 + }
111 147 }
112 148 }
113 - (new FrontEndScriptGenerator())->generateJsFile($formContents, $allFields, $contentIds, $postId, $formIDs, $preview);
114 - wp_enqueue_script('bit-form-all-script-test', $this->getJSFileSrc($postId), [], '', true);
149 + if (empty($formIDs)) {
150 + return;
151 + }
152 +
153 + $frontendScriptGenObj->generateJsFile($formContents, $allFields, $contentIds, $postId, $formIDs, $previewMode);
154 + wp_enqueue_script('bit-form-all-script-test', $this->getJSFileSrc($postId), [], $formUpdateVersion, true);
115 155 }
116 156
117 - private function deleteUnusedFormPageIds($postId, $formIDs) {
157 + private function deleteUnusedFormPageIds($postId, $formIDs)
158 + {
118 159 global $post;
119 160 if (!is_object($post) && !isset($post->ID)) {
120 161 return;
121 162 }
@@ -130,18 +171,31 @@
130 171 $generatedScriptPageIdsDecoded = json_decode($form->generated_script_page_ids, true);
131 172 $generatedScriptPageIds = is_array($generatedScriptPageIdsDecoded) ? array_keys($generatedScriptPageIdsDecoded) : [];
132 173 if (!empty($generatedScriptPageIds) && !in_array($formId, $formIDs) && in_array($postId, $generatedScriptPageIds)) {
133 174 unset($generatedScriptPageIdsDecoded[$postId]);
175 + if (empty($generatedScriptPageIdsDecoded)) {
176 + $generatedScriptPageIdsDecoded = new \stdClass();
177 + }
134 178 $regenerateScriptFlag = true;
135 179 $formModel->update(['generated_script_page_ids' => wp_json_encode($generatedScriptPageIdsDecoded)], ['id' => $formId]);
136 180 }
137 181 }
182 + if ($regenerateScriptFlag) {
183 + $formUpdateVersion = get_option('bit-form_form_update_version');
184 + if (!$formUpdateVersion) {
185 + $formUpdateVersion = 1;
186 + } else {
187 + $formUpdateVersion = (int) $formUpdateVersion + 1;
188 + }
189 + update_option('bit-form_form_update_version', $formUpdateVersion);
190 + }
138 191 return $regenerateScriptFlag;
139 192 }
140 193
141 - private function regenerateScriptChecker($formsIds) {
194 + private function regenerateScriptChecker($formsIds)
195 + {
142 196 global $post;
143 - if (!is_object($post) && !isset($post->ID)) {
197 + if (!is_a($post, 'WP_Post') && !isset($post->ID)) {
144 198 return;
145 199 }
146 200 $postId = $post->ID;
147 201 $regenerateScriptFlag = false;
@@ -146,11 +200,13 @@
146 200 $postId = $post->ID;
147 201 $regenerateScriptFlag = false;
148 202 $formModel = new FormModel();
149 203 foreach ($formsIds as $formId) {
150 - $formInstance = new FormManager($formId);
204 + $formInstance = FormManager::getInstance($formId);
205 + if (!$formInstance->isExist()) {
206 + continue;
207 + }
151 208 $generatedPages = $formInstance->getFormData('generated_script_page_ids');
152 -
153 209 if (empty($generatedPages)) {
154 210 $regenerateScriptFlag = true;
155 211 } elseif (is_object($generatedPages) && (!isset($generatedPages->{$postId}) || (isset($generatedPages->{$postId}) && false === $generatedPages->{$postId}))) {
156 212 $regenerateScriptFlag = true;
@@ -157,9 +213,11 @@
157 213 }
158 214 if (!$regenerateScriptFlag) {
159 215 continue;
160 216 }
161 -
217 + if (!is_object($generatedPages)) {
218 + $generatedPages = (object) [];
219 + }
162 220 $generatedPages->{$postId} = true;
163 221 $formModel->update(
164 222 [
165 223 'generated_script_page_ids' => \wp_json_encode($generatedPages)
@@ -171,9 +229,10 @@
171 229 }
172 230 return $regenerateScriptFlag;
173 231 }
174 232
175 - private function addInlineScript($code, $handle = '', $position = 'after') {
233 + private function addInlineScript($code, $handle = '', $position = 'after')
234 + {
176 235 $scriptHandle = !empty($handle) ? $handle : 'bf-inline-script';
177 236 if (!wp_script_is($scriptHandle)) {
178 237 wp_register_script($scriptHandle, '', [], '', true);
179 238 wp_enqueue_script($scriptHandle);
@@ -180,9 +239,10 @@
180 239 }
181 240 wp_add_inline_script($scriptHandle, $code, $position);
182 241 }
183 242
184 - private function addInlineStyle($code, $handle = '') {
243 + private function addInlineStyle($code, $handle = '')
244 + {
185 245 $styleHandle = !empty($handle) ? $handle : 'bf-inline-style';
186 246 if (!wp_style_is($styleHandle)) {
187 247 wp_register_style($styleHandle, '', [], '', true);
188 248 wp_enqueue_style($styleHandle);
@@ -189,9 +249,10 @@
189 249 }
190 250 wp_add_inline_style($styleHandle, $code);
191 251 }
192 252
193 - private function triggerWorkflowOnLoad($formID, $shortCodeCounter, $fields) {
253 + private function triggerWorkflowOnLoad($formID, $shortCodeCounter, $fields, $workFlowRunType = 'create')
254 + {
194 255 $FrontendFormManager = new FrontendFormManager($formID, $shortCodeCounter);
195 256 $previousValue = $this->getValuesFromQueryParams();
196 257 $formContent = $FrontendFormManager->getFormContentWithValue($previousValue);
197 258 if (!empty($formContent->workFlowExist)) {
@@ -197,9 +258,9 @@
197 258 if (!empty($formContent->workFlowExist)) {
198 259 $workFlowRunHelper = new WorkFlow($formID);
199 260 if (!empty($formContent->workFlowExist->onload)) {
200 261 $workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad(
201 - 'create',
262 + $workFlowRunType,
202 263 $fields
203 264 );
204 265
205 266 if (!empty($workFlowreturnedOnLoad['fields'])) {
@@ -210,10 +271,11 @@
210 271
211 272 return $fields;
212 273 }
213 274
214 - private function executeOnUserInput($formID, $shortCodeCounter, $fields) {
215 - $FrontendFormManager = new FrontendFormManager($formID, $shortCodeCounter);
275 + private function executeOnUserInput($formID, $shortCodeCounter, $fields)
276 + {
277 + $FrontendFormManager = FrontendFormManager::getInstance($formID, $shortCodeCounter);
216 278 $previousValue = $this->getValuesFromQueryParams();
217 279 $formContent = $FrontendFormManager->getFormContentWithValue($previousValue);
218 280 $customCodesExist = strpos(FrontEndScriptGenerator::getCustomCodes($formID)['JavaScript'], 'bfVars');
219 281 if ($customCodesExist || (!empty($formContent->workFlowExist) && !empty($formContent->workFlowExist->oninput))) {
@@ -221,12 +283,13 @@
221 283 return $workFlowRunHelper->executeOnUserInput('create', $fields);
222 284 }
223 285 }
224 286
225 - private function getValuesFromQueryParams() {
226 - $reqField = $_SERVER['QUERY_STRING'];
287 + private function getValuesFromQueryParams()
288 + {
227 289 $queryParamsValue = [];
228 - if (!empty($reqField)) {
290 + if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
291 + $reqField = $_SERVER['QUERY_STRING'];
229 292 foreach (explode('&', $reqField) as $keyValue) {
230 293 // $pattern = '/([a-zA-Z0-9])([a-zA-Z])\=+/';
231 294 $pattern = '/([^.]+)=(.*?)([^.]+)/';
232 295 $matches = preg_match($pattern, $keyValue, $matchFormat);
@@ -244,16 +307,23 @@
244 307
245 308 return $queryParamsValue;
246 309 }
247 310
248 - public function handleFrontendRenderRequest($atts, $formPreview = false) {
249 - static $shortCodeCounter = 0;
250 - $shortCodeCounter += 1;
251 -
252 - if ($formPreview) {
253 - // when $formPreview is true, it means that the atts is the form id
254 - $formID = $atts;
311 + public function handleFrontendRenderRequest($atts)
312 + {
313 + $formType = isset($atts['type']) ? $atts['type'] : 'classic';
314 + $formPreview = isset($atts['form_preview']) ? $atts['form_preview'] : false;
315 + if (isset($atts['form_id'])) {
316 + $formID = intval($atts['form_id']);
317 + }
318 + if (isset($atts['entry_id'])) {
319 + $entryId = intval($atts['entry_id']);
320 + } elseif (isset($_GET['bf_entry_id'])) {
321 + $entryId = $_GET['bf_entry_id'];
255 322 } else {
323 + $entryId = false;
324 + }
325 + if (isset($atts['id'])) {
256 326 $atts = shortcode_atts(['id' => 0], $atts);
257 327 $formID = intval($atts['id']);
258 328 }
259 329
@@ -263,15 +333,31 @@
263 333
264 334 if (!$this->isExist($formID)) {
265 335 return sprintf(__('#%s no. Form doesn\'t exists', 'bit-form'), $formID);
266 336 }
267 - FrontendHelpers::handleBfFormIdsSession($formID);
268 - $FrontendFormManager = new FrontendFormManager($formID, $shortCodeCounter);
269 337
338 + // check for abandoned form entry id
339 + $isAbandoned = false;
340 + if (empty($entryId) && Utilities::isPro() && class_exists('\BitCode\BitFormPro\Admin\FormSettings\FormAbandonment')) {
341 + $FormAbandonment = new FormAbandonment($formID);
342 + $isAbandoned = $FormAbandonment->checkAbandonedFormEntryId();
343 + }
344 +
345 + FrontendHelpers::setBfFrontendFormIds($formID);
346 + $bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds;
347 + $shortCodeCounter = count($bfFrontendFormIds);
348 + $FrontendFormManager = FrontendFormManager::getInstance($formID, $shortCodeCounter);
349 +
350 + if (!$FrontendFormManager->checkStatus()) {
351 + return sprintf(__('#%s no. Form is not active', 'bit-form'), $formID);
352 + }
353 + ob_start();
354 + $this->loadAssets($formID, $formType);
355 +
270 356 $font = $FrontendFormManager->getFont();
271 357
272 358 if ($font && !$formPreview) {
273 - wp_enqueue_style('google-font', $font, '1.0.0', true);
359 + wp_enqueue_style('bf-google-font', $font, '1.0.0', true);
274 360 }
275 361
276 362 if (!empty($_GET['token']) && !empty($_GET['id'])) {
277 363 $this->validPassowordResetToken($_GET['token'], $_GET['id'], $formID);
@@ -282,137 +368,258 @@
282 368 $FormIdentifier = esc_js($FrontendFormManager->getFormIdentifier());
283 369 $nonce = $FrontendFormManager->getFormToken();
284 370 $file = count($FrontendFormManager->getUploadFields()) > 0 ? $FrontendFormManager->getUploadFields() : false;
285 371
286 - if ($FrontendFormManager->checkStatus()) {
287 - $FrontendFormManager->setViewCount();
372 + $FrontendFormManager->setViewCount();
288 373
289 - $formContent = $FrontendFormManager->getFormContentWithValue($previousValue);
290 - $fields = $formContent->fields;
291 - $layout = $formContent->layout;
292 - $buttons = !empty($formContent->buttons) ? $formContent->buttons : '';
293 - $additional = $formContent->additional;
374 + $formContent = $FrontendFormManager->getFormContentWithValue($previousValue);
375 + $fields = $formContent->fields;
376 + $layout = $formContent->layout;
377 + $nestedLayout = isset($formContent->nestedLayout) ? $formContent->nestedLayout : (object) [];
378 + $buttons = !empty($formContent->buttons) ? $formContent->buttons : '';
379 + $additional = $formContent->additional;
294 380
295 - $fields = $this->triggerWorkflowOnLoad($formID, $shortCodeCounter, $fields);
296 - $workFlowreturnedOnUserInput = $this->executeOnUserInput($formID, $shortCodeCounter, $fields);
381 + // $workFlowRunType = $entryId ? 'edit' : 'create';
382 + if ($entryId && (FrontendHelpers::is_current_user_can_access($formID, 'entryEditAccess'))) {
383 + $workFlowRunType = 'edit';
384 + $adminFormHandler = new AdminFormHandler();
385 + $getEntry = $adminFormHandler->getSingleEntry($formID, $entryId);
386 + if (FrontendHelpers::is_current_user_can_access($formID, 'entryEditAccess', '', $getEntry->__user_id)) {
387 + $fields = $this->setFieldsValue($fields, $formID, $entryId);
388 + } elseif (!$isAbandoned) {
389 + $entryId = false;
390 + $workFlowRunType = 'create';
391 + }
392 + } else {
393 + $entryId = false;
394 + $workFlowRunType = 'create';
395 + }
297 396
298 - // only for form not preview
299 - if (!wp_style_is('bitform-style' . $formID)) {
300 - $this->loadAssets(true, $formID, $file);
397 + // if ($entryId) {
398 + // $fields = $this->setFieldsValue($fields, $formID, $entryId);
399 + // }
400 +
401 + $fields = apply_filters('bitform_filter_before_workflow_onload_fields', $fields, $formID);
402 + $fields = $this->triggerWorkflowOnLoad($formID, $shortCodeCounter, $fields, $workFlowRunType);
403 + $fields = apply_filters('bitform_filter_after_workflow_onload_fields', $fields, $formID);
404 + do_action('bitform_onload_fields', $fields, $formID);
405 + $workFlowreturnedOnUserInput = $this->executeOnUserInput($formID, $shortCodeCounter, $fields);
406 +
407 + // test for form before remove
408 + $noLabelFieldTypes = ['decision-box', 'gdpr', 'html', 'shortcode', 'button', 'paypal', 'razorpay', 'recaptcha', 'turnstile', 'hcaptcha', 'stripe', 'spacer'];
409 + foreach ($fields as $fldKey => $field) {
410 + if (!in_array($field->typ, $noLabelFieldTypes) && isset($field->lbl)) {
411 + $lblReplaceToBackslash = str_replace('$_bf_$', '\\', $field->lbl);
412 + $fields->{$fldKey}->lbl = FieldValueHandler::replaceSmartTagWithValue($lblReplaceToBackslash);
301 413 }
414 + }
415 + $fieldsKey = $FrontendFormManager->getFieldsKey();
302 416
303 - // test for form before remove
304 - $noLabel = ['decision-box', 'html', 'button', 'paypal', 'razorpay', 'recaptcha'];
305 - $newFields = json_decode(json_encode($fields), true);
306 - foreach ($newFields as $fldKey => $field) {
307 - if (!in_array($field['typ'], $noLabel) && isset($field['lbl'])) {
308 - $lblReplaceToBackslash = str_replace('$_bf_$', '\\', $field['lbl']);
309 - $newFields[$fldKey]['lbl'] = FieldValueHandler::replaceSmartTagWithValue($lblReplaceToBackslash);
310 - }
311 - }
312 - $fieldsKey = $FrontendFormManager->getFieldsKey();
417 + $captchaV3Settings = $FrontendFormManager->getCaptchaV3Settings();
418 + if ($FrontendFormManager->getCaptchaSettings() || $captchaV3Settings || $FrontendFormManager->getTurnstileSettings() || $FrontendFormManager->isFieldTypeExist('hcaptcha')) {
419 + $integrationHandler = new IntegrationHandler(0);
420 + $allFormIntegrations = $integrationHandler->getAllIntegration('app');
421 + if (!is_wp_error($allFormIntegrations)) {
422 + foreach ($allFormIntegrations as $integration) {
423 + if (
424 + $FrontendFormManager->getCaptchaSettings()
425 + && !is_null($integration->integration_type)
426 + && 'gReCaptcha' === $integration->integration_type
427 + ) {
428 + $integrationDetails = json_decode($integration->integration_details);
429 + $integrationDetails->id = $integration->id;
430 + $reCAPTCHA = $integrationDetails;
431 + $reCAPTCHAVersion = 'v2';
432 + }
313 433
314 - $captchaV3Settings = $FrontendFormManager->getCaptchaV3Settings();
315 - if ($FrontendFormManager->getCaptchaSettings() || $captchaV3Settings) {
316 - $integrationHandler = new IntegrationHandler(0);
317 - $allFormIntegrations = $integrationHandler->getAllIntegration('app');
318 - if (!is_wp_error($allFormIntegrations)) {
319 - foreach ($allFormIntegrations as $integration) {
320 - if (
321 - $FrontendFormManager->getCaptchaSettings()
322 - && !is_null($integration->integration_type)
323 - && 'gReCaptcha' === $integration->integration_type
324 - ) {
434 + if (
435 + $FrontendFormManager->getTurnstileSettings()
436 + && !is_null($integration->integration_type)
437 + && 'turnstileCaptcha' === $integration->integration_type
438 + ) {
439 + $integrationDetails = json_decode($integration->integration_details);
440 + $turnstileSiteKey = $integrationDetails->siteKey;
441 + }
442 +
443 + if (
444 + $FrontendFormManager->isFieldTypeExist('hcaptcha')
445 + && !is_null($integration->integration_type)
446 + && 'hcaptcha' === $integration->integration_type
447 + ) {
448 + $integrationDetails = json_decode($integration->integration_details);
449 + $hCaptchaSiteKey = $integrationDetails->siteKey;
450 + }
451 +
452 + if ($captchaV3Settings) {
453 + if (!is_null($integration->integration_type) && 'gReCaptchaV3' === $integration->integration_type) {
325 454 $integrationDetails = json_decode($integration->integration_details);
326 455 $integrationDetails->id = $integration->id;
327 456 $reCAPTCHA = $integrationDetails;
328 - $reCAPTCHAVersion = 'v2';
457 + $reCAPTCHAVersion = 'v3';
329 458 }
459 + }
460 + }
461 + }
462 + }
330 463
331 - if ($captchaV3Settings) {
332 - if (!is_null($integration->integration_type) && 'gReCaptchaV3' === $integration->integration_type) {
333 - $integrationDetails = json_decode($integration->integration_details);
334 - $integrationDetails->id = $integration->id;
335 - $reCAPTCHA = $integrationDetails;
336 - $reCAPTCHAVersion = 'v3';
337 - }
338 - }
339 - }
464 + if ($captchaV3Settings && !empty($reCAPTCHA->siteKey)) {
465 + // DANGER: no matter what, DONT CHANGE THE SCRIPT ID OF THIS SCRIPT
466 + $scriptId = BITFORMS_PREFIX . 'recaptcha';
467 + wp_enqueue_script($scriptId, "https://www.google.com/recaptcha/api.js?render={$reCAPTCHA->siteKey}");
468 + }
469 +
470 + $configs = [
471 + 'bf_separator' => BITFORMS_BF_SEPARATOR,
472 + ];
473 +
474 + // check if fields has paypal or razorpay
475 + $paymentFields = ['paypal', 'razorpay', 'stripe'];
476 + $paymentFieldData = [];
477 + foreach ($fields as $key => $field) {
478 + if (in_array($field->typ, $paymentFields)) {
479 + $paymentFieldData[$key] = $field;
480 + }
481 + }
482 +
483 + if (!empty($paymentFieldData)) {
484 + $integrationHandler = new IntegrationHandler(0);
485 + foreach ($paymentFieldData as $fldKey => $fldData) {
486 + $paymentIntegration = $integrationHandler->getAIntegration($fldData->payIntegID);
487 + if (is_wp_error($paymentIntegration)) {
488 + continue;
340 489 }
490 + if ('paypal' === $fldData->typ) {
491 + $integrationDetails = json_decode($paymentIntegration[0]->integration_details);
492 + $clientID = $integrationDetails->clientID;
493 + $fields->{$fldKey}->clientId = $clientID;
494 + } elseif ('razorpay' === $fldData->typ) {
495 + $integrationDetails = json_decode($paymentIntegration[0]->integration_details);
496 + $clientID = $integrationDetails->apiKey;
497 + $fields->{$fldKey}->clientId = $clientID;
498 + } elseif ('stripe' === $fldData->typ) {
499 + $integrationDetails = json_decode($paymentIntegration[0]->integration_details);
500 + $publishableKey = $integrationDetails->publishableKey;
501 + $fields->{$fldKey}->publishableKey = $publishableKey;
502 + }
341 503 }
504 + }
342 505
343 - $configs = [
344 - 'bf_separator' => BITFORMS_BF_SEPARATOR,
506 + $bitFormFrontArr = [
507 + 'ajaxURL' => admin_url('admin-ajax.php'),
508 + 'nonce' => $nonce,
509 + 'version' => BITFORMS_VERSION,
510 + 'layout' => $layout,
511 + 'nestedLayout' => $nestedLayout,
512 + 'fields' => $fields,
513 + 'buttons' => $buttons,
514 + 'fieldsKey' => $fieldsKey,
515 + 'file' => $file,
516 + 'configs' => $configs,
517 + 'formId' => $formID,
518 + 'appID' => "bitforms_{$formID}",
519 + 'GCLID' => $FrontendFormManager->isGCLIDEnabled(),
520 + 'assetUrl' => BITFORMS_ASSET_URI,
521 + 'onfieldCondition' => !empty($workFlowreturnedOnUserInput['onfield_input_conditions']) ? $workFlowreturnedOnUserInput['onfield_input_conditions'] : false,
522 + 'smartTags' => SmartTags::smartTags(SmartTags::getPostUserData()),
523 + 'paymentCallbackUrl' => get_rest_url() . 'bitform/v1/payments/razorpay',
524 + 'gRecaptchaSiteKey' => !empty($reCAPTCHA->siteKey) ? $reCAPTCHA->siteKey : null,
525 + 'gRecaptchaVersion' => !empty($reCAPTCHAVersion) ? $reCAPTCHAVersion : null,
526 + 'turnstileSiteKey' => !empty($turnstileSiteKey) ? $turnstileSiteKey : null,
527 + 'hCaptchaSiteKey' => !empty($hCaptchaSiteKey) ? $hCaptchaSiteKey : null,
528 + ];
529 +
530 + if ($entryId) {
531 + $bitFormFrontArr['entryId'] = $entryId;
532 + }
533 +
534 + if (isset($additional->enabled->validateFocusLost)) {
535 + $bitFormFrontArr['validateFocusLost'] = true;
536 + }
537 +
538 + if (!empty($isAbandoned)) {
539 + $bitFormFrontArr['oldValues'] = $this->getFieldsValue($formID, $isAbandoned);
540 + if (empty($entryId)) {
541 + $bitFormFrontArr['entryId'] = $isAbandoned;
542 + }
543 + }
544 +
545 + $formInfo = $FrontendFormManager->getFormInfo();
546 + if (is_array($layout) && count($layout) > 1) {
547 + $multiStepSettings = isset($formInfo->multiStepSettings) ? $formInfo->multiStepSettings : null;
548 + $newTempSettings = (object) [
549 + 'validateOnStepChange' => isset($multiStepSettings->validateOnStepChange) ? $multiStepSettings->validateOnStepChange : false,
550 + 'maintainStepHistory' => isset($multiStepSettings->maintainStepHistory) ? $multiStepSettings->maintainStepHistory : false,
551 + 'saveProgress' => isset($multiStepSettings->saveProgress) ? $multiStepSettings->saveProgress : false,
552 + 'showPercentage' => isset($multiStepSettings->progressSettings->showPercentage) ? $multiStepSettings->progressSettings->showPercentage : false,
345 553 ];
346 -
347 - $bitFormFrontArr = [
348 - 'ajaxURL' => admin_url('admin-ajax.php'),
349 - 'nonce' => $nonce,
350 - 'version' => BITFORMS_VERSION,
351 - 'layout' => $layout,
352 - 'fields' => $newFields,
353 - 'buttons' => $buttons,
354 - 'fieldsKey' => $fieldsKey,
355 - 'file' => $file,
356 - 'configs' => $configs,
357 - 'formId' => $formID,
358 - 'GCLID' => $FrontendFormManager->isGCLIDEnabled(),
359 - 'assetUrl' => BITFORMS_ASSET_URI,
360 - 'onfieldCondition' => !empty($workFlowreturnedOnUserInput['onfield_input_conditions']) ? $workFlowreturnedOnUserInput['onfield_input_conditions'] : false,
361 - 'smartTags' => !empty($workFlowreturnedOnUserInput['smart_tags']) ? $workFlowreturnedOnUserInput['smart_tags'] : [],
362 - 'paymentCallbackUrl' => get_rest_url() . 'bitform/v1/payments/razorpay',
363 - 'gRecaptchaSiteKey' => !empty($reCAPTCHA->siteKey) ? $reCAPTCHA->siteKey : null,
364 - 'gRecaptchaVersion' => !empty($reCAPTCHAVersion) ? $reCAPTCHAVersion : null,
554 + $bitFormFrontArr['formInfo'] = (object) [
555 + 'multiStepSettings' => $newTempSettings
365 556 ];
557 + }
366 558
367 - if (isset($additional->enabled->validateFocusLost)) {
368 - $bitFormFrontArr['validateFocusLost'] = true;
559 + if (Helpers::property_exists_nested($formInfo, 'conversationalSettings->enable', true)) {
560 + if (!isset($bitFormFrontArr['formInfo'])) {
561 + $bitFormFrontArr['formInfo'] = new \stdClass();
369 562 }
563 + $bitFormFrontArr['formInfo']->conversationalSettings = $formInfo->conversationalSettings;
564 + }
370 565
371 - $bitFormsFront = apply_filters(
372 - 'bitforms_localized_script',
373 - $bitFormFrontArr
374 - );
566 + $formAbandonmentSettings = $FrontendFormManager->getFormAbandonmentSettings();
567 + if (Helpers::property_exists_nested($formAbandonmentSettings, 'active', true)) {
568 + $bitFormFrontArr['formSettings'] = (object)[
569 + 'formAbandonment' => $formAbandonmentSettings
570 + ];
571 + }
375 572
376 - $fields = \json_encode($fields);
377 - $layout = \json_encode($layout);
378 - $buttons = \json_encode($buttons);
379 - $frontArr = json_encode($bitFormFrontArr);
573 + $bitFormsFront = apply_filters(
574 + 'bitforms_localized_script',
575 + $bitFormFrontArr
576 + );
380 577
381 - $bfGlobals = "if(!window.bf_globals) {
578 + $layout = wp_json_encode($layout);
579 + $buttons = wp_json_encode($buttons);
580 + $frontArr = wp_json_encode($bitFormFrontArr);
581 +
582 + $bfGlobals = <<<BFGLOBALS
583 + if(!window.bf_globals) {
382 584 window.bf_globals = {}
383 585 } if(!window.bf_globals.{$FormIdentifier}) {
384 586 window.bf_globals.{$FormIdentifier} = {}
385 587 }
386 - window.bf_globals.{$FormIdentifier} = {...window.bf_globals.{$FormIdentifier}, ...{$frontArr}};";
387 - $this->addInlineScript($bfGlobals, 'bit-form-all-script', 'before');
588 + if(document.getElementById('{$FormIdentifier}')) {
589 + window.bf_globals.{$FormIdentifier} = {
590 + ...window.bf_globals.{$FormIdentifier},
591 + ...{$frontArr}
592 + };
593 + }
594 +BFGLOBALS;
388 595
389 - $html = '';
596 + if ('conversational' === $formType
597 + && isset($formContent->formInfo->conversationalSettings->enable)
598 + && $formContent->formInfo->conversationalSettings->enable) {
599 + $html = $FrontendFormManager->conversationalFormView($fields, $file, $errorMessages);
600 + } else {
601 + $html = $FrontendFormManager->formView($fields, $file, $errorMessages);
602 + }
390 603
391 - FrontendHelpers::isPageBuilder();
392 - global $isPageBuilder;
393 - if ($isPageBuilder) {
394 - $newFormId = $formID . '-formid';
395 - $formStyles = file_get_contents(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $newFormId . '.css');
396 - $html .= '<style>' . $formStyles . '</style>';
397 - }
398 - $html .= $FrontendFormManager->formView($fields, $file, $errorMessages);
399 - // if form preview then return html otherwise echo with output buffer
400 - if ($formPreview) {
401 - $formViewObject = new \stdClass();
402 - $formViewObject->html = $html;
403 - $formViewObject->font = $font;
404 - $formViewObject->bfGlobals = $bfGlobals;
405 - return $formViewObject;
406 - }
407 -
408 - ob_start();
409 - echo trim($html);
410 - return ob_get_clean();
604 + // if form preview then return html otherwise echo with output buffer
605 + if ($formPreview) {
606 + ob_clean();
607 + $formViewObject = new \stdClass();
608 + $formViewObject->html = $html;
609 + $formViewObject->font = $font;
610 + $formViewObject->bfGlobals = $bfGlobals;
611 + return $formViewObject;
411 612 }
613 + $html .= <<<BFGLOBALSSCRIPT
614 + <script id="bit-form-bf-globals-{$FormIdentifier}">{$bfGlobals}</script>
615 +BFGLOBALSSCRIPT;
616 + echo trim($html);
617 + return ob_get_clean();
412 618 }
413 619
414 - private function isExist($formID) {
620 + private function isExist($formID)
621 + {
415 622 $formModel = new FormModel();
416 623 $form = $formModel->get(
417 624 [
418 625 'id'
@@ -426,50 +633,149 @@
426 633 }
427 634 return false;
428 635 }
429 636
430 - public function loadAssets($recheck = false, $formID = 0, $isFileExists = false) {
431 - FrontendHelpers::getFormIdsFromPost();
432 - FrontendHelpers::isPageBuilder();
433 - global $bfUniqFormIds;
434 - global $bfMultipleFormsExists;
637 + private function getFieldsValue($formID, $entryID)
638 + {
639 + $FrontendFormManager = FrontendFormManager::getInstance($formID, 1);
640 + $formEntryModel = new FormEntryMetaModel();
641 + $metaValues = $formEntryModel->get(
642 + [
643 + 'meta_key',
644 + 'meta_value'
645 + ],
646 + [
647 + 'bitforms_form_entry_id' => $entryID,
648 + ]
649 + );
650 + $formFields = $FrontendFormManager->getFields();
651 + $fldsData = (object) [];
652 + if (!is_wp_error($metaValues)) {
653 + foreach ($metaValues as $metaValue) {
654 + $metaKey = $metaValue->meta_key;
655 + $metaVal = $metaValue->meta_value;
656 + // if meta value is array then convert to string
657 + if (preg_match('/^\[.*\]$/', $metaVal)) {
658 + $metaVal = json_decode($metaVal);
659 + //check is it array of objects
660 + if (is_array($metaVal) && is_object($metaVal[0])) {
661 + $metaVal = $metaValue->meta_value;
662 + } else {
663 + $metaVal = implode(BITFORMS_BF_SEPARATOR, $metaVal);
664 + }
665 + }
666 + if (!isset($fldsData->{$metaKey})) {
667 + $fldsData->{$metaKey} = '';
668 + }
669 + $fldsData->{$metaKey} = $metaVal;
670 + if (isset($formFields[$metaKey]['type']) && in_array($formFields[$metaKey]['type'], ['file-up', 'advanced-file-up'])) {
671 + $fldsData->{$metaKey} = $metaValue->meta_value;
672 + }
673 + }
674 + }
435 675
436 - if ($formID) {
676 + return $fldsData;
677 + }
678 +
679 + public function setFieldsValue($fields, $formID, $entryID)
680 + {
681 + $formEntryModel = new FormEntryMetaModel();
682 + $metaValues = $formEntryModel->get(
683 + [
684 + 'meta_key',
685 + 'meta_value'
686 + ],
687 + [
688 + 'bitforms_form_entry_id' => $entryID,
689 + ]
690 + );
691 + if (!is_wp_error($metaValues)) {
692 + foreach ($metaValues as $metaValue) {
693 + $metaKey = $metaValue->meta_key;
694 + $metaVal = $metaValue->meta_value;
695 + // if meta value is array then convert to string
696 + if (preg_match('/^\[.*\]$/', $metaVal)) {
697 + $metaVal = json_decode($metaVal);
698 + //check is it array of objects
699 + if (is_array($metaVal) && is_object($metaVal[0])) {
700 + $metaVal = $metaValue->meta_value;
701 + } else {
702 + $metaVal = implode(BITFORMS_BF_SEPARATOR, $metaVal);
703 + }
704 + }
705 + if (property_exists($fields, $metaKey)) {
706 + $fields->{$metaKey}->val = $metaVal;
707 + if ('file-up' === $fields->{$metaKey}->typ || 'advanced-file-up' === $fields->{$metaKey}->typ) {
708 + $fields->{$metaKey}->val = $metaValue->meta_value;
709 + $fields->{$metaKey}->config->oldFiles = $metaValue->meta_value;
710 + $urlQuery = wp_parse_url(FileDownloadProvider::getBaseDownloadURL(), PHP_URL_QUERY);
711 + $baseDLURL = FileDownloadProvider::getBaseDownloadURL();
712 + $baseDLURL = empty($urlQuery) ? $baseDLURL . '?' : $baseDLURL . '&';
713 + $fields->{$metaKey}->config->baseDLURL = $baseDLURL . "formID={$formID}&entryID={$entryID}";
714 + }
715 + }
716 + }
717 + }
718 + return $fields;
719 + }
720 +
721 + public function loadAssets($formID = 0, $fromType = 'classic')
722 + {
723 + $bfUniqFormIds = FrontendHelpers::getAllFormIdsInPage();
724 + $isPageBuilder = FrontendHelpers::$isPageBuilder;
725 + $bfMultipleFormsExists = $isPageBuilder ? true : count($bfUniqFormIds) > 1;
726 +
727 + if (!empty($formID)) {
437 728 $formIds = [$formID];
438 - if (count($bfUniqFormIds) > 1) {
439 - $bfMultipleFormsExists = true;
440 - }
441 729 } else {
442 730 $formIds = $bfUniqFormIds;
443 731 }
444 -
445 732 foreach ($formIds as $formID) {
733 + global $bitform_dequeued_styles;
734 + if (is_array($bitform_dequeued_styles) && in_array($formID, $bitform_dequeued_styles)) {
735 + continue;
736 + }
446 737 if ($bfMultipleFormsExists) {
447 738 $newFormId = $formID . '-formid';
448 739 } else {
449 740 $newFormId = $formID;
450 741 }
451 - if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $newFormId . '.css')) {
452 - $styleModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $newFormId . '.css');
453 - wp_register_style(
454 - 'bitform-style' . $formID,
455 - BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-' . $newFormId . '.css',
742 + $formUpdateVersion = get_option('bit-form_form_update_version');
743 + if (!wp_style_is('bitform-style-' . $newFormId) && is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $newFormId . '.css')) {
744 + wp_enqueue_style(
745 + 'bitform-style-' . $newFormId,
746 + BITFORMS_UPLOAD_BASE_URL . "/form-styles/bitform-{$newFormId}.css",
456 747 [],
457 - $styleModifyTime,
458 - 'all'
748 + $formUpdateVersion
459 749 );
460 - wp_enqueue_style('bitform-style' . $formID);
750 + if ($isPageBuilder) {
751 + $formStyle = file_get_contents(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $newFormId . '.css');
752 + echo sprintf("<style id='bitform-style-{$newFormId}'>%s</style>", $formStyle);
753 + }
461 754 }
462 - if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-custom-' . $newFormId . '.css')) {
463 - $styleModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-custom-' . $newFormId . '.css');
464 - wp_register_style(
755 + if (!wp_style_is('bitform-style-custom-' . $formID) && is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-custom-' . $formID . '.css')) {
756 + wp_enqueue_style(
465 757 'bitform-style-custom-' . $formID,
466 - BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-custom-' . $newFormId . '.css',
758 + BITFORMS_UPLOAD_BASE_URL . "/form-styles/bitform-custom-{$formID}.css",
467 759 [],
468 - $styleModifyTime,
469 - 'all'
760 + $formUpdateVersion
470 761 );
471 - wp_enqueue_style('bitform-style-custom-' . $formID);
762 + if ($isPageBuilder) {
763 + $formStyle = file_get_contents(BITFORMS_CONTENT_DIR . '/form-styles/bitform-custom-' . $formID . '.css');
764 + echo sprintf("<style id='bitform-style-custom-{$formID}'>%s</style>", $formStyle);
765 + }
766 + }
767 + // load conversational form css
768 + if ('conversational' === $fromType) {
769 + if (!wp_style_is('bitform-conversational-style-' . $formID) &&
770 + is_readable(BITFORMS_CONTENT_DIR . "/form-styles/bitform-conversational-{$formID}.css")) {
771 + wp_enqueue_style(
772 + 'bitform-conversational-style',
773 + BITFORMS_UPLOAD_BASE_URL . "/form-styles/bitform-conversational-{$formID}.css",
774 + [],
775 + $formUpdateVersion
776 + );
777 + }
472 778 }
473 779 }
474 780 }
475 781 }