Skip to content

Commit

Permalink
Closes #6760: E_ERROR in 3.16.2: Argument #1 ($microseconds) must be …
Browse files Browse the repository at this point in the history
…of type int. In line 164 of PreloadUrl.php (#6766)
  • Loading branch information
jeawhanlee authored Jul 8, 2024
1 parent 45ac1fd commit 3734e33
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
11 changes: 9 additions & 2 deletions inc/Engine/Preload/Controller/PreloadUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,19 @@ public function preload_url( string $url ) {
$duration = ( microtime( true ) - $start ); // Duration of the request.
}

$default_delay_between = 500000;

/**
* Filter the delay between each preload request.
*
* @param float $delay_between the defined delay.
* @param int $delay_between the defined delay.
*/
$delay_between = apply_filters( 'rocket_preload_delay_between_requests', 500000 );
$delay_between = apply_filters( 'rocket_preload_delay_between_requests', $default_delay_between );
$delay_between = absint( $delay_between );

if ( empty( $delay_between ) ) {
$delay_between = $default_delay_between;
}

usleep( $delay_between );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,40 @@
],
],
],
'shouldStillPreloadOnlyOnceWhenRocketPreloadDelayBetweenRequestsIsAStringAndMobileCacheDisabledAndCheckDuration' => [
'config' => [
'transient_check_duration' => false,
'url' => 'url',
'cache_exists' => false,
'cache_mobile' => false,
'user_agent' => 'user_agent',
'rocket_preload_delay_between_requests' => "hello",
'request' => [
'config' => [
'blocking' => true,
'timeout' => 20,
'user-agent' => 'WP Rocket/Preload',
'sslverify' => false,
],
],
],
],
'shouldStillPreloadOnlyOnceWhenRocketPreloadDelayBetweenRequestsIsAFloatAndMobileCacheDisabledAndCheckDuration' => [
'config' => [
'transient_check_duration' => false,
'url' => 'url',
'cache_exists' => false,
'cache_mobile' => false,
'user_agent' => 'user_agent',
'rocket_preload_delay_between_requests' => 5.2,
'request' => [
'config' => [
'blocking' => true,
'timeout' => 20,
'user-agent' => 'WP Rocket/Preload',
'sslverify' => false,
],
],
],
],
];
11 changes: 11 additions & 0 deletions tests/Unit/inc/Engine/Preload/Controller/PreloadUrl/preloadUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace WP_Rocket\Tests\Unit\inc\Engine\Preload\Controller\PreloadUrl;

use Brain\Monkey\Functions;
use Brain\Monkey\Filters;
use Mockery;
use WP_Filesystem_Direct;
use WP_Rocket\Admin\Options_Data;
Expand Down Expand Up @@ -44,6 +45,7 @@ protected function setUp(): void {
* @dataProvider configTestData
*/
public function testShouldDoAsExpected( $config ) {

Functions\expect( 'get_transient' )
->atMost()
->once()
Expand Down Expand Up @@ -81,6 +83,7 @@ public function testShouldDoAsExpected( $config ) {

protected function expectDesktopRequest( $config ) {
if ( $config['cache_exists'] ) {

Functions\expect( 'wp_safe_remote_get' )
->with( $config['url'] . '/', $config['request']['config'] )
->never();
Expand All @@ -90,6 +93,14 @@ protected function expectDesktopRequest( $config ) {

Functions\expect( 'wp_safe_remote_get' )
->with( $config['url'] . '/', $config['request']['config'] );

$delay_value = 500000;

if ( isset( $config['rocket_preload_delay_between_requests'] ) ) {
$delay_value = $config['rocket_preload_delay_between_requests'];
}

Filters\expectApplied( 'rocket_preload_delay_between_requests' )->with( 500000 )->andReturn( $delay_value );
}

protected function expectMobileRequest( $config ) {
Expand Down

0 comments on commit 3734e33

Please sign in to comment.