Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/Engine/Cache/Purge.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
3
+
namespace WP_Rocket\Engine\Cache;
4
+
5
+
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
6
+
use DirectoryIterator;
7
+
use Exception;
8
+
use WP_Filesystem_Direct;
9
+
use WP_Term;
10
+
use WP_Post;
11
+
12
+
/**
13
+
* Cache Purge handling class
14
+
*/
15
+
class Purge {
16
+
/**
17
+
* Filesystem instance
18
+
*
19
+
* @var WP_Filesystem_Direct
20
+
*/
21
+
private $filesystem;
22
+
23
+
/**
24
+
* Cache Query
25
+
*
26
+
* @var Cache
27
+
*/
28
+
protected $query;
29
+
30
+
/**
31
+
* Initialize the class.
32
+
*
33
+
* @param WP_Filesystem_Direct $filesystem Filesystem instance.
34
+
* @param Cache $query Cache query instance.
35
+
*/
36
+
public function __construct( $filesystem, Cache $query ) {
37
+
$this->filesystem = $filesystem;
38
+
$this->query = $query;
39
+
}
40
+
41
+
/**
42
+
* Purges cache for the dates archives of a post
43
+
*
44
+
* @param WP_Post $post Post object.
45
+
* @return void
46
+
*/
47
+
public function purge_dates_archives( $post ) {
48
+
foreach ( $this->get_dates_archives( $post ) as $url ) {
49
+
$this->purge_url( $url, true );
50
+
}
51
+
}
52
+
53
+
/**
54
+
* Purge URL cache.
55
+
*
56
+
* @param string $url URL to be purged.
57
+
* @param boolean $pagination Purge also pagination.
58
+
* @return void
59
+
*/
60
+
public function purge_url( string $url, $pagination = false ) {
61
+
global $wp_rewrite;
62
+
63
+
$parsed_url = $this->parse_url( $url );
64
+
65
+
foreach ( _rocket_get_cache_dirs( $parsed_url['host'] ) as $dir ) {
66
+
$path = $dir . $parsed_url['path'];
67
+
68
+
if ( ! $this->filesystem->exists( $path ) ) {
69
+
continue;
70
+
}
71
+
72
+
foreach ( $this->get_iterator( $path ) as $item ) {
73
+
74
+
if ( $item->isFile() ) {
75
+
$this->filesystem->delete( $item->getPathname() );
76
+
continue;
77
+
}
78
+
79
+
if ( str_contains( $item->getPathname(), '#' ) ) {
80
+
$this->filesystem->delete( $item->getPathname(), true );
81
+
}
82
+
}
83
+
84
+
if ( $pagination ) {
85
+
$this->maybe_remove_dir( $path . DIRECTORY_SEPARATOR . $wp_rewrite->pagination_base );
86
+
}
87
+
}
88
+
}
89
+
90
+
/**
91
+
* Gets the dates archives URLs for the provided post
92
+
*
93
+
* @param WP_Post $post Post object.
94
+
* @return array
95
+
*/
96
+
private function get_dates_archives( $post ) {
97
+
$time = get_the_time( 'Y-m-d', $post );
98
+
99
+
if ( empty( $time ) ) {
100
+
return [];
101
+
}
102
+
103
+
$date = explode( '-', $time );
104
+
$urls = [
105
+
get_year_link( $date[0] ),
106
+
get_month_link( $date[0], $date[1] ),
107
+
get_day_link( $date[0], $date[1], $date[2] ),
108
+
];
109
+
110
+
/**
111
+
* Filter the list of dates URLs.
112
+
*
113
+
* @since 1.1.0
114
+
*
115
+
* @param array $urls List of dates URLs.
116
+
*/
117
+
return (array) apply_filters( 'rocket_post_dates_urls', $urls );
118
+
}
119
+
120
+
/**
121
+
* Parses URL and return the parts array
122
+
*
123
+
* @since 3.6.1
124
+
*
125
+
* @param string $url URL to parse.
126
+
* @return array
127
+
*/
128
+
private function parse_url( $url ) {
129
+
$parsed_url = get_rocket_parse_url( $url );
130
+
131
+
/** This filter is documented in inc/front/htaccess.php */
132
+
if ( apply_filters( 'rocket_url_no_dots', false ) ) {
133
+
$parsed_url['host'] = str_replace( '.', '_', $parsed_url['host'] );
134
+
}
135
+
136
+
return $parsed_url;
137
+
}
138
+
139
+
/**
140
+
* Gets the iterator for the given path
141
+
*
142
+
* @since 3.6.1
143
+
*
144
+
* @param string $path Absolute path.
145
+
* @return DirectoryIterator|array
146
+
*/
147
+
private function get_iterator( $path ) {
148
+
try {
149
+
$iterator = new DirectoryIterator( $path );
150
+
} catch ( Exception $e ) {
151
+
// No action required, as logging not enabled.
152
+
$iterator = [];
153
+
}
154
+
155
+
return $iterator;
156
+
}
157
+
158
+
/**
159
+
* Recursively remove the provided directory and its content
160
+
*
161
+
* @since 3.6.1
162
+
*
163
+
* @param string $dir Absolute path for the directory.
164
+
* @return void
165
+
*/
166
+
private function maybe_remove_dir( $dir ) {
167
+
if ( $this->filesystem->is_dir( $dir ) ) {
168
+
rocket_rrmdir( $dir, [], $this->filesystem );
169
+
}
170
+
}
171
+
172
+
/**
173
+
* Purge all terms archives urls associated to a specific post.
174
+
*
175
+
* @since 3.6.1
176
+
*
177
+
* @param WP_Post $post Post object.
178
+
*/
179
+
public function purge_post_terms_urls( WP_Post $post ) {
180
+
$urls = $this->get_post_terms_urls( $post );
181
+
foreach ( $urls as $url ) {
182
+
$this->purge_url( $url, true );
183
+
}
184
+
/**
185
+
* Action to preload urls after cleaning cache.
186
+
*
187
+
* @param array $urls urls to preload.
188
+
*/
189
+
do_action( 'rocket_after_clean_terms', $urls );
190
+
}
191
+
192
+
/**
193
+
* Get all terms archives urls associated to a specific post.
194
+
*
195
+
* @since 3.6.1
196
+
*
197
+
* @param WP_Post $post Post object.
198
+
*
199
+
* @return array $urls List of taxonomies URLs
200
+
*/
201
+
private function get_post_terms_urls( WP_Post $post ) {
202
+
$urls = [];
203
+
$taxonomies = get_object_taxonomies( get_post_type( $post->ID ), 'objects' );
204
+
/**
205
+
* Filters the taxonomies excluded from post purge
206
+
*
207
+
* @since 3.9.1
208
+
*
209
+
* @param array $excluded_taxonomies Array of excluded taxonomies names.
210
+
*/
211
+
$excluded_taxonomies = apply_filters( 'rocket_exclude_post_taxonomy', [] );
212
+
213
+
foreach ( $taxonomies as $taxonomy ) {
214
+
if (
215
+
! $taxonomy->public
216
+
||
217
+
in_array( $taxonomy->name, $excluded_taxonomies, true )
218
+
) {
219
+
continue;
220
+
}
221
+
222
+
// Get the terms related to post.
223
+
$terms = get_the_terms( $post->ID, $taxonomy->name );
224
+
225
+
if ( empty( $terms ) || is_wp_error( $terms ) ) {
226
+
continue;
227
+
}
228
+
foreach ( $terms as $term ) {
229
+
$term_url = get_term_link( $term->slug, $taxonomy->name );
230
+
if ( ! is_wp_error( $term_url ) ) {
231
+
$urls[] = $term_url;
232
+
}
233
+
if ( ! is_taxonomy_hierarchical( $taxonomy->name ) ) {
234
+
continue;
235
+
}
236
+
$ancestors = (array) get_ancestors( $term->term_id, $taxonomy->name );
237
+
foreach ( $ancestors as $ancestor ) {
238
+
$ancestor_object = get_term( $ancestor, $taxonomy->name );
239
+
if ( ! $ancestor_object instanceof WP_Term ) {
240
+
continue;
241
+
}
242
+
$ancestor_term_url = get_term_link( $ancestor_object->slug, $taxonomy->name );
243
+
if ( ! is_wp_error( $ancestor_term_url ) ) {
244
+
$urls[] = $ancestor_term_url;
245
+
}
246
+
}
247
+
}
248
+
}
249
+
250
+
// Remove entries with empty values in array.
251
+
$urls = array_filter( $urls, 'is_string' );
252
+
253
+
/**
254
+
* Filter the list of taxonomies URLs
255
+
*
256
+
* @since 1.1.0
257
+
*
258
+
* @param array $urls List of taxonomies URLs
259
+
*/
260
+
return apply_filters( 'rocket_post_terms_urls', $urls );
261
+
}
262
+
263
+
/**
264
+
* Purge single cache file(s) added in the Never Cache URL(s).
265
+
*
266
+
* @param array $old_value An array of previous values for the settings.
267
+
* @param array $value An array of submitted values for the settings.
268
+
* @return void
269
+
*/
270
+
public function purge_cache_reject_uri_partially( array $old_value, array $value ): void {
271
+
// Bail out if cache_reject_uri key is not in the settings array.
272
+
if ( ! array_key_exists( 'cache_reject_uri', $old_value ) || ! array_key_exists( 'cache_reject_uri', $value ) ) {
273
+
return;
274
+
}
275
+
276
+
// Get change in uris.
277
+
$diff = array_diff( $value['cache_reject_uri'], $old_value['cache_reject_uri'] );
278
+
279
+
// Bail out if values has not changed.
280
+
if ( empty( $diff ) ) {
281
+
return;
282
+
}
283
+
284
+
$urls = [];
285
+
$wildcard = '(.*)';
286
+
foreach ( $diff as $path ) {
287
+
// Check if string is a path or pattern.
288
+
if ( strpos( $path, $wildcard ) !== false ) {
289
+
$pattern = preg_replace( '#\(\.\*\).*#', '*', $path );
290
+
$results = $this->query->query(
291
+
[
292
+
'search' => $pattern,
293
+
'search_columns' => [ 'url' ],
294
+
'status' => 'completed',
295
+
]
296
+
);
297
+
298
+
foreach ( $results as $result ) {
299
+
$urls[] = $result->url;
300
+
}
301
+
302
+
continue;
303
+
}
304
+
305
+
// Get full url of never cache path.
306
+
$urls[] = home_url( $path );
307
+
}
308
+
rocket_clean_files( array_unique( $urls ) );
309
+
}
310
+
}
311
+