|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Integrations\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Commands\CreateReview; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Controllers\AbstractController; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Database\Cache; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Review; |
|
11
|
|
|
|
|
12
|
|
|
class Controller extends AbstractController |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @action site-reviews/cache/flush |
|
16
|
|
|
*/ |
|
17
|
|
|
public function flush(string $loggedMessage, Review $review): void |
|
18
|
|
|
{ |
|
19
|
|
|
$loggedMessage = $loggedMessage ?: "flushed_review_{$review->ID}"; |
|
20
|
|
|
$this->flushCache($loggedMessage, $review->assigned_posts); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @action site-reviews/review/created |
|
25
|
|
|
*/ |
|
26
|
17 |
|
public function flushAfterCreated(Review $review, CreateReview $command): void |
|
27
|
|
|
{ |
|
28
|
17 |
|
if (defined('WP_IMPORTING')) { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
17 |
|
if (!$review->is_approved) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
17 |
|
$postIds = array_merge($command->assigned_posts, [$command->post_id]); |
|
35
|
17 |
|
$postIds = Arr::uniqueInt($postIds); |
|
36
|
17 |
|
$this->flushCache("review_{$review->ID}_created", $postIds); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @action site-reviews/migration/end |
|
41
|
|
|
*/ |
|
42
|
36 |
|
public function flushAfterMigrated(): void |
|
43
|
|
|
{ |
|
44
|
36 |
|
$this->flushCache('plugin_migrated'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @action site-reviews/review/transitioned |
|
49
|
|
|
*/ |
|
50
|
|
|
public function flushAfterTransitioned(Review $review, string $new, string $old): void |
|
51
|
|
|
{ |
|
52
|
|
|
if (did_action('site-reviews/review/updated')) { |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
if (!in_array('publish', [$new, $old])) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
$status = [ |
|
59
|
|
|
'publish' => 'approved', |
|
60
|
|
|
'pending' => 'unapproved', |
|
61
|
|
|
'trash' => 'trashed', |
|
62
|
|
|
][$new] ?? $new; |
|
63
|
|
|
$this->flushCache("review_{$review->ID}_{$status}", $review->assigned_posts); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @action site-reviews/review/updated |
|
68
|
|
|
*/ |
|
69
|
|
|
public function flushAfterUpdated(Review $review): void |
|
70
|
|
|
{ |
|
71
|
|
|
if (did_action('site-reviews/review/transitioned')) { |
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
$this->flushCache("review_{$review->ID}_updated", $review->assigned_posts); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @action site-reviews/cache/flush_all |
|
79
|
|
|
*/ |
|
80
|
|
|
public function flushAll(string $loggedMessage): void |
|
81
|
|
|
{ |
|
82
|
|
|
$this->flushCache($loggedMessage); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
36 |
|
protected function flushCache(string $loggedMessage, ?array $postIds = null): void |
|
86
|
|
|
{ |
|
87
|
36 |
|
if ([] === $postIds && !glsr()->filterBool('cache/flush_when_empty_assigned_posts', true)) { |
|
88
|
|
|
return; |
|
89
|
|
|
} |
|
90
|
36 |
|
$postIds = Arr::consolidate($postIds); |
|
91
|
36 |
|
$this->purgeEnduranceCache($postIds); |
|
92
|
36 |
|
$this->purgeFlyingPressCache($postIds); |
|
93
|
36 |
|
$this->purgeHummingbirdCache($postIds); |
|
94
|
36 |
|
$this->purgeLitespeedCache($postIds); |
|
95
|
36 |
|
$this->purgeNitropackCache($postIds); |
|
96
|
36 |
|
$this->purgeSiteGroundCache($postIds); |
|
97
|
36 |
|
$this->purgeSwiftPerformanceCache($postIds); |
|
98
|
36 |
|
$this->purgeW3TotalCache($postIds); |
|
99
|
36 |
|
$this->purgeWPFastestCache($postIds); |
|
100
|
36 |
|
$this->purgeWPOptimizeCache($postIds); |
|
101
|
36 |
|
$this->purgeWPRocketCache($postIds); |
|
102
|
36 |
|
$this->purgeWPSuperCache($postIds); |
|
103
|
36 |
|
$this->logResult($loggedMessage, $postIds); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
36 |
|
protected function logResult(string $message, array $postIds): void |
|
107
|
|
|
{ |
|
108
|
36 |
|
$message = trim($message); |
|
109
|
36 |
|
$message = $message ?: 'flushed'; |
|
110
|
36 |
|
if (!str_starts_with($message, 'flushed')) { |
|
111
|
36 |
|
$message = Str::prefix($message, 'flushed_after_'); |
|
112
|
|
|
} |
|
113
|
36 |
|
$flushed = empty($postIds) ? 'all' : implode(', ', $postIds); |
|
114
|
36 |
|
glsr_log()->debug("cache::{$message} [{$flushed}]"); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @see https://github.com/bluehost/endurance-page-cache/ |
|
119
|
|
|
*/ |
|
120
|
36 |
|
protected function purgeEnduranceCache(array $postIds = []): void |
|
121
|
|
|
{ |
|
122
|
|
|
// This is a sloppy plugin, the only option we have is to purge the entire cache... |
|
123
|
36 |
|
do_action('epc_purge'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @see https://flyingpress.com/ |
|
128
|
|
|
*/ |
|
129
|
36 |
|
protected function purgeFlyingPressCache(array $postIds = []): void |
|
130
|
|
|
{ |
|
131
|
36 |
|
if (!class_exists('FlyingPress\Purge')) { |
|
132
|
36 |
|
return; |
|
133
|
|
|
} |
|
134
|
|
|
if (is_callable(['FlyingPress\Purge', 'purge_pages']) && empty($postIds)) { |
|
135
|
|
|
\FlyingPress\Purge::purge_pages(); |
|
136
|
|
|
} elseif (is_callable(['FlyingPress\Purge', 'purge_url'])) { |
|
137
|
|
|
foreach ($postIds as $postId) { |
|
138
|
|
|
\FlyingPress\Purge::purge_url(get_permalink($postId)); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @see https://premium.wpmudev.org/docs/api-plugin-development/hummingbird-api-docs/#action-wphb_clear_page_cache |
|
145
|
|
|
*/ |
|
146
|
36 |
|
protected function purgeHummingbirdCache(array $postIds = []): void |
|
147
|
|
|
{ |
|
148
|
36 |
|
if (empty($postIds)) { |
|
149
|
36 |
|
do_action('wphb_clear_page_cache'); |
|
150
|
|
|
} |
|
151
|
36 |
|
foreach ($postIds as $postId) { |
|
152
|
17 |
|
do_action('wphb_clear_page_cache', $postId); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @see https://wordpress.org/plugins/litespeed-cache/ |
|
158
|
|
|
*/ |
|
159
|
36 |
|
protected function purgeLitespeedCache(array $postIds = []): void |
|
160
|
|
|
{ |
|
161
|
36 |
|
if (empty($postIds)) { |
|
162
|
36 |
|
do_action('litespeed_purge_all'); |
|
163
|
|
|
} |
|
164
|
36 |
|
foreach ($postIds as $postId) { |
|
165
|
17 |
|
do_action('litespeed_purge_post', $postId); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @see https://nitropack.io/ |
|
171
|
|
|
*/ |
|
172
|
36 |
|
protected function purgeNitropackCache(array $postIds = []): void |
|
173
|
|
|
{ |
|
174
|
36 |
|
if (!function_exists('nitropack_invalidate') || !function_exists('nitropack_get_cacheable_object_types')) { |
|
175
|
36 |
|
return; |
|
176
|
|
|
} |
|
177
|
|
|
if (!get_option('nitropack-autoCachePurge', 1)) { |
|
178
|
|
|
return; |
|
179
|
|
|
} |
|
180
|
|
|
if (empty($postIds)) { |
|
181
|
|
|
nitropack_invalidate(null, null, 'Invalidating all pages after creating/updating/deleting one or more unassigned reviews'); |
|
182
|
|
|
return; |
|
183
|
|
|
} |
|
184
|
|
|
foreach ($postIds as $postId) { |
|
185
|
|
|
$cacheableTypes = nitropack_get_cacheable_object_types(); |
|
186
|
|
|
$post = get_post($postId); |
|
187
|
|
|
$postType = $post->post_type ?? 'post'; |
|
188
|
|
|
$postTitle = $post->post_title ?? ''; |
|
189
|
|
|
if (in_array($postType, $cacheableTypes)) { |
|
190
|
|
|
nitropack_invalidate(null, "single:{$postId}", sprintf('Invalidating "%s" after creating/updating/deleting an assigned review', $postTitle)); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @see https://wordpress.org/plugins/sg-cachepress/ |
|
197
|
|
|
*/ |
|
198
|
36 |
|
protected function purgeSiteGroundCache(array $postIds = []): void |
|
199
|
|
|
{ |
|
200
|
36 |
|
if (function_exists('sg_cachepress_purge_everything') && empty($postIds)) { |
|
201
|
|
|
sg_cachepress_purge_everything(); |
|
202
|
|
|
} |
|
203
|
36 |
|
if (function_exists('sg_cachepress_purge_cache')) { |
|
204
|
|
|
foreach ($postIds as $postId) { |
|
205
|
|
|
sg_cachepress_purge_cache(get_permalink($postId)); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @see https://swiftperformance.io/ |
|
212
|
|
|
*/ |
|
213
|
36 |
|
protected function purgeSwiftPerformanceCache(array $postIds = []): void |
|
214
|
|
|
{ |
|
215
|
36 |
|
if (!class_exists('Swift_Performance_Cache')) { |
|
216
|
36 |
|
return; |
|
217
|
|
|
} |
|
218
|
|
|
if (empty($postIds)) { |
|
219
|
|
|
\Swift_Performance_Cache::clear_all_cache(); |
|
220
|
|
|
} else { |
|
221
|
|
|
\Swift_Performance_Cache::clear_post_cache_array($postIds); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @see https://wordpress.org/plugins/w3-total-cache/ |
|
227
|
|
|
*/ |
|
228
|
36 |
|
protected function purgeW3TotalCache(array $postIds = []): void |
|
229
|
|
|
{ |
|
230
|
36 |
|
if (function_exists('w3tc_flush_all') && empty($postIds)) { |
|
231
|
|
|
w3tc_flush_all(); |
|
232
|
|
|
} |
|
233
|
36 |
|
if (function_exists('w3tc_flush_post')) { |
|
234
|
|
|
foreach ($postIds as $postId) { |
|
235
|
|
|
w3tc_flush_post($postId); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @see https://www.wpfastestcache.com/ |
|
242
|
|
|
*/ |
|
243
|
36 |
|
protected function purgeWPFastestCache(array $postIds = []): void |
|
244
|
|
|
{ |
|
245
|
36 |
|
if (empty($postIds)) { |
|
246
|
36 |
|
do_action('wpfc_clear_all_cache'); |
|
247
|
|
|
} |
|
248
|
36 |
|
foreach ($postIds as $postId) { |
|
249
|
17 |
|
do_action('wpfc_clear_post_cache_by_id', false, $postId); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @see https://getwpo.com/documentation/#Purging-the-cache-from-an-other-plugin-or-theme |
|
255
|
|
|
*/ |
|
256
|
36 |
|
protected function purgeWPOptimizeCache(array $postIds = []): void |
|
257
|
|
|
{ |
|
258
|
36 |
|
if (function_exists('WP_Optimize') && empty($postIds)) { |
|
259
|
|
|
WP_Optimize()->get_page_cache()->purge(); |
|
260
|
|
|
} |
|
261
|
36 |
|
if (class_exists('WPO_Page_Cache')) { |
|
262
|
|
|
foreach ($postIds as $postId) { |
|
263
|
|
|
\WPO_Page_Cache::delete_single_post_cache($postId); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @see https://docs.wp-rocket.me/article/93-rocketcleanpost |
|
270
|
|
|
*/ |
|
271
|
36 |
|
protected function purgeWPRocketCache(array $postIds = []): void |
|
272
|
|
|
{ |
|
273
|
36 |
|
if (function_exists('rocket_clean_home') && empty($postIds)) { |
|
274
|
|
|
rocket_clean_home(); |
|
275
|
|
|
} |
|
276
|
36 |
|
if (function_exists('rocket_clean_post')) { |
|
277
|
|
|
foreach ($postIds as $postId) { |
|
278
|
|
|
rocket_clean_post($postId); |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @see https://wordpress.org/plugins/wp-super-cache/ |
|
285
|
|
|
*/ |
|
286
|
36 |
|
protected function purgeWPSuperCache(array $postIds = []): void |
|
287
|
|
|
{ |
|
288
|
36 |
|
if (function_exists('wp_cache_clear_cache') && empty($postIds)) { |
|
289
|
|
|
wp_cache_clear_cache(); |
|
290
|
|
|
} |
|
291
|
36 |
|
if (function_exists('wp_cache_post_change')) { |
|
292
|
|
|
foreach ($postIds as $postId) { |
|
293
|
|
|
wp_cache_post_change($postId); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|