Skip to content

Commit

Permalink
Closes #5549 Clear cache table on domain change (#6696)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabrisrp authored Jun 20, 2024
1 parent 1b3b8a5 commit bc4f606
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 45 deletions.
57 changes: 48 additions & 9 deletions inc/Engine/Preload/Admin/Settings.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Preload\Admin;

use WP_Rocket\Engine\Preload\Controller\PreloadUrl;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Preload\Controller\{LoadInitialSitemap, PreloadUrl};
use WP_Rocket\Engine\Preload\Database\Tables\Cache as CacheTable;

class Settings {

/**
* Instance of options handler.
*
* @var Options_Data
*/
protected $options;
private $options;

/**
* PreloadUrl instance
Expand All @@ -21,15 +22,33 @@ class Settings {
*/
private $preload_url;

/**
* LoadInitialSitemap instance
*
* @var LoadInitialSitemap
*/
private $load_initial_sitemap;

/**
* CacheTable instance
*
* @var CacheTable
*/
private $cache_table;

/**
* Instantiate the class
*
* @param Options_Data $options Instance of options handler.
* @param PreloadUrl $preload_url PreloadUrl instance.
* @param Options_Data $options Instance of options handler.
* @param PreloadUrl $preload_url PreloadUrl instance.
* @param LoadInitialSitemap $load_initial_sitemap LoadInitialSitemap instance.
* @param CacheTable $cache_table CacheTable instance.
*/
public function __construct( Options_Data $options, PreloadUrl $preload_url ) {
$this->options = $options;
$this->preload_url = $preload_url;
public function __construct( Options_Data $options, PreloadUrl $preload_url, LoadInitialSitemap $load_initial_sitemap, CacheTable $cache_table ) {
$this->options = $options;
$this->preload_url = $preload_url;
$this->load_initial_sitemap = $load_initial_sitemap;
$this->cache_table = $cache_table;
}

/**
Expand Down Expand Up @@ -102,11 +121,31 @@ public function is_enabled(): bool {
}

/**
* Preload the homepage
* Preload the homepage.
*
* @return void
*/
public function preload_homepage() {
if ( ! $this->is_enabled() ) {
return;
}

$this->preload_url->preload_url( home_url() );
}

/**
* Clear cache table and preload.
*
* @return void
*/
public function clear_and_preload() {
$this->cache_table->truncate_cache_table();

if ( ! $this->is_enabled() ) {
return;
}

$this->load_initial_sitemap->load_initial_sitemap();
$this->preload_url->preload_url( home_url() );
}
}
36 changes: 15 additions & 21 deletions inc/Engine/Preload/Admin/Subscriber.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Preload\Admin;

use stdClass;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Preload\Controller\ClearCache;

use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Logger\Logger;
use WP_Rocket\Engine\Admin\Settings\Settings as AdminSettings;

class Subscriber implements Subscriber_Interface {

/**
* Options instance.
*
* @var Options_Data
*/
protected $options;

/**
* Settings instance.
*
Expand All @@ -28,12 +15,10 @@ class Subscriber implements Subscriber_Interface {

/**
* Creates an instance of the class.
*
* @param Options_Data $options Options instance.
* @param Settings $settings Settings instance.
* @param Settings $settings Settings instance.
*/
public function __construct( Options_Data $options, Settings $settings ) {
$this->options = $options;
public function __construct( Settings $settings ) {
$this->settings = $settings;
}

Expand All @@ -50,7 +35,7 @@ public static function get_subscribed_events() {
'rocket_options_changed' => 'preload_homepage',
'switch_theme' => 'preload_homepage',
'rocket_after_clean_used_css' => 'preload_homepage',
'rocket_domain_options_changed' => 'preload_homepage',
'rocket_domain_options_changed' => 'clear_and_preload',
'rocket_input_sanitize' => 'sanitize_options',
'wp_rocket_upgrade' => [ 'maybe_clean_cron', 15, 2 ],
];
Expand All @@ -66,14 +51,23 @@ public function maybe_display_preload_notice() {
}

/**
* Preload the homepage after changing the settings
* Preload the homepage
*
* @return void
*/
public function preload_homepage() {
$this->settings->preload_homepage();
}

/**
* Clear the cache table and preload
*
* @return void
*/
public function clear_and_preload() {
$this->settings->clear_and_preload();
}

/**
* Sanitizes Preload Excluded URI option when saving the settings
*
Expand Down
14 changes: 14 additions & 0 deletions inc/Engine/Preload/Database/Tables/Cache.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Preload\Database\Tables;

Expand Down Expand Up @@ -96,4 +97,17 @@ public function add_is_locked_column() {

return $this->is_success( $created );
}

/**
* Truncate cache table.
*
* @return bool
*/
public function truncate_cache_table(): bool {
if ( ! $this->exists() ) {
return false;
}

return $this->truncate();
}
}
5 changes: 3 additions & 2 deletions inc/Engine/Preload/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public function register(): void {

$this->getContainer()->add( 'preload_settings', Settings::class )
->addArgument( $options )
->addArgument( $preload_url_controller );
->addArgument( $preload_url_controller )
->addArgument( $this->getContainer()->get( 'load_initial_sitemap_controller' ) )
->addArgument( $this->getContainer()->get( 'preload_caches_table' ) );

$preload_settings = $this->getContainer()->get( 'preload_settings' );

Expand Down Expand Up @@ -161,7 +163,6 @@ public function register(): void {
->addTag( 'common_subscriber' );

$this->getContainer()->add( 'preload_admin_subscriber', AdminSubscriber::class )
->addArgument( $options )
->addArgument( $preload_settings )
->addTag( 'common_subscriber' );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [
'testShouldOnlyTruncateWhenPreloadDisabled' => [
'config' => [
'options' => [
'manual_preload' => 0,
],
],
'expected' => false,
],
'testShouldPreloadHomepageAndSitemap' => [
'config' => [
'options' => [
'manual_preload' => 1,
],
],
'expected' => true,
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [
'testShouldDoNothingWhenPreloadDisabled' => [
'config' => [
'options' => [
'manual_preload' => 0,
],
],
'expected' => false,
],
'testShouldPreloadHomepage' => [
'config' => [
'options' => [
'manual_preload' => 1,
],
],
'expected' => true,
],
];
68 changes: 68 additions & 0 deletions tests/Unit/inc/Engine/Preload/Admin/Settings/clearAndPreload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Tests\Unit\inc\Engine\Preload\Admin\Settings;

use Brain\Monkey\Functions;
use Mockery;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Preload\Admin\Settings;
use WP_Rocket\Engine\Preload\Controller\{LoadInitialSitemap, PreloadUrl};
use WP_Rocket\Engine\Preload\Database\Tables\Cache as CacheTable;
use WP_Rocket\Tests\Unit\TestCase;

/**
* Test class covering \WP_Rocket\Engine\Preload\Admin\Settings::clear_and_preload
*
* @group Preload
*/
class TestClearAndPreload extends TestCase {
private $settings;
private $options;
private $preload_url;
private $sitemap;
private $cache;

protected function setUp(): void {
parent::setUp();

$this->options = Mockery::mock( Options_Data::class );
$this->preload_url = Mockery::mock( PreloadUrl::class );
$this->sitemap = Mockery::mock( LoadInitialSitemap::class );
$this->cache = $this->createMock( CacheTable::class );
$this->settings = new Settings(
$this->options,
$this->preload_url,
$this->sitemap,
$this->cache
);
}

/**
* @dataProvider configTestData
*/
public function testShouldDoExpected( $config, $expected ) {
Functions\when( 'home_url' )->justReturn( 'http://example.org/' );

$this->cache->expects( $this->once() )
->method( 'truncate_cache_table' );

$this->options->shouldReceive( 'get' )
->with( 'manual_preload', 0 )
->andReturn( $config['options']['manual_preload'] );

if ( $expected ) {
$this->sitemap->shouldReceive( 'load_initial_sitemap' )
->once();
$this->preload_url->shouldReceive( 'preload_url' )
->once();
} else {
$this->sitemap->shouldReceive( 'load_initial_sitemap' )
->never();
$this->preload_url->shouldReceive( 'preload_url' )
->never();
}

$this->settings->clear_and_preload();
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,58 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Tests\Unit\inc\Engine\Preload\Admin\Settings;

use Brain\Monkey\Functions;
use Mockery;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Preload\Admin\Settings;
use WP_Rocket\Engine\Preload\Controller\PreloadUrl;
use WP_Rocket\Engine\Preload\Controller\{LoadInitialSitemap, PreloadUrl};
use WP_Rocket\Engine\Preload\Database\Tables\Cache as CacheTable;
use WP_Rocket\Tests\Unit\TestCase;

/**
* Test class covering \WP_Rocket\Engine\Preload\Admin\Settings::maybe_display_preload_notice
* @group Preload
*
* @group Preload
*/
class Test_MaybeDisplayPreloadNotice extends TestCase {
protected $settings;
protected $options;

protected function setUp(): void
{
protected function setUp(): void {
parent::setUp();
$this->options = Mockery::mock(Options_Data::class);
$this->settings = new Settings($this->options, Mockery::mock(PreloadUrl::class));

$this->options = Mockery::mock( Options_Data::class );
$this->settings = new Settings(
$this->options,
Mockery::mock( PreloadUrl::class ),
Mockery::mock( LoadInitialSitemap::class ),
$this->createMock( CacheTable::class )
);

$this->stubTranslationFunctions();
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected ) {
Functions\when('get_current_screen')->justReturn( $config['screen'] );
Functions\when('current_user_can')->justReturn( $config['has_right'] );
Functions\when('get_current_user_id')->justReturn( 1 );
Functions\when('get_user_meta')->justReturn([]);
Functions\when( 'get_current_screen' )->justReturn( $config['screen'] );
Functions\when( 'current_user_can' )->justReturn( $config['has_right'] );
Functions\when( 'get_current_user_id' )->justReturn( 1 );
Functions\when( 'get_user_meta' )->justReturn( [] );

Functions\expect( 'rocket_dismiss_box' )
->with( 'preload_notice' )
->atMost()
->once();

$this->options->allows()->get('manual_preload', 0)->andReturns($config['enabled']);
Functions\when('get_transient')->justReturn($config['transient']);
$this->options->allows()->get( 'manual_preload', 0 )->andReturns( $config['enabled'] );
Functions\when( 'get_transient' )->justReturn( $config['transient'] );

if ( $expected ) {
Functions\expect('rocket_notice_html')->once()->with($expected['notice']);
Functions\expect( 'rocket_notice_html' )->once()->with( $expected['notice'] );
} else {
Functions\expect('rocket_notice_html')->never();
}
Expand Down
Loading

0 comments on commit bc4f606

Please sign in to comment.