Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Simple_Delete | |
100.00% |
16 / 16 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
| apply_to_path | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 | |||
| delete_dir | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| delete_file | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Path_Actions; |
| 4 | |
| 5 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Filesystem_Utils; |
| 6 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Logger; |
| 7 | use SplFileInfo; |
| 8 | |
| 9 | /** |
| 10 | * Delete a file or directory, non-recursively. |
| 11 | */ |
| 12 | class Simple_Delete implements Path_Action { |
| 13 | /** |
| 14 | * Delete a file or directory. |
| 15 | * |
| 16 | * @param SplFileInfo $file The file or directory to delete. |
| 17 | * @return int The number of files or directories deleted. |
| 18 | */ |
| 19 | public function apply_to_path( SplFileInfo $file ) { |
| 20 | if ( $file->isDir() && Filesystem_Utils::is_dir_empty( $file->getPathname() ) ) { |
| 21 | Logger::debug( 'rmdir: ' . $file->getPathname() ); |
| 22 | return $this->delete_dir( $file ); |
| 23 | } elseif ( $file->isFile() ) { |
| 24 | // Do not delete index.html files independently. We will only delete them when the directory is empty. |
| 25 | if ( $file->getFilename() === 'index.html' ) { |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | // Delete a file in the directory |
| 30 | Logger::debug( 'unlink: ' . $file->getPathname() ); |
| 31 | $this->delete_file( $file ); |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | private function delete_dir( SplFileInfo $file ) { |
| 39 | $count = 0; |
| 40 | if ( Filesystem_Utils::is_dir_empty( $file->getPathname() ) ) { |
| 41 | // An empty directory will still have an index.html file, which we will delete with the directory. |
| 42 | $count += Filesystem_Utils::delete_empty_dir( $file->getPathname() ); |
| 43 | } |
| 44 | |
| 45 | return $count; |
| 46 | } |
| 47 | |
| 48 | private function delete_file( SplFileInfo $file ) { |
| 49 | @unlink( $file->getPathname() ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink, WordPress.PHP.NoSilencedErrors.Discouraged |
| 50 | return true; |
| 51 | } |
| 52 | } |