| @@ -3,15 +3,18 @@ | ||
| 3 | 3 | * Resource Hints processor — pure HTML transformer for resource hints. |
| 4 | 4 | * |
| 5 | 5 | * Given a fully-rendered page and the Preload module's options, it: |
| 6 | 6 | * 1. Ranks every eligible <img> by the largest declared size it can read — |
| 7 | - * width×height attributes, else the widest srcset candidate — and emits | |
| 7 | + * width×height attributes, else the widest srcset candidate — boosted by | |
| 8 | + * the author's own priority signals (fetchpriority="high", an explicit | |
| 9 | + * loading="eager") and lightly weighted by document position, and emits | |
| 8 | 10 | * a <link rel="preload" as="image" fetchpriority="high"> for the top N |
| 9 | 11 | * in the <head>, carrying srcset/sizes as imagesrcset/imagesizes so the |
| 10 | 12 | * browser can pick the right candidate — then adds fetchpriority="high" |
| 11 | - * to the <img> itself so it beats any loading="lazy" the theme set. | |
| 12 | - * Ranking by size rather than document position: the first images on a | |
| 13 | - * real page are usually header chrome, not the hero (#96). | |
| 13 | + * to the <img> itself. Ranking by size rather than document position: | |
| 14 | + * the first images on a real page are usually header chrome, not the | |
| 15 | + * hero (#96). Images inside <footer>/<nav>/<aside>, images the theme | |
| 16 | + * explicitly lazy-loads, and tiny images never compete (FBS-84576). | |
| 14 | 17 | * 2. Emits <link rel="preconnect"> for detected web-font hosts |
| 15 | 18 | * (fonts.googleapis.com + fonts.gstatic.com) and any user-supplied |
| 16 | 19 | * hosts, deduped. |
| 17 | 20 | * |
| @@ -60,8 +63,20 @@ | ||
| 60 | 63 | [ $html, $preload ] = self::build_lcp_preload( $html, $count, $exclusions ); |
| 61 | 64 | $hints .= $preload; |
| 62 | 65 | } |
| 63 | 66 | |
| 67 | + // The manual list runs AFTER the automatic pick so it can deduplicate | |
| 68 | + // against it: a URL both name should carry ONE hint, not two. It exists | |
| 69 | + // for the image the detector cannot see — most often a hero section's | |
| 70 | + // CSS background-image, where the LCP element is a <div> with none of | |
| 71 | + // the width/height/fetchpriority signals the scorer reads. On the site | |
| 72 | + // that surfaced this, 3.3s of a 5.3s mobile LCP was pure discovery | |
| 73 | + // delay for exactly such an image. (FBS-84578) | |
| 74 | + $manual = array_filter( array_map( 'strval', (array) ( $opts['preload_images'] ?? array() ) ) ); | |
| 75 | + if ( ! empty( $manual ) ) { | |
| 76 | + $hints .= self::build_manual_image_preloads( $manual, $hints ); | |
| 77 | + } | |
| 78 | + | |
| 64 | 79 | // Full-page eager promotion for Lazy-excluded heroes (FBS-83553 H2). The |
| 65 | 80 | // Lazy module only filters the_content/thumbnail/avatar/widget, so a |
| 66 | 81 | // theme/builder hero printed OUTSIDE those keeps WP core's |
| 67 | 82 | // loading="lazy". This pass runs over the whole document, so it can reach |
| @@ -80,8 +95,61 @@ | ||
| 80 | 95 | return self::inject_into_head( $html, $hints ); |
| 81 | 96 | } |
| 82 | 97 | |
| 83 | 98 | /** |
| 99 | + * How many entries of the manual preload list are honoured. Preloading | |
| 100 | + * competes with the page for its top network priority — a long list | |
| 101 | + * inverts the benefit, so the cap is deliberately small. | |
| 102 | + */ | |
| 103 | + private const MAX_MANUAL_PRELOADS = 3; | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * One `<link rel="preload" as="image">` per manual list entry. | |
| 107 | + * | |
| 108 | + * Entries are full URLs or site-relative paths. Anything that is neither | |
| 109 | + * (a data: URI, a bare word) is skipped rather than guessed at, and a URL | |
| 110 | + * the automatic pick already emitted is skipped too — one hint per image, | |
| 111 | + * whoever names it first. | |
| 112 | + * | |
| 113 | + * @param array<int,string> $urls The configured list. | |
| 114 | + * @param string $existing_hints Hints already built this request. | |
| 115 | + */ | |
| 116 | + private static function build_manual_image_preloads( array $urls, string $existing_hints ): string { | |
| 117 | + /** | |
| 118 | + * Filter the manual image-preload list before it is emitted. | |
| 119 | + * | |
| 120 | + * @param array<int,string> $urls Configured URLs, in panel order. | |
| 121 | + */ | |
| 122 | + $urls = (array) apply_filters( 'xspeed_preload_images', $urls ); | |
| 123 | + | |
| 124 | + $out = ''; | |
| 125 | + $seen = array(); | |
| 126 | + foreach ( $urls as $url ) { | |
| 127 | + $url = trim( (string) $url ); | |
| 128 | + if ( '' === $url ) { | |
| 129 | + continue; | |
| 130 | + } | |
| 131 | + // A full URL or a site-relative path; nothing else is guessable. | |
| 132 | + $is_absolute = (bool) preg_match( '#^https?://#i', $url ); | |
| 133 | + $is_relative = '' !== $url && '/' === $url[0] && ( strlen( $url ) < 2 || '/' !== $url[1] ); | |
| 134 | + if ( ! $is_absolute && ! $is_relative ) { | |
| 135 | + continue; | |
| 136 | + } | |
| 137 | + $href = esc_url( $url ); | |
| 138 | + if ( '' === $href || isset( $seen[ $href ] ) || false !== strpos( $existing_hints, 'href="' . $href . '"' ) ) { | |
| 139 | + continue; | |
| 140 | + } | |
| 141 | + $seen[ $href ] = true; | |
| 142 | + $out .= '<link rel="preload" as="image" href="' . $href . '" fetchpriority="high">'; | |
| 143 | + if ( count( $seen ) >= self::MAX_MANUAL_PRELOADS ) { | |
| 144 | + break; | |
| 145 | + } | |
| 146 | + } | |
| 147 | + | |
| 148 | + return $out; | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 84 | 152 | * Strip core `loading="lazy"` and add `fetchpriority="high"` + |
| 85 | 153 | * `decoding="async"` on every <img> whose tag matches one of the given |
| 86 | 154 | * exclusion substrings. Mirrors what Lazy_Loader does for an excluded image |
| 87 | 155 | * inside the_content, but page-wide so heroes outside it are covered too. |
| @@ -187,11 +255,14 @@ | ||
| 187 | 255 | // nothing — it just adds a high-priority request competing with the |
| 188 | 256 | // one that matters, and the feature reported success either way. The |
| 189 | 257 | // marker list and size gate were heuristics layered on top of the |
| 190 | 258 | // wrong primitive rather than replacing it. (#96) |
| 191 | - $candidates = array(); | |
| 192 | - if ( preg_match_all( '#<img\b[^>]*>#i', $html, $matches ) ) { | |
| 193 | - foreach ( $matches[0] as $index => $tag ) { | |
| 259 | + $candidates = array(); | |
| 260 | + $skip_ranges = self::chrome_container_ranges( $html ); | |
| 261 | + if ( preg_match_all( '#<img\b[^>]*>#i', $html, $matches, PREG_OFFSET_CAPTURE ) ) { | |
| 262 | + foreach ( $matches[0] as $index => $match ) { | |
| 263 | + [ $tag, $offset ] = $match; | |
| 264 | + | |
| 194 | 265 | // Skip anything the user excluded. |
| 195 | 266 | $excluded = false; |
| 196 | 267 | foreach ( $exclusions as $needle ) { |
| 197 | 268 | if ( '' !== $needle && false !== stripos( $tag, $needle ) ) { |
| @@ -202,8 +273,25 @@ | ||
| 202 | 273 | if ( $excluded ) { |
| 203 | 274 | continue; |
| 204 | 275 | } |
| 205 | 276 | |
| 277 | + // An image inside <footer>/<nav>/<aside> is site chrome by | |
| 278 | + // construction — a footer brand strip or FAQ illustration can | |
| 279 | + // never be the LCP element, whatever size it declares. On the | |
| 280 | + // FBS-84576 repro these decoys outranked the real hero three | |
| 281 | + // times on one layout. | |
| 282 | + if ( self::offset_in_ranges( $offset, $skip_ranges ) ) { | |
| 283 | + continue; | |
| 284 | + } | |
| 285 | + | |
| 286 | + // An image the theme explicitly lazy-loads is never the | |
| 287 | + // intended LCP — the author has already said "this can wait". | |
| 288 | + // Preloading it would contradict the markup and steal | |
| 289 | + // bandwidth from the image that matters. (FBS-84576) | |
| 290 | + if ( 'lazy' === strtolower( self::attr( $tag, 'loading' ) ) ) { | |
| 291 | + continue; | |
| 292 | + } | |
| 293 | + | |
| 206 | 294 | // Resolve the EFFECTIVE image URL. Page builders + JS lazy |
| 207 | 295 | // loaders park a placeholder (a data: URI or a 1px spacer) in |
| 208 | 296 | // `src` and the real URL in `data-src`, so the hero the browser |
| 209 | 297 | // actually paints is behind data-src. (FBS-83553 H1) |
| @@ -222,14 +310,44 @@ | ||
| 222 | 310 | 'tag' => $tag, |
| 223 | 311 | 'src' => $src, |
| 224 | 312 | 'srcset' => $srcset, |
| 225 | 313 | 'sizes' => $sizes, |
| 226 | - 'score' => self::lcp_score( $tag, $srcset ), | |
| 314 | + 'score' => self::weighted_score( self::lcp_score( $tag, $srcset ), $tag, $index ), | |
| 227 | 315 | 'order' => $index, |
| 228 | 316 | ); |
| 229 | 317 | } |
| 230 | 318 | } |
| 231 | 319 | |
| 320 | + // PASS 1b — the same for CSS background images. | |
| 321 | + // | |
| 322 | + // On a page builder the hero is usually a background-image on the | |
| 323 | + // section, not an <img>, so an <img>-only candidate set never contained | |
| 324 | + // the element that actually paints as LCP. It preloaded whatever <img> | |
| 325 | + // happened to be there — measured at 0ms against the feature switched | |
| 326 | + // off, while spending a high-priority fetch on the critical path — or, | |
| 327 | + // on a page with no <img> at all, emitted nothing. (#247) | |
| 328 | + foreach ( self::background_candidates( $html, $exclusions, $skip_ranges ) as $bg ) { | |
| 329 | + $candidates[] = $bg; | |
| 330 | + } | |
| 331 | + | |
| 332 | + // PASS 1c — <video poster="…">. A full-screen hero video paints its | |
| 333 | + // poster first, and that first frame IS the LCP; measured on a live | |
| 334 | + // page, a preloaded poster cut the LCP load delay from 1.5 s to 21 ms. | |
| 335 | + foreach ( self::video_poster_candidates( $html, $exclusions, $skip_ranges ) as $vp ) { | |
| 336 | + $candidates[] = $vp; | |
| 337 | + } | |
| 338 | + | |
| 339 | + // PASS 1d — background rules in inline <style> blocks. Page builders | |
| 340 | + // put the hero's background-image in generated per-post CSS printed | |
| 341 | + // inline (Elementor's `.elementor-N .elementor-element-X` rules), not | |
| 342 | + // in a style attribute — so PASS 1b never saw the element that | |
| 343 | + // actually paints as LCP on five of seven measured sites. External | |
| 344 | + // stylesheets stay out for the same reasons as before (#247): fetching | |
| 345 | + // CSS from an output-buffer pass costs more than the preload saves. | |
| 346 | + foreach ( self::style_block_candidates( $html, $exclusions, $skip_ranges ) as $sb ) { | |
| 347 | + $candidates[] = $sb; | |
| 348 | + } | |
| 349 | + | |
| 232 | 350 | if ( empty( $candidates ) ) { |
| 233 | 351 | return array( $html, '' ); |
| 234 | 352 | } |
| 235 | 353 | |
| @@ -261,9 +379,15 @@ | ||
| 261 | 379 | ); |
| 262 | 380 | if ( ! $already ) { |
| 263 | 381 | $preload .= self::preload_link( $w['src'], $w['srcset'], $w['sizes'] ); |
| 264 | 382 | } |
| 265 | - $chosen[ $w['order'] ] = true; | |
| 383 | + // Only <img> winners are promoted in PASS 2 — there is no | |
| 384 | + // fetchpriority/loading attribute to fix on a background element, | |
| 385 | + // and its `order` is offset past every <img> index precisely so it | |
| 386 | + // can never select one for rewriting. | |
| 387 | + if ( empty( $w['background'] ) ) { | |
| 388 | + $chosen[ $w['order'] ] = true; | |
| 389 | + } | |
| 266 | 390 | } |
| 267 | 391 | |
| 268 | 392 | // Rewrite only the winning tags. Counting occurrences rather than |
| 269 | 393 | // matching on tag text, because the same markup can legitimately |
| @@ -290,8 +414,381 @@ | ||
| 290 | 414 | return array( (string) $html, $preload ); |
| 291 | 415 | } |
| 292 | 416 | |
| 293 | 417 | /** |
| 418 | + * Collect CSS `background-image` heroes as LCP candidates. | |
| 419 | + * | |
| 420 | + * Only INLINE `style` attributes are read. A background declared in an | |
| 421 | + * external stylesheet is invisible here by design: resolving it would mean | |
| 422 | + * fetching and parsing CSS from inside an output-buffer pass, and the URL a | |
| 423 | + * selector resolves to depends on cascade order we cannot evaluate from | |
| 424 | + * markup. Builders that put the hero in a generated per-post stylesheet are | |
| 425 | + * therefore still unserved — worth doing, but not at this cost. (#247) | |
| 426 | + * | |
| 427 | + * Scores are the element's declared pixel area so a background competes | |
| 428 | + * against an <img> in the SAME units — the whole point being that the | |
| 429 | + * bigger of the two should win regardless of which kind it is. | |
| 430 | + * | |
| 431 | + * @param string $html Full page HTML. | |
| 432 | + * @param string[] $exclusions Substring patterns the user excluded. | |
| 433 | + * @param array<int,array{0:int,1:int}> $skip_ranges Byte ranges of chrome containers. | |
| 434 | + * @return array<int,array{tag:string,src:string,srcset:string,sizes:string,score:float,order:int,background:bool}> | |
| 435 | + */ | |
| 436 | + private static function background_candidates( string $html, array $exclusions, array $skip_ranges ): array { | |
| 437 | + if ( ! preg_match_all( '#<(?:div|section|header|figure|a|span|li|main|article|aside)\b[^>]*\sstyle\s*=\s*(["\']).*?\1[^>]*>#is', $html, $matches, PREG_OFFSET_CAPTURE ) ) { | |
| 438 | + return array(); | |
| 439 | + } | |
| 440 | + | |
| 441 | + $found = array(); | |
| 442 | + foreach ( $matches[0] as $index => $match ) { | |
| 443 | + [ $tag, $offset ] = $match; | |
| 444 | + | |
| 445 | + // The same chrome-container gate as <img>: a background painted | |
| 446 | + // inside <footer>/<nav>/<aside> is never the hero. (FBS-84576) | |
| 447 | + if ( self::offset_in_ranges( $offset, $skip_ranges ) ) { | |
| 448 | + continue; | |
| 449 | + } | |
| 450 | + | |
| 451 | + $style = self::attr( $tag, 'style' ); | |
| 452 | + if ( '' === $style || false === stripos( $style, 'background' ) ) { | |
| 453 | + continue; | |
| 454 | + } | |
| 455 | + | |
| 456 | + $src = self::background_url( $style ); | |
| 457 | + if ( '' === $src ) { | |
| 458 | + continue; | |
| 459 | + } | |
| 460 | + | |
| 461 | + foreach ( $exclusions as $needle ) { | |
| 462 | + if ( '' !== $needle && false !== stripos( $tag, $needle ) ) { | |
| 463 | + continue 2; | |
| 464 | + } | |
| 465 | + } | |
| 466 | + | |
| 467 | + // Same chrome/opt-out gates as <img>. A logo painted as a background | |
| 468 | + // is no more the hero than a logo in an <img>. | |
| 469 | + if ( self::looks_too_small( $tag ) ) { | |
| 470 | + continue; | |
| 471 | + } | |
| 472 | + | |
| 473 | + $area = self::style_area( $style ); | |
| 474 | + if ( 0 === $area ) { | |
| 475 | + // Nothing readable. Deliberately non-zero for the same reason | |
| 476 | + // UNKNOWN_SIZE_SCORE is: an unmeasurable background must still | |
| 477 | + // beat nothing on a page that declares no sizes at all, while | |
| 478 | + // losing to anything we can actually measure. | |
| 479 | + $area = self::UNKNOWN_SIZE_SCORE; | |
| 480 | + } | |
| 481 | + | |
| 482 | + $found[] = array( | |
| 483 | + 'tag' => $tag, | |
| 484 | + 'src' => $src, | |
| 485 | + 'srcset' => '', | |
| 486 | + 'sizes' => '', | |
| 487 | + 'score' => (float) $area, | |
| 488 | + // Offset so a background never ties ahead of an <img> that | |
| 489 | + // appeared earlier in the document; ties still break on order. | |
| 490 | + 'order' => 100000 + $index, | |
| 491 | + 'background' => true, | |
| 492 | + ); | |
| 493 | + } | |
| 494 | + | |
| 495 | + return $found; | |
| 496 | + } | |
| 497 | + | |
| 498 | + /** | |
| 499 | + * Collect `<video poster="…">` first frames as LCP candidates. | |
| 500 | + * | |
| 501 | + * The poster is what the viewer sees until (and unless) the video plays — | |
| 502 | + * on a background-video hero, delayed by the Lazy module, it is the ONLY | |
| 503 | + * frame the initial paint has. Scored like a background: the element's | |
| 504 | + * declared inline-style area, or the unknown-size floor, with the order | |
| 505 | + * offset past every <img> so a poster never ties ahead of one. | |
| 506 | + * | |
| 507 | + * @param string $html Full page HTML. | |
| 508 | + * @param string[] $exclusions Substring patterns the user excluded. | |
| 509 | + * @param array<int,array{0:int,1:int}> $skip_ranges Byte ranges of chrome containers. | |
| 510 | + * @return array<int,array{tag:string,src:string,srcset:string,sizes:string,score:float,order:int,background:bool}> | |
| 511 | + */ | |
| 512 | + private static function video_poster_candidates( string $html, array $exclusions, array $skip_ranges ): array { | |
| 513 | + if ( ! preg_match_all( '#<video\b[^>]*\bposter\s*=\s*(["\'])(.*?)\1[^>]*>#i', $html, $matches, PREG_OFFSET_CAPTURE ) ) { | |
| 514 | + return array(); | |
| 515 | + } | |
| 516 | + | |
| 517 | + $found = array(); | |
| 518 | + foreach ( $matches[0] as $index => $match ) { | |
| 519 | + [ $tag, $offset ] = $match; | |
| 520 | + $src = trim( html_entity_decode( $matches[2][ $index ][0], ENT_QUOTES ) ); | |
| 521 | + if ( '' === $src || 0 === stripos( $src, 'data:' ) ) { | |
| 522 | + continue; | |
| 523 | + } | |
| 524 | + if ( self::offset_in_ranges( $offset, $skip_ranges ) ) { | |
| 525 | + continue; | |
| 526 | + } | |
| 527 | + foreach ( $exclusions as $needle ) { | |
| 528 | + if ( '' !== $needle && false !== stripos( $tag, $needle ) ) { | |
| 529 | + continue 2; | |
| 530 | + } | |
| 531 | + } | |
| 532 | + if ( self::looks_too_small( $tag ) ) { | |
| 533 | + continue; | |
| 534 | + } | |
| 535 | + | |
| 536 | + $area = self::style_area( self::attr( $tag, 'style' ) ); | |
| 537 | + if ( 0 === $area ) { | |
| 538 | + $area = self::UNKNOWN_SIZE_SCORE; | |
| 539 | + } | |
| 540 | + | |
| 541 | + $found[] = array( | |
| 542 | + 'tag' => $tag, | |
| 543 | + 'src' => $src, | |
| 544 | + 'srcset' => '', | |
| 545 | + 'sizes' => '', | |
| 546 | + 'score' => (float) $area, | |
| 547 | + 'order' => 100000 + $index, | |
| 548 | + 'background' => true, | |
| 549 | + ); | |
| 550 | + } | |
| 551 | + | |
| 552 | + return $found; | |
| 553 | + } | |
| 554 | + | |
| 555 | + /** | |
| 556 | + * How many <style>-block background rules are considered per page. The | |
| 557 | + * scan is linear, but each rule costs one class-lookup pass over the | |
| 558 | + * body, so a pathological page (thousands of generated rules) is capped | |
| 559 | + * rather than trusted. | |
| 560 | + */ | |
| 561 | + private const STYLE_RULE_BUDGET = 40; | |
| 562 | + | |
| 563 | + /** | |
| 564 | + * Collect background-image rules from inline <style> blocks whose | |
| 565 | + * selector matches an element in the body. | |
| 566 | + * | |
| 567 | + * The match is deliberately narrow: the rule's RIGHTMOST simple selector | |
| 568 | + * must carry a class or id, and the first element in the body bearing it | |
| 569 | + * (outside chrome containers) is taken as the painted element. Rules | |
| 570 | + * inside @media (or any other at-rule block) are skipped — a desktop-only | |
| 571 | + * background preloaded on mobile is a wasted high-priority fetch, and the | |
| 572 | + * markup gives no viewport to resolve the query against. | |
| 573 | + * | |
| 574 | + * @param string $html Full page HTML. | |
| 575 | + * @param string[] $exclusions Substring patterns the user excluded. | |
| 576 | + * @param array<int,array{0:int,1:int}> $skip_ranges Byte ranges of chrome containers. | |
| 577 | + * @return array<int,array{tag:string,src:string,srcset:string,sizes:string,score:float,order:int,background:bool}> | |
| 578 | + */ | |
| 579 | + private static function style_block_candidates( string $html, array $exclusions, array $skip_ranges ): array { | |
| 580 | + if ( ! preg_match_all( '#<style\b[^>]*>(.*?)</style\s*>#is', $html, $blocks ) ) { | |
| 581 | + return array(); | |
| 582 | + } | |
| 583 | + | |
| 584 | + $found = array(); | |
| 585 | + $budget = self::STYLE_RULE_BUDGET; | |
| 586 | + foreach ( $blocks[1] as $css ) { | |
| 587 | + if ( $budget <= 0 ) { | |
| 588 | + break; | |
| 589 | + } | |
| 590 | + $css = self::strip_at_rule_blocks( $css ); | |
| 591 | + if ( false === stripos( $css, 'url(' ) ) { | |
| 592 | + continue; | |
| 593 | + } | |
| 594 | + // One flat rule at a time: selector list up to '{', body to '}'. | |
| 595 | + if ( ! preg_match_all( '#(?:^|})\s*([^{}]{1,512})\{([^{}]*)\}#s', $css, $rules, PREG_SET_ORDER ) ) { | |
| 596 | + continue; | |
| 597 | + } | |
| 598 | + foreach ( $rules as $rule ) { | |
| 599 | + if ( $budget <= 0 ) { | |
| 600 | + break 2; | |
| 601 | + } | |
| 602 | + if ( false === stripos( $rule[2], 'url(' ) ) { | |
| 603 | + continue; | |
| 604 | + } | |
| 605 | + $src = self::background_url( $rule[2] ); | |
| 606 | + if ( '' === $src ) { | |
| 607 | + continue; | |
| 608 | + } | |
| 609 | + --$budget; | |
| 610 | + // First selector of the list, rightmost compound of it. | |
| 611 | + $selector = trim( (string) strtok( $rule[1], ',' ) ); | |
| 612 | + $parts = preg_split( '#[\s>+~]+#', $selector ); | |
| 613 | + $last = (string) end( $parts ); | |
| 614 | + // The last class or id token of that compound. Pseudo-classes | |
| 615 | + // (:hover, ::before) mean the background is not the initial | |
| 616 | + // paint, so they disqualify the rule. | |
| 617 | + if ( false !== strpos( $last, ':' ) ) { | |
| 618 | + continue; | |
| 619 | + } | |
| 620 | + if ( ! preg_match( '#([.\#])([-\w]+)$#', $last, $tok ) ) { | |
| 621 | + continue; | |
| 622 | + } | |
| 623 | + $el = '.' === $tok[1] | |
| 624 | + ? self::first_element_with_class( $html, $tok[2], $skip_ranges ) | |
| 625 | + : self::first_element_with_id( $html, $tok[2], $skip_ranges ); | |
| 626 | + if ( null === $el ) { | |
| 627 | + continue; | |
| 628 | + } | |
| 629 | + [ $tag, $offset ] = $el; | |
| 630 | + foreach ( $exclusions as $needle ) { | |
| 631 | + if ( '' !== $needle && false !== stripos( $tag, $needle ) ) { | |
| 632 | + continue 2; | |
| 633 | + } | |
| 634 | + } | |
| 635 | + if ( self::looks_too_small( $tag ) ) { | |
| 636 | + continue; | |
| 637 | + } | |
| 638 | + $area = self::style_area( self::attr( $tag, 'style' ) ); | |
| 639 | + if ( 0 === $area ) { | |
| 640 | + $area = self::UNKNOWN_SIZE_SCORE; | |
| 641 | + } | |
| 642 | + $found[] = array( | |
| 643 | + 'tag' => $tag, | |
| 644 | + 'src' => $src, | |
| 645 | + 'srcset' => '', | |
| 646 | + 'sizes' => '', | |
| 647 | + 'score' => (float) $area, | |
| 648 | + // Offset past the inline-style backgrounds: a rule-matched | |
| 649 | + // background is one inference step less certain, so it must | |
| 650 | + // never tie ahead of one read straight off the element. | |
| 651 | + 'order' => 200000 + $offset, | |
| 652 | + 'background' => true, | |
| 653 | + ); | |
| 654 | + } | |
| 655 | + } | |
| 656 | + | |
| 657 | + return $found; | |
| 658 | + } | |
| 659 | + | |
| 660 | + /** | |
| 661 | + * CSS with every at-rule BLOCK (@media, @supports, @container, …) removed, | |
| 662 | + * by brace depth — a regex cannot pair nested braces. Flat at-rules | |
| 663 | + * (@import, @charset) have no block and pass through harmlessly. | |
| 664 | + */ | |
| 665 | + private static function strip_at_rule_blocks( string $css ): string { | |
| 666 | + $out = ''; | |
| 667 | + $len = strlen( $css ); | |
| 668 | + $i = 0; | |
| 669 | + while ( $i < $len ) { | |
| 670 | + $at = strpos( $css, '@', $i ); | |
| 671 | + if ( false === $at ) { | |
| 672 | + return $out . substr( $css, $i ); | |
| 673 | + } | |
| 674 | + $brace = strpos( $css, '{', $at ); | |
| 675 | + $semi = strpos( $css, ';', $at ); | |
| 676 | + $out .= substr( $css, $i, $at - $i ); | |
| 677 | + if ( false === $brace || ( false !== $semi && $semi < $brace ) ) { | |
| 678 | + // Flat at-rule — skip to its semicolon (or end). | |
| 679 | + $i = false === $semi ? $len : $semi + 1; | |
| 680 | + continue; | |
| 681 | + } | |
| 682 | + // Block at-rule — skip to its matching close brace. | |
| 683 | + $depth = 1; | |
| 684 | + $i = $brace + 1; | |
| 685 | + while ( $i < $len && $depth > 0 ) { | |
| 686 | + $c = $css[ $i ]; | |
| 687 | + if ( '{' === $c ) { | |
| 688 | + ++$depth; | |
| 689 | + } elseif ( '}' === $c ) { | |
| 690 | + --$depth; | |
| 691 | + } | |
| 692 | + ++$i; | |
| 693 | + } | |
| 694 | + } | |
| 695 | + return $out; | |
| 696 | + } | |
| 697 | + | |
| 698 | + /** | |
| 699 | + * The first element in the BODY carrying $class (outside chrome ranges), | |
| 700 | + * as [tag, offset], or null. Body-only, so a head <meta> can never match | |
| 701 | + * and a hit's offset is comparable with the chrome ranges. | |
| 702 | + * | |
| 703 | + * @return array{0:string,1:int}|null | |
| 704 | + */ | |
| 705 | + private static function first_element_with_class( string $html, string $class, array $skip_ranges ): ?array { | |
| 706 | + $body = stripos( $html, '<body' ); | |
| 707 | + $from = false === $body ? 0 : $body; | |
| 708 | + if ( ! preg_match_all( '#<[a-z][^>]*\bclass\s*=\s*(["\'])[^"\']*(?<![-\w])' . preg_quote( $class, '#' ) . '(?![-\w])[^"\']*\1[^>]*>#i', $html, $m, PREG_OFFSET_CAPTURE, $from ) ) { | |
| 709 | + return null; | |
| 710 | + } | |
| 711 | + foreach ( $m[0] as $match ) { | |
| 712 | + if ( ! self::offset_in_ranges( $match[1], $skip_ranges ) ) { | |
| 713 | + return array( $match[0], $match[1] ); | |
| 714 | + } | |
| 715 | + } | |
| 716 | + return null; | |
| 717 | + } | |
| 718 | + | |
| 719 | + /** | |
| 720 | + * The first element carrying id="$id" (outside chrome ranges), as | |
| 721 | + * [tag, offset], or null. | |
| 722 | + * | |
| 723 | + * @return array{0:string,1:int}|null | |
| 724 | + */ | |
| 725 | + private static function first_element_with_id( string $html, string $id, array $skip_ranges ): ?array { | |
| 726 | + $body = stripos( $html, '<body' ); | |
| 727 | + $from = false === $body ? 0 : $body; | |
| 728 | + if ( ! preg_match( '#<[a-z][^>]*\bid\s*=\s*(["\'])' . preg_quote( $id, '#' ) . '\1[^>]*>#i', $html, $m, PREG_OFFSET_CAPTURE, $from ) ) { | |
| 729 | + return null; | |
| 730 | + } | |
| 731 | + if ( self::offset_in_ranges( $m[0][1], $skip_ranges ) ) { | |
| 732 | + return null; | |
| 733 | + } | |
| 734 | + return array( $m[0][0], $m[0][1] ); | |
| 735 | + } | |
| 736 | + | |
| 737 | + /** | |
| 738 | + * Pull a real image URL out of a `background`/`background-image` declaration. | |
| 739 | + * | |
| 740 | + * Returns '' for anything with nothing to fetch: a gradient (which is a | |
| 741 | + * background-image but not a resource), a data: URI, or `none`. | |
| 742 | + */ | |
| 743 | + private static function background_url( string $style ): string { | |
| 744 | + // Decode BEFORE parsing. Builders emit the url() quotes HTML-encoded | |
| 745 | + // inside a style attribute (url("/hero.jpg")), and `"` | |
| 746 | + // carries a semicolon — so splitting the declaration on `;` first | |
| 747 | + // truncated the value to `url("` and found no URL at all. | |
| 748 | + $style = html_entity_decode( $style, ENT_QUOTES ); | |
| 749 | + | |
| 750 | + if ( ! preg_match( '#background(?:-image)?\s*:\s*((?:[^;\'"]|"[^"]*"|\'[^\']*\')+)#i', $style, $decl ) ) { | |
| 751 | + return ''; | |
| 752 | + } | |
| 753 | + if ( ! preg_match( '#url\(\s*(["\']?)(.*?)\1\s*\)#is', $decl[1], $m ) ) { | |
| 754 | + return ''; | |
| 755 | + } | |
| 756 | + $url = trim( $m[2] ); | |
| 757 | + if ( '' === $url || 0 === stripos( $url, 'data:' ) ) { | |
| 758 | + return ''; | |
| 759 | + } | |
| 760 | + return $url; | |
| 761 | + } | |
| 762 | + | |
| 763 | + /** | |
| 764 | + * Declared pixel area from an inline style, or 0 when it can't be read. | |
| 765 | + * | |
| 766 | + * Only px is honoured. A percentage or viewport unit resolves against a | |
| 767 | + * containing block we cannot see from markup, and guessing one produced the | |
| 768 | + * wrong winner more often than declining to. | |
| 769 | + */ | |
| 770 | + private static function style_area( string $style ): int { | |
| 771 | + $w = self::style_px( $style, 'width' ); | |
| 772 | + $h = self::style_px( $style, 'height' ); | |
| 773 | + if ( $w > 0 && $h > 0 ) { | |
| 774 | + return $w * $h; | |
| 775 | + } | |
| 776 | + if ( $w > 0 ) { | |
| 777 | + return (int) round( $w * $w * self::ASSUMED_ASPECT_RATIO ); | |
| 778 | + } | |
| 779 | + return 0; | |
| 780 | + } | |
| 781 | + | |
| 782 | + /** One px-valued CSS length from an inline style, or 0. */ | |
| 783 | + private static function style_px( string $style, string $prop ): int { | |
| 784 | + if ( preg_match( '#(?:^|;)\s*' . preg_quote( $prop, '#' ) . '\s*:\s*(\d+(?:\.\d+)?)px#i', $style, $m ) ) { | |
| 785 | + return (int) round( (float) $m[1] ); | |
| 786 | + } | |
| 787 | + return 0; | |
| 788 | + } | |
| 789 | + | |
| 790 | + /** | |
| 294 | 791 | * How likely is this <img> to be the LCP element? Higher wins. |
| 295 | 792 | * |
| 296 | 793 | * Rendered area is the best available proxy, and we can only read what the |
| 297 | 794 | * markup declares: |
| @@ -307,13 +804,13 @@ | ||
| 307 | 804 | * |
| 308 | 805 | * @param string $tag The full <img> tag. |
| 309 | 806 | * @param string $srcset Resolved srcset (may come from data-srcset). |
| 310 | 807 | */ |
| 311 | - private static function lcp_score( string $tag, string $srcset ): int { | |
| 808 | + private static function lcp_score( string $tag, string $srcset ): float { | |
| 312 | 809 | $w = self::attr( $tag, 'width' ); |
| 313 | 810 | $h = self::attr( $tag, 'height' ); |
| 314 | 811 | if ( '' !== $w && '' !== $h && is_numeric( $w ) && is_numeric( $h ) ) { |
| 315 | - return (int) $w * (int) $h; | |
| 812 | + return (float) ( (int) $w * (int) $h ); | |
| 316 | 813 | } |
| 317 | 814 | |
| 318 | 815 | $widest = self::widest_srcset_width( $srcset ); |
| 319 | 816 | if ( $widest > 0 ) { |
| @@ -323,15 +820,125 @@ | ||
| 323 | 820 | // 1024w sidebar thumbnail (1 048 576) beat a declared 1200×600 |
| 324 | 821 | // hero (720 000) — a regression on exactly the mixed pages that |
| 325 | 822 | // document order used to get right, since the hero usually comes |
| 326 | 823 | // first. Assuming a 16:9 box keeps both sides in the same units. |
| 327 | - return (int) round( $widest * $widest * self::ASSUMED_ASPECT_RATIO ); | |
| 824 | + return round( $widest * $widest * self::ASSUMED_ASPECT_RATIO ); | |
| 328 | 825 | } |
| 329 | 826 | |
| 330 | - return self::UNKNOWN_SIZE_SCORE; | |
| 827 | + return (float) self::UNKNOWN_SIZE_SCORE; | |
| 331 | 828 | } |
| 332 | 829 | |
| 333 | 830 | /** |
| 831 | + * Fold the author's own priority signals and document position into an | |
| 832 | + * area score. (FBS-84576) | |
| 833 | + * | |
| 834 | + * Boosts are ADDITIVE, in area units, so they can rescue an image whose | |
| 835 | + * size the markup doesn't declare: a hero with no width/height and no | |
| 836 | + * `w`-descriptor srcset scores UNKNOWN_SIZE_SCORE, and multiplying that | |
| 837 | + * by any factor still loses to a 548×136 logo that declares itself. This | |
| 838 | + * is exactly the live miss — the real hero carried loading="eager" | |
| 839 | + * fetchpriority="high" and lost to three dimension-declaring decoys. | |
| 840 | + * | |
| 841 | + * - fetchpriority="high" is the strongest signal there is: the author | |
| 842 | + * (or WP core's own LCP detection) has already named this image the | |
| 843 | + * hero. Worth a hero-sized area. | |
| 844 | + * - An EXPLICIT loading="eager" is a weaker but deliberate "load me | |
| 845 | + * now" (the default is eager, so writing it out is a choice). | |
| 846 | + * | |
| 847 | + * Position is a light multiplicative weight — earlier is better, but the | |
| 848 | + * spread is capped well under 5× so it can only break near-ties, never | |
| 849 | + * outrank a genuinely larger image further down (the logo-vs-hero case). | |
| 850 | + * | |
| 851 | + * @param float $score Base area score from lcp_score() / style_area(). | |
| 852 | + * @param string $tag The candidate's full tag (for the signal attrs). | |
| 853 | + * @param int $order Document-order index of the candidate. | |
| 854 | + */ | |
| 855 | + private static function weighted_score( float $score, string $tag, int $order ): float { | |
| 856 | + if ( 'high' === strtolower( self::attr( $tag, 'fetchpriority' ) ) ) { | |
| 857 | + $score += self::FETCHPRIORITY_HIGH_BOOST; | |
| 858 | + } | |
| 859 | + if ( 'eager' === strtolower( self::attr( $tag, 'loading' ) ) ) { | |
| 860 | + $score += self::EAGER_BOOST; | |
| 861 | + } | |
| 862 | + return $score * self::position_weight( $order ); | |
| 863 | + } | |
| 864 | + | |
| 865 | + /** | |
| 866 | + * Document-position weight: 1.25 for the first image, easing to 1.0 by | |
| 867 | + * the tenth. The whole spread is 25%, far under the 5× area difference it | |
| 868 | + * must never override — it exists only to keep the old first-wins | |
| 869 | + * behaviour for images we can't tell apart. | |
| 870 | + */ | |
| 871 | + private static function position_weight( int $order ): float { | |
| 872 | + return 1.0 + 0.25 * max( 0.0, 1.0 - $order / 10 ); | |
| 873 | + } | |
| 874 | + | |
| 875 | + /** | |
| 876 | + * Area-unit boost for fetchpriority="high" — roughly a 940×530 hero, so | |
| 877 | + * an explicitly-marked image outranks any mid-page decoy even when its | |
| 878 | + * own size is unreadable, while a genuinely huge unmarked image can still | |
| 879 | + * beat a marked small one. | |
| 880 | + */ | |
| 881 | + private const FETCHPRIORITY_HIGH_BOOST = 500000.0; | |
| 882 | + | |
| 883 | + /** | |
| 884 | + * Area-unit boost for an explicit loading="eager" — roughly 420×240, | |
| 885 | + * enough to break ties in favour of the author's intent without letting | |
| 886 | + * an eager logo outrank a plain hero. | |
| 887 | + */ | |
| 888 | + private const EAGER_BOOST = 100000.0; | |
| 889 | + | |
| 890 | + /** | |
| 891 | + * Byte ranges of <footer>/<nav>/<aside> regions. Nesting-aware per tag | |
| 892 | + * name (a nav inside a nav extends the range); an unclosed open tag | |
| 893 | + * poisons through to the end of the document, which errs on the side of | |
| 894 | + * not preloading — the safe direction, since a wrong preload is worse | |
| 895 | + * than none. (FBS-84576) | |
| 896 | + * | |
| 897 | + * @return array<int,array{0:int,1:int}> [start, end] byte offsets. | |
| 898 | + */ | |
| 899 | + private static function chrome_container_ranges( string $html ): array { | |
| 900 | + $ranges = array(); | |
| 901 | + foreach ( array( 'footer', 'nav', 'aside' ) as $name ) { | |
| 902 | + // (?=[\s/>]) rather than \b: a word boundary sits before the `-` | |
| 903 | + // of a custom element, so `<nav\b` would swallow `<nav-menu>`. | |
| 904 | + if ( ! preg_match_all( '#<(/?)' . $name . '(?=[\s/>])[^>]*>#i', $html, $m, PREG_OFFSET_CAPTURE ) ) { | |
| 905 | + continue; | |
| 906 | + } | |
| 907 | + $depth = 0; | |
| 908 | + $start = 0; | |
| 909 | + foreach ( $m[0] as $i => $match ) { | |
| 910 | + $closing = '' !== $m[1][ $i ][0]; | |
| 911 | + if ( ! $closing ) { | |
| 912 | + if ( 0 === $depth ) { | |
| 913 | + $start = $match[1]; | |
| 914 | + } | |
| 915 | + ++$depth; | |
| 916 | + } elseif ( $depth > 0 ) { | |
| 917 | + --$depth; | |
| 918 | + if ( 0 === $depth ) { | |
| 919 | + $ranges[] = array( $start, $match[1] ); | |
| 920 | + } | |
| 921 | + } | |
| 922 | + } | |
| 923 | + if ( $depth > 0 ) { | |
| 924 | + $ranges[] = array( $start, strlen( $html ) ); | |
| 925 | + } | |
| 926 | + } | |
| 927 | + return $ranges; | |
| 928 | + } | |
| 929 | + | |
| 930 | + /** Does a byte offset fall inside any of the given [start, end] ranges? */ | |
| 931 | + private static function offset_in_ranges( int $offset, array $ranges ): bool { | |
| 932 | + foreach ( $ranges as $range ) { | |
| 933 | + if ( $offset > $range[0] && $offset < $range[1] ) { | |
| 934 | + return true; | |
| 935 | + } | |
| 936 | + } | |
| 937 | + return false; | |
| 938 | + } | |
| 939 | + | |
| 940 | + /** | |
| 334 | 941 | * Score for an image whose size we can't read at all. |
| 335 | 942 | * |
| 336 | 943 | * Deliberately non-zero: an unmeasurable image must still beat nothing and |
| 337 | 944 | * still be preloadable on a page where no image declares its size. But it |
| @@ -492,13 +1099,21 @@ | ||
| 492 | 1099 | */ |
| 493 | 1100 | private const NON_HERO_MARKERS = array( 'logo', 'icon', 'avatar', 'gravatar', 'spinner', 'emoji', 'site-icon', 'custom-logo' ); |
| 494 | 1101 | |
| 495 | 1102 | /** |
| 1103 | + * Below this declared area (px²) an image is a badge/thumb/divider, never | |
| 1104 | + * an LCP hero — 10 000 is a 100×100 square, or a 500×20 strip. Applied | |
| 1105 | + * only when BOTH dimensions are readable. (FBS-84576) | |
| 1106 | + */ | |
| 1107 | + private const MIN_LCP_AREA = 10000; | |
| 1108 | + | |
| 1109 | + /** | |
| 496 | 1110 | * Is this <img> too small / too chrome-like to be the LCP hero? True when |
| 497 | 1111 | * either (a) it carries a logo/icon/avatar marker, (b) an explicit |
| 498 | - * `data-no-lcp` opt-out, or (c) BOTH width and height are present and both | |
| 499 | - * are ≤ the threshold. Missing dimensions are NOT guessed — an image whose | |
| 500 | - * size we can't read still competes. (FBS-83553 H1 "logo before hero".) | |
| 1112 | + * `data-no-lcp` opt-out, or (c) BOTH width and height are present and | |
| 1113 | + * both are ≤ the dimension threshold, or their area is under | |
| 1114 | + * MIN_LCP_AREA. Missing dimensions are NOT guessed — an image whose size | |
| 1115 | + * we can't read still competes. (FBS-83553 H1 "logo before hero".) | |
| 501 | 1116 | */ |
| 502 | 1117 | private static function looks_too_small( string $tag ): bool { |
| 503 | 1118 | if ( false !== stripos( $tag, 'data-no-lcp' ) ) { |
| 504 | 1119 | return true; |
| @@ -514,8 +1129,11 @@ | ||
| 514 | 1129 | $w = self::attr( $tag, 'width' ); |
| 515 | 1130 | $h = self::attr( $tag, 'height' ); |
| 516 | 1131 | if ( '' === $w || '' === $h || ! is_numeric( $w ) || ! is_numeric( $h ) ) { |
| 517 | 1132 | return false; // unknown size — don't guess; let it compete. |
| 1133 | + } | |
| 1134 | + if ( (int) $w * (int) $h < self::MIN_LCP_AREA ) { | |
| 1135 | + return true; | |
| 518 | 1136 | } |
| 519 | 1137 | return (int) $w <= self::MIN_LCP_DIMENSION && (int) $h <= self::MIN_LCP_DIMENSION; |
| 520 | 1138 | } |
| 521 | 1139 | |