Skip to content

Commit

Permalink
Add data-od-related-tag attributes to LINK to assist with debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Aug 8, 2024
1 parent 9ea5628 commit 7c377b4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
5 changes: 3 additions & 2 deletions plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
10 changes: 10 additions & 0 deletions plugins/optimization-detective/class-od-link-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7c377b4

Please sign in to comment.