Skip to content

Commit

Permalink
Fix/update default fetched links to 10 and parse unappended url to is…
Browse files Browse the repository at this point in the history
… home (#6563)
  • Loading branch information
jeawhanlee authored Apr 18, 2024
1 parent 132a9f0 commit 5d51e47
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
4 changes: 3 additions & 1 deletion inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class APIClient extends BaseAPIClient {
* @return array
*/
public function add_to_atf_queue( string $url ): array {
$is_home = Utils::is_home( $url );

$url = add_query_arg(
[
'wpr_imagedimensions' => 1,
Expand All @@ -24,7 +26,7 @@ public function add_to_atf_queue( string $url ): array {

$config = [
'optimization_list' => '',
'is_home' => Utils::is_home( $url ),
'is_home' => $is_home,
];

return $this->add_to_queue( $url, $config );
Expand Down
11 changes: 9 additions & 2 deletions inc/Engine/Media/AboveTheFold/WarmUp/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\License\API\User;
use WP_Rocket\Engine\Common\Utils;

class Controller {

Expand Down Expand Up @@ -125,12 +126,16 @@ function ( $link ) {
function ( $link ) use ( $home_url ) {
$link_host = wp_parse_url( $link );
$site_host = wp_parse_url( $home_url );

/**
* Check for valid link.
* Check that no external link.
* Check that it's not home.
*/
return wp_http_validate_url( $link ) && $link_host['host'] === $site_host['host'];
$is_valid_url = wp_http_validate_url( $link );
$is_same_host = isset( $link_host['host'] ) ? $link_host['host'] === $site_host['host'] : false;
$is_not_home = ! Utils::is_home( $link );

return $is_valid_url && $is_same_host && $is_not_home;
}
);

Expand All @@ -144,6 +149,8 @@ function ( $link ) use ( $home_url ) {
*/
$link_number = apply_filters( 'rocket_atf_warmup_links_number', 10 );
$links = array_slice( $links, 0, $link_number );
// Add home url to the list of links.
$links[] = home_url();

return $links;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
$html_no_found_links = '<!DOCTYPE html><html class="no-js" lang="en-US"><head><title></title><link href="https://fonts.gstatic.com" crossorigin rel="preconnect" /><link rel="stylesheet" id="wp-block-library-css" href="https://example.org/wp-includes/css/dist/block-library/style.min.css?ver=6.4.3" media="all" /></head><body><button data-link="https://example.org/hello-world">Click Here</button></body></html>';
$html_no_valid_links = '<!DOCTYPE html><html class="no-js" lang="en-US"><head><title></title><link href="https://fonts.gstatic.com" crossorigin rel="preconnect" /><link rel="stylesheet" id="wp-block-library-css" href="https://example.org/wp-includes/css/dist/block-library/style.min.css?ver=6.4.3" media="all" /></head><body><button data-link="https://example.org/hello-world">Click Here</button><a href="#hero">Goto Top</a><a href="javascript:void(0)">Click Bait</a></body></html>';
$html_valid_links_among_valid_links = '<!DOCTYPE html><html class="no-js" lang="en-US"><head><title></title><link href="https://fonts.gstatic.com" crossorigin rel="preconnect" /><link rel="stylesheet" id="wp-block-library-css" href="https://example.org/wp-includes/css/dist/block-library/style.min.css?ver=6.4.3" media="all" /></head><body><button data-link="https://example.org/hello-world">Click Here</button><a href="#hero">Goto Top</a><a href="javascript:void(0)">Click Bait</a><a href="https://example.org/hello-world">Hello World</a><a href="https://example.org/another-day">Another Day</a><a href="https://example.org/rich-dad-poor-dad">Rich Dad Poor Dad</a></body></html>';
$html_valid_links_among_invalid_links = '<!DOCTYPE html><html class="no-js" lang="en-US"><head><title></title><link href="https://fonts.gstatic.com" crossorigin rel="preconnect" /><link rel="stylesheet" id="wp-block-library-css" href="https://example.org/wp-includes/css/dist/block-library/style.min.css?ver=6.4.3" media="all" /></head><body><button data-link="https://example.org/hello-world">Click Here</button><a href="#hero">Goto Top</a><a href="javascript:void(0)">Click Bait</a><a href="https://example.org/hello-world">Hello World</a><a href="https://example.org/another-day">Another Day</a><a href="https://example.org/rich-dad-poor-dad">Rich Dad Poor Dad</a></body></html>';
$html_external_links = '<!DOCTYPE html><html class="no-js" lang="en-US"><head><title></title><link href="https://fonts.gstatic.com" crossorigin rel="preconnect" /><link rel="stylesheet" id="wp-block-library-css" href="https://example.org/wp-includes/css/dist/block-library/style.min.css?ver=6.4.3" media="all" /></head><body><button data-link="https://example.org/hello-world">Click Here</button><a href="https://wordpress.org/hello-world">Hello World</a><a href="https://wordpress.org/another-day">Another Day</a><a href="https://wordpress.org/rich-dad-poor-dad">Rich Dad Poor Dad</a></body></html>';
$html_valid_links_among_external_links = '<!DOCTYPE html><html class="no-js" lang="en-US"><head><title></title><link href="https://fonts.gstatic.com" crossorigin rel="preconnect" /><link rel="stylesheet" id="wp-block-library-css" href="https://example.org/wp-includes/css/dist/block-library/style.min.css?ver=6.4.3" media="all" /></head><body><button data-link="https://example.org/hello-world">Click Here</button><a href="https://wordpress.org/hello-world">Hello World</a><a href="https://wordpress.org/another-day">Another Day</a><a href="https://wordpress.org/rich-dad-poor-dad">Rich Dad Poor Dad</a><a href="https://example.org/hello-world">Hello World</a><a href="https://example.org/another-day">Another Day</a><a href="https://example.org/rich-dad-poor-dad">Rich Dad Poor Dad</a></body></html>';
$html_links_without_duplicate = '<!DOCTYPE html><html class="no-js" lang="en-US"><head><title></title><link href="https://fonts.gstatic.com" crossorigin rel="preconnect" /><link rel="stylesheet" id="wp-block-library-css" href="https://example.org/wp-includes/css/dist/block-library/style.min.css?ver=6.4.3" media="all" /></head><body><button data-link="https://example.org/hello-world">Click Here</button><a href="https://example.org/hello-world">Hello World</a><a href="https://example.org/another-day">Another Day</a><a href="https://example.org/rich-dad-poor-dad">Rich Dad Poor Dad</a><a href="https://example.org/hello-world">Hello World</a><a href="https://example.org/another-day">Another Day</a><a href="https://example.org/rich-dad-poor-dad">Rich Dad Poor Dad</a><a href="https://example.org/rebecca-brown-he-came-to-set-the-captives-free">Buy (He came to set the captives free) - Rebecca Brown</a></body></html>';
$html_links_with_relative_url = '<!DOCTYPE html><html class="no-js" lang="en-US"><head><title></title><link href="https://fonts.gstatic.com" crossorigin rel="preconnect" /><link rel="stylesheet" id="wp-block-library-css" href="https://example.org/wp-includes/css/dist/block-library/style.min.css?ver=6.4.3" media="all" /></head><body><button data-link="https://example.org/hello-world">Click Here</button><a href="https://example.org/hello-world">Hello World</a><a href="https://example.org/another-day">Another Day</a><a href="/rich-dad-poor-dad">Rich Dad Poor Dad</a><a href="/rebecca-brown-he-came-to-set-the-captives-free">Buy (He came to set the captives free) - Rebecca Brown</a></body></html>';
$html_with_ten_links_and_home = '<!DOCTYPE html><html class="no-js" lang="en-US"><head><title></title><link href="https://fonts.gstatic.com" crossorigin="preconnect"/><link rel="stylesheet" id="wp-block-library-css" href="https://example.org/wp-includes/css/dist/block-library/style.min.css?ver=6.4.3" media="all"/></head><body><button data-link="https://example.org/hello-world">Click Here</button><a href="https://example.org/hello-world-2">Hello World 2</a><a href="https://example.org/hello-world-3">Hello World 3</a><a href="https://example.org/hello-world-4">Hello World 4</a><a href="https://example.org/hello-world-5">Hello World 5</a><a href="https://example.org/hello-world-6">Hello World 6</a><a href="https://example.org/hello-world-7">Hello World 7</a><a href="https://example.org/hello-world-8">Hello World 8</a><a href="https://example.org/hello-world-9">Hello World 9</a><a href="https://example.org/rich-dad-poor-dad">Rich Dad Poor Dad</a><a href="https://example.org/rebecca-brown-he-came-to-set-the-captives-free">Buy (He came to set the captives free) - Rebecca Brown</a><a href="https://example.org">Home</a></body></html>';

return [
'shouldReturnEmptyWhenLicenseExpired' => [
Expand Down Expand Up @@ -55,7 +56,7 @@
],
'expected' => [],
],
'shouldReturnEmptyWithNoValidLinks' => [
'shouldReturnOnlyHomeWithNoValidLinks' => [
'config' => [
'license_expired' => false,
'headers' => [
Expand All @@ -70,7 +71,9 @@
],
],
],
'expected' => [],
'expected' => [
'https://example.org',
],
],
'shouldReturnValidLinksAmongInvalidLinks' => [
'config' => [
Expand All @@ -81,7 +84,7 @@
],
'found_link' => true,
'response' => [
'body' => $html_valid_links_among_valid_links,
'body' => $html_valid_links_among_invalid_links,
'response' => [
'code' => 200,
],
Expand All @@ -91,9 +94,10 @@
'https://example.org/hello-world',
'https://example.org/another-day',
'https://example.org/rich-dad-poor-dad',
'https://example.org',
],
],
'shouldReturnEmptyWithExternalLinks' => [
'shouldReturnOnlyHomeWithExternalLinks' => [
'config' => [
'license_expired' => false,
'headers' => [
Expand All @@ -108,7 +112,9 @@
],
],
],
'expected' => [],
'expected' => [
'https://example.org',
],
],
'shouldReturnValidLinksAmongExternalLinks' => [
'config' => [
Expand All @@ -129,6 +135,7 @@
'https://example.org/hello-world',
'https://example.org/another-day',
'https://example.org/rich-dad-poor-dad',
'https://example.org',
],
],
'shouldReturnLinksWithoutDuplicate' => [
Expand All @@ -151,6 +158,7 @@
'https://example.org/another-day',
'https://example.org/rich-dad-poor-dad',
'https://example.org/rebecca-brown-he-came-to-set-the-captives-free',
'https://example.org',
],
],
'shouldReturnLinksWithRelativeUrl' => [
Expand All @@ -173,6 +181,36 @@
'https://example.org/another-day',
'https://example.org/rich-dad-poor-dad',
'https://example.org/rebecca-brown-he-came-to-set-the-captives-free',
'https://example.org',
],
],
'shouldReturnTenLinksPlusHome' => [
'config' => [
'license_expired' => false,
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
],
'found_link' => true,
'response' => [
'body' => $html_with_ten_links_and_home,
'response' => [
'code' => 200,
],
],
],
'expected' => [
'https://example.org/hello-world-2',
'https://example.org/hello-world-3',
'https://example.org/hello-world-4',
'https://example.org/hello-world-5',
'https://example.org/hello-world-6',
'https://example.org/hello-world-7',
'https://example.org/hello-world-8',
'https://example.org/hello-world-9',
'https://example.org/rich-dad-poor-dad',
'https://example.org/rebecca-brown-he-came-to-set-the-captives-free',
'https://example.org',
],
],
];

0 comments on commit 5d51e47

Please sign in to comment.