diff --git a/inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php b/inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php index 251e657f6b..f6112a9944 100644 --- a/inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php +++ b/inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php @@ -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, @@ -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 ); diff --git a/inc/Engine/Media/AboveTheFold/WarmUp/Controller.php b/inc/Engine/Media/AboveTheFold/WarmUp/Controller.php index 3cf75fb066..7f463e3177 100644 --- a/inc/Engine/Media/AboveTheFold/WarmUp/Controller.php +++ b/inc/Engine/Media/AboveTheFold/WarmUp/Controller.php @@ -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 { @@ -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; } ); @@ -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; } diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php b/tests/Fixtures/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php index 5886e69681..0a2211646e 100644 --- a/tests/Fixtures/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php @@ -1,11 +1,12 @@ '; $html_no_valid_links = 'Goto TopClick Bait'; -$html_valid_links_among_valid_links = 'Goto TopClick BaitHello WorldAnother DayRich Dad Poor Dad'; +$html_valid_links_among_invalid_links = 'Goto TopClick BaitHello WorldAnother DayRich Dad Poor Dad'; $html_external_links = 'Hello WorldAnother DayRich Dad Poor Dad'; $html_valid_links_among_external_links = 'Hello WorldAnother DayRich Dad Poor DadHello WorldAnother DayRich Dad Poor Dad'; $html_links_without_duplicate = 'Hello WorldAnother DayRich Dad Poor DadHello WorldAnother DayRich Dad Poor DadBuy (He came to set the captives free) - Rebecca Brown'; $html_links_with_relative_url = 'Hello WorldAnother DayRich Dad Poor DadBuy (He came to set the captives free) - Rebecca Brown'; +$html_with_ten_links_and_home = 'Hello World 2Hello World 3Hello World 4Hello World 5Hello World 6Hello World 7Hello World 8Hello World 9Rich Dad Poor DadBuy (He came to set the captives free) - Rebecca BrownHome'; return [ 'shouldReturnEmptyWhenLicenseExpired' => [ @@ -55,7 +56,7 @@ ], 'expected' => [], ], - 'shouldReturnEmptyWithNoValidLinks' => [ + 'shouldReturnOnlyHomeWithNoValidLinks' => [ 'config' => [ 'license_expired' => false, 'headers' => [ @@ -70,7 +71,9 @@ ], ], ], - 'expected' => [], + 'expected' => [ + 'https://example.org', + ], ], 'shouldReturnValidLinksAmongInvalidLinks' => [ 'config' => [ @@ -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, ], @@ -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' => [ @@ -108,7 +112,9 @@ ], ], ], - 'expected' => [], + 'expected' => [ + 'https://example.org', + ], ], 'shouldReturnValidLinksAmongExternalLinks' => [ 'config' => [ @@ -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' => [ @@ -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' => [ @@ -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', ], ], ];