From 7c377b4ec4d66e64b1e8cd02e9b11c271ec2b3d3 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 8 Aug 2024 16:07:45 -0700 Subject: [PATCH] Add data-od-related-tag attributes to LINK to assist with debugging --- .../class-embed-optimizer-tag-visitor.php | 5 +++-- ...rioritizer-background-image-styled-tag-visitor.php | 11 ++++++----- .../class-image-prioritizer-img-tag-visitor.php | 2 ++ .../class-od-link-collection.php | 10 ++++++++++ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php b/plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php index 4791b845c..095a3551d 100644 --- a/plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php +++ b/plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php @@ -109,8 +109,9 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool { foreach ( $preconnect_hrefs as $preconnect_href ) { $context->link_collection->add_link( array( - 'rel' => 'preconnect', - 'href' => $preconnect_href, + 'rel' => 'preconnect', + 'href' => $preconnect_href, + 'data-od-related-tag' => $context->processor->get_xpath(), ) ); } diff --git a/plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php b/plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php index 8e6a01f52..c83fa9cf1 100644 --- a/plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php +++ b/plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php @@ -57,11 +57,12 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool { // If this element is the LCP (for a breakpoint group), add a preload link for it. foreach ( $context->url_metrics_group_collection->get_groups_by_lcp_element( $xpath ) as $group ) { $link_attributes = array( - 'rel' => 'preload', - 'fetchpriority' => 'high', - 'as' => 'image', - 'href' => $background_image_url, - 'media' => 'screen', + 'rel' => 'preload', + 'fetchpriority' => 'high', + 'as' => 'image', + 'href' => $background_image_url, + 'media' => 'screen', + 'data-od-related-tag' => $xpath, ); $context->link_collection->add_link( diff --git a/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php b/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php index 4395cdaf8..1f6ae5f83 100644 --- a/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php +++ b/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php @@ -117,6 +117,8 @@ static function ( string $value ): bool { $link_attributes['media'] = 'screen'; + $link_attributes['data-od-related-tag'] = $context->processor->get_xpath(); + $context->link_collection->add_link( $link_attributes, $group->get_minimum_viewport_width(), diff --git a/plugins/optimization-detective/class-od-link-collection.php b/plugins/optimization-detective/class-od-link-collection.php index 28084c9bd..034cb3302 100644 --- a/plugins/optimization-detective/class-od-link-collection.php +++ b/plugins/optimization-detective/class-od-link-collection.php @@ -96,6 +96,16 @@ public function add_link( array $attributes, ?int $minimum_viewport_width = null } } + // Allow for custom attributes to be added as well, namely data attributes. + foreach ( $attributes as $key => $value ) { + if ( ! is_string( $key ) ) { + $throw_invalid_argument_exception( __( 'All attribute keys must be strings.', 'optimization-detective' ) ); + } + if ( ! is_string( $value ) ) { + $throw_invalid_argument_exception( __( 'All attribute values must be strings.', 'optimization-detective' ) ); + } + } + $this->links_by_rel[ $attributes['rel'] ][] = array( 'attributes' => $attributes, 'minimum_viewport_width' => $minimum_viewport_width,