| @@ -1,8 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Frontend\Form; |
| 4 | 4 | |
| 5 | +if (!defined('ABSPATH')) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | + | |
| 9 | +use BitCode\BitForm\Admin\Form\AdminFormHandler; | |
| 5 | 10 | use BitCode\BitForm\Admin\Form\FrontEndScriptGenerator; |
| 6 | 11 | use BitCode\BitForm\Admin\Form\Helpers; |
| 7 | 12 | use BitCode\BitForm\Core\Database\FormEntryMetaModel; |
| 8 | 13 | use BitCode\BitForm\Core\Database\FormModel; |
| @@ -7,17 +12,24 @@ | ||
| 7 | 12 | use BitCode\BitForm\Core\Database\FormEntryMetaModel; |
| 8 | 13 | use BitCode\BitForm\Core\Database\FormModel; |
| 9 | 14 | use BitCode\BitForm\Core\Form\FormManager; |
| 10 | 15 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 16 | +use BitCode\BitForm\Core\Util\EscapingHelper; | |
| 11 | 17 | use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 12 | 18 | use BitCode\BitForm\Core\Util\FileDownloadProvider; |
| 19 | +use BitCode\BitForm\Core\Util\FileHandler; | |
| 13 | 20 | use BitCode\BitForm\Core\Util\FrontendHelpers; |
| 21 | +use BitCode\BitForm\Core\Util\Log; | |
| 22 | +use BitCode\BitForm\Core\Util\SmartTagRegistry; | |
| 23 | +use BitCode\BitForm\Core\Util\SmartTags; | |
| 14 | 24 | use BitCode\BitForm\Core\Util\Utilities; |
| 15 | 25 | use BitCode\BitForm\Core\WorkFlow\WorkFlow; |
| 16 | -use BitCode\BitFormPro\Admin\FormSettings\FormAbandonment; | |
| 17 | 26 | |
| 18 | 27 | final class FrontendFormHandler |
| 19 | 28 | { |
| 29 | + /** Largest stored signature inlined into the page as a data URI. */ | |
| 30 | + private const MAX_INLINE_SIGNATURE_BYTES = 2097152; | |
| 31 | + | |
| 20 | 32 | public function __construct() |
| 21 | 33 | { |
| 22 | 34 | // before markup load - formids [], posts [1,2] |
| 23 | 35 | add_action('wp_enqueue_scripts', [$this, 'loadAssets']); |
| @@ -23,9 +35,11 @@ | ||
| 23 | 35 | add_action('wp_enqueue_scripts', [$this, 'loadAssets']); |
| 24 | 36 | // markup loads - formids [] |
| 25 | 37 | add_shortcode('bitform', [$this, 'handleFrontendRenderRequest']); |
| 26 | 38 | // after markup load - formids [1,35,3] |
| 27 | - add_action('wp_footer', [$this, 'generateJS']); | |
| 39 | + // After popup plugins render at 10 (that is when popup-only forms register | |
| 40 | + // their formID), before wp_print_footer_scripts at 20. | |
| 41 | + add_action('wp_footer', [$this, 'generateJS'], 15); | |
| 28 | 42 | } |
| 29 | 43 | |
| 30 | 44 | private function validPassowordResetToken($token, $userID, $formId) |
| 31 | 45 | { |
| @@ -48,16 +62,49 @@ | ||
| 48 | 62 | } |
| 49 | 63 | |
| 50 | 64 | private function getJSFileSrc($postId) |
| 51 | 65 | { |
| 52 | - $formUpdateVersion = get_option('bit-form_form_update_version'); | |
| 66 | + $formUpdateVersion = get_option('bitform_form_update_version'); | |
| 53 | 67 | $formScriptSrc = BITFORMS_UPLOAD_BASE_URL . "/form-scripts/$postId/bitform-js-$postId.js?bfv=$formUpdateVersion"; |
| 54 | 68 | |
| 55 | 69 | return $formScriptSrc; |
| 56 | 70 | } |
| 57 | 71 | |
| 72 | + private function getJSFilePath($postId) | |
| 73 | + { | |
| 74 | + return BITFORMS_CONTENT_DIR . "/form-scripts/$postId/bitform-js-$postId.js"; | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Does this page's bundle need (re)generating? | |
| 79 | + * | |
| 80 | + * The DB flag alone is not enough: a page marked generated whose file was never written | |
| 81 | + * (crashed generation, unwritable uploads dir) would enqueue a 404 forever. Conversely a | |
| 82 | + * file that cannot be written must not make every request rebuild it, so a missing file | |
| 83 | + * is retried on a backoff window rather than on every hit. | |
| 84 | + * | |
| 85 | + * @param int $postId | |
| 86 | + * @param bool $regenerateScriptFlag DB-side verdict from regenerateScriptChecker() | |
| 87 | + * | |
| 88 | + * @return bool | |
| 89 | + */ | |
| 90 | + private function needsScriptGeneration($postId, $regenerateScriptFlag) | |
| 91 | + { | |
| 92 | + if (file_exists($this->getJSFilePath($postId))) { | |
| 93 | + return (bool) $regenerateScriptFlag; | |
| 94 | + } | |
| 95 | + $retryKey = 'bitforms_js_regen_' . $postId; | |
| 96 | + if (get_transient($retryKey)) { | |
| 97 | + return false; | |
| 98 | + } | |
| 99 | + set_transient($retryKey, 1, 5 * MINUTE_IN_SECONDS); | |
| 100 | + return true; | |
| 101 | + } | |
| 102 | + | |
| 58 | 103 | public function generateJs($formID = null, $entryID = null, $formType = null) |
| 59 | 104 | { |
| 105 | + // bitform-js-{postId}.js is disk-cached per post ID with no language key. | |
| 106 | + // Display strings must stay out of it and travel in bf_globals per request. | |
| 60 | 107 | // return true; |
| 61 | 108 | $isFormPreview = get_transient('bitform_form_preview'); |
| 62 | 109 | if ($isFormPreview && !$formID) { |
| 63 | 110 | delete_transient('bitform_form_preview'); |
| @@ -76,12 +123,12 @@ | ||
| 76 | 123 | $formIDs = []; |
| 77 | 124 | $previewMode = 'classic'; |
| 78 | 125 | $postId = ''; |
| 79 | 126 | |
| 80 | - $formUpdateVersion = get_option('bit-form_form_update_version'); | |
| 127 | + $formUpdateVersion = get_option('bitform_form_update_version'); | |
| 81 | 128 | if ($formID) { |
| 82 | 129 | $formIDs[] = $formID; |
| 83 | - $FrontendFormManager = new FrontendFormManager($formID, 1); | |
| 130 | + $FrontendFormManager = FrontendFormManager::getInstance($formID, 1); | |
| 84 | 131 | $formInfo = $FrontendFormManager->getFormInfo(); |
| 85 | 132 | $FormIdentifier = esc_js($FrontendFormManager->getFormIdentifier()); |
| 86 | 133 | $formContent = $FrontendFormManager->getFormContentWithValue($this->getValuesFromQueryParams()); |
| 87 | 134 | $formContent->formId = $formID; |
| @@ -117,8 +164,10 @@ | ||
| 117 | 164 | if (!$regenerateScriptFlag) { |
| 118 | 165 | $regenerateScriptFlag = $this->deleteUnusedFormPageIds($postId, $bfUniqFormIds); |
| 119 | 166 | } |
| 120 | 167 | $isJsGenerating = get_option('bitforms_frontend_js_generating'); |
| 168 | + // The fast path also requires the cached bundle to exist on disk, not just be flagged in the DB. | |
| 169 | + $regenerateScriptFlag = $this->needsScriptGeneration($postId, $regenerateScriptFlag); | |
| 121 | 170 | if (!$regenerateScriptFlag && !$isJsGenerating && !empty($formIDs)) { |
| 122 | 171 | wp_enqueue_script('bit-form-all-script-test', $this->getJSFileSrc($postId), [], $formUpdateVersion, true); |
| 123 | 172 | return; |
| 124 | 173 | } |
| @@ -123,9 +172,9 @@ | ||
| 123 | 172 | return; |
| 124 | 173 | } |
| 125 | 174 | foreach ($bfFrontendFormIds as $index => $formId) { |
| 126 | 175 | $shortCodeCounter = $index + 1; |
| 127 | - $FrontendFormManager = new FrontendFormManager($formId, $shortCodeCounter); | |
| 176 | + $FrontendFormManager = FrontendFormManager::getInstance($formId, $shortCodeCounter); | |
| 128 | 177 | $formInfo = $FrontendFormManager->getFormInfo(); |
| 129 | 178 | $FormIdentifier = esc_js($FrontendFormManager->getFormIdentifier()); |
| 130 | 179 | $formContent = $FrontendFormManager->getFormContentWithValue($this->getValuesFromQueryParams()); |
| 131 | 180 | $formContent->formId = $formId; |
| @@ -148,8 +197,16 @@ | ||
| 148 | 197 | return; |
| 149 | 198 | } |
| 150 | 199 | |
| 151 | 200 | $frontendScriptGenObj->generateJsFile($formContents, $allFields, $contentIds, $postId, $formIDs, $previewMode); |
| 201 | + if ('preview' === $previewMode) { | |
| 202 | + return; | |
| 203 | + } | |
| 204 | + // Only mark the page as generated once the bundle is verifiably on disk; otherwise the | |
| 205 | + // next request must retry generation instead of fast-pathing to a stale/missing file. | |
| 206 | + if (!empty($bfUniqFormIds) && file_exists($this->getJSFilePath($postId))) { | |
| 207 | + $this->markScriptGenerated($bfUniqFormIds, $postId); | |
| 208 | + } | |
| 152 | 209 | wp_enqueue_script('bit-form-all-script-test', $this->getJSFileSrc($postId), [], $formUpdateVersion, true); |
| 153 | 210 | } |
| 154 | 211 | |
| 155 | 212 | private function deleteUnusedFormPageIds($postId, $formIDs) |
| @@ -165,9 +222,9 @@ | ||
| 165 | 222 | ); |
| 166 | 223 | $regenerateScriptFlag = false; |
| 167 | 224 | foreach ($forms as $form) { |
| 168 | 225 | $formId = $form->id; |
| 169 | - $generatedScriptPageIdsDecoded = json_decode($form->generated_script_page_ids, true); | |
| 226 | + $generatedScriptPageIdsDecoded = json_decode((string) $form->generated_script_page_ids, true); | |
| 170 | 227 | $generatedScriptPageIds = is_array($generatedScriptPageIdsDecoded) ? array_keys($generatedScriptPageIdsDecoded) : []; |
| 171 | 228 | if (!empty($generatedScriptPageIds) && !in_array($formId, $formIDs) && in_array($postId, $generatedScriptPageIds)) { |
| 172 | 229 | unset($generatedScriptPageIdsDecoded[$postId]); |
| 173 | 230 | if (empty($generatedScriptPageIdsDecoded)) { |
| @@ -177,15 +234,15 @@ | ||
| 177 | 234 | $formModel->update(['generated_script_page_ids' => wp_json_encode($generatedScriptPageIdsDecoded)], ['id' => $formId]); |
| 178 | 235 | } |
| 179 | 236 | } |
| 180 | 237 | if ($regenerateScriptFlag) { |
| 181 | - $formUpdateVersion = get_option('bit-form_form_update_version'); | |
| 238 | + $formUpdateVersion = get_option('bitform_form_update_version'); | |
| 182 | 239 | if (!$formUpdateVersion) { |
| 183 | 240 | $formUpdateVersion = 1; |
| 184 | 241 | } else { |
| 185 | 242 | $formUpdateVersion = (int) $formUpdateVersion + 1; |
| 186 | 243 | } |
| 187 | - update_option('bit-form_form_update_version', $formUpdateVersion); | |
| 244 | + update_option('bitform_form_update_version', $formUpdateVersion); | |
| 188 | 245 | } |
| 189 | 246 | return $regenerateScriptFlag; |
| 190 | 247 | } |
| 191 | 248 | |
| @@ -195,27 +252,45 @@ | ||
| 195 | 252 | if (!is_a($post, 'WP_Post') && !isset($post->ID)) { |
| 196 | 253 | return; |
| 197 | 254 | } |
| 198 | 255 | $postId = $post->ID; |
| 199 | - $regenerateScriptFlag = false; | |
| 200 | - $formModel = new FormModel(); | |
| 256 | + // Read-only check. Marking the page as generated is deferred to markScriptGenerated(), | |
| 257 | + // called only after the bundle file is actually written — marking here left the DB | |
| 258 | + // saying "generated" while the file stayed stale whenever generation failed mid-way. | |
| 201 | 259 | foreach ($formsIds as $formId) { |
| 202 | - $formInstance = new FormManager($formId); | |
| 260 | + $formInstance = FormManager::getInstance($formId); | |
| 203 | 261 | if (!$formInstance->isExist()) { |
| 204 | 262 | continue; |
| 205 | 263 | } |
| 206 | 264 | $generatedPages = $formInstance->getFormData('generated_script_page_ids'); |
| 207 | 265 | if (empty($generatedPages)) { |
| 208 | - $regenerateScriptFlag = true; | |
| 209 | - } elseif (is_object($generatedPages) && (!isset($generatedPages->{$postId}) || (isset($generatedPages->{$postId}) && false === $generatedPages->{$postId}))) { | |
| 210 | - $regenerateScriptFlag = true; | |
| 266 | + return true; | |
| 211 | 267 | } |
| 212 | - if (!$regenerateScriptFlag) { | |
| 268 | + if (is_object($generatedPages) && (!isset($generatedPages->{$postId}) || false === $generatedPages->{$postId})) { | |
| 269 | + return true; | |
| 270 | + } | |
| 271 | + } | |
| 272 | + return false; | |
| 273 | + } | |
| 274 | + | |
| 275 | + private function markScriptGenerated($formsIds, $postId) | |
| 276 | + { | |
| 277 | + // Fetched via FormModel rather than FormManager: FormManager keeps its row in a static | |
| 278 | + // property shared across instances, so after the render loop it holds the last form's | |
| 279 | + // data regardless of which instance is asked. | |
| 280 | + $formModel = new FormModel(); | |
| 281 | + foreach ($formsIds as $formId) { | |
| 282 | + $form = $formModel->get(['generated_script_page_ids'], ['id' => $formId]); | |
| 283 | + if (is_wp_error($form) || empty($form)) { | |
| 213 | 284 | continue; |
| 214 | 285 | } |
| 286 | + $generatedPages = Utilities::jsonObj($form[0]->generated_script_page_ids ?? ''); | |
| 215 | 287 | if (!is_object($generatedPages)) { |
| 216 | 288 | $generatedPages = (object) []; |
| 217 | 289 | } |
| 290 | + if (!empty($generatedPages->{$postId})) { | |
| 291 | + continue; | |
| 292 | + } | |
| 218 | 293 | $generatedPages->{$postId} = true; |
| 219 | 294 | $formModel->update( |
| 220 | 295 | [ |
| 221 | 296 | 'generated_script_page_ids' => \wp_json_encode($generatedPages) |
| @@ -224,16 +299,16 @@ | ||
| 224 | 299 | 'id' => $formId, |
| 225 | 300 | ] |
| 226 | 301 | ); |
| 227 | 302 | } |
| 228 | - return $regenerateScriptFlag; | |
| 229 | 303 | } |
| 230 | 304 | |
| 231 | 305 | private function addInlineScript($code, $handle = '', $position = 'after') |
| 232 | 306 | { |
| 233 | 307 | $scriptHandle = !empty($handle) ? $handle : 'bf-inline-script'; |
| 308 | + $formUpdateVersion = get_option('bitform_form_update_version'); | |
| 234 | 309 | if (!wp_script_is($scriptHandle)) { |
| 235 | - wp_register_script($scriptHandle, '', [], '', true); | |
| 310 | + wp_register_script($scriptHandle, '', [], $formUpdateVersion, true); | |
| 236 | 311 | wp_enqueue_script($scriptHandle); |
| 237 | 312 | } |
| 238 | 313 | wp_add_inline_script($scriptHandle, $code, $position); |
| 239 | 314 | } |
| @@ -240,10 +315,11 @@ | ||
| 240 | 315 | |
| 241 | 316 | private function addInlineStyle($code, $handle = '') |
| 242 | 317 | { |
| 243 | 318 | $styleHandle = !empty($handle) ? $handle : 'bf-inline-style'; |
| 319 | + $formUpdateVersion = get_option('bitform_form_update_version'); | |
| 244 | 320 | if (!wp_style_is($styleHandle)) { |
| 245 | - wp_register_style($styleHandle, '', [], '', true); | |
| 321 | + wp_register_style($styleHandle, '', [], $formUpdateVersion); | |
| 246 | 322 | wp_enqueue_style($styleHandle); |
| 247 | 323 | } |
| 248 | 324 | wp_add_inline_style($styleHandle, $code); |
| 249 | 325 | } |
| @@ -269,37 +345,37 @@ | ||
| 269 | 345 | |
| 270 | 346 | return $fields; |
| 271 | 347 | } |
| 272 | 348 | |
| 273 | - private function executeOnUserInput($formID, $shortCodeCounter, $fields) | |
| 349 | + private function executeOnUserInput($formID, $shortCodeCounter, $workFlowRunType = 'create') | |
| 274 | 350 | { |
| 275 | - $FrontendFormManager = new FrontendFormManager($formID, $shortCodeCounter); | |
| 351 | + $FrontendFormManager = FrontendFormManager::getInstance($formID, $shortCodeCounter); | |
| 276 | 352 | $previousValue = $this->getValuesFromQueryParams(); |
| 277 | 353 | $formContent = $FrontendFormManager->getFormContentWithValue($previousValue); |
| 278 | 354 | $customCodesExist = strpos(FrontEndScriptGenerator::getCustomCodes($formID)['JavaScript'], 'bfVars'); |
| 279 | 355 | if ($customCodesExist || (!empty($formContent->workFlowExist) && !empty($formContent->workFlowExist->oninput))) { |
| 280 | 356 | $workFlowRunHelper = new WorkFlow($formID); |
| 281 | - return $workFlowRunHelper->executeOnUserInput('create', $fields); | |
| 357 | + return $workFlowRunHelper->executeOnUserInput($workFlowRunType); | |
| 282 | 358 | } |
| 283 | 359 | } |
| 284 | 360 | |
| 285 | 361 | private function getValuesFromQueryParams() |
| 286 | 362 | { |
| 287 | - $reqField = $_SERVER['QUERY_STRING']; | |
| 363 | + // Read-only: query string parsed to pre-fill form fields. Values are sanitized per field before use. | |
| 288 | 364 | $queryParamsValue = []; |
| 289 | - if (!empty($reqField)) { | |
| 365 | + if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) { | |
| 366 | + $reqField = wp_unslash($_SERVER['QUERY_STRING']); | |
| 290 | 367 | foreach (explode('&', $reqField) as $keyValue) { |
| 291 | - // $pattern = '/([a-zA-Z0-9])([a-zA-Z])\=+/'; | |
| 292 | - $pattern = '/([^.]+)=(.*?)([^.]+)/'; | |
| 293 | - $matches = preg_match($pattern, $keyValue, $matchFormat); | |
| 294 | - if ($matches) { | |
| 368 | + if (false !== strpos($keyValue, '=')) { | |
| 295 | 369 | list($field, $value) = explode('=', $keyValue, 2); |
| 296 | 370 | |
| 297 | 371 | if (!trim($value)) { |
| 298 | 372 | continue; |
| 299 | 373 | } |
| 300 | - | |
| 301 | - $queryParamsValue[$field][] = sanitize_text_field(urldecode($value)); | |
| 374 | + $field = sanitize_text_field(urldecode($field)); | |
| 375 | + if (!empty($field)) { | |
| 376 | + $queryParamsValue[$field][] = sanitize_text_field(urldecode($value)); | |
| 377 | + } | |
| 302 | 378 | } |
| 303 | 379 | } |
| 304 | 380 | } |
| 305 | 381 | |
| @@ -314,8 +390,11 @@ | ||
| 314 | 390 | $formID = intval($atts['form_id']); |
| 315 | 391 | } |
| 316 | 392 | if (isset($atts['entry_id'])) { |
| 317 | 393 | $entryId = intval($atts['entry_id']); |
| 394 | + // Read-only: entry ID from query string for shortcode render. No state mutation. | |
| 395 | + } elseif (isset($_GET['bf_entry_id']) && !is_array($_GET['bf_entry_id'])) { | |
| 396 | + $entryId = intval(sanitize_text_field(wp_unslash($_GET['bf_entry_id']))); | |
| 318 | 397 | } else { |
| 319 | 398 | $entryId = false; |
| 320 | 399 | } |
| 321 | 400 | if (isset($atts['id'])) { |
| @@ -327,24 +406,22 @@ | ||
| 327 | 406 | return __('Form ID cannot be empty', 'bit-form'); |
| 328 | 407 | } |
| 329 | 408 | |
| 330 | 409 | if (!$this->isExist($formID)) { |
| 410 | + /* translators: %s: form ID */ | |
| 331 | 411 | return sprintf(__('#%s no. Form doesn\'t exists', 'bit-form'), $formID); |
| 332 | 412 | } |
| 333 | 413 | |
| 334 | - // check for abandoned form entry id | |
| 335 | - $isAbandoned = false; | |
| 336 | - if (empty($entryId) && Utilities::isPro() && class_exists('\BitCode\BitFormPro\Admin\FormSettings\FormAbandonment')) { | |
| 337 | - $FormAbandonment = new FormAbandonment($formID); | |
| 338 | - $isAbandoned = $FormAbandonment->checkAbandonedFormEntryId(); | |
| 339 | - } | |
| 414 | + // Add-ons may detect whether the current visitor is resuming an abandoned entry. | |
| 415 | + $isAbandoned = (bool) apply_filters('bitform_is_abandoned_entry', false, $formID, $entryId, $atts); | |
| 340 | 416 | |
| 341 | 417 | FrontendHelpers::setBfFrontendFormIds($formID); |
| 342 | 418 | $bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds; |
| 343 | 419 | $shortCodeCounter = count($bfFrontendFormIds); |
| 344 | - $FrontendFormManager = new FrontendFormManager($formID, $shortCodeCounter); | |
| 420 | + $FrontendFormManager = FrontendFormManager::getInstance($formID, $shortCodeCounter); | |
| 345 | 421 | |
| 346 | 422 | if (!$FrontendFormManager->checkStatus()) { |
| 423 | + /* translators: %s: form ID */ | |
| 347 | 424 | return sprintf(__('#%s no. Form is not active', 'bit-form'), $formID); |
| 348 | 425 | } |
| 349 | 426 | ob_start(); |
| 350 | 427 | $this->loadAssets($formID, $formType); |
| @@ -354,10 +431,11 @@ | ||
| 354 | 431 | if ($font && !$formPreview) { |
| 355 | 432 | wp_enqueue_style('bf-google-font', $font, '1.0.0', true); |
| 356 | 433 | } |
| 357 | 434 | |
| 435 | + // Read-only: password reset token from URL for display-time validation. No state written until form is submitted. | |
| 358 | 436 | if (!empty($_GET['token']) && !empty($_GET['id'])) { |
| 359 | - $this->validPassowordResetToken($_GET['token'], $_GET['id'], $formID); | |
| 437 | + $this->validPassowordResetToken(sanitize_text_field(wp_unslash($_GET['token'])), sanitize_text_field(wp_unslash($_GET['id'])), $formID); | |
| 360 | 438 | } |
| 361 | 439 | |
| 362 | 440 | $previousValue = $this->getValuesFromQueryParams(); |
| 363 | 441 | $errorMessages = []; // delete |
| @@ -373,23 +451,38 @@ | ||
| 373 | 451 | $nestedLayout = isset($formContent->nestedLayout) ? $formContent->nestedLayout : (object) []; |
| 374 | 452 | $buttons = !empty($formContent->buttons) ? $formContent->buttons : ''; |
| 375 | 453 | $additional = $formContent->additional; |
| 376 | 454 | |
| 377 | - $workFlowRunType = $entryId ? 'edit' : 'create'; | |
| 378 | - if ($entryId) { | |
| 379 | - $fields = $this->setFieldsValue($fields, $formID, $entryId); | |
| 455 | + // $workFlowRunType = $entryId ? 'edit' : 'create'; | |
| 456 | + if ($entryId && (FrontendHelpers::is_current_user_can_access($formID, 'entryEditAccess'))) { | |
| 457 | + $workFlowRunType = 'edit'; | |
| 458 | + $adminFormHandler = new AdminFormHandler(); | |
| 459 | + $getEntry = $adminFormHandler->getSingleEntry($formID, $entryId); | |
| 460 | + if (FrontendHelpers::is_current_user_can_access($formID, 'entryEditAccess', '', $getEntry->__user_id)) { | |
| 461 | + $fields = $this->setFieldsValue($fields, $formID, $entryId); | |
| 462 | + } elseif (!$isAbandoned) { | |
| 463 | + $entryId = false; | |
| 464 | + $workFlowRunType = 'create'; | |
| 465 | + } | |
| 466 | + } else { | |
| 467 | + $entryId = false; | |
| 468 | + $workFlowRunType = 'create'; | |
| 380 | 469 | } |
| 381 | 470 | |
| 471 | + // if ($entryId) { | |
| 472 | + // $fields = $this->setFieldsValue($fields, $formID, $entryId); | |
| 473 | + // } | |
| 474 | + | |
| 382 | 475 | $fields = apply_filters('bitform_filter_before_workflow_onload_fields', $fields, $formID); |
| 383 | 476 | $fields = $this->triggerWorkflowOnLoad($formID, $shortCodeCounter, $fields, $workFlowRunType); |
| 384 | 477 | $fields = apply_filters('bitform_filter_after_workflow_onload_fields', $fields, $formID); |
| 385 | 478 | do_action('bitform_onload_fields', $fields, $formID); |
| 386 | - $workFlowreturnedOnUserInput = $this->executeOnUserInput($formID, $shortCodeCounter, $fields); | |
| 479 | + $workFlowreturnedOnUserInput = $this->executeOnUserInput($formID, $shortCodeCounter, $workFlowRunType); | |
| 387 | 480 | |
| 388 | 481 | // test for form before remove |
| 389 | - $noLabel = ['decision-box', 'html', 'button', 'paypal', 'razorpay', 'recaptcha']; | |
| 482 | + $noLabelFieldTypes = ['decision-box', 'gdpr', 'html', 'shortcode', 'button', 'paypal', 'razorpay', 'recaptcha', 'turnstile', 'hcaptcha', 'stripe', 'spacer']; | |
| 390 | 483 | foreach ($fields as $fldKey => $field) { |
| 391 | - if (!in_array($field->typ, $noLabel) && isset($field->lbl)) { | |
| 484 | + if (!in_array($field->typ, $noLabelFieldTypes) && isset($field->lbl)) { | |
| 392 | 485 | $lblReplaceToBackslash = str_replace('$_bf_$', '\\', $field->lbl); |
| 393 | 486 | $fields->{$fldKey}->lbl = FieldValueHandler::replaceSmartTagWithValue($lblReplaceToBackslash); |
| 394 | 487 | } |
| 395 | 488 | } |
| @@ -395,9 +488,9 @@ | ||
| 395 | 488 | } |
| 396 | 489 | $fieldsKey = $FrontendFormManager->getFieldsKey(); |
| 397 | 490 | |
| 398 | 491 | $captchaV3Settings = $FrontendFormManager->getCaptchaV3Settings(); |
| 399 | - if ($FrontendFormManager->getCaptchaSettings() || $captchaV3Settings || $FrontendFormManager->getTurnstileSettings()) { | |
| 492 | + if ($FrontendFormManager->getCaptchaSettings() || $captchaV3Settings || $FrontendFormManager->getTurnstileSettings() || $FrontendFormManager->isFieldTypeExist('hcaptcha')) { | |
| 400 | 493 | $integrationHandler = new IntegrationHandler(0); |
| 401 | 494 | $allFormIntegrations = $integrationHandler->getAllIntegration('app'); |
| 402 | 495 | if (!is_wp_error($allFormIntegrations)) { |
| 403 | 496 | foreach ($allFormIntegrations as $integration) { |
| @@ -405,12 +498,14 @@ | ||
| 405 | 498 | $FrontendFormManager->getCaptchaSettings() |
| 406 | 499 | && !is_null($integration->integration_type) |
| 407 | 500 | && 'gReCaptcha' === $integration->integration_type |
| 408 | 501 | ) { |
| 409 | - $integrationDetails = json_decode($integration->integration_details); | |
| 410 | - $integrationDetails->id = $integration->id; | |
| 411 | - $reCAPTCHA = $integrationDetails; | |
| 412 | - $reCAPTCHAVersion = 'v2'; | |
| 502 | + $integrationDetails = Utilities::jsonObj($integration->integration_details); | |
| 503 | + if ($integrationDetails) { | |
| 504 | + $integrationDetails->id = $integration->id; | |
| 505 | + $reCAPTCHA = $integrationDetails; | |
| 506 | + $reCAPTCHAVersion = 'v2'; | |
| 507 | + } | |
| 413 | 508 | } |
| 414 | 509 | |
| 415 | 510 | if ( |
| 416 | 511 | $FrontendFormManager->getTurnstileSettings() |
| @@ -416,18 +511,29 @@ | ||
| 416 | 511 | $FrontendFormManager->getTurnstileSettings() |
| 417 | 512 | && !is_null($integration->integration_type) |
| 418 | 513 | && 'turnstileCaptcha' === $integration->integration_type |
| 419 | 514 | ) { |
| 420 | - $integrationDetails = json_decode($integration->integration_details); | |
| 421 | - $turnstileSiteKey = $integrationDetails->siteKey; | |
| 515 | + $integrationDetails = Utilities::jsonObj($integration->integration_details); | |
| 516 | + $turnstileSiteKey = $integrationDetails->siteKey ?? ''; | |
| 422 | 517 | } |
| 423 | 518 | |
| 519 | + if ( | |
| 520 | + $FrontendFormManager->isFieldTypeExist('hcaptcha') | |
| 521 | + && !is_null($integration->integration_type) | |
| 522 | + && 'hcaptcha' === $integration->integration_type | |
| 523 | + ) { | |
| 524 | + $integrationDetails = Utilities::jsonObj($integration->integration_details); | |
| 525 | + $hCaptchaSiteKey = $integrationDetails->siteKey ?? ''; | |
| 526 | + } | |
| 527 | + | |
| 424 | 528 | if ($captchaV3Settings) { |
| 425 | 529 | if (!is_null($integration->integration_type) && 'gReCaptchaV3' === $integration->integration_type) { |
| 426 | - $integrationDetails = json_decode($integration->integration_details); | |
| 427 | - $integrationDetails->id = $integration->id; | |
| 428 | - $reCAPTCHA = $integrationDetails; | |
| 429 | - $reCAPTCHAVersion = 'v3'; | |
| 530 | + $integrationDetails = Utilities::jsonObj($integration->integration_details); | |
| 531 | + if ($integrationDetails) { | |
| 532 | + $integrationDetails->id = $integration->id; | |
| 533 | + $reCAPTCHA = $integrationDetails; | |
| 534 | + $reCAPTCHAVersion = 'v3'; | |
| 535 | + } | |
| 430 | 536 | } |
| 431 | 537 | } |
| 432 | 538 | } |
| 433 | 539 | } |
| @@ -435,9 +541,11 @@ | ||
| 435 | 541 | |
| 436 | 542 | if ($captchaV3Settings && !empty($reCAPTCHA->siteKey)) { |
| 437 | 543 | // DANGER: no matter what, DONT CHANGE THE SCRIPT ID OF THIS SCRIPT |
| 438 | 544 | $scriptId = BITFORMS_PREFIX . 'recaptcha'; |
| 439 | - wp_enqueue_script($scriptId, "https://www.google.com/recaptcha/api.js?render={$reCAPTCHA->siteKey}"); | |
| 545 | + // External Google reCAPTCHA script; version managed by URL query param. Loaded in header because | |
| 546 | + // standalone form views do not render wp_footer(), making footer enqueue unreliable. | |
| 547 | + wp_enqueue_script($scriptId, "https://www.google.com/recaptcha/api.js?render={$reCAPTCHA->siteKey}", [], null, false); | |
| 440 | 548 | } |
| 441 | 549 | |
| 442 | 550 | $configs = [ |
| 443 | 551 | 'bf_separator' => BITFORMS_BF_SEPARATOR, |
| @@ -455,22 +563,23 @@ | ||
| 455 | 563 | if (!empty($paymentFieldData)) { |
| 456 | 564 | $integrationHandler = new IntegrationHandler(0); |
| 457 | 565 | foreach ($paymentFieldData as $fldKey => $fldData) { |
| 458 | 566 | $paymentIntegration = $integrationHandler->getAIntegration($fldData->payIntegID); |
| 459 | - if(is_wp_error($paymentIntegration)) { | |
| 567 | + if (is_wp_error($paymentIntegration)) { | |
| 460 | 568 | continue; |
| 461 | 569 | } |
| 570 | + $paymentIntegrationRow = Utilities::firstRow($paymentIntegration); | |
| 462 | 571 | if ('paypal' === $fldData->typ) { |
| 463 | - $integrationDetails = json_decode($paymentIntegration[0]->integration_details); | |
| 464 | - $clientID = $integrationDetails->clientID; | |
| 572 | + $integrationDetails = Utilities::jsonObj($paymentIntegrationRow->integration_details ?? ''); | |
| 573 | + $clientID = $integrationDetails->clientID ?? ''; | |
| 465 | 574 | $fields->{$fldKey}->clientId = $clientID; |
| 466 | 575 | } elseif ('razorpay' === $fldData->typ) { |
| 467 | - $integrationDetails = json_decode($paymentIntegration[0]->integration_details); | |
| 468 | - $clientID = $integrationDetails->apiKey; | |
| 576 | + $integrationDetails = Utilities::jsonObj($paymentIntegrationRow->integration_details ?? ''); | |
| 577 | + $clientID = $integrationDetails->apiKey ?? ''; | |
| 469 | 578 | $fields->{$fldKey}->clientId = $clientID; |
| 470 | 579 | } elseif ('stripe' === $fldData->typ) { |
| 471 | - $integrationDetails = json_decode($paymentIntegration[0]->integration_details); | |
| 472 | - $publishableKey = $integrationDetails->publishableKey; | |
| 580 | + $integrationDetails = Utilities::jsonObj($paymentIntegrationRow->integration_details ?? ''); | |
| 581 | + $publishableKey = $integrationDetails->publishableKey ?? ''; | |
| 473 | 582 | $fields->{$fldKey}->publishableKey = $publishableKey; |
| 474 | 583 | } |
| 475 | 584 | } |
| 476 | 585 | } |
| @@ -490,17 +599,19 @@ | ||
| 490 | 599 | 'appID' => "bitforms_{$formID}", |
| 491 | 600 | 'GCLID' => $FrontendFormManager->isGCLIDEnabled(), |
| 492 | 601 | 'assetUrl' => BITFORMS_ASSET_URI, |
| 493 | 602 | 'onfieldCondition' => !empty($workFlowreturnedOnUserInput['onfield_input_conditions']) ? $workFlowreturnedOnUserInput['onfield_input_conditions'] : false, |
| 494 | - 'smartTags' => !empty($workFlowreturnedOnUserInput['smart_tags']) ? $workFlowreturnedOnUserInput['smart_tags'] : [], | |
| 603 | + 'smartTags' => $this->buildFrontendSmartTags($formID, $workFlowreturnedOnUserInput, $fields), | |
| 495 | 604 | 'paymentCallbackUrl' => get_rest_url() . 'bitform/v1/payments/razorpay', |
| 496 | 605 | 'gRecaptchaSiteKey' => !empty($reCAPTCHA->siteKey) ? $reCAPTCHA->siteKey : null, |
| 497 | 606 | 'gRecaptchaVersion' => !empty($reCAPTCHAVersion) ? $reCAPTCHAVersion : null, |
| 498 | - 'turnstileSiteKey' => !empty($turnstileSiteKey) ? $turnstileSiteKey : null | |
| 607 | + 'turnstileSiteKey' => !empty($turnstileSiteKey) ? $turnstileSiteKey : null, | |
| 608 | + 'hCaptchaSiteKey' => !empty($hCaptchaSiteKey) ? $hCaptchaSiteKey : null, | |
| 499 | 609 | ]; |
| 500 | 610 | |
| 501 | 611 | if ($entryId) { |
| 502 | 612 | $bitFormFrontArr['entryId'] = $entryId; |
| 613 | + self::markResponseUncacheable(); | |
| 503 | 614 | } |
| 504 | 615 | |
| 505 | 616 | if (isset($additional->enabled->validateFocusLost)) { |
| 506 | 617 | $bitFormFrontArr['validateFocusLost'] = true; |
| @@ -506,15 +617,18 @@ | ||
| 506 | 617 | $bitFormFrontArr['validateFocusLost'] = true; |
| 507 | 618 | } |
| 508 | 619 | |
| 509 | 620 | if (!empty($isAbandoned)) { |
| 621 | + // One visitor's typed values, so this response must not be page-cached. | |
| 510 | 622 | $bitFormFrontArr['oldValues'] = $this->getFieldsValue($formID, $isAbandoned); |
| 623 | + self::markResponseUncacheable(); | |
| 511 | 624 | if (empty($entryId)) { |
| 512 | - $bitFormFrontArr['entryId'] = $isAbandoned; | |
| 625 | + $bitFormFrontArr['entryId'] = $entryId; | |
| 513 | 626 | } |
| 514 | 627 | } |
| 515 | 628 | |
| 516 | 629 | $formInfo = $FrontendFormManager->getFormInfo(); |
| 630 | + $bitFormFrontArr['formName'] = $formInfo->formName ?? ''; | |
| 517 | 631 | if (is_array($layout) && count($layout) > 1) { |
| 518 | 632 | $multiStepSettings = isset($formInfo->multiStepSettings) ? $formInfo->multiStepSettings : null; |
| 519 | 633 | $newTempSettings = (object) [ |
| 520 | 634 | 'validateOnStepChange' => isset($multiStepSettings->validateOnStepChange) ? $multiStepSettings->validateOnStepChange : false, |
| @@ -533,34 +647,42 @@ | ||
| 533 | 647 | } |
| 534 | 648 | $bitFormFrontArr['formInfo']->conversationalSettings = $formInfo->conversationalSettings; |
| 535 | 649 | } |
| 536 | 650 | |
| 537 | - $bitFormsFront = apply_filters( | |
| 538 | - 'bitforms_localized_script', | |
| 539 | - $bitFormFrontArr | |
| 540 | - ); | |
| 651 | + $formAbandonmentSettings = $FrontendFormManager->getFormAbandonmentSettings(); | |
| 652 | + if (Helpers::property_exists_nested($formAbandonmentSettings, 'active', true)) { | |
| 653 | + $bitFormFrontArr['formSettings'] = (object)[ | |
| 654 | + 'formAbandonment' => $formAbandonmentSettings | |
| 655 | + ]; | |
| 656 | + } | |
| 541 | 657 | |
| 542 | 658 | $layout = wp_json_encode($layout); |
| 543 | 659 | $buttons = wp_json_encode($buttons); |
| 544 | 660 | $frontArr = wp_json_encode($bitFormFrontArr); |
| 545 | 661 | |
| 546 | - $bfGlobals = <<<BFGLOBALS | |
| 547 | - if(!window.bf_globals) { | |
| 548 | - window.bf_globals = {} | |
| 549 | - } if(!window.bf_globals.{$FormIdentifier}) { | |
| 550 | - window.bf_globals.{$FormIdentifier} = {} | |
| 662 | + $bfGlobals = sprintf(' | |
| 663 | + if(!window.bf_globals) { | |
| 664 | + window.bf_globals = {} | |
| 665 | + } if(!window.bf_globals.%1$s) { | |
| 666 | + window.bf_globals.%1$s = {} | |
| 551 | 667 | } |
| 552 | - if(document.getElementById('{$FormIdentifier}')) { | |
| 553 | - window.bf_globals.{$FormIdentifier} = {...window.bf_globals.{$FormIdentifier}, ...{$frontArr}}; | |
| 554 | - } | |
| 555 | -BFGLOBALS; | |
| 668 | + window.bf_globals.%1$s = { | |
| 669 | + ...window.bf_globals.%1$s, | |
| 670 | + ...%2$s | |
| 671 | + }; | |
| 672 | + if (typeof window.bitformInit === "function") { window.bitformInit("%1$s"); }', $FormIdentifier, $frontArr); | |
| 556 | 673 | |
| 674 | + // Inert copy of the config. Optimizers only rewrite executable scripts, so | |
| 675 | + // this survives and travels with the markup; the runtime hydrates from it | |
| 676 | + // whenever bf_globals is missing. | |
| 677 | + $configTag = self::buildFormConfigTag($FormIdentifier, $bitFormFrontArr); | |
| 678 | + | |
| 557 | 679 | if ('conversational' === $formType |
| 558 | 680 | && isset($formContent->formInfo->conversationalSettings->enable) |
| 559 | 681 | && $formContent->formInfo->conversationalSettings->enable) { |
| 560 | - $html = $FrontendFormManager->conversationalFormView($fields, $file, $errorMessages); | |
| 682 | + $html = $FrontendFormManager->conversationalFormView($fields, $file, $errorMessages, null, !empty($entryId)); | |
| 561 | 683 | } else { |
| 562 | - $html = $FrontendFormManager->formView($fields, $file, $errorMessages); | |
| 684 | + $html = $FrontendFormManager->formView($fields, $file, $errorMessages, null, !empty($entryId)); | |
| 563 | 685 | } |
| 564 | 686 | |
| 565 | 687 | // if form preview then return html otherwise echo with output buffer |
| 566 | 688 | if ($formPreview) { |
| @@ -568,17 +690,142 @@ | ||
| 568 | 690 | $formViewObject = new \stdClass(); |
| 569 | 691 | $formViewObject->html = $html; |
| 570 | 692 | $formViewObject->font = $font; |
| 571 | 693 | $formViewObject->bfGlobals = $bfGlobals; |
| 694 | + $formViewObject->configTag = $configTag; | |
| 695 | + $formViewObject->formContent = $formContent; | |
| 572 | 696 | return $formViewObject; |
| 573 | 697 | } |
| 574 | - $html .= <<<BFGLOBALSSCRIPT | |
| 575 | - <script id="bit-form-bf-globals-{$FormIdentifier}">{$bfGlobals}</script> | |
| 576 | -BFGLOBALSSCRIPT; | |
| 577 | - echo trim($html); | |
| 698 | + | |
| 699 | + $bfGlobalsHandle = 'bitform-bf-globals-' . sanitize_key($FormIdentifier); | |
| 700 | + $this->addInlineScript($bfGlobals, $bfGlobalsHandle, 'after'); | |
| 701 | + $this->emitShowPickerBridge(); | |
| 702 | + | |
| 703 | + // Printed outside wp_kses rather than allowing <script> in form markup. | |
| 704 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built by buildFormConfigTag(), JSON_HEX_* escaped. | |
| 705 | + echo $configTag; | |
| 706 | + echo wp_kses(trim($html), EscapingHelper::getFormAllowedHtml($formContent)); | |
| 578 | 707 | return ob_get_clean(); |
| 579 | 708 | } |
| 580 | 709 | |
| 710 | + /** | |
| 711 | + * Keep per-visitor config (oldValues, entryId) out of full-page caches. | |
| 712 | + * | |
| 713 | + * @return void | |
| 714 | + */ | |
| 715 | + public static function markResponseUncacheable() | |
| 716 | + { | |
| 717 | + // DONOTCACHEPAGE does the work; caches read it at shutdown. Rendering | |
| 718 | + // usually runs after headers are sent, so nocache_headers() is a bonus. | |
| 719 | + if (!defined('DONOTCACHEPAGE')) { | |
| 720 | + define('DONOTCACHEPAGE', true); | |
| 721 | + } | |
| 722 | + if (!headers_sent() && function_exists('nocache_headers')) { | |
| 723 | + nocache_headers(); | |
| 724 | + } | |
| 725 | + } | |
| 726 | + | |
| 727 | + /** | |
| 728 | + * Build the inert JSON config block for a rendered form. | |
| 729 | + * | |
| 730 | + * JSON_HEX_* escapes < > & as \u00XX so no field value can close the script | |
| 731 | + * element or inject markup. | |
| 732 | + * | |
| 733 | + * @param string $formIdentifier | |
| 734 | + * @param array $config | |
| 735 | + * | |
| 736 | + * @return string | |
| 737 | + */ | |
| 738 | + public static function buildFormConfigTag($formIdentifier, $config) | |
| 739 | + { | |
| 740 | + $json = wp_json_encode($config, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); | |
| 741 | + if (false === $json) { | |
| 742 | + return ''; | |
| 743 | + } | |
| 744 | + // Some optimizers wrap any inline <script>, including application/json, | |
| 745 | + // in DOMContentLoaded boilerplate that corrupts the JSON. These attributes | |
| 746 | + // make the common ones skip it; the JS side also salvage-parses. | |
| 747 | + return sprintf( | |
| 748 | + '<script type="application/json" class="bf-form-config" id="bf-config-%1$s" data-bf-form="%1$s" data-no-optimize="1" data-no-defer="1" data-no-minify="1" data-cfasync="false" nowprocket>%2$s</script>', | |
| 749 | + esc_attr($formIdentifier), | |
| 750 | + $json | |
| 751 | + ); | |
| 752 | + } | |
| 753 | + | |
| 754 | + /** | |
| 755 | + * Build the smart-tag map exposed to the browser in window.bf_globals[formId].smartTags. | |
| 756 | + * | |
| 757 | + * Security: the legacy code shipped the ENTIRE ~43-tag map to every visitor, leaking | |
| 758 | + * PII (admin/user/author email) and freezing per-visitor request data (IP, time, | |
| 759 | + * browser, referer) into cacheable HTML. We now emit ONLY tags that are (a) actually | |
| 760 | + * referenced by this form's client-evaluated surfaces — conditional logic, payment | |
| 761 | + * notes, admin custom JS — AND (b) flagged frontend-safe in the registry (static/post | |
| 762 | + * context only). Sensitive (identity) and request/visitor tags are never emitted; they | |
| 763 | + * resolve server-side at submit time instead. | |
| 764 | + * | |
| 765 | + * @param int|string $formID | |
| 766 | + * @param mixed $workflowConditions on-field input conditions (client-evaluated) | |
| 767 | + * @param mixed $fields form fields object (carries payment notes, etc.) | |
| 768 | + * @return array<string,string> | |
| 769 | + */ | |
| 770 | + private function buildFrontendSmartTags($formID, $workflowConditions, $fields) | |
| 771 | + { | |
| 772 | + // Haystack = only surfaces the browser actually evaluates against smartTags. | |
| 773 | + $haystack = wp_json_encode($workflowConditions) . ' ' . wp_json_encode($fields); | |
| 774 | + $customJs = FrontEndScriptGenerator::getCustomCodes($formID)['JavaScript']; | |
| 775 | + if (is_string($customJs) && '' !== $customJs) { | |
| 776 | + $haystack .= ' ' . $customJs; | |
| 777 | + } | |
| 778 | + | |
| 779 | + $ctx = SmartTags::getPostUserData(); | |
| 780 | + $frontendSmartTags = []; | |
| 781 | + $referenced = []; | |
| 782 | + foreach (SmartTags::smartTagFieldKeys() as $key) { | |
| 783 | + if (!SmartTagRegistry::isFrontendExposable($key)) { | |
| 784 | + continue; // identity / request / param tags never travel to the browser | |
| 785 | + } | |
| 786 | + // Match '${' . key prefix so keys containing spaces/slashes/commas are handled. | |
| 787 | + if (false !== strpos($haystack, '${' . $key)) { | |
| 788 | + $referenced[] = $key; | |
| 789 | + $frontendSmartTags[$key] = SmartTagRegistry::resolve($key, $ctx); | |
| 790 | + } | |
| 791 | + } | |
| 792 | + | |
| 793 | + /** | |
| 794 | + * Escape hatch: a site that genuinely needs an extra tag client-side can opt it | |
| 795 | + * back in explicitly here, rather than core shipping everything by default. | |
| 796 | + * | |
| 797 | + * @param array<string,string> $frontendSmartTags resolved frontend-safe smart tags | |
| 798 | + * @param int|string $formID | |
| 799 | + * @param string[] $referenced keys detected in client surfaces | |
| 800 | + */ | |
| 801 | + return apply_filters('bitform_frontend_smarttags', $frontendSmartTags, $formID, $referenced); | |
| 802 | + } | |
| 803 | + | |
| 804 | + /** | |
| 805 | + * Delegated listener that opens the native picker on date/time inputs marked | |
| 806 | + * with data-bf-show-picker. Replaces the legacy hardcoded onclick attribute. | |
| 807 | + * Registered as inline script once per request via wp_add_inline_script so | |
| 808 | + * the markup never travels through wp_kses(). | |
| 809 | + */ | |
| 810 | + private function emitShowPickerBridge() | |
| 811 | + { | |
| 812 | + static $emitted = false; | |
| 813 | + if ($emitted) { | |
| 814 | + return; | |
| 815 | + } | |
| 816 | + $emitted = true; | |
| 817 | + $code = 'if(!window.__bfShowPickerBound){window.__bfShowPickerBound=true;document.addEventListener("click",function(e){var t=e.target;if(t&&t.matches&&t.matches("input[data-bf-show-picker=\"1\"]")&&typeof t.showPicker==="function"){try{t.showPicker();}catch(_){}}});}'; | |
| 818 | + $this->addInlineScript($code, 'bitform-show-picker-bridge', 'after'); | |
| 819 | + } | |
| 820 | + | |
| 821 | + /** | |
| 822 | + * Does this form row exist? | |
| 823 | + * | |
| 824 | + * @param int $formID | |
| 825 | + * | |
| 826 | + * @return bool | |
| 827 | + */ | |
| 581 | 828 | private function isExist($formID) |
| 582 | 829 | { |
| 583 | 830 | $formModel = new FormModel(); |
| 584 | 831 | $form = $formModel->get( |
| @@ -588,16 +835,37 @@ | ||
| 588 | 835 | [ |
| 589 | 836 | 'id' => $formID, |
| 590 | 837 | ] |
| 591 | 838 | ); |
| 592 | - if (!is_wp_error($form)) { | |
| 593 | - return true; | |
| 839 | + | |
| 840 | + if (is_wp_error($form)) { | |
| 841 | + if ('result_empty' !== $form->get_error_code()) { | |
| 842 | + Log::debug_log([ | |
| 843 | + 'message' => 'Form lookup failed — reported to the visitor as a missing form', | |
| 844 | + 'formID' => $formID, | |
| 845 | + 'code' => $form->get_error_code(), | |
| 846 | + 'error' => $form->get_error_message(), | |
| 847 | + ]); | |
| 848 | + } | |
| 849 | + | |
| 850 | + return false; | |
| 594 | 851 | } |
| 595 | - return false; | |
| 852 | + | |
| 853 | + if (empty($form)) { | |
| 854 | + Log::debug_log([ | |
| 855 | + 'message' => 'Form lookup returned no rows without an error (is the form table present?)', | |
| 856 | + 'formID' => $formID, | |
| 857 | + ]); | |
| 858 | + | |
| 859 | + return false; | |
| 860 | + } | |
| 861 | + | |
| 862 | + return true; | |
| 596 | 863 | } |
| 597 | 864 | |
| 598 | 865 | private function getFieldsValue($formID, $entryID) |
| 599 | 866 | { |
| 867 | + $FrontendFormManager = FrontendFormManager::getInstance($formID, 1); | |
| 600 | 868 | $formEntryModel = new FormEntryMetaModel(); |
| 601 | 869 | $metaValues = $formEntryModel->get( |
| 602 | 870 | [ |
| 603 | 871 | 'meta_key', |
| @@ -606,8 +874,9 @@ | ||
| 606 | 874 | [ |
| 607 | 875 | 'bitforms_form_entry_id' => $entryID, |
| 608 | 876 | ] |
| 609 | 877 | ); |
| 878 | + $formFields = $FrontendFormManager->getFields(); | |
| 610 | 879 | $fldsData = (object) []; |
| 611 | 880 | if (!is_wp_error($metaValues)) { |
| 612 | 881 | foreach ($metaValues as $metaValue) { |
| 613 | 882 | $metaKey = $metaValue->meta_key; |
| @@ -625,8 +894,11 @@ | ||
| 625 | 894 | if (!isset($fldsData->{$metaKey})) { |
| 626 | 895 | $fldsData->{$metaKey} = ''; |
| 627 | 896 | } |
| 628 | 897 | $fldsData->{$metaKey} = $metaVal; |
| 898 | + if (isset($formFields[$metaKey]['type']) && in_array($formFields[$metaKey]['type'], ['file-up', 'advanced-file-up'])) { | |
| 899 | + $fldsData->{$metaKey} = $metaValue->meta_value; | |
| 900 | + } | |
| 629 | 901 | } |
| 630 | 902 | } |
| 631 | 903 | |
| 632 | 904 | return $fldsData; |
| @@ -644,8 +916,23 @@ | ||
| 644 | 916 | 'bitforms_form_entry_id' => $entryID, |
| 645 | 917 | ] |
| 646 | 918 | ); |
| 647 | 919 | if (!is_wp_error($metaValues)) { |
| 920 | + $urlQuery = wp_parse_url(FileDownloadProvider::getBaseDownloadURL(), PHP_URL_QUERY); | |
| 921 | + $baseDLURL = FileDownloadProvider::getBaseDownloadURL(); | |
| 922 | + $baseDLURL = empty($urlQuery) ? $baseDLURL . '?' : $baseDLURL . '&'; | |
| 923 | + $baseDLURL .= "formID={$formID}&entryID={$entryID}"; | |
| 924 | + | |
| 925 | + foreach ($fields as $field) { | |
| 926 | + if ('file-up' === $field->typ || 'advanced-file-up' === $field->typ) { | |
| 927 | + if (!isset($field->config)) { | |
| 928 | + $field->config = (object) []; | |
| 929 | + } elseif (is_array($field->config)) { | |
| 930 | + $field->config = (object) $field->config; | |
| 931 | + } | |
| 932 | + $field->config->baseDLURL = $baseDLURL; | |
| 933 | + } | |
| 934 | + } | |
| 648 | 935 | foreach ($metaValues as $metaValue) { |
| 649 | 936 | $metaKey = $metaValue->meta_key; |
| 650 | 937 | $metaVal = $metaValue->meta_value; |
| 651 | 938 | // if meta value is array then convert to string |
| @@ -662,18 +949,59 @@ | ||
| 662 | 949 | $fields->{$metaKey}->val = $metaVal; |
| 663 | 950 | if ('file-up' === $fields->{$metaKey}->typ || 'advanced-file-up' === $fields->{$metaKey}->typ) { |
| 664 | 951 | $fields->{$metaKey}->val = $metaValue->meta_value; |
| 665 | 952 | $fields->{$metaKey}->config->oldFiles = $metaValue->meta_value; |
| 666 | - $urlQuery = wp_parse_url(FileDownloadProvider::getBaseDownloadURL(), PHP_URL_QUERY); | |
| 667 | - $baseDLURL = FileDownloadProvider::getBaseDownloadURL(); | |
| 668 | - $baseDLURL = empty($urlQuery) ? $baseDLURL . '?' : $baseDLURL . '&'; | |
| 669 | - $fields->{$metaKey}->config->baseDLURL = $baseDLURL . "formID={$formID}&entryID={$entryID}"; | |
| 670 | 953 | } |
| 954 | + if ('signature' === $fields->{$metaKey}->typ) { | |
| 955 | + $this->setOldSignature($fields->{$metaKey}, $formID, $entryID, $metaValue->meta_value); | |
| 956 | + } | |
| 671 | 957 | } |
| 672 | 958 | } |
| 673 | 959 | } |
| 960 | + return $fields; | |
| 961 | + } | |
| 674 | 962 | |
| 675 | - return $fields; | |
| 963 | + /** Give the signature field its stored signature: a data URI to redraw, and the name it posts back as `_old`. */ | |
| 964 | + private function setOldSignature($field, $formID, $entryID, $storedValue) | |
| 965 | + { | |
| 966 | + $fileName = is_string($storedValue) ? trim($storedValue) : ''; | |
| 967 | + $decoded = json_decode($fileName, true); | |
| 968 | + if (is_array($decoded)) { | |
| 969 | + $fileName = empty($decoded) ? '' : trim((string) reset($decoded)); | |
| 970 | + } | |
| 971 | + // signature-failed.png means the stored signature was never usable. | |
| 972 | + if ('' === $fileName || 'signature-failed.png' === $fileName) { | |
| 973 | + return; | |
| 974 | + } | |
| 975 | + $fileName = sanitize_file_name($fileName); | |
| 976 | + if (!isset($field->config)) { | |
| 977 | + $field->config = (object) []; | |
| 978 | + } elseif (is_array($field->config)) { | |
| 979 | + $field->config = (object) $field->config; | |
| 980 | + } | |
| 981 | + $field->config->oldSignatureFile = $fileName; | |
| 982 | + | |
| 983 | + $filePath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR . $fileName; | |
| 984 | + if (!is_file($filePath) || !is_readable($filePath)) { | |
| 985 | + return; | |
| 986 | + } | |
| 987 | + // The types getSignatureFilePath() writes; wp_check_filetype() reports none for SVG. | |
| 988 | + $signatureMimeTypes = ['png' => 'image/png', 'jpg' => 'image/jpeg', 'svg' => 'image/svg+xml']; | |
| 989 | + $extension = strtolower((string) pathinfo($fileName, PATHINFO_EXTENSION)); | |
| 990 | + if (!isset($signatureMimeTypes[$extension])) { | |
| 991 | + return; | |
| 992 | + } | |
| 993 | + $mimeType = $signatureMimeTypes[$extension]; | |
| 994 | + // A hand-drawn signature is a few KB; a larger file is not worth inlining. | |
| 995 | + if (filesize($filePath) > self::MAX_INLINE_SIGNATURE_BYTES) { | |
| 996 | + return; | |
| 997 | + } | |
| 998 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_get_contents -- local upload dir read, inlined as a data URI for the signature pad. | |
| 999 | + $contents = file_get_contents($filePath); | |
| 1000 | + if (false === $contents || '' === $contents) { | |
| 1001 | + return; | |
| 1002 | + } | |
| 1003 | + $field->config->oldSignature = 'data:' . $mimeType . ';base64,' . base64_encode($contents); | |
| 676 | 1004 | } |
| 677 | 1005 | |
| 678 | 1006 | public function loadAssets($formID = 0, $fromType = 'classic') |
| 679 | 1007 | { |
| @@ -695,9 +1023,9 @@ | ||
| 695 | 1023 | $newFormId = $formID . '-formid'; |
| 696 | 1024 | } else { |
| 697 | 1025 | $newFormId = $formID; |
| 698 | 1026 | } |
| 699 | - $formUpdateVersion = get_option('bit-form_form_update_version'); | |
| 1027 | + $formUpdateVersion = get_option('bitform_form_update_version'); | |
| 700 | 1028 | if (!wp_style_is('bitform-style-' . $newFormId) && is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $newFormId . '.css')) { |
| 701 | 1029 | wp_enqueue_style( |
| 702 | 1030 | 'bitform-style-' . $newFormId, |
| 703 | 1031 | BITFORMS_UPLOAD_BASE_URL . "/form-styles/bitform-{$newFormId}.css", |
| @@ -705,9 +1033,9 @@ | ||
| 705 | 1033 | $formUpdateVersion |
| 706 | 1034 | ); |
| 707 | 1035 | if ($isPageBuilder) { |
| 708 | 1036 | $formStyle = file_get_contents(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $newFormId . '.css'); |
| 709 | - echo sprintf("<style id='bitform-style-{$newFormId}'>%s</style>", $formStyle); | |
| 1037 | + echo '<style id="bitform-style-' . esc_attr((string) $newFormId) . '">' . wp_kses($formStyle, []) . '</style>'; | |
| 710 | 1038 | } |
| 711 | 1039 | } |
| 712 | 1040 | if (!wp_style_is('bitform-style-custom-' . $formID) && is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-custom-' . $formID . '.css')) { |
| 713 | 1041 | wp_enqueue_style( |
| @@ -717,9 +1045,9 @@ | ||
| 717 | 1045 | $formUpdateVersion |
| 718 | 1046 | ); |
| 719 | 1047 | if ($isPageBuilder) { |
| 720 | 1048 | $formStyle = file_get_contents(BITFORMS_CONTENT_DIR . '/form-styles/bitform-custom-' . $formID . '.css'); |
| 721 | - echo sprintf("<style id='bitform-style-custom-{$formID}'>%s</style>", $formStyle); | |
| 1049 | + echo '<style id="bitform-style-custom-' . esc_attr((string) $formID) . '">' . wp_kses($formStyle, []) . '</style>'; | |
| 722 | 1050 | } |
| 723 | 1051 | } |
| 724 | 1052 | // load conversational form css |
| 725 | 1053 | if ('conversational' === $fromType) { |