diff --git a/plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php b/plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php index 3b2636ee81..a1d02b98f1 100644 --- a/plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php +++ b/plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php @@ -81,6 +81,7 @@ private function is_embed_wrapper( OD_HTML_Tag_Processor $processor ): bool { * Otherwise, if the embed is not in any initial viewport, it will add lazy-loading logic. * * @since 0.2.0 + * @since n.e.x.t Adds preconnect links for each viewport group and skips if the element is not in the viewport for that group. * * @param OD_Tag_Visitor_Context $context Tag visitor context. * @return bool Whether the tag should be tracked in URL Metrics. @@ -109,7 +110,8 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool { && $context->url_metric_group_collection->get_last_group()->count() > 0 ) { - $max_intersection_ratio = $context->url_metric_group_collection->get_element_max_intersection_ratio( self::get_embed_wrapper_xpath( $processor->get_xpath() ) ); + $embed_wrapper_xpath = self::get_embed_wrapper_xpath( $processor->get_xpath() ); + $max_intersection_ratio = $context->url_metric_group_collection->get_element_max_intersection_ratio( $embed_wrapper_xpath ); if ( $max_intersection_ratio > 0 ) { /* * The following embeds have been chosen for optimization due to their relative popularity among all embed types. @@ -171,12 +173,20 @@ 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, - ) - ); + foreach ( $context->url_metric_group_collection as $group ) { + if ( ! ( $group->get_element_max_intersection_ratio( $embed_wrapper_xpath ) > 0.0 ) ) { + continue; + } + + $context->link_collection->add_link( + array( + 'rel' => 'preconnect', + 'href' => $preconnect_href, + ), + $group->get_minimum_viewport_width(), + $group->get_maximum_viewport_width() + ); + } } } elseif ( embed_optimizer_update_markup( $processor, false ) && ! $this->added_lazy_script ) { $processor->append_body_html( wp_get_inline_script_tag( embed_optimizer_get_lazy_load_script(), array( 'type' => 'module' ) ) ); diff --git a/plugins/embed-optimizer/hooks.php b/plugins/embed-optimizer/hooks.php index 452794d030..f61e439b45 100644 --- a/plugins/embed-optimizer/hooks.php +++ b/plugins/embed-optimizer/hooks.php @@ -42,7 +42,7 @@ function embed_optimizer_add_non_optimization_detective_hooks(): void { * @param string $optimization_detective_version Current version of the optimization detective plugin. */ function embed_optimizer_init_optimization_detective( string $optimization_detective_version ): void { - $required_od_version = '0.7.0'; + $required_od_version = '0.9.0'; if ( version_compare( (string) strtok( $optimization_detective_version, '-' ), $required_od_version, '<' ) ) { add_action( 'admin_notices', diff --git a/plugins/embed-optimizer/tests/test-cases/single-twitter-embed-inside-viewport-without-resized-data.php b/plugins/embed-optimizer/tests/test-cases/single-twitter-embed-inside-viewport-without-resized-data.php index 113950c837..37886051b9 100644 --- a/plugins/embed-optimizer/tests/test-cases/single-twitter-embed-inside-viewport-without-resized-data.php +++ b/plugins/embed-optimizer/tests/test-cases/single-twitter-embed-inside-viewport-without-resized-data.php @@ -33,8 +33,8 @@