Fix: hide debug HTML comments in REST and Ajax responses#1021
Conversation
There was a problem hiding this comment.
Pull request overview
This PR prevents WP Super Cache debug HTML comments (<!-- ... -->) from being appended to non-HTML responses (notably REST/JSON, WooCommerce API, and Ajax), avoiding response corruption for API consumers while preserving existing debug logging behavior.
Changes:
- Add early-bail conditions in
wp_cache_append_tag()to skip appending debug HTML comments for REST/JSON/WC API and Ajax requests. - Apply the same early-bail logic in
wp_cache_add_to_buffer()so other debug HTML comment insertions are also suppressed for non-HTML response contexts.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
4d15180 to
18753eb
Compare
…e check The REST/Ajax/JSON/WC bail condition was copy-pasted into both wp_cache_append_tag() and wp_cache_add_to_buffer(). Move it into a single named predicate so the two call sites can't drift, and drop the explanatory comment the inline condition needed.
Pushed a small refactor on top of your cherry-pick (2ac3825) and marked the PR ready for review. The REST/Ajax/JSON/WC bail condition was copy-pasted identically into both function wpsc_skip_debug_output( $buffer ) {
return strpos( $buffer, '<html' ) === false ||
( defined( 'REST_REQUEST' ) && REST_REQUEST ) ||
( defined( 'JSON_REQUEST' ) && JSON_REQUEST ) ||
( defined( 'WC_API_REQUEST' ) && WC_API_REQUEST ) ||
( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() );
}Both call sites are now One open item: this path has no automated coverage (see #1051), so the suppression is currently verified by the manual test plan only. |
advanced-cache.php already treats XMLRPC_REQUEST as a non-HTML response type; keep wpsc_skip_debug_output() consistent so XML-RPC bodies stay clean regardless of the <html sniff.
Note on the In practice XML-RPC requests never reach this code path — It's kept for intent/parity with The other clauses are reachable and necessary: REST ( |
Summary
<!-- ... -->) to non-HTML responses such as REST API, Ajax, JSON API, and WooCommerce API requests.REST_REQUEST,JSON_REQUEST,WC_API_REQUESTconstants andwp_doing_ajax()to bothwp_cache_append_tag()andwp_cache_add_to_buffer(), bailing early when the response is not standard HTML.Originally proposed by @jom in #436.
This is a manual cherry-pick of the changes from closed PR #436, adapted to the current codebase. See #436.
Test plan
/wp-json/wp/v2/posts) do not contain<!-- ... -->debug comments when Super Cache debug mode is enabled.admin-ajax.php) do not contain HTML debug comments.<htmltag) continue to be excluded as before.