Make WordPress Core

Changeset 55318

Timestamp:
02/13/2023 06:32:40 PM (18 months ago)
Author:
flixos90
Message:

Media: Enhance logic to determine LCP image in block themes and avoid lazy-loading it.

[52065] originally introduced the logic to guess the LCP image based on certain heuristics and to not lazy-load that image. However, with the introduction of block themes, that logic was not functioning correctly, resulting in all featured images to be lazy-loaded, regardless of whether it was the LCP image or not.

Together with an update to the core/post-featured-image block included in [55079], this changeset fixes the logic to correctly handle featured images in block themes as well.

Additionally, in combination with an update to the core/template-part block from [55246], this changeset includes an enhancement which uses the benefits of block template parts to avoid lazy-loading images in the header block template part, making the lazy-loading heuristics even more accurate for sites using a block theme.

Props flixos90, adamsilverstein, mamaduka, antonvlasenko, shahidul95, reduanmasud, costdev, mukesh27, ironprogrammer, manfcarlo, robinwpdeveloper, spacedmonkey.
Fixes #56930.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template.php

    r54817 r55318  
    242242    $content = convert_smilies( $content );
    243243    $content = shortcode_unautop( $content );
    244     $content = wp_filter_content_tags( $content );
     244    $content = wp_filter_content_tags( $content );
    245245    $content = do_shortcode( $content );
    246246    $content = str_replace( ']]>', ']]>', $content );
  • trunk/src/wp-includes/media.php

    r55251 r55318  
    54455445 */
    54465446function wp_get_loading_attr_default( $context ) {
    5447     // Only elements with 'the_content' or 'the_post_thumbnail' context have special handling.
    5448     if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) {
     5447    // Skip lazy-loading for the overall block template, as it is handled more granularly.
     5448    if ( 'template' === $context ) {
     5449        return false;
     5450    }
     5451
     5452    // Do not lazy-load images in the header block template part, as they are likely above the fold.
     5453    $header_area = WP_TEMPLATE_PART_AREA_HEADER;
     5454    if ( "template_part_{$header_area}" === $context ) {
     5455        return false;
     5456    }
     5457
     5458    /*
     5459     * The first elements in 'the_content' or 'the_post_thumbnail' should not be lazy-loaded,
     5460     * as they are likely above the fold.
     5461     */
     5462    if ( 'the_content' === $context || 'the_post_thumbnail' === $context ) {
     5463        // Only elements within the main query loop have special handling.
     5464        if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {
     5465            return 'lazy';
     5466        }
     5467
     5468        // Increase the counter since this is a main query content element.
     5469        $content_media_count = wp_increase_content_media_count();
     5470
     5471        // If the count so far is below the threshold, return `false` so that the `loading` attribute is omitted.
     5472        if ( $content_media_count <= wp_omit_loading_attr_threshold() ) {
     5473            return false;
     5474        }
     5475
     5476        // For elements after the threshold, lazy-load them as usual.
    54495477        return 'lazy';
    54505478    }
    54515479
    5452     // Only elements within the main query loop have special handling.
    5453     if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {
    5454         return 'lazy';
    5455     }
    5456 
    5457     // Increase the counter since this is a main query content element.
    5458     $content_media_count = wp_increase_content_media_count();
    5459 
    5460     // If the count so far is below the threshold, return `false` so that the `loading` attribute is omitted.
    5461     if ( $content_media_count <= wp_omit_loading_attr_threshold() ) {
    5462         return false;
    5463     }
    5464 
    5465     // For elements after the threshold, lazy-load them as usual.
     5480    // Lazy-load by default for any unknown context.
    54665481    return 'lazy';
    54675482}
  • trunk/tests/phpunit/tests/media.php

    r55278 r55318  
    35483548
    35493549    /**
     3550
     3551
    35503552     * @ticket 53675
     3553
     3554
     3555
     3556
    35513557     * @dataProvider data_wp_get_loading_attr_default
    35523558     *
     
    35893595            $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) );
    35903596        }
     3597
     3598
     3599
     3600
    35913601    }
    35923602
     
    37013711    }
    37023712
     3713
     3714
     3715
     3716
     3717
     3718
     3719
     3720
     3721
     3722
     3723
     3724
     3725
     3726
     3727
     3728
     3729
     3730
     3731
     3732
     3733
     3734
     3735
     3736
     3737
     3738
     3739
     3740
     3741
     3742
     3743
     3744
     3745
     3746
     3747
     3748
     3749
     3750
     3751
     3752
     3753
     3754
     3755
     3756
     3757
     3758
     3759
     3760
     3761
     3762
     3763
     3764
     3765
     3766
     3767
     3768
     3769
     3770
     3771
     3772
     3773
     3774
     3775
     3776
     3777
     3778
     3779
     3780
     3781
     3782
     3783
     3784
     3785
     3786
     3787
     3788
     3789
     3790
     3791
     3792
     3793
     3794
     3795
     3796
     3797
     3798
     3799
     3800
     3801
     3802
     3803
     3804
     3805
     3806
     3807
     3808
     3809
     3810
     3811
     3812
     3813
     3814
     3815
     3816
     3817
     3818
     3819
     3820
     3821
     3822
     3823
     3824
     3825
     3826
     3827
     3828
     3829
     3830
     3831
     3832
     3833
     3834
     3835
     3836
     3837
     3838
     3839
     3840
     3841
     3842
     3843
     3844
     3845
     3846
     3847
     3848
     3849
     3850
     3851
     3852
     3853
     3854
     3855
     3856
     3857
     3858
     3859
     3860
     3861
     3862
     3863
     3864
     3865
    37033866    private function reset_content_media_count() {
    37043867        // Get current value without increasing.
Note: See TracChangeset for help on using the changeset viewer.