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

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