[wp-trac] [WordPress Trac] #58894: Enhance `wp_get_loading_optimization_attributes()` to support arbitrary context values

WordPress Trac noreply at wordpress.org
Mon Sep 11 16:21:04 UTC 2023


#58894: Enhance `wp_get_loading_optimization_attributes()` to support arbitrary
context values
-------------------------+--------------------------
 Reporter:  flixos90     |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  6.4
Component:  Media        |     Version:
 Severity:  normal       |  Resolution:
 Keywords:  needs-patch  |     Focuses:  performance
-------------------------+--------------------------

Comment (by flixos90):

 @mukesh27 Currently, the logic in
 `wp_get_loading_optimization_attributes()` is heavily tied to specific
 context values. For example, if you want to display a custom image in the
 header to be enforced without `loading="lazy"`, you would need to pass the
 `get_header_image_tag` context, even if the image is not in fact the
 header image, so that is confusing and inflexible.

 So technically speaking, this ticket is fundamentally about replacing the
 two big `switch` clauses with more generally applicable logic that is
 mostly decoupled from specific contexts.

 More specifically, here is what I would propose:
 * Replace the switch clause for the early return if `doing_filter(
 'the_content' )` with a more general clause: It should return early if the
 current context is ''anything'' other than 'the_content' while
 `doing_filter( 'the_content' )`.
 * Replace the bigger switch clause with a set of `if elseif` clauses that
 ignores context, except for the "header enforced contexts":
     * Define a list with "header enforced contexts", which by default
 should only have `'template_part_' . WP_TEMPLATE_PART_AREA_HEADER` and
 `'get_header_image_tag'`. If the current context is one of them, run the
 logic that is currently within that 1st `case` group. That could be the
 first `if` condition.
     * Then use the exact logic from the 2nd `case` group (i.e. `if ( !
 is_admin() && in_the_loop() && is_main_query() ) { ...`) as `elseif`.
     * Then use the exact logic from the 3rd `case` group (i.e. `if (
 $wp_query->before_loop ...`) as `elseif`.

 That way, all the logic, except for the "header enforced contexts" one
 becomes context unaware, allowing more flexibility for plugins.

 Something that needs to be ensured while making these changes is that all
 existing tests must still pass without modifications.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58894#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list