]*)>/i',
function ( $matches ) use ( $exclusions, &$skip_count, $max_skip ) {
$attrs = $matches[1];
// Skip if already has loading attribute.
if ( stripos( $attrs, 'loading=' ) !== false ) {
return $matches[0];
}
// Skip LCP/hero images (first image on page).
if ( $skip_count < $max_skip ) {
$skip_count++;
// Add fetchpriority to LCP candidate.
if ( stripos( $attrs, 'fetchpriority=' ) === false ) {
return '
';
}
return $matches[0];
}
// Check exclusions.
foreach ( $exclusions as $exclusion ) {
if ( stripos( $attrs, $exclusion ) !== false ) {
return $matches[0];
}
}
// Skip logos and header images - never change logo or apply lazy-loading.
if ( preg_match( '/(logo|header|brand|company-logo|site-logo|custom-logo|masthead|BleuTorch)/i', $attrs ) || stripos( $attrs, 'data-wpsco-protected=' ) !== false ) {
return $matches[0];
}
$attrs_clean = rtrim( $attrs );
$closing = '>';
if ( substr( $attrs_clean, -1 ) === '/' ) {
$attrs_clean = rtrim( substr( $attrs_clean, 0, -1 ) );
$closing = ' />';
}
return '
]*)>/i',
function ( $matches ) {
$attrs = $matches[1];
if ( stripos( $attrs, 'loading=' ) !== false ) {
return $matches[0];
}
return '