PurgeActionsSubscriber.php
3.71 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
<?php
namespace WP_Rocket\Engine\Cache;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Logger\Logger;
/**
* Subscriber for the cache purge actions
*
* @since 3.5
*/
class PurgeActionsSubscriber implements Subscriber_Interface {
/**
* WP Rocket options instance
*
* @var Options_Data
*/
private $options;
/**
* Purge instance
*
* @var Purge
*/
private $purge;
/**
* Constructor
*
* @param Options_Data $options WP Rocket options instance.
* @param Purge $purge Purge instance.
*/
public function __construct( Options_Data $options, Purge $purge ) {
$this->options = $options;
$this->purge = $purge;
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'profile_update' => 'purge_user_cache',
'delete_user' => 'purge_user_cache',
'create_term' => [ 'maybe_purge_cache_on_term_change', 10, 3 ],
'edit_term' => [ 'maybe_purge_cache_on_term_change', 10, 3 ],
'delete_term' => [ 'maybe_purge_cache_on_term_change', 10, 3 ],
'after_rocket_clean_post' => [
[ 'purge_dates_archives' ],
[ 'purge_post_terms_urls' ],
],
'rocket_rucss_complete_job_status' => [ 'purge_url_cache', 100 ],
'rocket_rucss_after_clearing_usedcss' => 'purge_url_cache',
'rocket_after_save_dynamic_lists' => 'purge_cache',
];
}
/**
* Purges the cache of the corresponding user
*
* @since 3.5
*
* @param int $user_id User ID.
* @return void
*/
public function purge_user_cache( $user_id ) {
if ( ! $this->should_purge_user_cache() ) {
return;
}
rocket_clean_user( $user_id );
}
/**
* Purges the cache when a public term is created|updated|deleted
*
* @since 3.5.5
*
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
* @return void
*/
public function maybe_purge_cache_on_term_change( $term_id, $tt_id, $taxonomy ) {
if ( ! $this->is_taxonomy_public( $taxonomy ) ) {
return;
}
rocket_clean_domain();
}
/**
* Purges cache for the dates archives of a post after cleaning the post
*
* @param WP_Post $post Post object.
* @return void
*/
public function purge_dates_archives( $post ) {
$this->purge->purge_dates_archives( $post );
}
/**
* Purge all terms archives urls associated to a specific post.
*
* @param WP_Post $post Post object.
* @return void
*/
public function purge_post_terms_urls( $post ) {
$this->purge->purge_post_terms_urls( $post );
}
/**
* Checks if the given taxonomy is public
*
* @param string $name Taxonomy name.
* @return bool
*/
private function is_taxonomy_public( $name ) {
$taxonomy = get_taxonomy( $name );
if ( false === $taxonomy ) {
return false;
}
return ( $taxonomy->public && $taxonomy->publicly_queryable );
}
/**
* Checks if the user cache should be purged
*
* @since 3.5
*
* @return boolean
*/
private function should_purge_user_cache() {
if ( ! $this->options->get( 'cache_logged_user', 0 ) ) {
return false;
}
// This filter is documented in /inc/functions/files.php.
return ! (bool) apply_filters( 'rocket_common_cache_logged_users', false );
}
/**
* Purge cache after RUCSS
*
* @param string $url URL to be purged.
*
* @return void
*/
public function purge_url_cache( string $url ) {
// Flush cache for this url.
Logger::debug( 'RUCSS: Purge the cache for url: ' . $url );
$this->purge->purge_url( $url );
}
/**
* Clean the whole cache
*
* @return void
*/
public function purge_cache() {
rocket_clean_domain();
}
}