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
← All changes | includes/Admin/Form/FrontEndScriptGenerator.php +300 -157 2.02.7.0 View file →
@@ -1,15 +1,22 @@
1 1 <?php
2 2
3 3 namespace BitCode\BitForm\Admin\Form;
4 4
5 +use BitCode\BitForm\Admin\Form\InitJs\Paypal;
6 +use BitCode\BitForm\Admin\Form\InitJs\Razorpay;
7 +use BitCode\BitForm\Admin\Form\InitJs\Recaptcha;
5 8 use BitCode\BitForm\Admin\Form\InitJs\RecaptchaV3;
6 -use BitCode\BitForm\Core\Form\FormManager;
7 -use BitCode\BitForm\Core\Integration\IntegrationHandler;
9 +use BitCode\BitForm\Admin\Form\InitJs\ScriptLoader;
10 +use BitCode\BitForm\Admin\Form\InitJs\Stripe;
8 11 use BitCode\BitForm\Core\Util\Log;
12 +use BitCode\BitForm\Core\Util\Utilities;
13 +use BitCode\BitForm\Core\WorkFlow\WorkFlowHandler;
14 +use BitCode\BitFormPro\Admin\FormSettings\FormAbandonment;
9 15 use WP_Error;
10 16
11 -class FrontEndScriptGenerator {
17 +class FrontEndScriptGenerator
18 +{
12 19 private $_scriptAddedFlags;
13 20 private $_fields;
14 21 private $_formContents;
15 22 private $_jsFilesNeeded;
@@ -15,9 +22,10 @@
15 22 private $_jsFilesNeeded;
16 23 private $_loadedScriptsList;
17 24 private $_validationJsFilesNeeded;
18 25
19 - public function __construct() {
26 + public function __construct()
27 + {
20 28 $this->_scriptAddedFlags = [];
21 29 $this->_fields = [];
22 30 $this->_jsFilesNeeded = [];
23 31 $this->_loadedScriptsList = [];
@@ -23,15 +31,19 @@
23 31 $this->_loadedScriptsList = [];
24 32 $this->_validationJsFilesNeeded = ScriptFilePriorityManager::validationScriptFile();
25 33 }
26 34
27 - private static function generateBfGlobalObjJS($contentIds = []) {
28 - // ["bitforms_3_submit_85_1", "bitforms_3_submit_85_2"]
29 - // bf_globals.{contentId}.inits
35 + private static function generateBfGlobalObjJS($contentIds = [])
36 + {
30 37 $contentidArray = json_encode($contentIds);
31 38 return <<<GLOBALOBJ
32 39 if(!window.bf_globals){ window.bf_globals = {};}
33 40 $contentidArray.forEach(function(contentId){
41 + const form = document.getElementById(contentId);
42 + if(!form){
43 + delete window.bf_globals[contentId];
44 + return;
45 + }
34 46 if(!window.bf_globals[contentId]){
35 47 window.bf_globals[contentId] = {inits: {}, contentId: contentId};
36 48 }else{
37 49 window.bf_globals[contentId].inits = {};
@@ -36,13 +48,15 @@
36 48 }else{
37 49 window.bf_globals[contentId].inits = {};
38 50 window.bf_globals[contentId].contentId = contentId;
39 51 }
52 +
40 53 });
41 54 GLOBALOBJ;
42 55 }
43 56
44 - private static function isValidationNeeded($fldData) {
57 + private static function isValidationNeeded($fldData)
58 + {
45 59 $validationsToCheck = ['valid->req', 'valid->regexr', 'mn', 'mx', 'opt', 'err->invalid->show'];
46 60 foreach ($validationsToCheck as $property) {
47 61 $needValidation = Helpers::property_exists_nested($fldData, $property);
48 62 if ($needValidation) {
@@ -51,9 +65,10 @@
51 65 }
52 66 return false;
53 67 }
54 68
55 - public function generateJsFile($formContents, $fields, $contentIds, $postId, $formIDs = null, $preview = false) {
69 + public function generateJsFile($formContents, $fields, $contentIds, $postId, $formIDs = null, $preview = false)
70 + {
56 71 add_option('bitforms_frontend_js_generating', true);
57 72 $this->_formContents = $formContents;
58 73 $this->_fields = $fields;
59 74
@@ -59,11 +74,8 @@
59 74
60 75 $this->appendJs(self::generateBfGlobalObjJS($contentIds), $postId, $preview, 'w');
61 76 $this->_jsFilesNeeded = ScriptFilePriorityManager::jsFile();
62 77
63 - // Recaptcha v3 script is loaded separately
64 - $this->appendJs($this->getRecaptchaV3Script($formIDs), $postId, $preview);
65 -
66 78 // helper js files for whole form
67 79 foreach ($this->_jsFilesNeeded['helperScript'] as $jsFile) {
68 80 $this->addScriptInLoadedScriptsList($jsFile);
69 81 }
@@ -76,13 +88,25 @@
76 88
77 89 // for frontend helper js files
78 90 $this->jsFrontendScript();
79 91
80 - // for hidden Field when catch plugin on
92 + // for hidden Field when cache plugin on
81 93 $this->jsHiddenFieldScript();
82 94
95 + // for form abandonment scripts
96 + $this->jsFormAbandonmentScripts();
97 +
98 + // multi step form scripts
99 + $this->multiStepFormScripts();
100 +
83 101 //⚠👆 every script file generate method call before this method
84 102 $this->appendJs($this->getScript(), $postId, $preview);
103 + $this->appendJs($this->generateFieldConfigsJs($contentIds), $postId, $preview);
104 + // init all js events and functions at last
105 + $this->appendJs(self::getFileFromAssetsJs('bitform-init.min.js'), $postId, $preview);
106 + if (defined('ELEMENTOR_PRO_VERSION')) {
107 + $this->appendJs(self::getFileFromAssetsJs('bitform-elementor.min.js'), $postId, $preview);
108 + }
85 109
86 110 // for js file minification
87 111 $this->appendJs(self::getCustomJsCodes($contentIds), $postId, $preview);
88 112 delete_option('bitforms_frontend_js_generating');
@@ -88,46 +112,74 @@
88 112 delete_option('bitforms_frontend_js_generating');
89 113 return true;
90 114 }
91 115
92 -/*
93 - public function generateJsFile($formContents, $fields, $contentIds, $postId, $formIDs = null, $preview = false) {
94 - $this->_formContents = $formContents;
95 - $this->_fields = $fields;
116 + private function multiStepFormScripts()
117 + {
118 + foreach ($this->_formContents as $formContent) {
119 + $layout = $formContent->layout;
120 + if (is_array($layout) && count($layout) > 1) {
121 + $files = ScriptFilePriorityManager::multiStepFiles();
122 + foreach ($files as $file) {
123 + $this->addScriptInLoadedScriptsList($file);
124 + }
125 + }
126 + }
127 + }
96 128
97 - $script = '';
98 - $script .= self::generateBfGlobalObjJS($contentIds);
99 - $this->_jsFilesNeeded = ScriptFilePriorityManager::jsFile();
129 + private function jsFormAbandonmentScripts()
130 + {
131 + if (!Utilities::isPro() || !class_exists('\BitCode\BitFormPro\Admin\FormSettings\FormAbandonment')) {
132 + return;
133 + }
134 + // check if any of the form has form abandonment enabled
135 + $allFiles = [];
136 + foreach ($this->_formContents as $formContent) {
137 + $formAbandonmentSettings = FormAbandonment::getFormAbandonmentSettings($formContent->formId);
138 + if (!empty($formAbandonmentSettings->saveFormDraft)) {
139 + $neededFiles = ScriptFilePriorityManager::formAbandonmentNeededFiles('autoSave');
140 + $allFiles = array_merge($allFiles, $neededFiles);
141 + break;
142 + }
100 143
101 - // Recaptcha v3 script is loaded separately
102 - $script .= $this->getRecaptchaV3Script($formIDs);
103 -
104 - // custom js files for whole form
105 - foreach ($this->_jsFilesNeeded['helperScript'] as $jsFile) {
144 + $workflowHandler = new WorkFlowHandler($formContent->formId);
145 + $allWorkflows = $workflowHandler->getAllworkFlow();
146 + foreach ($allWorkflows as $workflow) {
147 + foreach ($workflow['conditions'] as $cond) {
148 + if (!empty($cond->actions) && !empty($cond->actions->fields)) {
149 + foreach ($cond->actions->fields as $fldAction) {
150 + if (empty($fldAction->field) || empty($fldAction->action)) {
151 + continue;
152 + }
153 + if ('_bf_form' === $fldAction->field && 'save_draft' === $fldAction->action) {
154 + $neededFiles = ScriptFilePriorityManager::formAbandonmentNeededFiles();
155 + $allFiles = array_merge($allFiles, $neededFiles);
156 + }
157 + }
158 + }
159 + }
160 + }
161 + }
162 + if (isset($this->_fields['button'])) {
163 + foreach ($this->_fields['button'] as $button) {
164 + if ('save-draft' === $button['field']->btnTyp) {
165 + $neededFiles = ScriptFilePriorityManager::formAbandonmentNeededFiles('draftButton');
166 + $allFiles = array_merge($allFiles, $neededFiles);
167 + break;
168 + }
169 + }
170 + }
171 + //
172 + if (empty($allFiles)) {
173 + return;
174 + }
175 + foreach ($allFiles as $jsFile) {
106 176 $this->addScriptInLoadedScriptsList($jsFile);
107 177 }
178 + }
108 179
109 - // for add validation js files
110 - $this->jsValidationNeededFile();
111 -
112 - // js files for all fields
113 - $this->jsFieldsNeededFile();
114 -
115 - // for frontend helper js files
116 - $this->jsFrontendScript();
117 -
118 - // for hidden Field when catch plugin on
119 - $this->jsHiddenFieldScript();
120 -
121 - //⚠👆 every script file generate method call before this method
122 - $script .= $this->getScript();
123 -
124 - // for js file minification
125 - $script = Helpers::minifyJS($script);
126 -
127 - $script .= self::getCustomJsCodes($contentIds);
128 -
129 - // write script file
180 + private function appendJs($script, $postId, $preview, $mode = 'a')
181 + {
130 182 $fileName = '';
131 183 $path = '';
132 184 if ($preview) {
133 185 $fileName = "preview-{$postId}.js";
@@ -135,42 +187,13 @@
135 187 } else {
136 188 $fileName = "bitform-js-$postId.js";
137 189 $path = "form-scripts/{$postId}";
138 190 }
139 - Helpers::saveFile($path, $fileName, $script, 'w');
140 - return true;
191 + Helpers::saveFile($path, $fileName, Helpers::minifyJs($script), $mode);
141 192 }
142 -*/
143 - private function appendJs($script, $postId, $preview, $mode = 'a') {
144 - $fileName = '';
145 - $path = '';
146 - if ($preview) {
147 - $fileName = "preview-{$postId}.js";
148 - $path = 'form-scripts';
149 - } else {
150 - $fileName = "bitform-js-$postId.js";
151 - $path = "form-scripts/{$postId}";
152 - }
153 - Helpers::saveFile($path, $fileName, Helpers::minifyJS($script), $mode);
154 - }
155 193
156 - private function getRecaptchaV3Script($formIds) {
157 - foreach ($formIds as $formID) {
158 - if (in_array('RecaptchaV3', $this->_scriptAddedFlags)) {
159 - break;
160 - }
161 - $getCaptchaV3Settings = (new FormManager($formID))->getCaptchaV3Settings();
162 - if ($getCaptchaV3Settings) {
163 - $integrationHandler = new IntegrationHandler(0);
164 - $allFormIntegrations = $integrationHandler->getAllIntegration('app', 'gReCaptchaV3');
165 - $siteKey = json_decode($allFormIntegrations[0]->integration_details)->siteKey;
166 - $this->_scriptAddedFlags[] = 'RecaptchaV3';
167 - return RecaptchaV3::init($siteKey);
168 - }
169 - }
170 - }
171 -
172 - private function getScript() {
194 + private function getScript()
195 + {
173 196 usort($this->_loadedScriptsList, function ($a, $b) {
174 197 return $a['priority'] - $b['priority'];
175 198 });
176 199
@@ -183,17 +206,8 @@
183 206 continue;
184 207 }
185 208 continue;
186 209 }
187 - if ('init' === $scriptFileArr['scriptTyp']) {
188 - $fieldObjInstance = $scriptFileArr['source'] . $fileNam;
189 - if (!class_exists($fieldObjInstance)) {
190 - continue;
191 - } // when class not found, skip load script
192 - $encodedConf = json_encode($scriptFileArr['field']->fldConfigs);
193 - $script .= $fieldObjInstance::init($scriptFileArr['fk'], $encodedConf, $scriptFileArr['contentId']);
194 - continue;
195 - }
196 210 if ('custom' === $scriptFileArr['scriptTyp']) {
197 211 $fileInstance = $scriptFileArr['source'] . $fileNam;
198 212 if (!class_exists($fileInstance)) {
199 213 continue;
@@ -203,9 +217,10 @@
203 217 }
204 218 return $script;
205 219 }
206 220
207 - private function jsValidationNeededFile() {
221 + private function jsValidationNeededFile()
222 + {
208 223 foreach ($this->_fields as $flds) {
209 224 foreach ($flds as $fld) {
210 225 $fldData = $fld['field'];
211 226 // for validation js files
@@ -246,9 +261,10 @@
246 261 }
247 262 }
248 263 }
249 264
250 - private function jsFieldsNeededFile() {
265 + private function jsFieldsNeededFile()
266 + {
251 267 foreach ($this->_fields as $typ => $flds) {
252 268 if (!array_key_exists($typ, $this->_jsFilesNeeded)) {
253 269 continue;
254 270 }
@@ -270,21 +286,12 @@
270 286 if (!isset($scriptFile['scriptTyp'])) {
271 287 $this->addScriptInLoadedScriptsList($scriptFile);
272 288 continue;
273 289 }
274 - if ('init' === $scriptFile['scriptTyp']) {
290 + if ('custom' === $scriptFile['scriptTyp'] && Helpers::property_exists_nested($fld['field'], $scriptFile['path'], true)) {
275 291 $scriptFile['field'] = $fld['field'];
276 292 $scriptFile['fk'] = $fld['fk'];
277 - $scriptFile['formID'] = $fld['formID'];
278 293 $scriptFile['contentId'] = $fld['contentId'];
279 - $fld['field']->fldConfigs = self::generateFieldConfig($fld['fk'], $fld['field'], $fld['formID']);
280 - $this->_loadedScriptsList[] = $scriptFile;
281 - continue;
282 - }
283 - if ('custom' === $scriptFile['scriptTyp']) {
284 - $scriptFile['field'] = $fld['field'];
285 - $scriptFile['fk'] = $fld['fk'];
286 - $scriptFile['contentId'] = $fld['contentId'];
287 294 $this->addScriptInLoadedScriptsList($scriptFile);
288 295 }
289 296 }
290 297 }
@@ -290,14 +297,15 @@
290 297 }
291 298 }
292 299 }
293 300
294 - private static function getCustomJsCodes($contentIds = []) {
301 + private static function getCustomJsCodes($contentIds = [])
302 + {
295 303 $script = 'let bfContentId = "", bfVars= "";';
296 304 foreach ($contentIds as $contentId) {
297 305 $jsCode = self::getCustomCodes(explode('_', $contentId)[1])['JavaScript'];
298 306 if (!empty($jsCode)) {
299 - $script .= " bfContentId = '{$contentId}'; bfVars = window.bf_globals.{$contentId}.smartTags; {$jsCode}";
307 + $script .= " if(bfSelect('#{$contentId}')){ bfContentId = '{$contentId}'; bfVars = window.bf_globals.{$contentId}.smartTags; {$jsCode}}";
300 308 }
301 309 }
302 310
303 311 return $script;
@@ -302,9 +310,10 @@
302 310
303 311 return $script;
304 312 }
305 313
306 - private static function getFileFromAssetsJs($fileName) {
314 + private static function getFileFromAssetsJs($fileName)
315 + {
307 316 $sourceJsFile = BITFORMS_PLUGIN_DIR_PATH . 'assets' . DIRECTORY_SEPARATOR . $fileName;
308 317 if (file_exists($sourceJsFile)) {
309 318 return file_get_contents($sourceJsFile);
310 319 }
@@ -311,9 +320,10 @@
311 320 Log::debug_log('file not found: ' . $fileName);
312 321 return false;
313 322 }
314 323
315 - public static function saveCssFile($formId, $atomicCssText) {
324 + public static function saveCssFile($formId, $atomicCssText)
325 + {
316 326 $path = 'form-styles';
317 327 $fileName = "bitform-$formId.css";
318 328 if (!isset($formId) || '' === $formId) {
319 329 return new WP_Error('missing_form_id', __('Error Occurred, Please Reload', 'bit-form'));
@@ -320,9 +330,10 @@
320 330 }
321 331 return Helpers::saveFile($path, $fileName, $atomicCssText, 'w');
322 332 }
323 333
324 - public static function customCodeFile($formId, $customCodes) {
334 + public static function customCodeFile($formId, $customCodes)
335 + {
325 336 // for js file
326 337 $path = 'form-scripts';
327 338 $fileName = "bitform-custom-$formId.js";
328 339 self::customCodeFileSaveOrDelete($customCodes->JavaScript, $path, $fileName);
@@ -334,9 +345,10 @@
334 345
335 346 return true;
336 347 }
337 348
338 - private static function customCodeFileSaveOrDelete($script, $path, $fileName) {
349 + public static function customCodeFileSaveOrDelete($script, $path, $fileName)
350 + {
339 351 if ($script) {
340 352 Helpers::saveFile($path, $fileName, $script, 'w');
341 353 } else {
342 354 $uploadPath = "$path/$fileName";
@@ -347,9 +359,10 @@
347 359 }
348 360 return true;
349 361 }
350 362
351 - public static function getCustomCodes($formId) {
363 + public static function getCustomCodes($formId)
364 + {
352 365 $customCodes = ['JavaScript' => '', 'CSS' => ''];
353 366 $customJsPath = Helpers::generatePathDirOrFile("form-scripts/bitform-custom-$formId.js");
354 367 $customCodes['JavaScript'] = Helpers::fileRead($customJsPath);
355 368
@@ -358,70 +371,197 @@
358 371
359 372 return $customCodes;
360 373 }
361 374
362 - public static function generateFieldConfig($fieldKey, $field, $formID) {
363 - $fieldType = $field->typ;
364 - $missingConfigs = ScriptFilePriorityManager::fieldMissingConfigsProps($fieldType, $field) ?: [];
365 - $hasConfigProp = in_array('config', $missingConfigs);
366 - $fldConf = (object) [];
367 - if (false === $hasConfigProp && isset($field->config)) {
368 - $fldConf = $field->config;
375 + private $fldContainersByType = [
376 + 'select' => '.__$fk__-dpd-fld-wrp',
377 + 'country' => '.__$fk__-country-fld-wrp',
378 + 'currency' => '.__$fk__-currency-fld-wrp',
379 + 'phone-number' => '.__$fk__-phone-fld-wrp',
380 + 'file-up' => '.__$fk__-file-up-wrpr',
381 + 'advanced-file-up' => '#filepond-__$fk__-container',
382 + 'paypal' => '.__$fk__-paypal-wrp',
383 + 'razorpay' => '.__$fk__-razorpay-wrp',
384 + 'recaptcha' => '.__$fk__-recaptcha-wrp',
385 + 'stripe' => '.__$fk__-stripe-fld',
386 + 'repeater' => '.__$fk__-rpt-fld-wrp',
387 + 'signature' => '.__$fk__-inp-fld-wrp',
388 + 'rating' => '.__$fk__-inp-fld-wrp',
389 + ];
390 +
391 + private function generateFieldConfigsJs()
392 + {
393 + $customFlds = ['select', 'country', 'currency', 'phone-number', 'file-up', 'advanced-file-up', 'paypal', 'razorpay', 'stripe', 'recaptcha', 'repeater', 'signature', 'rating'];
394 + $allFieldTypes = array_keys($this->_fields);
395 + $customFldsInForms = array_intersect($allFieldTypes, $customFlds);
396 + $formContents = $this->_formContents;
397 + $recaptchaV3Enabled = false;
398 + foreach ($formContents as $content) {
399 + if (isset($content->additional->enabled->recaptchav3) && $content->additional->enabled->recaptchav3) {
400 + $recaptchaV3Enabled = true;
401 + break;
402 + }
369 403 }
370 - $vars = ['fieldKey' => $fieldKey, 'formID' => $formID];
371 - foreach ($missingConfigs as $confPath => $fldPath) {
372 - $value = '';
373 - if (isset($fldPath['var']) && $vars[$fldPath['var']]) {
374 - $value = $vars[$fldPath['var']];
404 + if (empty($customFldsInForms) && !$recaptchaV3Enabled) {
405 + return '';
406 + }
407 +
408 + $containers = [];
409 + foreach ($customFldsInForms as $customFldTyp) {
410 + if (isset($this->fldContainersByType[$customFldTyp])) {
411 + $containers[$customFldTyp] = $this->fldContainersByType[$customFldTyp];
375 412 }
376 - if (empty($value) && isset($fldPath['path'])) {
377 - $value = Helpers::getDataFromNestedPath($field, $fldPath['path']);
413 + }
414 +
415 + $customFldConfigPaths = [];
416 +
417 + foreach ($customFldsInForms as $customFldTyp) {
418 + $allConfs = ScriptFilePriorityManager::getAllFldConfs();
419 + if (isset($allConfs[$customFldTyp])) {
420 + $customFldConfigPaths[$customFldTyp] = $allConfs[$customFldTyp];
421 + } else {
422 + $customFldConfigPaths[$customFldTyp] = (object) [];
378 423 }
379 - if (empty($value) && isset($fldPath['val'])) {
380 - $value = $fldPath['val'];
381 - }
382 - $fldConf = Helpers::setDataToNestedPath($fldConf, $confPath, $value);
383 424 }
384 - return $fldConf;
385 - }
386 425
387 - private function getJsFuncForValidation() {
388 - $validationCmnFunc = ScriptFilePriorityManager::validationCommonFunction();
389 - foreach ($validationCmnFunc as $funcName => $funcArr) {
390 - // get need validation function from validationScriptFile()
391 - $scriptFile = $this->_validationJsFilesNeeded[$funcName];
392 - if (isset($funcArr['notFields'])) {
393 - if (is_array($funcArr['notFields'])) {
394 - foreach ($funcArr['notFields'] as $notField) {
395 - if (!isset($this->_fields[$notField])) {
396 - $this->addScriptInLoadedScriptsList($scriptFile);
397 - }
426 + $customFldConfigPaths = json_encode($customFldConfigPaths);
427 + $containers = json_encode($containers);
428 +
429 + $scriptLoaderFields = ['paypal', 'razorpay', 'stripe', 'recaptcha'];
430 + $scriptLoadedNeeded = array_intersect($scriptLoaderFields, $customFldsInForms) || $recaptchaV3Enabled;
431 + if ($scriptLoadedNeeded) {
432 + $scriptLoaderJs = ScriptLoader::init();
433 + } else {
434 + $scriptLoaderJs = '';
435 + }
436 +
437 + if (in_array('paypal', $customFldsInForms)) {
438 + $paypalInitJs = Paypal::init();
439 + } else {
440 + $paypalInitJs = '';
441 + }
442 +
443 + if (in_array('razorpay', $customFldsInForms)) {
444 + $razorpayInitJs = Razorpay::init();
445 + } else {
446 + $razorpayInitJs = '';
447 + }
448 +
449 + if (in_array('stripe', $customFldsInForms)) {
450 + $stripeInitJs = Stripe::init();
451 + } else {
452 + $stripeInitJs = '';
453 + }
454 +
455 + if (in_array('recaptcha', $customFldsInForms)) {
456 + $recaptchaInitJs = Recaptcha::init();
457 + } else {
458 + $recaptchaInitJs = '';
459 + }
460 +
461 + if ($recaptchaV3Enabled) {
462 + $recaptchaV3InitJs = RecaptchaV3::init();
463 + } else {
464 + $recaptchaV3InitJs = '';
465 + }
466 +
467 + $script = <<<FIELDCONFIGJS
468 + const customFldConfigPaths = $customFldConfigPaths;
469 + const fldContainers = $containers;
470 +
471 + $scriptLoaderJs;
472 +
473 + function initAllCustomFlds (formContentId = null) {
474 + const allContendIds = formContentId ? [formContentId] : Object.keys(bf_globals);
475 + allContendIds.forEach((contentId) => {
476 + const contentData = bf_globals[contentId];
477 + const flds = bf_globals[contentId].fields;
478 + const fldKeys = Object.keys(flds).reverse();
479 + fldKeys.forEach((fldKey) => {
480 + const fldData = flds[fldKey];
481 + const fldType = fldData.typ;
482 + if(fldType === 'paypal') {
483 + $paypalInitJs;
484 + } else if(fldType === 'razorpay') {
485 + $razorpayInitJs;
486 + } else if(fldType === 'recaptcha') {
487 + $recaptchaInitJs;
488 + } else if(fldType === 'stripe') {
489 + $stripeInitJs;
490 + } else if (customFldConfigPaths[fldType]) {
491 + contentData.inits[fldKey] = getFldInstance(contentId, fldKey, fldType);
398 492 }
493 + });
494 + if(contentData.gRecaptchaVersion === 'v3' && contentData.gRecaptchaSiteKey){
495 + $recaptchaV3InitJs;
399 496 }
400 - }
401 - if (isset($funcArr['fields'])) {
402 - if (is_array($funcArr['fields'])) {
403 - foreach ($funcArr['fields'] as $fieldType) {
404 - if (isset($this->_fields[$fieldType])) {
405 - // for set function array in $this->_loadedScriptsList
406 - $this->addScriptInLoadedScriptsList($scriptFile);
407 - }
497 + });
498 + };
499 + function getFldInstance(contentId, fldKey, fldTyp, nestedSelector = '') {
500 + const fldClass = this['bit_'+fldTyp.replace(/-/g, '_')+'_field'];
501 + const selector = '#form-'+contentId+' '+nestedSelector+fldContainers[fldTyp].replace("__\$fk__", fldKey);
502 + if(!fldClass || !bfSelect(selector)) return;
503 + return new fldClass(selector, getFldConf(contentId, fldKey, fldTyp));
504 + };
505 + function getFldConf(contentId, fieldKey, fldTyp) {
506 + const fldData = bf_globals[contentId].fields[fieldKey];
507 + const fldConfPaths = Object.entries(customFldConfigPaths[fldTyp]);
508 + let fldConf = {};
509 + const { formId } = bf_globals[contentId];
510 + if (!("config" in customFldConfigPaths[fldTyp]) && "config" in fldData) fldConf = fldData.config;
511 + const varData = { contentId, fieldKey, formId };
512 + fldConfPaths.forEach(([ confPath, fldPath ]) => {
513 + let value = "";
514 + if (fldPath.var) value = varData[fldPath.var];
515 + if (!value && fldPath.path) {
516 + if(Array.isArray(fldPath.path)) {
517 + fldPath.path.forEach((path) => {
518 + if(!value) value = getDataFromNestedPath(fldData, path);
519 + });
520 + } else {
521 + value = getDataFromNestedPath(fldData, fldPath.path);
408 522 }
409 - } else {
410 - // for all field types
411 - $this->addScriptInLoadedScriptsList($scriptFile);
412 523 }
413 - }
414 -
415 - if (isset($funcArr['dependencies'])) {
416 - foreach ($funcArr['dependencies'] as $dependency) {
417 - $this->addScriptInLoadedScriptsList($dependency);
524 + if (!value && fldPath.val) {
525 + value = fldPath.val;
526 + if(typeof value === 'string') {
527 + Object.entries(varData).forEach(([key, val]) => {
528 + value = value.replace("__\$"+key+"__", val);
529 + });
530 + }
418 531 }
532 + fldConf = setDataToNestedPath(fldConf, confPath, value);
533 + });
534 + return fldConf;
535 + };
536 + function getDataFromNestedPath(data, key) {
537 + const keys = key.split("->");
538 + const lastKey = keys.pop();
539 + let current = {...data};
540 + for (const k of keys) {
541 + if (!(k in current)) return null;
542 + current = current[k];
419 543 }
544 + return current[lastKey] || null;
420 545 }
546 + function setDataToNestedPath(data, key, value) {
547 + const keys = key.split("->");
548 + const lastKey = keys.pop();
549 + let current = {...data};
550 + keys.forEach((k) => {
551 + if (!current[k]) current[k] = {};
552 + current = current[k];
553 + });
554 + current[lastKey] = value;
555 + return current;
556 + }
557 +FIELDCONFIGJS;
558 +
559 + return $script;
421 560 }
422 561
423 - private function addScriptInLoadedScriptsList($fileArr) {
562 + private function addScriptInLoadedScriptsList($fileArr)
563 + {
424 564 $fileName = $fileArr['filename'];
425 565 if (!in_array($fileName, $this->_scriptAddedFlags)) {
426 566 $this->_loadedScriptsList[] = $fileArr;
427 567 $this->_scriptAddedFlags[] = $fileName;
@@ -428,9 +568,10 @@
428 568 }
429 569 return true;
430 570 }
431 571
432 - private function jsFrontendScript() {
572 + private function jsFrontendScript()
573 + {
433 574 foreach ($this->_formContents as $formContent) {
434 575 if (Helpers::property_exists_nested($formContent, 'workFlowExist->oninput', true)) {
435 576 $fileArr = ScriptFilePriorityManager::validationScriptFile()['conditionalLogic'];
436 577 $this->addScriptInLoadedScriptsList($fileArr);
@@ -445,9 +586,10 @@
445 586 }
446 587 }
447 588 }
448 589
449 - private function jsHiddenFieldScript() {
590 + private function jsHiddenFieldScript()
591 + {
450 592 $appConfig = get_option('bitform_app_config');
451 593 if (Helpers::property_exists_nested($appConfig, 'cache_plugin', true)) {
452 594 $fileArr = ScriptFilePriorityManager::frontendScriptFile()['hidden-token-field'];
453 595 $this->addScriptInLoadedScriptsList($fileArr);
@@ -454,9 +596,10 @@
454 596 return;
455 597 }
456 598 }
457 599
458 - public function dd($data, $exit = false) {
600 + public function dd($data, $exit = false)
601 + {
459 602 echo '+++++++++++++';
460 603 echo '<pre>';
461 604 var_dump($data);
462 605 echo '</pre>';