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( '/';
},
$html
);
}
/**
* Safely load non-critical accessory stylesheets asynchronously.
* When css_async is enabled, preloads non-theme stylesheets while preserving
* the primary theme style to prevent Flash of Unstyled Content (FOUC).
*
* @param string $html HTML.
* @param array $settings Plugin settings.
* @return string
*/
private function make_stylesheets_async( $html, $settings = array() ) {
$async_all = ! empty( $settings['css_async'] );
$exclusions = ! empty( $settings['css_exclusions'] ) ? (array) $settings['css_exclusions'] : array();
// Safe whitelist of accessory styles that never cause layout shift or FOUC.
$safe_async_handles = array(
'dashicons',
'wp-block-library',
'contact-form-7',
'cookie-law-info',
'cookieblocker',
'wp-emoji-styles',
'font-awesome',
'photoswipe',
'prettyPhoto',
'jquery.selectBox',
'xoo-wsc-fonts',
'xoo-el-fonts',
'huge-icons',
'slick-theme',
'cr-frontend-css',
'popup-maker-site',
'popup-maker-block',
'shopengine-modal-styles',
'shopengine-swatches-loop',
'simple-line-icons',
'mfn-icons',
'htflexboxgrid',
'wp-color-picker',
'wc-stripe-blocks-checkout-style',
'flexible-shipping',
'xoo-aff-font-awesome',
);
// 1. Convert any legacy rel="preload" as="style" tags into media="print" non-blocking stylesheets to prevent cellular network congestion.
$html = preg_replace_callback(
'/]*rel=["\']preload["\'][^>]*as=["\']style["\'][^>]*)>(?:\s*