Skip to:
Content

BuddyPress.org

Changeset 12757


Ignore:
Timestamp:
10/16/2020 06:02:01 AM (7 months ago)
Author:
r-a-y
Message:

Add lazy loading support for images and iframes.

In order to take advantage of native browser support for lazy loading,
this commit adds the 'loading="lazy"' attribute to all <img> and
<iframe> elements used throughout BuddyPress (activity content,
private messaging content and avatar display).

The generic function, bp_core_add_loading_lazy_attribute(), is
introduced to help with this.

A special case is made for WordPress post embeds. As of this commit,
only Chromium-based browsers (77+) support iframe lazy loading if the
iframe is visible on load. (See https://web.dev/iframe-lazy-loading/#iframe-specific-lazy-loading-behavior.)
Since WordPress post embeds uses inline CSS to hide their iframe on
initial load, we need to remove their inline CSS so lazy loading can
be properly applied.

Props r-a-y, imath.

Fixes #8340.

Location:
trunk/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-embeds.php

    r12711 r12757  
    268268
    269269            // Set up thumb.
    270             $content = sprintf( '<div class="thumb" style="max-width:%1$spx">%2$s<a href="%3$s" rel="nofollow" onclick="top.location.href=\'%3$s\'"><img src="%4$s" alt="" /></a></div>', $thumb_width, $play_icon, esc_url( $url ), esc_url( $thumbnail ) );
     270            $content = sprintf( '<div class="thumb" style="max-width:%1$spx">%2$s<a href="%3$s" rel="nofollow" onclick="top.location.href=\'%3$s\'"><img loading="lazy" src="%4$s" alt="" /></a></div>', $thumb_width, $play_icon, esc_url( $url ), esc_url( $thumbnail ) );
    271271
    272272            // Show title.
  • trunk/src/bp-activity/bp-activity-filters.php

    r12756 r12757  
    9191add_filter( 'bp_get_activity_latest_update_excerpt', 'bp_activity_make_nofollow_filter' );
    9292add_filter( 'bp_get_activity_feed_item_description', 'bp_activity_make_nofollow_filter' );
     93
     94add_filter( 'bp_get_activity_content_body', 'bp_core_add_loading_lazy_attribute' );
     95add_filter( 'bp_activity_comment_content',  'bp_core_add_loading_lazy_attribute' );
    9396
    9497add_filter( 'pre_comment_content',                   'bp_activity_at_name_filter' );
  • trunk/src/bp-core/bp-core-filters.php

    r12678 r12757  
    5959add_filter( 'bp_email_set_content_plaintext', 'wp_strip_all_tags', 6 );
    6060add_filter( 'bp_email_set_subject', 'sanitize_text_field', 6 );
     61
     62// Avatars.
     63add_filter( 'bp_core_fetch_avatar', 'bp_core_add_loading_lazy_attribute' );
    6164
    6265/**
     
    901904
    902905/**
     906 * Add 'loading="lazy"' attribute into images and iframes.
     907 *
     908 * @since 7.0.0
     909 *
     910 * @string $content Content to inject attribute into.
     911 * @return string
     912 */
     913function bp_core_add_loading_lazy_attribute( $content = '' ) {
     914    if ( false === strpos( $content, '<img ' ) && false === strpos( $content, '<iframe ' ) ) {
     915        return $content;
     916    }
     917
     918    $content = str_replace( '<img ',    '<img loading="lazy" ',    $content );
     919    $content = str_replace( '<iframe ', '<iframe loading="lazy" ', $content );
     920
     921    // WordPress posts need their position absolute removed for lazyloading.
     922    $find_pos_absolute = ' style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" ';
     923    if ( false !== strpos( $content, 'data-secret=' ) && false !== strpos( $content, $find_pos_absolute ) ) {
     924        $content = str_replace( $find_pos_absolute, '', $content );
     925    }
     926
     927    return $content;
     928}
     929
     930/**
    903931 * Should BuddyPress load the mentions scripts and related assets, including results to prime the
    904932 * mentions suggestions?
  • trunk/src/bp-groups/bp-groups-blocks.php

    r12750 r12757  
    111111            '<div class="item-header-avatar">
    112112                <a href="%1$s">
    113                     <img src="%2$s" alt="%3$s" class="avatar">
     113                    <img loading="lazy" src="%2$s" alt="%3$s" class="avatar">
    114114                </a>
    115115            </div>',
  • trunk/src/bp-groups/classes/class-bp-groups-component.php

    r12749 r12757  
    893893
    894894                if ( empty( $bp->bp_options_avatar ) ) {
    895                     $bp->bp_options_avatar = '<img src="' . esc_url( bp_core_avatar_default_thumb() ) . '" alt="' . esc_attr__( 'No Group Profile Photo', 'buddypress' ) . '" class="avatar" />';
     895                    $bp->bp_options_avatar = '<img loading="lazy" src="' . esc_url( bp_core_avatar_default_thumb() ) . '" alt="' . esc_attr__( 'No Group Profile Photo', 'buddypress' ) . '" class="avatar" />';
    896896                }
    897897            }
  • trunk/src/bp-members/bp-members-blocks.php

    r12750 r12757  
    107107            '<div class="item-header-avatar">
    108108                <a href="%1$s">
    109                     <img src="%2$s" alt="%3$s" class="avatar">
     109                    <img loading="lazy" src="%2$s" alt="%3$s" class="avatar">
    110110                </a>
    111111            </div>',
  • trunk/src/bp-messages/bp-messages-filters.php

    r12745 r12757  
    7878add_filter( 'bp_get_message_thread_content',          'stripslashes_deep', 1 );
    7979
     80add_filter( 'bp_get_the_thread_message_content', 'bp_core_add_loading_lazy_attribute' );
     81
    8082// Personal data export.
    8183add_filter( 'wp_privacy_personal_data_exporters', 'bp_messages_register_personal_data_exporter' );
Note: See TracChangeset for help on using the changeset viewer.