Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion inc/ui/class-checkout-element.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,14 @@ protected function send_checkout_nocache_headers($atts) {
$headers = apply_filters('wu_checkout_nocache_headers', $headers, $atts, $this);

foreach ($headers as $name => $value) {
$name = trim(str_replace(["\r", "\n"], '', (string) $name));
$value = str_replace(["\r", "\n"], '', (string) $value);

header(sprintf('%s: %s', sanitize_key($name), $value), true);
if ('' === $name || ! preg_match('/^[A-Za-z0-9-]+$/', $name)) {
continue;
}

header(sprintf('%s: %s', $name, $value), true);
}
}

Expand Down Expand Up @@ -848,6 +853,26 @@ function loadCheckout() {
request.send(parts.join('&'));
}

function cleanupFallbackLoad() {
document.removeEventListener('DOMContentLoaded', fallbackLoad);
window.removeEventListener('scroll', fallbackLoad);
window.removeEventListener('resize', fallbackLoad);
}

function fallbackLoad() {
cleanupFallbackLoad();
loadCheckout();
}

function scheduleFallbackLoad() {
if ('loading' === document.readyState) {
document.addEventListener('DOMContentLoaded', fallbackLoad);
} else {
window.addEventListener('scroll', fallbackLoad);
window.addEventListener('resize', fallbackLoad);
}
Comment on lines +867 to +873

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Trigger fallback immediately when DOM is already ready.

When document.readyState !== 'loading', the fallback currently waits for a scroll/resize event. In late-execution contexts this can leave viewport defer mode idle until user interaction. Call fallbackLoad() immediately in that branch, and keep listeners only for the loading case.

Suggested patch
 function scheduleFallbackLoad() {
 	if ('loading' === document.readyState) {
 		document.addEventListener('DOMContentLoaded', fallbackLoad);
-	} else {
 		window.addEventListener('scroll', fallbackLoad);
 		window.addEventListener('resize', fallbackLoad);
+	} else {
+		fallbackLoad();
 	}
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@inc/ui/class-checkout-element.php` around lines 867 - 873, The
scheduleFallbackLoad function currently waits for scroll/resize when
document.readyState !== 'loading', which can delay fallback activation; change
scheduleFallbackLoad so that if document.readyState === 'loading' it adds
document.addEventListener('DOMContentLoaded', fallbackLoad), otherwise it calls
fallbackLoad() immediately and does NOT add scroll/resize listeners; update
references in scheduleFallbackLoad and ensure only the loading branch registers
listeners while the ready branch invokes fallbackLoad() directly.

}

if (button) {
button.addEventListener('click', function(event) {
event.preventDefault();
Expand All @@ -870,6 +895,8 @@ function loadCheckout() {
loadCheckout();
}
}).observe(root);
} else if ('viewport' === trigger) {
scheduleFallbackLoad();
}
})();
</script>
Expand Down
2 changes: 2 additions & 0 deletions tests/WP_Ultimo/UI/Checkout_Element_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public function test_deferred_output_renders_placeholder_without_nocache_action(
* Test live checkout output path contains explicit no-cache safeguards.
*/
public function test_source_contains_live_checkout_cache_safeguards(): void {
// Source-token assertions intentionally guard cache-safety hooks/headers that
// are otherwise hard to observe reliably in PHPUnit.
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$source = file_get_contents(dirname(__DIR__, 3) . '/inc/ui/class-checkout-element.php');

Expand Down
Loading