Skip to content

Commit

Permalink
Closes #6278: Do not add URLs to the wpr_rocket_cache table if Preloa…
Browse files Browse the repository at this point in the history
…d is disabled (#6305)

Co-authored-by: Rémy Perona <[email protected]>
  • Loading branch information
2 people authored and wordpressfan committed Mar 15, 2024
1 parent eb98fec commit 0a13971
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 4 deletions.
20 changes: 18 additions & 2 deletions inc/Engine/Preload/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ public function update_cache_row() {
return;
}

if ( (bool) ! $this->options->get( 'manual_preload', true ) ) {
return; // Bail out if preload is disabled.
}

$url = home_url( add_query_arg( [], $wp->request ) );

$detected = $this->mobile_detect->isMobile() && ! $this->mobile_detect->isTablet() ? 'mobile' : 'desktop';
Expand Down Expand Up @@ -239,6 +243,10 @@ public function delete_url_on_not_found() {
public function on_permalink_changed() {
$this->query->remove_all();
$this->queue->cancel_pending_jobs();
if ( ! $this->options->get( 'manual_preload', false ) ) {
return;
}

$this->queue->add_job_preload_job_load_initial_sitemap_async();
}

Expand Down Expand Up @@ -273,7 +281,9 @@ public function on_deactivation() {
* @return void
*/
public function clean_url( string $url ) {

if ( ! $this->options->get( 'manual_preload', 0 ) ) {
return;
}
$this->clear_cache->partial_clean( [ $url ] );
}

Expand All @@ -297,6 +307,10 @@ public function clean_full_cache() {
* @return void
*/
public function clean_partial_cache( $object, array $urls, $lang ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound
if ( ! $this->options->get( 'manual_preload', false ) ) {
return;
}

// Add Homepage URL to $purge_urls for preload.
$urls[] = get_rocket_i18n_home_url( $lang );

Expand All @@ -311,6 +325,9 @@ public function clean_partial_cache( $object, array $urls, $lang ) { // phpcs:ig
* @return void
*/
public function clean_urls( array $urls ) {
if ( ! $this->options->get( 'manual_preload', 0 ) ) {
return;
}

$this->clear_cache->partial_clean( $urls );
}
Expand Down Expand Up @@ -466,7 +483,6 @@ public function add_preload_excluded_uri( $regexes ): array {
* @return void
*/
public function remove_private_post( string $new_status, string $old_status, $post ) {

if ( $new_status === $old_status ) {
return;
}
Expand Down
23 changes: 22 additions & 1 deletion tests/Fixtures/inc/Engine/Preload/Subscriber/updateCacheRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'testCallActionWhenPreloaded' => [
'config' => [
'regexes' => [],
'manual_preload' => true,
'links' => [
[
'url' => 'http://example.org',
Expand All @@ -25,6 +26,7 @@
'testNoCallActionWhenNotPreloaded' => [
'config' => [
'regexes' => [],
'manual_preload' => true,
'links' => [
[
'url' => 'http://example.org',
Expand All @@ -49,6 +51,7 @@
'regexes' => [
'(.*)example.org(.*)'
],
'manual_preload' => true,
'links' => [
[
'url' => 'http://example.org',
Expand All @@ -66,5 +69,23 @@
],
]
]
]
],
'testShouldBailOutWHenPreloadDisabled' => [
'config' => [
'regexes' => [],
'manual_preload' => false,
'links' => [
],
'is_preloaded' => false,
],
'expected' => [
'url' => 'http://example.org',
'exists' => false,
'links' => [
[
'url' => 'http://example.org',
],
],
],
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function set_up()
{
parent::set_up();
add_filter('rocket_preload_exclude_urls', [$this, 'excluded']);
add_filter('pre_get_rocket_option_manual_preload', [$this, 'manual_preload']);
}

public static function tear_down_after_class()
Expand All @@ -33,6 +34,7 @@ public static function tear_down_after_class()
public function tear_down()
{
remove_filter('rocket_preload_exclude_urls', [$this, 'excluded']);
remove_filter('pre_get_rocket_option_manual_preload', [$this, 'rucss']);
parent::tear_down();
}

Expand All @@ -48,6 +50,7 @@ public function testShouldDoAsExpected($config, $expected) {

do_action('rocket_after_process_buffer');


if($config['is_preloaded']) {
$this->assertGreaterThan( 0, did_action('rocket_preload_completed') );
}
Expand All @@ -64,4 +67,8 @@ public function providerTestData() {
public function excluded($regexes): array {
return array_merge($regexes, $this->config['regexes']);
}

public function manual_preload() {
return $this->config['manual_preload'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @group ThirdParty
* @group Smush
* @group WithSmush
* @requires PHP >= 7.4
*/
class Test_IsSmushIframesLazyloadActive extends SmushSubscriberTestCase {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @group ThirdParty
* @group Smush
* @group WithSmush
* @requires PHP >= 7.4
*/
class Test_IsSmushLazyloadActive extends SmushSubscriberTestCase {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @group ThirdParty
* @group Smush
* @group WithSmush
* @requires PHP >= 7.4
*/
class Test_MaybeDeactivateRocketLazyload extends SmushSubscriberTestCase {
private $option_hook_prefix = 'pre_get_rocket_option_';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function setUp(): void
}

public function testShouldDoAsExpected() {
$this->queue->expects()->add_job_preload_job_load_initial_sitemap_async();
$this->options->expects()->get('manual_preload', false)->andReturn(false);
$this->query->expects(self::once())->method('remove_all');
$this->queue->expects()->cancel_pending_jobs();
$this->subscriber->on_permalink_changed();
Expand Down

0 comments on commit 0a13971

Please sign in to comment.