Toolbar: Add a filter to help remove site icons from toolbar for large multisite, and lazy load them by default.

This changeset introduces the `wp_admin_bar_show_site_icons` filter to help developers to hide site icons from the toolbar, as it may have negative performance impact on large multisites. It also adds a default lazy load behavior for these icons.

Props wslyhbb, sabernhardt, lkraav, kebbet, peterwilsoncc.
Fixes #54447

Built from https://develop.svn.wordpress.org/trunk@53100


git-svn-id: http://core.svn.wordpress.org/trunk@52689 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
audrasjb 2022-04-07 22:14:05 +00:00
parent 2c074169e7
commit b317b87147
2 changed files with 17 additions and 4 deletions

View File

@ -611,14 +611,27 @@ function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
) )
); );
/**
* Filters whether to show the site icons in toolbar.
*
* Returning false to this hook is the recommended way to hide site icons in the toolbar.
* A truthy return may have negative performance impact on large multisites.
*
* @since 6.0.0
*
* @param bool $show_site_icons Whether site icons should be shown in the toolbar. Default true.
*/
$show_site_icons = apply_filters( 'wp_admin_bar_show_site_icons', true );
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) { foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
switch_to_blog( $blog->userblog_id ); switch_to_blog( $blog->userblog_id );
if ( has_site_icon() ) { if ( true === $show_site_icons && has_site_icon() ) {
$blavatar = sprintf( $blavatar = sprintf(
'<img class="blavatar" src="%s" srcset="%s 2x" alt="" width="16" height="16" />', '<img class="blavatar" src="%s" srcset="%s 2x" alt="" width="16" height="16"%s />',
esc_url( get_site_icon_url( 16 ) ), esc_url( get_site_icon_url( 16 ) ),
esc_url( get_site_icon_url( 32 ) ) esc_url( get_site_icon_url( 32 ) ),
( wp_lazy_loading_enabled( 'img', 'site_icon_in_toolbar' ) ? ' loading="lazy"' : '' )
); );
} else { } else {
$blavatar = '<div class="blavatar"></div>'; $blavatar = '<div class="blavatar"></div>';

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.0-alpha-53099'; $wp_version = '6.0-alpha-53100';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.