Purge.php
5.49 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace WP_Rocket\Engine\Cache;
use DirectoryIterator;
use Exception;
use WP_Term;
use WP_Post;
/**
* Cache Purge handling class
*/
class Purge {
/**
* Filesystem instance
*
* @var WP_Filesystem_Direct
*/
private $filesystem;
/**
* Initialize the class
*
* @param WP_Filesystem_Direct $filesystem Filesystem instance.
*/
public function __construct( $filesystem ) {
$this->filesystem = $filesystem;
}
/**
* Purges cache for the dates archives of a post
*
* @param WP_Post $post Post object.
* @return void
*/
public function purge_dates_archives( $post ) {
foreach ( $this->get_dates_archives( $post ) as $url ) {
$this->purge_url( $url, true );
}
}
/**
* Purge URL cache.
*
* @param string $url URL to be purged.
* @param boolean $pagination Purge also pagination.
* @return void
*/
public function purge_url( $url, $pagination = false ) {
if ( ! is_string( $url ) ) {
return;
}
global $wp_rewrite;
$parsed_url = $this->parse_url( $url );
foreach ( _rocket_get_cache_dirs( $parsed_url['host'] ) as $dir ) {
$path = $dir . $parsed_url['path'];
if ( ! $this->filesystem->exists( $path ) ) {
continue;
}
foreach ( $this->get_iterator( $path ) as $item ) {
if ( $item->isFile() ) {
$this->filesystem->delete( $item->getPathname() );
}
}
if ( $pagination ) {
$this->maybe_remove_dir( $path . DIRECTORY_SEPARATOR . $wp_rewrite->pagination_base );
}
}
}
/**
* Gets the dates archives URLs for the provided post
*
* @param WP_Post $post Post object.
* @return array
*/
private function get_dates_archives( $post ) {
$time = get_the_time( 'Y-m-d', $post );
if ( empty( $time ) ) {
return [];
}
$date = explode( '-', $time );
$urls = [
get_year_link( $date[0] ),
get_month_link( $date[0], $date[1] ),
get_day_link( $date[0], $date[1], $date[2] ),
];
/**
* Filter the list of dates URLs.
*
* @since 1.1.0
*
* @param array $urls List of dates URLs.
*/
return (array) apply_filters( 'rocket_post_dates_urls', $urls );
}
/**
* Parses URL and return the parts array
*
* @since 3.6.1
*
* @param string $url URL to parse.
* @return array
*/
private function parse_url( $url ) {
$parsed_url = get_rocket_parse_url( $url );
/** This filter is documented in inc/front/htaccess.php */
if ( apply_filters( 'rocket_url_no_dots', false ) ) {
$parsed_url['host'] = str_replace( '.', '_', $parsed_url['host'] );
}
return $parsed_url;
}
/**
* Gets the iterator for the given path
*
* @since 3.6.1
*
* @param string $path Absolute path.
* @return DirectoryIterator|array
*/
private function get_iterator( $path ) {
try {
$iterator = new DirectoryIterator( $path );
} catch ( Exception $e ) {
// No action required, as logging not enabled.
$iterator = [];
}
return $iterator;
}
/**
* Recursively remove the provided directory and its content
*
* @since 3.6.1
*
* @param string $dir Absolute path for the directory.
* @return void
*/
private function maybe_remove_dir( $dir ) {
if ( $this->filesystem->is_dir( $dir ) ) {
rocket_rrmdir( $dir, [], $this->filesystem );
}
}
/**
* Purge all terms archives urls associated to a specific post.
*
* @since 3.6.1
*
* @param WP_Post $post Post object.
*/
public function purge_post_terms_urls( WP_Post $post ) {
$urls = $this->get_post_terms_urls( $post );
foreach ( $urls as $url ) {
$this->purge_url( $url, true );
}
/**
* Action to preload urls after cleaning cache.
*
* @param array urls to preload.
*/
do_action( 'rocket_after_clean_terms', $urls );
}
/**
* Get all terms archives urls associated to a specific post.
*
* @since 3.6.1
*
* @param WP_Post $post Post object.
*
* @return array $urls List of taxonomies URLs
*/
private function get_post_terms_urls( WP_Post $post ) {
$urls = [];
$taxonomies = get_object_taxonomies( get_post_type( $post->ID ), 'objects' );
/**
* Filters the taxonomies excluded from post purge
*
* @since 3.9.1
*
* @param array $excluded_taxonomies Array of excluded taxonomies names.
*/
$excluded_taxonomies = apply_filters( 'rocket_exclude_post_taxonomy', [] );
foreach ( $taxonomies as $taxonomy ) {
if (
! $taxonomy->public
||
in_array( $taxonomy->name, $excluded_taxonomies, true )
) {
continue;
}
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy->name );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
continue;
}
foreach ( $terms as $term ) {
$term_url = get_term_link( $term->slug, $taxonomy->name );
if ( ! is_wp_error( $term_url ) ) {
$urls[] = $term_url;
}
if ( ! is_taxonomy_hierarchical( $taxonomy->name ) ) {
continue;
}
$ancestors = (array) get_ancestors( $term->term_id, $taxonomy->name );
foreach ( $ancestors as $ancestor ) {
$ancestor_object = get_term( $ancestor, $taxonomy->name );
if ( ! $ancestor_object instanceof WP_Term ) {
continue;
}
$ancestor_term_url = get_term_link( $ancestor_object->slug, $taxonomy->name );
if ( ! is_wp_error( $ancestor_term_url ) ) {
$urls[] = $ancestor_term_url;
}
}
}
}
// Remove entries with empty values in array.
$urls = array_filter( $urls, 'is_string' );
/**
* Filter the list of taxonomies URLs
*
* @since 1.1.0
*
* @param array $urls List of taxonomies URLs
*/
return apply_filters( 'rocket_post_terms_urls', $urls );
}
}