Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data-od-related-tag attribute to links to assist with debugging #1452

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading