Skip to content

Commit

Permalink
Added logic to clean root files (#5761)
Browse files Browse the repository at this point in the history
Co-authored-by: Vasilis Manthos <vmanthos@gmail.com>
  • Loading branch information
CrochetFeve0251 and vmanthos committed Jun 28, 2023
1 parent 252cead commit fee96d7
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 8 deletions.
3 changes: 2 additions & 1 deletion inc/Engine/Preload/Subscriber.php
Expand Up @@ -100,6 +100,7 @@ public static function get_subscribed_events() {
],
'rocket_after_process_buffer' => 'update_cache_row',
'rocket_deactivation' => 'on_deactivation',
'rocket_reset_preload' => 'on_permalink_changed',
'permalink_structure_changed' => 'on_permalink_changed',
'wp_rocket_upgrade' => [ 'on_update', 16, 2 ],
'rocket_rucss_complete_job_status' => 'clean_url',
Expand Down Expand Up @@ -236,7 +237,7 @@ public function delete_url_on_not_found() {
public function on_permalink_changed() {
$this->query->remove_all();
$this->queue->cancel_pending_jobs();
$this->controller->load_initial_sitemap();
$this->queue->add_job_preload_job_load_initial_sitemap_async();
}

/**
Expand Down
105 changes: 99 additions & 6 deletions inc/ThirdParty/Plugins/I18n/WPML.php
Expand Up @@ -3,6 +3,7 @@
namespace WP_Rocket\ThirdParty\Plugins\I18n;

use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Filesystem_Direct;
use WP_Rocket\ThirdParty\ReturnTypesTrait;

/**
Expand All @@ -11,22 +12,46 @@
class WPML implements Subscriber_Interface {
use ReturnTypesTrait;

/**
* Filesystem instance.
*
* @var WP_Filesystem_Direct
*/
protected $filesystem;

/**
* Instantiate class.
*
* @param WP_Filesystem_Direct $filesystem Filesystem instance.
*/
public function __construct( WP_Filesystem_Direct $filesystem = null ) {
$this->filesystem = $filesystem ?: rocket_direct_filesystem();
}


/**
* Events for subscriber to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
if ( ! defined( 'ICL_SITEPRESS_VERSION' ) ) {
return [];
}

$events = [
'rocket_rucss_is_home_url' => [ 'is_secondary_home', 10, 2 ],
'rocket_preload_all_to_pending_condition' => 'clean_only_right_domain',
'rocket_preload_sitemap_before_queue' => 'add_languages_sitemaps',
'activate_sitepress-multilingual-cms/sitepress.php' => 'maybe_clear_on_disable',
'deactivate_sitepress-multilingual-cms/sitepress.php' => 'maybe_clear_on_disable',
];

if ( ! defined( 'ICL_SITEPRESS_VERSION' ) ) {
return $events;
}

$events['rocket_rucss_is_home_url'] = [ 'is_secondary_home', 10, 2 ];
$events['rocket_preload_all_to_pending_condition'] = 'clean_only_right_domain';
$events['rocket_preload_sitemap_before_queue'] = 'add_languages_sitemaps';
$events['after_rocket_clean_home'] = 'remove_root_cached_files';
$events['after_rocket_clean_domain'] = 'remove_root_cached_files';
$events['pre_update_option_icl_sitepress_settings'] = [ 'on_change_directory_for_default_language_clean_cache', 10, 2 ];

return $events;
}

Expand Down Expand Up @@ -100,4 +125,72 @@ public function add_languages_sitemaps( $sitemaps ) {
}
return array_unique( $new_sitemaps );
}

/**
* Remove root files when WPML is active.
*
* @return void
*/
public function remove_root_cached_files() {
$site_url = home_url();
$host_name = wp_parse_url( $site_url, PHP_URL_HOST );
$cache_folder_path = _rocket_get_wp_rocket_cache_path() . $host_name . '/';
$cache_folder_directory = $this->filesystem->dirlist( $cache_folder_path );

if ( ! is_array( $cache_folder_directory ) || ! is_array( array_keys( $cache_folder_directory ) ) ) {
return;
}

foreach ( array_keys( $cache_folder_directory ) as $entry ) {
if ( $this->filesystem->is_dir( $cache_folder_path . $entry ) ) {
continue;
}
$this->filesystem->delete( $cache_folder_path . $entry );
}
}

/**
* Reset cache when changing the option.
*
* @param array $new new configurations.
* @param array $old old configurations.
*
* @return array
*/
public function on_change_directory_for_default_language_clean_cache( $new, $old ) {
if ( ! is_array( $old ) || ! is_array( $new ) ) {
return $new;
}

if ( ! key_exists( 'urls', $old ) || ! key_exists( 'directory_for_default_language', $old['urls'] ) || ! key_exists( 'urls', $new ) || ! key_exists( 'directory_for_default_language', $new['urls'] ) || $new['urls']['directory_for_default_language'] === $old['urls']['directory_for_default_language'] ) {
return $new;
}

/**
* Reset WP Rocket Preload.
*/
do_action( 'rocket_reset_preload' );
rocket_clean_domain();

return $new;
}

/**
* Clear the cache when the option language directory is enabled.
*
* @return void
*/
public function maybe_clear_on_disable() {
$option = get_option( 'icl_sitepress_settings' );
if ( ! $option || ! is_array( $option ) || ! key_exists( 'urls', $option ) || ! key_exists( 'directory_for_default_language', $option['urls'] ) || false === $option['urls']['directory_for_default_language'] ) {
return;
}

/**
* Reset WP Rocket Preload.
*/
do_action( 'rocket_reset_preload' );
rocket_clean_cache_dir();
rocket_clean_domain();
}
}
@@ -0,0 +1,58 @@
<?php
return [
'noFieldsShouldNotClean' => [
'config' => [
'old' => [

],
'new' => [

],
'should_clean' => false,
],
'expected' => [

]
],
'SameFieldsShouldNotClean' => [
'config' => [
'old' => [
'urls' => [
'directory_for_default_language' => false,
]
],
'new' => [
'urls' => [
'directory_for_default_language' => false,
]
],
'should_clean' => false,
],
'expected' => [
'urls' => [
'directory_for_default_language' => false,
]
]
],
'differentFieldsShouldClean' => [
'config' => [
'old' => [
'urls' => [
'directory_for_default_language' => false,
]
],
'new' => [
'urls' => [
'directory_for_default_language' => true,
]
],
'should_clean' => true,
],
'expected' => [
'urls' => [
'directory_for_default_language' => true,
]
]
],

];
@@ -0,0 +1,15 @@
<?php
return [
'removeFilesOnRoot' => [
'config' => [
'home_url' => 'https/example.org',
'host' => 'example.org',
'entries' => [
'entry_path' => [],
],
'path' => 'path',
'cache_path' => 'pathexample.org/',
'entry_path' => 'pathexample.org/entry_path',
],
],
];
Expand Up @@ -38,7 +38,7 @@ protected function setUp(): void
}

public function testShouldDoAsExpected() {
$this->controller->expects()->load_initial_sitemap();
$this->queue->expects()->add_job_preload_job_load_initial_sitemap_async();
$this->query->expects(self::once())->method('remove_all');
$this->queue->expects()->cancel_pending_jobs();
$this->subscriber->on_permalink_changed();
Expand Down
@@ -0,0 +1,48 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\ThirdParty\Plugins\I18n\WPML;

use WP_Rocket\ThirdParty\Plugins\I18n\WPML;
use Mockery;
use WP_Filesystem_Direct;


use WP_Rocket\Tests\Unit\TestCase;
use Brain\Monkey\Functions;

/**
* @covers \WP_Rocket\ThirdParty\Plugins\I18n\WPML::on_change_directory_for_default_language_clean_cache
*/
class Test_onChangeDirectoryForDefaultLanguageCleanCache extends TestCase {

/**
* @var WP_Filesystem_Direct
*/
protected $filesystem;

/**
* @var WPML
*/
protected $wpml;

public function set_up() {
parent::set_up();
$this->filesystem = Mockery::mock(WP_Filesystem_Direct::class);

$this->wpml = new WPML($this->filesystem);
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected )
{
if($config['should_clean']) {
Functions\expect('rocket_clean_domain');
} else {
Functions\expect('rocket_clean_domain')->never();
}
$this->assertSame($expected, $this->wpml->on_change_directory_for_default_language_clean_cache($config['new'], $config['old']));

}
}
@@ -0,0 +1,36 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\ThirdParty\Plugins\I18n\WPML;

use Mockery;
use WP_Filesystem_Direct;
use WP_Rocket\Tests\Unit\TestCase;
use WP_Rocket\ThirdParty\Plugins\I18n\WPML;
use Brain\Monkey\Functions;
use Brain\Monkey\Filters;

class Test_RemoveRootCachedFiles extends TestCase
{
protected $subscriber;
protected $filesystem;

protected function set_up()
{
parent::set_up();
$this->filesystem = Mockery::mock( WP_Filesystem_Direct::class );
$this->subscriber = new WPML($this->filesystem);
}

/**
* @dataProvider configTestData
*/
public function testShouldAsExpected( $config ) {
Functions\expect('home_url')->andReturn($config['home_url']);
Functions\expect('wp_parse_url')->with($config['home_url'], PHP_URL_HOST)->andReturn($config['host']);
Functions\expect('_rocket_get_wp_rocket_cache_path')->with($config['home_url'], PHP_URL_HOST)->andReturn($config['path']);
$this->filesystem->expects()->dirlist($config['cache_path'])->andReturn($config['entries']);
$this->filesystem->expects()->is_dir($config['entry_path'])->andReturn(false);
$this->filesystem->expects()->delete($config['entry_path']);
$this->subscriber->remove_root_cached_files();
}
}

0 comments on commit fee96d7

Please sign in to comment.