Skip to content

Commit d6433ba

Browse files
committed
Leverage od_start_template_optimization action to initialize common LCP BG image data
1 parent 6abee9c commit d6433ba

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,25 @@ final class Image_Prioritizer_Background_Image_Styled_Tag_Visitor extends Image_
4949
* @since 0.3.0
5050
* @var array<array{OD_URL_Metric_Group, LcpElementExternalBackgroundImage}>
5151
*/
52-
private $group_common_lcp_element_external_background_images;
52+
private $group_common_lcp_element_external_background_images = array();
53+
54+
/**
55+
* Gathers the tuples of URL Metric group and the common LCP element external background image.
56+
*
57+
* This is called during the `od_start_template_optimization` action, which is before any tag visitor is first invoked.
58+
*
59+
* @since n.e.x.t
60+
*
61+
* @param OD_Template_Optimization_Context $context Template optimization context.
62+
*/
63+
public function gather_group_common_lcp_element_external_background_images( OD_Template_Optimization_Context $context ): void {
64+
foreach ( $context->url_metric_group_collection as $group ) {
65+
$common = $this->get_common_lcp_element_external_background_image( $group );
66+
if ( is_array( $common ) ) {
67+
$this->group_common_lcp_element_external_background_images[] = array( $group, $common );
68+
}
69+
}
70+
}
5371

5472
/**
5573
* Visits a tag.
@@ -141,19 +159,6 @@ private function get_common_lcp_element_external_background_image( OD_URL_Metric
141159
* @param OD_Tag_Visitor_Context $context Context.
142160
*/
143161
private function maybe_preload_external_lcp_background_image( OD_Tag_Visitor_Context $context ): void {
144-
// Gather the tuples of URL Metric group and the common LCP element external background image.
145-
// Note the groups of URL Metrics do not change across invocations, we just need to compute this once for all.
146-
// TODO: Instead of populating this here, it could be done once per invocation during the od_start_template_optimization action since the page's OD_URL_Metric_Group_Collection is available there.
147-
if ( ! is_array( $this->group_common_lcp_element_external_background_images ) ) {
148-
$this->group_common_lcp_element_external_background_images = array();
149-
foreach ( $context->url_metric_group_collection as $group ) {
150-
$common = $this->get_common_lcp_element_external_background_image( $group );
151-
if ( is_array( $common ) ) {
152-
$this->group_common_lcp_element_external_background_images[] = array( $group, $common );
153-
}
154-
}
155-
}
156-
157162
// There are no common LCP background images, so abort.
158163
if ( count( $this->group_common_lcp_element_external_background_images ) === 0 ) {
159164
return;

plugins/image-prioritizer/helper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function image_prioritizer_register_tag_visitors( OD_Tag_Visitor_Registry $regis
7979
$registry->register( 'image-prioritizer/img', $img_visitor );
8080

8181
$bg_image_visitor = new Image_Prioritizer_Background_Image_Styled_Tag_Visitor();
82+
add_action( 'od_start_template_optimization', array( $bg_image_visitor, 'gather_group_common_lcp_element_external_background_images' ) );
8283
$registry->register( 'image-prioritizer/background-image', $bg_image_visitor );
8384

8485
$video_visitor = new Image_Prioritizer_Video_Tag_Visitor();

0 commit comments

Comments
 (0)