Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/Engine/CriticalPath/DataManager.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
3
+
namespace WP_Rocket\Engine\CriticalPath;
4
+
5
+
use WP_Error;
6
+
use WP_Filesystem_Direct;
7
+
use WP_Rocket\Engine\Optimization\CSSTrait;
8
+
9
+
/**
10
+
* Class DataManager
11
+
*
12
+
* @package WP_Rocket\Engine\CriticalPath
13
+
*/
14
+
class DataManager {
15
+
use CSSTrait;
16
+
17
+
/**
18
+
* Base critical CSS path for posts.
19
+
*
20
+
* @var string
21
+
*/
22
+
private $critical_css_path;
23
+
24
+
/**
25
+
* Instance of the filesystem handler.
26
+
*
27
+
* @var WP_Filesystem_Direct
28
+
*/
29
+
private $filesystem;
30
+
31
+
/**
32
+
* DataManager constructor, adjust the critical css path for posts.
33
+
*
34
+
* @param string $critical_css_path path for main critical css folder.
35
+
* @param WP_Filesystem_Direct $filesystem Instance of the filesystem handler.
36
+
*/
37
+
public function __construct( $critical_css_path, $filesystem ) {
38
+
$this->critical_css_path = $critical_css_path . get_current_blog_id() . DIRECTORY_SEPARATOR;
39
+
$this->filesystem = $filesystem;
40
+
}
41
+
42
+
/**
43
+
* Save CPCSS into file.
44
+
*
45
+
* @since 3.6
46
+
*
47
+
* @param string $path Path for cpcss file related to this web page.
48
+
* @param string $cpcss CPCSS code to be saved.
49
+
* @param string $url URL for item to be used in error messages.
50
+
* @param bool $is_mobile If this is cpcss for mobile or not.
51
+
* @param string $item_type Optional. Type for this item if it's custom or specific type. Default: custom.
52
+
*
53
+
* @return bool|WP_Error
54
+
*/
55
+
public function save_cpcss( $path, $cpcss, $url, $is_mobile = false, $item_type = 'custom' ) {
56
+
$file_path_directory = dirname( $this->critical_css_path . $path );
57
+
58
+
if ( ! $this->filesystem->is_dir( $file_path_directory ) ) {
59
+
if ( ! rocket_mkdir_p( $file_path_directory ) ) {
60
+
61
+
return new WP_Error(
62
+
'cpcss_generation_failed',
63
+
// translators: %s = item URL.
64
+
sprintf(
65
+
$is_mobile
66
+
?
67
+
// translators: %s = item URL.
68
+
__( 'Critical CSS for %1$s on mobile not generated. Error: The destination folder could not be created.', 'rocket' )
69
+
:
70
+
// translators: %s = item URL.
71
+
__( 'Critical CSS for %1$s not generated. Error: The destination folder could not be created.', 'rocket' ),
72
+
( 'custom' === $item_type ) ? $url : $item_type
73
+
),
74
+
[
75
+
'status' => 400,
76
+
]
77
+
);
78
+
79
+
}
80
+
}
81
+
82
+
$cpcss = $this->apply_font_display_swap( $cpcss );
83
+
84
+
return rocket_put_content(
85
+
$this->critical_css_path . $path,
86
+
wp_strip_all_tags( $cpcss, true )
87
+
);
88
+
}
89
+
90
+
/**
91
+
* Delete critical css file by path.
92
+
*
93
+
* @param string $path Critical css file path to be deleted.
94
+
* @param bool $is_mobile If this is cpcss for mobile or not.
95
+
*
96
+
* @return bool|WP_Error
97
+
*/
98
+
public function delete_cpcss( $path, $is_mobile = false ) {
99
+
$full_path = $this->critical_css_path . $path;
100
+
101
+
if ( ! $this->filesystem->exists( $full_path ) ) {
102
+
return new WP_Error(
103
+
'cpcss_not_exists',
104
+
$is_mobile
105
+
?
106
+
__( 'Critical CSS file for mobile does not exist', 'rocket' )
107
+
:
108
+
__( 'Critical CSS file does not exist', 'rocket' ),
109
+
[
110
+
'status' => 400,
111
+
]
112
+
);
113
+
}
114
+
115
+
if ( ! $this->filesystem->delete( $full_path ) ) {
116
+
return new WP_Error(
117
+
'cpcss_deleted_failed',
118
+
$is_mobile
119
+
?
120
+
__( 'Critical CSS file for mobile cannot be deleted', 'rocket' )
121
+
:
122
+
__( 'Critical CSS file cannot be deleted', 'rocket' ),
123
+
[
124
+
'status' => 400,
125
+
]
126
+
);
127
+
}
128
+
129
+
return true;
130
+
}
131
+
132
+
/**
133
+
* Get job_id from cache based on item_url.
134
+
*
135
+
* @since 3.6
136
+
*
137
+
* @param string $item_url URL for item to be used in error messages.
138
+
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
139
+
*
140
+
* @return mixed
141
+
*/
142
+
public function get_cache_job_id( $item_url, $is_mobile = false ) {
143
+
$cache_key = $this->get_cache_key_from_url( $item_url, $is_mobile );
144
+
145
+
return get_transient( $cache_key );
146
+
}
147
+
148
+
/**
149
+
* Set Job_id for Item_url into cache.
150
+
*
151
+
* @since 3.6
152
+
*
153
+
* @param string $item_url URL for item to be used in error messages.
154
+
* @param string $job_id ID for the job to get details.
155
+
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
156
+
*
157
+
* @return bool
158
+
*/
159
+
public function set_cache_job_id( $item_url, $job_id, $is_mobile = false ) {
160
+
$cache_key = $this->get_cache_key_from_url( $item_url, $is_mobile );
161
+
162
+
return set_transient( $cache_key, $job_id, HOUR_IN_SECONDS );
163
+
}
164
+
165
+
/**
166
+
* Delete job_id from cache based on item_url.
167
+
*
168
+
* @since 3.6
169
+
*
170
+
* @param string $item_url URL for item to be used in error messages.
171
+
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
172
+
*
173
+
* @return bool
174
+
*/
175
+
public function delete_cache_job_id( $item_url, $is_mobile = false ) {
176
+
$cache_key = $this->get_cache_key_from_url( $item_url, $is_mobile );
177
+
178
+
return delete_transient( $cache_key );
179
+
}
180
+
181
+
/**
182
+
* Get cache key from url to be used in caching job_id.
183
+
*
184
+
* @since 3.6
185
+
*
186
+
* @param string $item_url URL for item to be used in error messages.
187
+
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
188
+
*
189
+
* @return string
190
+
*/
191
+
private function get_cache_key_from_url( $item_url, $is_mobile = false ) {
192
+
$encoded_url = md5( $item_url );
193
+
194
+
if ( $is_mobile ) {
195
+
$encoded_url .= '_mobile';
196
+
}
197
+
198
+
return 'rocket_specific_cpcss_job_' . $encoded_url;
199
+
}
200
+
}
201
+