#58891 closed enhancement (fixed)
Simplify logic in `wp_get_loading_optimization_attributes()`
Reported by: | flixos90 | Owned by: | flixos90 |
---|---|---|---|
Milestone: | 6.4 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Media | Keywords: | has-patch, has-unit-tests, has-dev-note |
Focuses: | performance | Cc: |
Description (last modified by )
The wp_get_loading_optimization_attributes()
function, which was introduced in 6.3 (see #58235) but inherited most of its logic from the now deprecated wp_get_loading_attr_default()
function introduced in 5.5, contains quite complex and hard-to-follow logic. While early returns are usually a good pattern, with the excessive mix of individual if-clauses and early returns, the function is hard to follow and thus complicated to fully understand, which makes it challenging for maintenance.
In order to decentralize the knowledge and understanding from just the few people that were involved in its implementation so far, this ticket aims to rewrite the function using a more coherent approach, which avoids most of the early returns and potentially uses a switch
clause for the different context values.
While refactoring on its own is generally discouraged in WordPress core, this refactoring will benefit, simplify, or even enable several other tickets:
It should however not be implemented as part of any of those tickets, since that could easily lead to oversights due to the complex logic. The goal of this ticket should be to refactor the logic in a way that all existing tests pass without any modifications. Coverage is quite extensive on this function (at least that!), so this is a good framework to follow here.
Change History (7)
This ticket was mentioned in PR #4912 on WordPress/wordpress-develop by @flixos90.
15 months ago
#2
- Keywords has-patch has-unit-tests added; needs-patch removed
@flixos90 commented on PR #4912:
15 months ago
#3
@joemcgill @kt-12 Would love to get your review on this.
@flixos90 commented on PR #4912:
14 months ago
#5
Committed in https://core.trac.wordpress.org/changeset/56347
#6
@
13 months ago
- Keywords needs-dev-note added
The 4 tickets related to wp_get_loading_optimization_attributes()
should receive a single dev note post explaining those changes.
This PR refactors the
wp_get_loading_optimization_attributes()
function to make it easier to follow and maintain. It removes all the early returns in favor of a more procedural and sequential single execution paths, with the only early returns remaining being those for which the function should _actually_ return early, as in not return anything / make any modifications to the respective tag. Additionally, several comments were added to help clarify the logic. Furthermore, with the sequential flow the function follows now, we no longer need the closures that were previously used to avoid duplicate code.As mentioned on the ticket, the goal of this is purely to enhance maintainability of the code without making any functional change. Given the substantial test coverage for this function that already exists in core (either with direct tests for
wp_get_loading_optimization_attributes()
or indirect tests forwp_filter_content_tags()
), all we need to ensure here is that these tests all pass without modification. While there are probably a few areas that we can improve (I noticed a few myself while working on this, and there are already separate tickets for some things), let's avoid any behavioral change in this PR to focus on purely refactoring without breaking anything.There's a single test change made here, which was for an overly strict test that unnecessarily relied on a specific order for the attributes returned, which does not actually matter.
Trac ticket: https://core.trac.wordpress.org/ticket/58891