0 ) { ob_end_flush(); } } /** * Master processor for the HTML document. * * @param string $html HTML content. * @return string */ public function process_html( $html ) { if ( empty( $html ) ) { return $html; } // Only process full HTML responses. if ( stripos( $html, 'inject_resource_hints( $html, $settings ); // 2. Inject font-display: swap to eliminate FOIT (Flash of Invisible Text). $html = $this->inject_font_display_swap( $html ); // 3. Remove Stripe express-checkout and payment gateway overhead on non-checkout pages. $html = $this->clean_non_checkout_bloat( $html ); // 3b. Guaranteed Mobile Logo & Brand Header Protection. $html = $this->protect_header_and_logo_images( $html ); $html = $this->inject_mobile_logo_guard( $html ); // 4. Optimize Hero LCP Image (supports CSS background images and content tags). $html = $this->optimize_lcp_image( $html, $settings ); // 5. Inject Missing Dimensions & Lazy Loading on secondary page images for CLS prevention. if ( ! empty( $settings['add_missing_dimensions'] ) || ! empty( $settings['lazy_load_images'] ) ) { $html = $this->optimize_page_images( $html, $settings ); } // 8. Delay Heavy Third-Party Trackers for INP/TBT elimination. if ( ! empty( $settings['js_delay'] ) || ! empty( $settings['safe_mode'] ) ) { $html = $this->delay_third_party_scripts( $html, $settings ); } // 9. Safe non-critical accessory CSS delivery (icons, modals, lightboxes). $html = $this->make_stylesheets_async( $html, $settings ); // 10. Minify HTML output if enabled. if ( ! empty( $settings['html_minify'] ) ) { $html = $this->minify_html( $html ); } return $html; } /** * Inject Resource Hints (preconnect, dns-prefetch) into . * * @param string $html HTML. * @param array $settings Settings. * @return string */ private function inject_resource_hints( $html, $settings ) { $hints = array(); // Site-configured CDN preconnect if active. if ( ! empty( $settings['cdn_enabled'] ) && ! empty( $settings['cdn_url'] ) ) { $cdn_host = wp_parse_url( $settings['cdn_url'], PHP_URL_HOST ); if ( $cdn_host ) { $hints[] = '<' . 'link rel="preconnect" href="' . esc_url( 'https://' . $cdn_host ) . '" crossorigin>'; $hints[] = '<' . 'link rel="dns-prefetch" href="' . esc_url( '//' . $cdn_host ) . '">'; } } if ( empty( $hints ) ) { return $html; } $hint_string = implode( "\n", array_unique( $hints ) ) . "\n"; return preg_replace( '/(]*>)/i', "$1\n" . $hint_string, $html, 1 ); } /** * Inject font-display: swap into @font-face rules and Google Fonts links * to eliminate FOIT (Flash of Invisible Text) and ensure instant text rendering. * * @param string $html HTML content. * @return string */ private function inject_font_display_swap( $html ) { // 1. Inject into inline @font-face blocks. $html = preg_replace_callback( '/@font-face\s*\{([^}]+)\}/is', function ( $matches ) { $content = $matches[1]; if ( stripos( $content, 'font-display' ) === false ) { return "@font-face {\n\tfont-display: swap;\n" . trim( $content ) . "\n}"; } return $matches[0]; }, $html ); // 2. Ensure Google Fonts links include &display=swap. $html = preg_replace_callback( '/(https?:\/\/fonts\.googleapis\.com\/css2?\?[^"\'\s>]+)/i', function ( $matches ) { $url = $matches[1]; if ( stripos( $url, 'display=' ) === false ) { return $url . '&display=swap'; } return $url; }, $html ); return $html; } /** * Remove Stripe express checkout and block checkout styles on non-checkout/non-cart pages. * Eliminates 1.6MB+ of unused Stripe scripts, React, and Lodash overhead on the homepage. * * @param string $html HTML content. * @return string */ private function clean_non_checkout_bloat( $html ) { $is_checkout = function_exists( 'is_checkout' ) && is_checkout(); $is_cart = function_exists( 'is_cart' ) && is_cart(); if ( $is_checkout || $is_cart ) { return $html; } // Strip express-checkout.js. $html = preg_replace( '/]*src=["\'][^"\']*express-checkout\.js[^"\']*["\'][^>]*><\/script>/is', '', $html ); // Strip wc-stripe-blocks-checkout-style stylesheet. $html = preg_replace( '/]*id=["\']wc-stripe-blocks-checkout-style-css["\'][^>]*>/is', '', $html ); return $html; } /** * Protect all header, logo, and brand images from lazy-loading and dimensional tampering. * Ensures eager rendering on mobile and desktop devices without disappearing or collapsing. * * @param string $html HTML content. * @return string */ private function protect_header_and_logo_images( $html ) { // 1. Protect any inside
,