Media: Enhance `wp_get_loading_optimization_attributes()` to support arbitrary context values.
The `wp_get_loading_optimization_attributes()` function, which was introduced in 6.3, based on the now deprecated `wp_get_loading_attr_default()` function introduced in 5.5, relies on a `$context` parameter based on which it may alter its behavior and the attributes returned. So far, it has only supported context values used within WordPress core. This changeset decouples the behaviors of the function from specific contexts, allowing for more flexibility. Theme and plugin developers will be able to rely on their own context values when rendering images in non-standard ways, rather than being forced to use a core context, to get the loading optimization benefits the function provides. As part of this change, a `wp_loading_optimization_force_header_contexts` filter is introduced, which allows filtering the map of context values and whether they should be considered header contexts, i.e. i.e. any image having one of these contexts will be assumed to appear above the fold. Props mukesh27, costdev, flixos90. Fixes #58894. Built from https://develop.svn.wordpress.org/trunk@56612 git-svn-id: http://core.svn.wordpress.org/trunk@56124 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
1b5fc476f8
commit
56b5244fdb
|
@ -5652,13 +5652,9 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) {
|
||||||
* can result in the first post content image being lazy-loaded or an image further down the page being marked as a
|
* can result in the first post content image being lazy-loaded or an image further down the page being marked as a
|
||||||
* high priority.
|
* high priority.
|
||||||
*/
|
*/
|
||||||
switch ( $context ) {
|
// TODO: Handle shortcode images together with the content (see https://core.trac.wordpress.org/ticket/58853).
|
||||||
case 'the_post_thumbnail':
|
if ( 'the_content' !== $context && 'do_shortcode' !== $context && doing_filter( 'the_content' ) ) {
|
||||||
case 'wp_get_attachment_image':
|
return $loading_attrs;
|
||||||
case 'widget_media_image':
|
|
||||||
if ( doing_filter( 'the_content' ) ) {
|
|
||||||
return $loading_attrs;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -5709,64 +5705,56 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( null === $maybe_in_viewport ) {
|
if ( null === $maybe_in_viewport ) {
|
||||||
switch ( $context ) {
|
$header_enforced_contexts = array(
|
||||||
// Consider elements with these header-specific contexts to be in viewport.
|
'template_part_' . WP_TEMPLATE_PART_AREA_HEADER => true,
|
||||||
case 'template_part_' . WP_TEMPLATE_PART_AREA_HEADER:
|
'get_header_image_tag' => true,
|
||||||
case 'get_header_image_tag':
|
);
|
||||||
$maybe_in_viewport = true;
|
|
||||||
$maybe_increase_count = true;
|
|
||||||
break;
|
|
||||||
// Count main content elements and detect whether in viewport.
|
|
||||||
case 'the_content':
|
|
||||||
case 'the_post_thumbnail':
|
|
||||||
case 'do_shortcode':
|
|
||||||
// Only elements within the main query loop have special handling.
|
|
||||||
if ( ! is_admin() && in_the_loop() && is_main_query() ) {
|
|
||||||
/*
|
|
||||||
* Get the content media count, since this is a main query
|
|
||||||
* content element. This is accomplished by "increasing"
|
|
||||||
* the count by zero, as the only way to get the count is
|
|
||||||
* to call this function.
|
|
||||||
* The actual count increase happens further below, based
|
|
||||||
* on the `$increase_count` flag set here.
|
|
||||||
*/
|
|
||||||
$content_media_count = wp_increase_content_media_count( 0 );
|
|
||||||
$increase_count = true;
|
|
||||||
|
|
||||||
// If the count so far is below the threshold, `loading` attribute is omitted.
|
/**
|
||||||
if ( $content_media_count < wp_omit_loading_attr_threshold() ) {
|
* Filters the header-specific contexts.
|
||||||
$maybe_in_viewport = true;
|
*
|
||||||
} else {
|
* @since 6.4.0
|
||||||
$maybe_in_viewport = false;
|
*
|
||||||
}
|
* @param array $default_header_enforced_contexts Map of contexts for which elements should be considered
|
||||||
}
|
* in the header of the page, as $context => $enabled
|
||||||
/*
|
* pairs. The $enabled should always be true.
|
||||||
* For the 'the_post_thumbnail' context, the following case
|
*/
|
||||||
* clause needs to be considered as well, therefore skip the
|
$header_enforced_contexts = apply_filters( 'wp_loading_optimization_force_header_contexts', $header_enforced_contexts );
|
||||||
* break statement here if the viewport has not been
|
|
||||||
* determined.
|
// Consider elements with these header-specific contexts to be in viewport.
|
||||||
*/
|
if ( isset( $header_enforced_contexts[ $context ] ) ) {
|
||||||
if ( 'the_post_thumbnail' !== $context || null !== $maybe_in_viewport ) {
|
$maybe_in_viewport = true;
|
||||||
break;
|
$maybe_increase_count = true;
|
||||||
}
|
} elseif ( ! is_admin() && in_the_loop() && is_main_query() ) {
|
||||||
// phpcs:ignore Generic.WhiteSpace.ScopeIndent.Incorrect
|
/*
|
||||||
// Consider elements before the loop as being in viewport.
|
* Get the content media count, since this is a main query
|
||||||
case 'wp_get_attachment_image':
|
* content element. This is accomplished by "increasing"
|
||||||
case 'widget_media_image':
|
* the count by zero, as the only way to get the count is
|
||||||
if (
|
* to call this function.
|
||||||
// Only apply for main query but before the loop.
|
* The actual count increase happens further below, based
|
||||||
$wp_query->before_loop && $wp_query->is_main_query()
|
* on the `$increase_count` flag set here.
|
||||||
/*
|
*/
|
||||||
* Any image before the loop, but after the header has started should not be lazy-loaded,
|
$content_media_count = wp_increase_content_media_count( 0 );
|
||||||
* except when the footer has already started which can happen when the current template
|
$increase_count = true;
|
||||||
* does not include any loop.
|
|
||||||
*/
|
// If the count so far is below the threshold, `loading` attribute is omitted.
|
||||||
&& did_action( 'get_header' ) && ! did_action( 'get_footer' )
|
if ( $content_media_count < wp_omit_loading_attr_threshold() ) {
|
||||||
) {
|
$maybe_in_viewport = true;
|
||||||
$maybe_in_viewport = true;
|
} else {
|
||||||
$maybe_increase_count = true;
|
$maybe_in_viewport = false;
|
||||||
}
|
}
|
||||||
break;
|
} elseif (
|
||||||
|
// Only apply for main query but before the loop.
|
||||||
|
$wp_query->before_loop && $wp_query->is_main_query()
|
||||||
|
/*
|
||||||
|
* Any image before the loop, but after the header has started should not be lazy-loaded,
|
||||||
|
* except when the footer has already started which can happen when the current template
|
||||||
|
* does not include any loop.
|
||||||
|
*/
|
||||||
|
&& did_action( 'get_header' ) && ! did_action( 'get_footer' )
|
||||||
|
) {
|
||||||
|
$maybe_in_viewport = true;
|
||||||
|
$maybe_increase_count = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '6.4-alpha-56611';
|
$wp_version = '6.4-alpha-56612';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
|
Loading…
Reference in New Issue