DataManager.php
5.11 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace WP_Rocket\Engine\CriticalPath;
use WP_Error;
use WP_Filesystem_Direct;
use WP_Rocket\Engine\Optimization\CSSTrait;
/**
* Class DataManager
*
* @package WP_Rocket\Engine\CriticalPath
*/
class DataManager {
use CSSTrait;
/**
* Base critical CSS path for posts.
*
* @var string
*/
private $critical_css_path;
/**
* Instance of the filesystem handler.
*
* @var WP_Filesystem_Direct
*/
private $filesystem;
/**
* DataManager constructor, adjust the critical css path for posts.
*
* @param string $critical_css_path path for main critical css folder.
* @param WP_Filesystem_Direct $filesystem Instance of the filesystem handler.
*/
public function __construct( $critical_css_path, $filesystem ) {
$this->critical_css_path = $critical_css_path . get_current_blog_id() . DIRECTORY_SEPARATOR;
$this->filesystem = $filesystem;
}
/**
* Save CPCSS into file.
*
* @since 3.6
*
* @param string $path Path for cpcss file related to this web page.
* @param string $cpcss CPCSS code to be saved.
* @param string $url URL for item to be used in error messages.
* @param bool $is_mobile If this is cpcss for mobile or not.
* @param string $item_type Optional. Type for this item if it's custom or specific type. Default: custom.
*
* @return bool|WP_Error
*/
public function save_cpcss( $path, $cpcss, $url, $is_mobile = false, $item_type = 'custom' ) {
$file_path_directory = dirname( $this->critical_css_path . $path );
if ( ! $this->filesystem->is_dir( $file_path_directory ) ) {
if ( ! rocket_mkdir_p( $file_path_directory ) ) {
return new WP_Error(
'cpcss_generation_failed',
// translators: %s = item URL.
sprintf(
$is_mobile
?
// translators: %s = item URL.
__( 'Critical CSS for %1$s on mobile not generated. Error: The destination folder could not be created.', 'rocket' )
:
// translators: %s = item URL.
__( 'Critical CSS for %1$s not generated. Error: The destination folder could not be created.', 'rocket' ),
( 'custom' === $item_type ) ? $url : $item_type
),
[
'status' => 400,
]
);
}
}
$cpcss = $this->apply_font_display_swap( $cpcss );
return rocket_put_content(
$this->critical_css_path . $path,
wp_strip_all_tags( $cpcss, true )
);
}
/**
* Delete critical css file by path.
*
* @param string $path Critical css file path to be deleted.
* @param bool $is_mobile If this is cpcss for mobile or not.
*
* @return bool|WP_Error
*/
public function delete_cpcss( $path, $is_mobile = false ) {
$full_path = $this->critical_css_path . $path;
if ( ! $this->filesystem->exists( $full_path ) ) {
return new WP_Error(
'cpcss_not_exists',
$is_mobile
?
__( 'Critical CSS file for mobile does not exist', 'rocket' )
:
__( 'Critical CSS file does not exist', 'rocket' ),
[
'status' => 400,
]
);
}
if ( ! $this->filesystem->delete( $full_path ) ) {
return new WP_Error(
'cpcss_deleted_failed',
$is_mobile
?
__( 'Critical CSS file for mobile cannot be deleted', 'rocket' )
:
__( 'Critical CSS file cannot be deleted', 'rocket' ),
[
'status' => 400,
]
);
}
return true;
}
/**
* Get job_id from cache based on item_url.
*
* @since 3.6
*
* @param string $item_url URL for item to be used in error messages.
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
*
* @return mixed
*/
public function get_cache_job_id( $item_url, $is_mobile = false ) {
$cache_key = $this->get_cache_key_from_url( $item_url, $is_mobile );
return get_transient( $cache_key );
}
/**
* Set Job_id for Item_url into cache.
*
* @since 3.6
*
* @param string $item_url URL for item to be used in error messages.
* @param string $job_id ID for the job to get details.
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
*
* @return bool
*/
public function set_cache_job_id( $item_url, $job_id, $is_mobile = false ) {
$cache_key = $this->get_cache_key_from_url( $item_url, $is_mobile );
return set_transient( $cache_key, $job_id, HOUR_IN_SECONDS );
}
/**
* Delete job_id from cache based on item_url.
*
* @since 3.6
*
* @param string $item_url URL for item to be used in error messages.
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
*
* @return bool
*/
public function delete_cache_job_id( $item_url, $is_mobile = false ) {
$cache_key = $this->get_cache_key_from_url( $item_url, $is_mobile );
return delete_transient( $cache_key );
}
/**
* Get cache key from url to be used in caching job_id.
*
* @since 3.6
*
* @param string $item_url URL for item to be used in error messages.
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
*
* @return string
*/
private function get_cache_key_from_url( $item_url, $is_mobile = false ) {
$encoded_url = md5( $item_url );
if ( $is_mobile ) {
$encoded_url .= '_mobile';
}
return 'rocket_specific_cpcss_job_' . $encoded_url;
}
}