| @@ -1,14 +1,19 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Admin\Form; |
| 4 | 4 | |
| 5 | -if (!defined('ABSPATH')) { | |
| 6 | - exit; | |
| 7 | -} | |
| 8 | - | |
| 9 | -use BitCode\BitForm\Core\Util\FileHandler; | |
| 5 | +use BitCode\BitForm\Admin\Form\InitJs\Paypal; | |
| 6 | +use BitCode\BitForm\Admin\Form\InitJs\Razorpay; | |
| 7 | +use BitCode\BitForm\Admin\Form\InitJs\Recaptcha; | |
| 8 | +use BitCode\BitForm\Admin\Form\InitJs\RecaptchaV3; | |
| 9 | +use BitCode\BitForm\Admin\Form\InitJs\ScriptLoader; | |
| 10 | +use BitCode\BitForm\Admin\Form\InitJs\Stripe; | |
| 11 | +use BitCode\BitForm\Admin\Form\InitJs\Turnstile; | |
| 10 | 12 | use BitCode\BitForm\Core\Util\Log; |
| 13 | +use BitCode\BitForm\Core\Util\Utilities; | |
| 14 | +use BitCode\BitForm\Core\WorkFlow\WorkFlowHandler; | |
| 15 | +use BitCode\BitFormPro\Admin\FormSettings\FormAbandonment; | |
| 11 | 16 | use WP_Error; |
| 12 | 17 | |
| 13 | 18 | class FrontEndScriptGenerator |
| 14 | 19 | { |
| @@ -29,11 +34,12 @@ | ||
| 29 | 34 | } |
| 30 | 35 | |
| 31 | 36 | private static function generateBfGlobalObjJS($contentIds = []) |
| 32 | 37 | { |
| 33 | - $contentidArray = wp_json_encode($contentIds); | |
| 34 | - return ' if(!window.bf_globals){ window.bf_globals = {};} | |
| 35 | - ' . $contentidArray . '.forEach(function(contentId){ | |
| 38 | + $contentidArray = json_encode($contentIds); | |
| 39 | + return <<<GLOBALOBJ | |
| 40 | + if(!window.bf_globals){ window.bf_globals = {};} | |
| 41 | + $contentidArray.forEach(function(contentId){ | |
| 36 | 42 | const form = document.getElementById(contentId); |
| 37 | 43 | if(!form){ |
| 38 | 44 | delete window.bf_globals[contentId]; |
| 39 | 45 | return; |
| @@ -44,14 +50,15 @@ | ||
| 44 | 50 | window.bf_globals[contentId].inits = {}; |
| 45 | 51 | window.bf_globals[contentId].contentId = contentId; |
| 46 | 52 | } |
| 47 | 53 | |
| 48 | - });'; | |
| 54 | + }); | |
| 55 | +GLOBALOBJ; | |
| 49 | 56 | } |
| 50 | 57 | |
| 51 | 58 | private static function isValidationNeeded($fldData) |
| 52 | 59 | { |
| 53 | - $validationsToCheck = ['valid->req', 'valid->regexr', 'mn', 'mx', 'opt', 'err->invalid->show', 'err->notVerified->show']; | |
| 60 | + $validationsToCheck = ['valid->req', 'valid->regexr', 'mn', 'mx', 'opt', 'err->invalid->show']; | |
| 54 | 61 | foreach ($validationsToCheck as $property) { |
| 55 | 62 | $needValidation = Helpers::property_exists_nested($fldData, $property); |
| 56 | 63 | if ($needValidation) { |
| 57 | 64 | return true; |
| @@ -59,9 +66,9 @@ | ||
| 59 | 66 | } |
| 60 | 67 | return false; |
| 61 | 68 | } |
| 62 | 69 | |
| 63 | - public function generateJsFile($formContents, $fields, $contentIds, $postId, $formIDs = null, $preview = 'classic') | |
| 70 | + public function generateJsFile($formContents, $fields, $contentIds, $postId, $formIDs = null, $preview = false) | |
| 64 | 71 | { |
| 65 | 72 | add_option('bitforms_frontend_js_generating', true); |
| 66 | 73 | $this->_formContents = $formContents; |
| 67 | 74 | $this->_fields = $fields; |
| @@ -85,11 +92,8 @@ | ||
| 85 | 92 | |
| 86 | 93 | // for hidden Field when cache plugin on |
| 87 | 94 | $this->jsHiddenFieldScript(); |
| 88 | 95 | |
| 89 | - // for recaptcha v3 (form-level setting) | |
| 90 | - $this->jsRecaptchaV3Scripts(); | |
| 91 | - | |
| 92 | 96 | // for form abandonment scripts |
| 93 | 97 | $this->jsFormAbandonmentScripts(); |
| 94 | 98 | |
| 95 | 99 | // multi step form scripts |
| @@ -94,11 +98,8 @@ | ||
| 94 | 98 | |
| 95 | 99 | // multi step form scripts |
| 96 | 100 | $this->multiStepFormScripts(); |
| 97 | 101 | |
| 98 | - // conversational form scripts | |
| 99 | - $this->conversationalFormScripts(); | |
| 100 | - | |
| 101 | 102 | //⚠👆 every script file generate method call before this method |
| 102 | 103 | $this->appendJs($this->getScript(), $postId, $preview); |
| 103 | 104 | $this->appendJs($this->generateFieldConfigsJs($contentIds), $postId, $preview); |
| 104 | 105 | // init all js events and functions at last |
| @@ -125,49 +126,64 @@ | ||
| 125 | 126 | } |
| 126 | 127 | } |
| 127 | 128 | } |
| 128 | 129 | |
| 129 | - private function conversationalFormScripts() | |
| 130 | + private function jsFormAbandonmentScripts() | |
| 130 | 131 | { |
| 132 | + if (!Utilities::isPro() || !class_exists('\BitCode\BitFormPro\Admin\FormSettings\FormAbandonment')) { | |
| 133 | + return; | |
| 134 | + } | |
| 135 | + // check if any of the form has form abandonment enabled | |
| 136 | + $allFiles = []; | |
| 131 | 137 | foreach ($this->_formContents as $formContent) { |
| 132 | - if (!empty($formContent->formInfo->conversationalSettings) && $formContent->formInfo->conversationalSettings->enable) { | |
| 133 | - $files = ScriptFilePriorityManager::conversationalFormFiles(); | |
| 134 | - foreach ($files as $file) { | |
| 135 | - $this->addScriptInLoadedScriptsList($file); | |
| 138 | + $formAbandonmentSettings = FormAbandonment::getFormAbandonmentSettings($formContent->formId); | |
| 139 | + if (!empty($formAbandonmentSettings->saveFormDraft)) { | |
| 140 | + $neededFiles = ScriptFilePriorityManager::formAbandonmentNeededFiles('autoSave'); | |
| 141 | + $allFiles = array_merge($allFiles, $neededFiles); | |
| 142 | + break; | |
| 143 | + } | |
| 144 | + | |
| 145 | + $workflowHandler = new WorkFlowHandler($formContent->formId); | |
| 146 | + $allWorkflows = $workflowHandler->getAllworkFlow(); | |
| 147 | + foreach ($allWorkflows as $workflow) { | |
| 148 | + foreach ($workflow['conditions'] as $cond) { | |
| 149 | + if (!empty($cond->actions) && !empty($cond->actions->fields)) { | |
| 150 | + foreach ($cond->actions->fields as $fldAction) { | |
| 151 | + if (empty($fldAction->field) || empty($fldAction->action)) { | |
| 152 | + continue; | |
| 153 | + } | |
| 154 | + if ('_bf_form' === $fldAction->field && 'save_draft' === $fldAction->action) { | |
| 155 | + $neededFiles = ScriptFilePriorityManager::formAbandonmentNeededFiles(); | |
| 156 | + $allFiles = array_merge($allFiles, $neededFiles); | |
| 157 | + } | |
| 158 | + } | |
| 159 | + } | |
| 136 | 160 | } |
| 137 | 161 | } |
| 138 | 162 | } |
| 139 | - } | |
| 140 | - | |
| 141 | - private function jsFormAbandonmentScripts() | |
| 142 | - { | |
| 143 | - /** | |
| 144 | - * Add-ons can inject required script files here. | |
| 145 | - * | |
| 146 | - * @param array $files Array of file arrays: ['priority' => int, 'filename' => string] | |
| 147 | - * @param array $formContents | |
| 148 | - * @param array $fields | |
| 149 | - */ | |
| 150 | - $files = apply_filters('bitform_form_abandonment_script_files', [], $this->_formContents, $this->_fields); | |
| 151 | - if (empty($files) || !is_array($files)) { | |
| 163 | + if (isset($this->_fields['button'])) { | |
| 164 | + foreach ($this->_fields['button'] as $button) { | |
| 165 | + if ('save-draft' === $button['field']->btnTyp) { | |
| 166 | + $neededFiles = ScriptFilePriorityManager::formAbandonmentNeededFiles(); | |
| 167 | + $allFiles = array_merge($allFiles, $neededFiles); | |
| 168 | + break; | |
| 169 | + } | |
| 170 | + } | |
| 171 | + } | |
| 172 | + // | |
| 173 | + if (empty($allFiles)) { | |
| 152 | 174 | return; |
| 153 | 175 | } |
| 154 | - foreach ($files as $jsFile) { | |
| 155 | - if (!empty($jsFile['filename']) && !empty($jsFile['priority'])) { | |
| 156 | - $this->addScriptInLoadedScriptsList($jsFile); | |
| 157 | - } | |
| 176 | + foreach ($allFiles as $jsFile) { | |
| 177 | + $this->addScriptInLoadedScriptsList($jsFile); | |
| 158 | 178 | } |
| 159 | 179 | } |
| 160 | 180 | |
| 161 | - private function appendJs($script, $postId, $previewMode, $mode = 'a') | |
| 181 | + private function appendJs($script, $postId, $preview, $mode = 'a') | |
| 162 | 182 | { |
| 163 | 183 | $fileName = ''; |
| 164 | 184 | $path = ''; |
| 165 | - | |
| 166 | - if ('conversational' === $previewMode) { | |
| 167 | - $fileName = "bitform-conversational-{$postId}.js"; | |
| 168 | - $path = 'form-scripts'; | |
| 169 | - } elseif ('preview' === $previewMode) { | |
| 185 | + if ($preview) { | |
| 170 | 186 | $fileName = "preview-{$postId}.js"; |
| 171 | 187 | $path = 'form-scripts'; |
| 172 | 188 | } else { |
| 173 | 189 | $fileName = "bitform-js-$postId.js"; |
| @@ -184,12 +200,22 @@ | ||
| 184 | 200 | |
| 185 | 201 | $script = ''; |
| 186 | 202 | foreach ($this->_loadedScriptsList as $scriptFileArr) { |
| 187 | 203 | $fileNam = $scriptFileArr['filename']; |
| 188 | - $fileContent = self::getFileFromAssetsJs($fileNam); | |
| 189 | - if ($fileContent) { | |
| 190 | - $script .= $fileContent; | |
| 204 | + if (!isset($scriptFileArr['scriptTyp']) || 'script' === $scriptFileArr['scriptTyp']) { | |
| 205 | + if (self::getFileFromAssetsJs($fileNam)) { | |
| 206 | + $script .= self::getFileFromAssetsJs($fileNam); | |
| 207 | + continue; | |
| 208 | + } | |
| 209 | + continue; | |
| 191 | 210 | } |
| 211 | + if ('custom' === $scriptFileArr['scriptTyp']) { | |
| 212 | + $fileInstance = $scriptFileArr['source'] . $fileNam; | |
| 213 | + if (!class_exists($fileInstance)) { | |
| 214 | + continue; | |
| 215 | + } // when class not found, skip load script | |
| 216 | + $script .= $fileInstance::init($scriptFileArr['fk'], $scriptFileArr['field'], $scriptFileArr['contentId']); | |
| 217 | + } | |
| 192 | 218 | } |
| 193 | 219 | return $script; |
| 194 | 220 | } |
| 195 | 221 | |
| @@ -218,30 +244,18 @@ | ||
| 218 | 244 | |
| 219 | 245 | $validationScriptFileMapping = ScriptFilePriorityManager::validationScriptFileMapping($fldData->typ); |
| 220 | 246 | if ($validationScriptFileMapping) { |
| 221 | 247 | foreach ($validationScriptFileMapping as $key => $value) { |
| 222 | - $paths = $value['paths'] ?? []; | |
| 223 | - $hasMatchingPath = false; | |
| 224 | - foreach ($paths as $path) { | |
| 248 | + foreach ($value['paths'] as $path) { | |
| 225 | 249 | if (Helpers::property_exists_nested($fldData, $path)) { |
| 226 | - $hasMatchingPath = true; | |
| 227 | - break; // one match is enough | |
| 250 | + $file = $this->_validationJsFilesNeeded[$key]; | |
| 251 | + $this->addScriptInLoadedScriptsList($file); | |
| 228 | 252 | } |
| 229 | 253 | } |
| 230 | - | |
| 231 | - if (!$hasMatchingPath) { | |
| 232 | - continue; | |
| 233 | - } | |
| 234 | - | |
| 235 | - // Add the main script once | |
| 236 | - if (!empty($this->_validationJsFilesNeeded[$key])) { | |
| 237 | - $this->addScriptInLoadedScriptsList($this->_validationJsFilesNeeded[$key]); | |
| 238 | - } | |
| 239 | - | |
| 240 | - // Add dependencies once each | |
| 241 | - foreach (($value['dependencies'] ?? []) as $dep) { | |
| 242 | - if (!empty($this->_validationJsFilesNeeded[$dep])) { | |
| 243 | - $this->addScriptInLoadedScriptsList($this->_validationJsFilesNeeded[$dep]); | |
| 254 | + if (isset($value['dependencies'])) { | |
| 255 | + foreach ($value['dependencies'] as $dependency) { | |
| 256 | + $dependencyFile = $this->_validationJsFilesNeeded[$dependency]; | |
| 257 | + $this->addScriptInLoadedScriptsList($dependencyFile); | |
| 244 | 258 | } |
| 245 | 259 | } |
| 246 | 260 | } |
| 247 | 261 | } |
| @@ -250,9 +264,8 @@ | ||
| 250 | 264 | } |
| 251 | 265 | |
| 252 | 266 | private function jsFieldsNeededFile() |
| 253 | 267 | { |
| 254 | - $filePondPluginList = apply_filters('bitform_filepond_plugins_list', []); | |
| 255 | 268 | foreach ($this->_fields as $typ => $flds) { |
| 256 | 269 | if (!array_key_exists($typ, $this->_jsFilesNeeded)) { |
| 257 | 270 | continue; |
| 258 | 271 | } |
| @@ -262,30 +275,26 @@ | ||
| 262 | 275 | if ('advanced-file-up' === $typ) { |
| 263 | 276 | $configs = $fld['field']->config; |
| 264 | 277 | foreach ($configs as $configKey => $config) { |
| 265 | 278 | if ($configs->$configKey) { |
| 266 | - if (array_key_exists($configKey, $filePondPluginList)) { | |
| 267 | - $this->addScriptInLoadedScriptsList($filePondPluginList[$configKey]); | |
| 279 | + $filepondPlugin = ScriptFilePriorityManager::filePondPlugins($configKey); | |
| 280 | + if ($filepondPlugin) { | |
| 281 | + $this->addScriptInLoadedScriptsList($filepondPlugin); | |
| 268 | 282 | } |
| 269 | 283 | } |
| 270 | 284 | } |
| 271 | 285 | } |
| 272 | 286 | foreach ($fldScriptArr as $scriptFile) { |
| 273 | - if (isset($scriptFile['paths']) && is_array($scriptFile['paths'])) { | |
| 274 | - $pathMatched = false; | |
| 275 | - foreach ($scriptFile['paths'] as $path) { | |
| 276 | - if (Helpers::property_exists_nested($fld['field'], $path)) { | |
| 277 | - $pathMatched = true; | |
| 278 | - break; | |
| 279 | - } | |
| 280 | - } | |
| 281 | - if (!$pathMatched) { | |
| 282 | - continue; | |
| 283 | - } | |
| 284 | - } elseif (isset($scriptFile['path']) && !Helpers::property_exists_nested($fld['field'], $scriptFile['path'])) { | |
| 287 | + if (!isset($scriptFile['scriptTyp'])) { | |
| 288 | + $this->addScriptInLoadedScriptsList($scriptFile); | |
| 285 | 289 | continue; |
| 286 | 290 | } |
| 287 | - $this->addScriptInLoadedScriptsList($scriptFile); | |
| 291 | + if ('custom' === $scriptFile['scriptTyp'] && Helpers::property_exists_nested($fld['field'], $scriptFile['path'], true)) { | |
| 292 | + $scriptFile['field'] = $fld['field']; | |
| 293 | + $scriptFile['fk'] = $fld['fk']; | |
| 294 | + $scriptFile['contentId'] = $fld['contentId']; | |
| 295 | + $this->addScriptInLoadedScriptsList($scriptFile); | |
| 296 | + } | |
| 288 | 297 | } |
| 289 | 298 | } |
| 290 | 299 | } |
| 291 | 300 | } |
| @@ -291,15 +300,13 @@ | ||
| 291 | 300 | } |
| 292 | 301 | |
| 293 | 302 | private static function getCustomJsCodes($contentIds = []) |
| 294 | 303 | { |
| 295 | - $script = 'let bfContentId = "", bfSlNo = "1", bfVars= "";'; | |
| 304 | + $script = 'let bfContentId = "", bfVars= "";'; | |
| 296 | 305 | foreach ($contentIds as $contentId) { |
| 297 | - $contentIdArr = explode('_', $contentId); | |
| 298 | - $jsCode = self::getCustomCodes($contentIdArr[1])['JavaScript']; | |
| 299 | - $bfSlNo = array_key_exists(3, $contentIdArr) ? $contentIdArr[3] : '1'; | |
| 306 | + $jsCode = self::getCustomCodes(explode('_', $contentId)[1])['JavaScript']; | |
| 300 | 307 | if (!empty($jsCode)) { |
| 301 | - $script .= " if(bfSelect('#{$contentId}')){ bfContentId = '{$contentId}'; bfSlNo = '{$bfSlNo}'; bfVars = window.bf_globals.{$contentId}.smartTags; {$jsCode}}"; | |
| 308 | + $script .= " if(bfSelect('#{$contentId}')){ bfContentId = '{$contentId}'; bfVars = window.bf_globals.{$contentId}.smartTags; {$jsCode}}"; | |
| 302 | 309 | } |
| 303 | 310 | } |
| 304 | 311 | |
| 305 | 312 | return $script; |
| @@ -329,10 +336,9 @@ | ||
| 329 | 336 | { |
| 330 | 337 | // for js file |
| 331 | 338 | $path = 'form-scripts'; |
| 332 | 339 | $fileName = "bitform-custom-$formId.js"; |
| 333 | - $filteredJs = Helpers::removeJsSingleLineComments($customCodes->JavaScript); | |
| 334 | - self::customCodeFileSaveOrDelete($filteredJs, $path, $fileName); | |
| 340 | + self::customCodeFileSaveOrDelete($customCodes->JavaScript, $path, $fileName); | |
| 335 | 341 | |
| 336 | 342 | // for css file |
| 337 | 343 | $path = 'form-styles'; |
| 338 | 344 | $fileName = "bitform-custom-$formId.css"; |
| @@ -347,9 +353,11 @@ | ||
| 347 | 353 | Helpers::saveFile($path, $fileName, $script, 'w'); |
| 348 | 354 | } else { |
| 349 | 355 | $uploadPath = "$path/$fileName"; |
| 350 | 356 | $uploadFilePath = Helpers::generatePathDirOrFile($uploadPath); |
| 351 | - FileHandler::deleteIsFileExists($uploadFilePath); | |
| 357 | + if (file_exists($uploadFilePath)) { | |
| 358 | + unlink($uploadFilePath); | |
| 359 | + } | |
| 352 | 360 | } |
| 353 | 361 | return true; |
| 354 | 362 | } |
| 355 | 363 | |
| @@ -365,31 +373,26 @@ | ||
| 365 | 373 | return $customCodes; |
| 366 | 374 | } |
| 367 | 375 | |
| 368 | 376 | private $fldContainersByType = [ |
| 369 | - 'address' => '.__$fk__-parent-fld-wrp', | |
| 370 | - 'select' => '.__$fk__-dpd-fld-wrp', | |
| 371 | - 'country' => '.__$fk__-country-fld-wrp', | |
| 372 | - 'currency' => '.__$fk__-currency-fld-wrp', | |
| 373 | - 'phone-number' => '.__$fk__-phone-fld-wrp', | |
| 374 | - 'file-up' => '.__$fk__-file-up-wrpr', | |
| 375 | - 'advanced-file-up' => '#filepond-__$fk__-container', | |
| 376 | - 'paypal' => '.__$fk__-paypal-wrp', | |
| 377 | - 'razorpay' => '.__$fk__-razorpay-wrp', | |
| 378 | - 'recaptcha' => '.__$fk__-recaptcha-wrp', | |
| 379 | - 'stripe' => '.__$fk__-stripe-fld', | |
| 380 | - 'mollie' => '.__$fk__-mollie-wrp', | |
| 381 | - 'repeater' => '.__$fk__-rpt-fld-wrp', | |
| 382 | - 'signature' => '.__$fk__-inp-fld-wrp', | |
| 383 | - 'rating' => '.__$fk__-inp-fld-wrp', | |
| 384 | - 'email-otp' => '.__$fk__-inp-fld-wrp', | |
| 385 | - 'hcaptcha' => '.__$fk__-h-captcha-wrp', | |
| 386 | - 'advanced-datetime' => '.__$fk__-advanced-datetime', | |
| 377 | + 'select' => '.__$fk__-dpd-fld-wrp', | |
| 378 | + 'country' => '.__$fk__-country-fld-wrp', | |
| 379 | + 'currency' => '.__$fk__-currency-fld-wrp', | |
| 380 | + 'phone-number' => '.__$fk__-phone-fld-wrp', | |
| 381 | + 'file-up' => '.__$fk__-file-up-wrpr', | |
| 382 | + 'advanced-file-up' => '#filepond-__$fk__-container', | |
| 383 | + 'paypal' => '.__$fk__-paypal-wrp', | |
| 384 | + 'razorpay' => '.__$fk__-razorpay-wrp', | |
| 385 | + 'recaptcha' => '.__$fk__-recaptcha-wrp', | |
| 386 | + 'stripe' => '.__$fk__-stripe-fld', | |
| 387 | + 'repeater' => '.__$fk__-rpt-fld-wrp', | |
| 388 | + 'signature' => '.__$fk__-inp-fld-wrp', | |
| 389 | + 'rating' => '.__$fk__-inp-fld-wrp', | |
| 387 | 390 | ]; |
| 388 | 391 | |
| 389 | 392 | private function generateFieldConfigsJs() |
| 390 | 393 | { |
| 391 | - $customFlds = ['address', 'select', 'country', 'currency', 'phone-number', 'file-up', 'advanced-file-up', 'paypal', 'razorpay', 'stripe', 'mollie', 'recaptcha', 'repeater', 'signature', 'rating', 'turnstile', 'hcaptcha', 'advanced-datetime', 'email-otp']; | |
| 394 | + $customFlds = ['select', 'country', 'currency', 'phone-number', 'file-up', 'advanced-file-up', 'paypal', 'razorpay', 'stripe', 'recaptcha', 'repeater', 'signature', 'rating', 'turnstile']; | |
| 392 | 395 | $allFieldTypes = array_keys($this->_fields); |
| 393 | 396 | $customFldsInForms = array_intersect($allFieldTypes, $customFlds); |
| 394 | 397 | $formContents = $this->_formContents; |
| 395 | 398 | $recaptchaV3Enabled = false; |
| @@ -411,10 +414,10 @@ | ||
| 411 | 414 | } |
| 412 | 415 | |
| 413 | 416 | $customFldConfigPaths = []; |
| 414 | 417 | |
| 415 | - $allConfs = ScriptFilePriorityManager::getAllFldConfs(); | |
| 416 | 418 | foreach ($customFldsInForms as $customFldTyp) { |
| 419 | + $allConfs = ScriptFilePriorityManager::getAllFldConfs(); | |
| 417 | 420 | if (isset($allConfs[$customFldTyp])) { |
| 418 | 421 | $customFldConfigPaths[$customFldTyp] = $allConfs[$customFldTyp]; |
| 419 | 422 | } else { |
| 420 | 423 | $customFldConfigPaths[$customFldTyp] = (object) []; |
| @@ -420,48 +423,92 @@ | ||
| 420 | 423 | $customFldConfigPaths[$customFldTyp] = (object) []; |
| 421 | 424 | } |
| 422 | 425 | } |
| 423 | 426 | |
| 424 | - $customFldConfigPaths = wp_json_encode($customFldConfigPaths); | |
| 425 | - $containers = wp_json_encode($containers); | |
| 427 | + $customFldConfigPaths = json_encode($customFldConfigPaths); | |
| 428 | + $containers = json_encode($containers); | |
| 426 | 429 | |
| 427 | - $script = ' const customFldConfigPaths = ' . $customFldConfigPaths . '; | |
| 428 | - const fldContainers = ' . $containers . '; | |
| 430 | + $scriptLoaderFields = ['paypal', 'razorpay', 'stripe', 'recaptcha', 'turnstile']; | |
| 431 | + $scriptLoadedNeeded = array_intersect($scriptLoaderFields, $customFldsInForms) || $recaptchaV3Enabled; | |
| 432 | + if ($scriptLoadedNeeded) { | |
| 433 | + $scriptLoaderJs = ScriptLoader::init(); | |
| 434 | + } else { | |
| 435 | + $scriptLoaderJs = ''; | |
| 436 | + } | |
| 437 | + | |
| 438 | + if (in_array('paypal', $customFldsInForms)) { | |
| 439 | + $paypalInitJs = Paypal::init(); | |
| 440 | + } else { | |
| 441 | + $paypalInitJs = ''; | |
| 442 | + } | |
| 443 | + | |
| 444 | + if (in_array('razorpay', $customFldsInForms)) { | |
| 445 | + $razorpayInitJs = Razorpay::init(); | |
| 446 | + } else { | |
| 447 | + $razorpayInitJs = ''; | |
| 448 | + } | |
| 449 | + | |
| 450 | + if (in_array('stripe', $customFldsInForms)) { | |
| 451 | + $stripeInitJs = Stripe::init(); | |
| 452 | + } else { | |
| 453 | + $stripeInitJs = ''; | |
| 454 | + } | |
| 455 | + | |
| 456 | + if (in_array('recaptcha', $customFldsInForms)) { | |
| 457 | + $recaptchaInitJs = Recaptcha::init(); | |
| 458 | + } else { | |
| 459 | + $recaptchaInitJs = ''; | |
| 460 | + } | |
| 461 | + | |
| 462 | + if (in_array('turnstile', $customFldsInForms)) { | |
| 463 | + $turnstileInitJs = Turnstile::init(); | |
| 464 | + } else { | |
| 465 | + $turnstileInitJs = ''; | |
| 466 | + } | |
| 467 | + | |
| 468 | + if ($recaptchaV3Enabled) { | |
| 469 | + $recaptchaV3InitJs = RecaptchaV3::init(); | |
| 470 | + } else { | |
| 471 | + $recaptchaV3InitJs = ''; | |
| 472 | + } | |
| 473 | + | |
| 474 | + $script = <<<FIELDCONFIGJS | |
| 475 | + const customFldConfigPaths = $customFldConfigPaths; | |
| 476 | + const fldContainers = $containers; | |
| 429 | 477 | |
| 478 | + $scriptLoaderJs; | |
| 479 | + | |
| 430 | 480 | function initAllCustomFlds (formContentId = null) { |
| 431 | 481 | const allContendIds = formContentId ? [formContentId] : Object.keys(bf_globals); |
| 432 | 482 | allContendIds.forEach((contentId) => { |
| 433 | 483 | const contentData = bf_globals[contentId]; |
| 434 | - if(!contentData?.inits) contentData.inits = {}; | |
| 435 | - const flds = bf_globals[contentId]?.fields || {}; | |
| 484 | + const flds = bf_globals[contentId].fields; | |
| 436 | 485 | const fldKeys = Object.keys(flds).reverse(); |
| 437 | 486 | fldKeys.forEach((fldKey) => { |
| 438 | 487 | const fldData = flds[fldKey]; |
| 439 | 488 | const fldType = fldData.typ; |
| 440 | - if(fldType === \'paypal\') { | |
| 441 | - initPaypalFld(contentId, fldKey, fldData, fldType); | |
| 442 | - } else if(fldType === \'razorpay\') { | |
| 443 | - initRazorpayFld(contentId, fldKey, fldType); | |
| 444 | - } else if(fldType === \'recaptcha\') { | |
| 445 | - initRecaptchaFld(contentId, fldKey, fldType); | |
| 446 | - } else if(fldType === \'turnstile\') { | |
| 447 | - initTurnstileFld(contentId, fldKey); | |
| 448 | - } else if(fldType === \'hcaptcha\') { | |
| 449 | - initHCaptchaFld(contentId, fldKey, fldType); | |
| 450 | - } else if(fldType === \'stripe\' || fldType === \'mollie\') { | |
| 451 | - initStripeFld(contentId, fldKey, fldType); | |
| 489 | + if(fldType === 'paypal') { | |
| 490 | + $paypalInitJs; | |
| 491 | + } else if(fldType === 'razorpay') { | |
| 492 | + $razorpayInitJs; | |
| 493 | + } else if(fldType === 'recaptcha') { | |
| 494 | + $recaptchaInitJs; | |
| 495 | + } else if(fldType === 'turnstile') { | |
| 496 | + $turnstileInitJs; | |
| 497 | + } else if(fldType === 'stripe') { | |
| 498 | + $stripeInitJs; | |
| 452 | 499 | } else if (customFldConfigPaths[fldType]) { |
| 453 | 500 | contentData.inits[fldKey] = getFldInstance(contentId, fldKey, fldType); |
| 454 | 501 | } |
| 455 | 502 | }); |
| 456 | - if(contentData.gRecaptchaVersion === \'v3\' && contentData.gRecaptchaSiteKey){ | |
| 457 | - initRecaptchaV3Fld(contentId, contentData); | |
| 503 | + if(contentData.gRecaptchaVersion === 'v3' && contentData.gRecaptchaSiteKey){ | |
| 504 | + $recaptchaV3InitJs; | |
| 458 | 505 | } |
| 459 | 506 | }); |
| 460 | 507 | }; |
| 461 | - function getFldInstance(contentId, fldKey, fldTyp, nestedSelector = \'\') { | |
| 462 | - const fldClass = this[\'bit_\'+fldTyp.replace(/-/g, \'_\')+\'_field\']; | |
| 463 | - const selector = \'#form-\'+contentId+\' \'+nestedSelector+fldContainers[fldTyp].replace("__$fk__", fldKey); | |
| 508 | + function getFldInstance(contentId, fldKey, fldTyp, nestedSelector = '') { | |
| 509 | + const fldClass = this['bit_'+fldTyp.replace(/-/g, '_')+'_field']; | |
| 510 | + const selector = '#form-'+contentId+' '+nestedSelector+fldContainers[fldTyp].replace("__\$fk__", fldKey); | |
| 464 | 511 | if(!fldClass || !bfSelect(selector)) return; |
| 465 | 512 | return new fldClass(selector, getFldConf(contentId, fldKey, fldTyp)); |
| 466 | 513 | }; |
| 467 | 514 | function getFldConf(contentId, fieldKey, fldTyp) { |
| @@ -482,13 +529,13 @@ | ||
| 482 | 529 | } else { |
| 483 | 530 | value = getDataFromNestedPath(fldData, fldPath.path); |
| 484 | 531 | } |
| 485 | 532 | } |
| 486 | - if (!value && "val" in fldPath) { | |
| 533 | + if (!value && fldPath.val) { | |
| 487 | 534 | value = fldPath.val; |
| 488 | - if(typeof value === \'string\') { | |
| 535 | + if(typeof value === 'string') { | |
| 489 | 536 | Object.entries(varData).forEach(([key, val]) => { |
| 490 | - value = value.replace("__$"+key+"__", val); | |
| 537 | + value = value.replace("__\$"+key+"__", val); | |
| 491 | 538 | }); |
| 492 | 539 | } |
| 493 | 540 | } |
| 494 | 541 | fldConf = setDataToNestedPath(fldConf, confPath, value); |
| @@ -514,9 +561,10 @@ | ||
| 514 | 561 | current = current[k]; |
| 515 | 562 | }); |
| 516 | 563 | current[lastKey] = value; |
| 517 | 564 | return current; |
| 518 | - }'; | |
| 565 | + } | |
| 566 | +FIELDCONFIGJS; | |
| 519 | 567 | |
| 520 | 568 | return $script; |
| 521 | 569 | } |
| 522 | 570 | |
| @@ -537,10 +585,8 @@ | ||
| 537 | 585 | $fileArr = ScriptFilePriorityManager::validationAndOtherScriptFile()['conditionalLogic']; |
| 538 | 586 | $this->addScriptInLoadedScriptsList($fileArr); |
| 539 | 587 | $fileArr = ScriptFilePriorityManager::validationAndOtherScriptFile()['resetPlaceholders']; |
| 540 | 588 | $this->addScriptInLoadedScriptsList($fileArr); |
| 541 | - $fileArr = ScriptFilePriorityManager::validationAndOtherScriptFile()['bfResetDefaultValue']; | |
| 542 | - $this->addScriptInLoadedScriptsList($fileArr); | |
| 543 | 589 | $fileArr = ScriptFilePriorityManager::validationAndOtherScriptFile()['validateFocusLost']; |
| 544 | 590 | $this->addScriptInLoadedScriptsList($fileArr); |
| 545 | 591 | $fileArr = ScriptFilePriorityManager::frontendScriptFile()['observeElm']; |
| 546 | 592 | $this->addScriptInLoadedScriptsList($fileArr); |
| @@ -558,19 +604,8 @@ | ||
| 558 | 604 | if (Helpers::property_exists_nested($appConfig, 'cache_plugin', true)) { |
| 559 | 605 | $fileArr = ScriptFilePriorityManager::frontendScriptFile()['hidden-token-field']; |
| 560 | 606 | $this->addScriptInLoadedScriptsList($fileArr); |
| 561 | 607 | return; |
| 562 | - } | |
| 563 | - } | |
| 564 | - | |
| 565 | - private function jsRecaptchaV3Scripts() | |
| 566 | - { | |
| 567 | - foreach ($this->_formContents as $content) { | |
| 568 | - if (isset($content->additional->enabled->recaptchav3) && $content->additional->enabled->recaptchav3) { | |
| 569 | - $this->addScriptInLoadedScriptsList(['priority' => 302, 'filename' => 'scriptLoader.min.js']); | |
| 570 | - $this->addScriptInLoadedScriptsList(['priority' => 303, 'filename' => 'initRecaptchaV3Fld.min.js']); | |
| 571 | - return; | |
| 572 | - } | |
| 573 | 608 | } |
| 574 | 609 | } |
| 575 | 610 | |
| 576 | 611 | public function dd($data, $exit = false) |