Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logic to clean root files #5761

Merged
merged 25 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
88c7d22
Added logic to clean root files
CrochetFeve0251 Feb 15, 2023
265c96f
Fixed issue with the namespace from the WP_Filesystem_Direct
CrochetFeve0251 Feb 17, 2023
6407448
Fixed issue with the namespace from the WP_Filesystem_Direct
CrochetFeve0251 Feb 17, 2023
2c64a20
Merge branch 'develop' into bug/5611-wpml-clean-root-directory
vmanthos Mar 14, 2023
e11950d
Fixed code standards
CrochetFeve0251 Mar 15, 2023
be6e17f
Added logic to clear cache on cache from directory_for_default_language
CrochetFeve0251 Mar 20, 2023
eac9e06
Removed useless data tests
CrochetFeve0251 Mar 20, 2023
4924990
Merge branch 'develop' into bug/5611-wpml-clean-root-directory
vmanthos Mar 28, 2023
8dd7775
Prevent crash on $cache_folder_directory not being an array
CrochetFeve0251 Mar 28, 2023
b1b2c91
Merge remote-tracking branch 'origin/bug/5611-wpml-clean-root-directo…
CrochetFeve0251 Mar 28, 2023
b9040d8
Fixed code standards
CrochetFeve0251 Mar 28, 2023
3521768
Added a reset from preload when changing the option
CrochetFeve0251 Mar 30, 2023
ea6dc87
Merge branch 'develop' into bug/5611-wpml-clean-root-directory
CrochetFeve0251 Mar 30, 2023
e76cc3c
Fixed code standards
CrochetFeve0251 Mar 30, 2023
fba52bc
Added clearing when wpml is disabled or enabled
CrochetFeve0251 Apr 17, 2023
6200af3
Added reset preload to enabled logic
CrochetFeve0251 Apr 17, 2023
3ad6a77
Merge branch 'develop' into bug/5611-wpml-clean-root-directory
vmanthos Apr 19, 2023
243189f
Fixed activation issue
CrochetFeve0251 Apr 19, 2023
acb7a78
Merge remote-tracking branch 'origin/bug/5611-wpml-clean-root-directo…
CrochetFeve0251 Apr 19, 2023
5fa456d
Fixed code standards
CrochetFeve0251 Apr 19, 2023
e5dbcd7
Merge branch 'develop' into bug/5611-wpml-clean-root-directory
vmanthos May 15, 2023
feb0872
Added clean cache on enabled/disable
CrochetFeve0251 May 16, 2023
d7dbad4
Merge branch 'develop' into bug/5611-wpml-clean-root-directory
vmanthos May 18, 2023
12321bc
Fixed QA review
CrochetFeve0251 May 22, 2023
ce96f94
Merge branch 'develop' into bug/5611-wpml-clean-root-directory
vmanthos Jun 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions inc/ThirdParty/Plugins/I18n/WPML.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WP_Rocket\ThirdParty\Plugins\I18n;

use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Tests\Fixtures\WP_Filesystem_Direct;
CrochetFeve0251 marked this conversation as resolved.
Show resolved Hide resolved
use WP_Rocket\ThirdParty\ReturnTypesTrait;

/**
Expand All @@ -11,6 +12,23 @@
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.
*
Expand All @@ -25,6 +43,8 @@ public static function get_subscribed_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',
'after_rocket_clean_home' => 'remove_root_cached_files',
'after_rocket_clean_domain' => 'remove_root_cached_files',
];

return $events;
Expand Down Expand Up @@ -100,4 +120,22 @@ 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 );
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 );
}
}
}
Original file line number Diff line number Diff line change
@@ -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',
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

use Mockery;
use WP_Rocket\Tests\Fixtures\WP_Filesystem_Direct;
use WP_Rocket\Tests\Unit\TestCase;
use WP_Rocket\ThirdParty\Plugins\I18n\WPML;
use Brain\Monkey\Functions;
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();
}
}