Skip to content

Commit

Permalink
Add method that returns an array of URLs to preconnect to based on em…
Browse files Browse the repository at this point in the history
…bed type

Signed-off-by: Shyamsundar Gadde <[email protected]>
  • Loading branch information
ShyamGadde committed Dec 20, 2024
1 parent 7f08b4e commit d9fc859
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,14 @@ private function reduce_layout_shifts( OD_Tag_Visitor_Context $context ): void {
}

/**
* Adds preconnect links for embed resources.
* Gets preconnect URLs based on embed type
*
* @since n.e.x.t
*
* @param OD_Tag_Visitor_Context $context Tag visitor context, with the cursor currently at an embed block.
* @param OD_HTML_Tag_Processor $processor Processor.
* @return array<non-empty-string> Array of URLs to preconnect to.
*/
private function add_preconnect_links( OD_Tag_Visitor_Context $context ): void {
$processor = $context->processor;
$embed_wrapper_xpath = self::get_embed_wrapper_xpath( $processor->get_xpath() );

private function get_preconnect_urls( OD_HTML_Tag_Processor $processor ): array {
/*
* The following embeds have been chosen for optimization due to their relative popularity among all embed types.
* See <https://colab.sandbox.google.com/drive/1nSpg3qoCLY-cBTV2zOUkgUCU7R7X2f_R?resourcekey=0-MgT7Ur0pT__vw-5_AHjgWQ#scrollTo=utZv59sXzXvS>.
Expand Down Expand Up @@ -267,6 +265,21 @@ private function add_preconnect_links( OD_Tag_Visitor_Context $context ): void {
$preconnect_hrefs[] = 'https://i.pinimg.com';
}

return $preconnect_hrefs;
}

/**
* Adds preconnect links for embed resources.
*
* @since n.e.x.t
*
* @param OD_Tag_Visitor_Context $context Tag visitor context, with the cursor currently at an embed block.
*/
private function add_preconnect_links( OD_Tag_Visitor_Context $context ): void {
$processor = $context->processor;
$embed_wrapper_xpath = self::get_embed_wrapper_xpath( $processor->get_xpath() );

$preconnect_hrefs = $this->get_preconnect_urls( $processor );
foreach ( $preconnect_hrefs as $preconnect_href ) {
foreach ( $context->url_metric_group_collection as $group ) {
if ( ! ( $group->get_element_max_intersection_ratio( $embed_wrapper_xpath ) > 0.0 ) ) {
Expand Down

0 comments on commit d9fc859

Please sign in to comment.