Filesystem.php
3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Optimization\RUCSS\Controller;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class Filesystem {
/**
* WP Filesystem instance
*
* @var WP_Filesystem_Direct
*/
private $filesystem;
/**
* Path to the used CSS storage
*
* @var string
*/
private $path;
/**
* Instantiate the class
*
* @param string $base_path Base path to the used CSS storage.
* @param WP_Filesystem_Direct $filesystem WP Filesystem instance.
*/
public function __construct( $base_path, $filesystem = null ) {
$this->filesystem = is_null( $filesystem ) ? rocket_direct_filesystem() : $filesystem;
$this->path = $base_path . get_current_blog_id() . '/';
}
/**
* Get the file path for the used CSS file.
*
* @param string $hash Hash of the file contents.
*
* @return string Path for the used CSS file.
*/
private function get_usedcss_full_path( string $hash ) {
return $this->path . $this->hash_to_path( $hash ) . '.css' . ( function_exists( 'gzdecode' ) ? '.gz' : '' );
}
/**
* Gets the used CSS content corresponding to the provided hash
*
* @param string $hash Hash of the corresponding used CSS.
*
* @return string
*/
public function get_used_css( string $hash ): string {
$file = $this->get_usedcss_full_path( $hash );
if ( ! $this->filesystem->exists( $file ) ) {
return '';
}
$file_contents = $this->filesystem->get_contents( $file );
$css = function_exists( 'gzdecode' ) ? gzdecode( $file_contents ) : $file_contents;
if ( ! $css ) {
return '';
}
return $css;
}
/**
* Writes the used CSS to the filesystem
*
* @param string $hash Hash to use for the filename.
* @param string $used_css Used CSS content.
*
* @return bool
*/
public function write_used_css( string $hash, string $used_css ): bool {
$file = $this->get_usedcss_full_path( $hash );
if ( ! rocket_mkdir_p( dirname( $file ) ) ) {
return false;
}
// This filter is documented in inc/classes/Buffer/class-cache.php.
$css = function_exists( 'gzencode' ) ? gzencode( $used_css, apply_filters( 'rocket_gzencode_level_compression', 6 ) ) : $used_css;
if ( ! $css ) {
return false;
}
return $this->filesystem->put_contents( $file, $css, rocket_get_filesystem_perms( 'file' ) );
}
/**
* Deletes the used CSS files for the corresponding hash
*
* @since 3.11.4
*
* @param string $hash md5 hash string.
*
* @return bool
*/
public function delete_used_css( string $hash ): bool {
$file = $this->get_usedcss_full_path( $hash );
return $this->filesystem->delete( $file, false, 'f' );
}
/**
* Deletes all the used CSS files
*
* @since 3.11.4
*
* @return void
*/
public function delete_all_used_css() {
try {
$dir = new RecursiveDirectoryIterator( $this->path, \FilesystemIterator::SKIP_DOTS );
$items = new RecursiveIteratorIterator( $dir, RecursiveIteratorIterator::CHILD_FIRST );
foreach ( $items as $item ) {
$this->filesystem->delete( $item );
}
} catch ( \Exception $e ) {
return;
}
}
/**
* Checks if the used CSS storage folder is writable
*
* @since 3.11.4
*
* @return bool
*/
public function is_writable_folder() {
if ( ! $this->filesystem->exists( $this->path ) ) {
rocket_mkdir_p( $this->path );
}
return $this->filesystem->is_writable( $this->path );
}
/**
* Converts hash to path with filtered number of levels
*
* @since 3.11.4
*
* @param string $hash md5 hash string.
*
* @return string
*/
private function hash_to_path( string $hash ): string {
/**
* Filters the number of sub-folders level to create for used CSS storage
*
* @since 3.11.4
*
* @param int $levels Number of levels.
*/
$levels = apply_filters( 'rocket_used_css_dir_level', 3 );
$base = substr( $hash, 0, $levels );
$remain = substr( $hash, $levels );
$path_array = str_split( $base );
$path_array[] = $remain;
return implode( '/', $path_array );
}
}