Test Failed
Push — develop ( 6f947a...7aea1a )
by Paul
18:36
created

Controller::purgeSwiftPerformanceCache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 3
cts 6
cp 0.5
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 4.125
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 25
    public function flushAfterCreated(Review $review, CreateReview $command): void
27
    {
28 25
        if (defined('WP_IMPORTING')) {
29
            return;
30
        }
31 25
        if (!$review->is_approved) {
32 1
            return;
33
        }
34 25
        $postIds = array_merge($command->assigned_posts, [$command->post_id]);
35 25
        $postIds = Arr::uniqueInt($postIds);
36 25
        $this->flushCache("review_{$review->ID}_created", $postIds);
37
    }
38
39
    /**
40
     * @action site-reviews/migration/end
41
     */
42 45
    public function flushAfterMigrated(): void
43
    {
44 45
        $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 45
    protected function flushCache(string $loggedMessage, ?array $postIds = null): void
86
    {
87 45
        if ([] === $postIds && !glsr()->filterBool('cache/flush_when_empty_assigned_posts', true)) {
88
            return;
89
        }
90 45
        $postIds = Arr::consolidate($postIds);
91 45
        $this->purgeCloudFlare($postIds);
92 45
        $this->purgeEnduranceCache($postIds);
93 45
        $this->purgeFlyingPressCache($postIds);
94 45
        $this->purgeHummingbirdCache($postIds);
95 45
        $this->purgeLitespeedCache($postIds);
96 45
        $this->purgeNitropackCache($postIds);
97 45
        $this->purgeSiteGroundCache($postIds);
98 45
        $this->purgeSwiftPerformanceCache($postIds);
99 45
        $this->purgeW3TotalCache($postIds);
100 45
        $this->purgeWPFastestCache($postIds);
101 45
        $this->purgeWPOptimizeCache($postIds);
102 45
        $this->purgeWPRocketCache($postIds);
103 45
        $this->purgeWPSuperCache($postIds);
104 45
        $this->logResult($loggedMessage, $postIds);
105
    }
106
107 45
    protected function logResult(string $message, array $postIds): void
108
    {
109 45
        $message = trim($message);
110 45
        $message = $message ?: 'flushed';
111 45
        if (!str_starts_with($message, 'flushed')) {
112 45
            $message = Str::prefix($message, 'flushed_after_');
113
        }
114 45
        $flushed = empty($postIds) ? 'all' : implode(', ', $postIds);
115 45
        glsr_log()->debug("cache::{$message} [{$flushed}]");
116
    }
117
118
    /**
119
     * @see https://wordpress.org/plugins/cloudflare/
120
     * @see https://developers.cloudflare.com/automatic-platform-optimization/
121
     */
122 45
    protected function purgeCloudFlare(array $postIds = []): void
123
    {
124 45
        if (!class_exists('CF\WordPress\Hooks')) {
125 45
            return;
126
        }
127
        if (!empty($postIds)) {
128
            do_action('site-reviews/cache/flush', $postIds);
129
        } else {
130
            do_action('site-reviews/cache/flush_all');
131
        }
132
    }
133
134
    /**
135
     * @see https://github.com/bluehost/endurance-page-cache/
136
     */
137 45
    protected function purgeEnduranceCache(array $postIds = []): void
138
    {
139
        // This is a sloppy plugin, the only option we have is to purge the entire cache...
140 45
        do_action('epc_purge');
141
    }
142
143
    /**
144
     * @see https://flyingpress.com/
145
     */
146 45
    protected function purgeFlyingPressCache(array $postIds = []): void
147
    {
148 45
        if (!class_exists('FlyingPress\Purge')) {
149 45
            return;
150
        }
151
        if (is_callable(['FlyingPress\Purge', 'purge_pages']) && empty($postIds)) {
152
            \FlyingPress\Purge::purge_pages();
153
        } elseif (is_callable(['FlyingPress\Purge', 'purge_url'])) {
154
            foreach ($postIds as $postId) {
155
                \FlyingPress\Purge::purge_url(get_permalink($postId));
156
            }
157
        }
158
    }
159
160
    /**
161
     * @see https://premium.wpmudev.org/docs/api-plugin-development/hummingbird-api-docs/#action-wphb_clear_page_cache
162
     */
163 45
    protected function purgeHummingbirdCache(array $postIds = []): void
164
    {
165 45
        if (empty($postIds)) {
166 45
            do_action('wphb_clear_page_cache');
167
        }
168 45
        foreach ($postIds as $postId) {
169 25
            do_action('wphb_clear_page_cache', $postId);
170
        }
171
    }
172
173
    /**
174
     * @see https://wordpress.org/plugins/litespeed-cache/
175
     */
176 45
    protected function purgeLitespeedCache(array $postIds = []): void
177
    {
178 45
        if (empty($postIds)) {
179 45
            do_action('litespeed_purge_all');
180
        }
181 45
        foreach ($postIds as $postId) {
182 25
            do_action('litespeed_purge_post', $postId);
183
        }
184
    }
185
186
    /**
187
     * @see https://nitropack.io/
188
     */
189 45
    protected function purgeNitropackCache(array $postIds = []): void
190
    {
191 45
        if (!function_exists('nitropack_invalidate') || !function_exists('nitropack_get_cacheable_object_types')) {
192 45
            return;
193
        }
194
        if (!get_option('nitropack-autoCachePurge', 1)) {
195
            return;
196
        }
197
        if (empty($postIds)) {
198
            nitropack_invalidate(null, null, 'Invalidating all pages after creating/updating/deleting one or more unassigned reviews');
199
            return;
200
        }
201
        foreach ($postIds as $postId) {
202
            $cacheableTypes = nitropack_get_cacheable_object_types();
203
            $post = get_post($postId);
204
            $postType = $post->post_type ?? 'post';
205
            $postTitle = $post->post_title ?? '';
206
            if (in_array($postType, $cacheableTypes)) {
207
                nitropack_invalidate(null, "single:{$postId}", sprintf('Invalidating "%s" after creating/updating/deleting an assigned review', $postTitle));
208
            }
209
        }
210
    }
211
212
    /**
213
     * @see https://wordpress.org/plugins/sg-cachepress/
214
     */
215 45
    protected function purgeSiteGroundCache(array $postIds = []): void
216
    {
217 45
        if (function_exists('sg_cachepress_purge_everything') && empty($postIds)) {
218
            sg_cachepress_purge_everything();
219
        }
220 45
        if (function_exists('sg_cachepress_purge_cache')) {
221
            foreach ($postIds as $postId) {
222
                sg_cachepress_purge_cache(get_permalink($postId));
223
            }
224
        }
225
    }
226
227
    /**
228
     * @see https://swiftperformance.io/
229
     */
230 45
    protected function purgeSwiftPerformanceCache(array $postIds = []): void
231
    {
232 45
        if (!class_exists('Swift_Performance_Cache')) {
233 45
            return;
234
        }
235
        if (empty($postIds)) {
236
            \Swift_Performance_Cache::clear_all_cache();
237
        } else {
238
            \Swift_Performance_Cache::clear_post_cache_array($postIds);
239
        }
240
    }
241
242
    /**
243
     * @see https://wordpress.org/plugins/w3-total-cache/
244
     */
245 45
    protected function purgeW3TotalCache(array $postIds = []): void
246
    {
247 45
        if (function_exists('w3tc_flush_all') && empty($postIds)) {
248
            w3tc_flush_all();
249
        }
250 45
        if (function_exists('w3tc_flush_post')) {
251
            foreach ($postIds as $postId) {
252
                w3tc_flush_post($postId);
253
            }
254
        }
255
    }
256
257
    /**
258
     * @see https://www.wpfastestcache.com/
259
     */
260 45
    protected function purgeWPFastestCache(array $postIds = []): void
261
    {
262 45
        if (empty($postIds)) {
263 45
            do_action('wpfc_clear_all_cache');
264
        }
265 45
        foreach ($postIds as $postId) {
266 25
            do_action('wpfc_clear_post_cache_by_id', false, $postId);
267
        }
268
    }
269
270
    /**
271
     * @see https://getwpo.com/documentation/#Purging-the-cache-from-an-other-plugin-or-theme
272
     */
273 45
    protected function purgeWPOptimizeCache(array $postIds = []): void
274
    {
275 45
        if (function_exists('WP_Optimize') && empty($postIds)) {
276
            WP_Optimize()->get_page_cache()->purge();
277
        }
278 45
        if (class_exists('WPO_Page_Cache')) {
279
            foreach ($postIds as $postId) {
280
                \WPO_Page_Cache::delete_single_post_cache($postId);
281
            }
282
        }
283
    }
284
285
    /**
286
     * @see https://docs.wp-rocket.me/article/93-rocketcleanpost
287
     */
288 45
    protected function purgeWPRocketCache(array $postIds = []): void
289
    {
290 45
        if (function_exists('rocket_clean_home') && empty($postIds)) {
291
            rocket_clean_home();
292
        }
293 45
        if (function_exists('rocket_clean_post')) {
294
            foreach ($postIds as $postId) {
295
                rocket_clean_post($postId);
296
            }
297
        }
298
    }
299
300
    /**
301
     * @see https://wordpress.org/plugins/wp-super-cache/
302
     */
303 45
    protected function purgeWPSuperCache(array $postIds = []): void
304
    {
305 45
        if (function_exists('wp_cache_clear_cache') && empty($postIds)) {
306
            wp_cache_clear_cache();
307
        }
308 45
        if (function_exists('wp_cache_post_change')) {
309
            foreach ($postIds as $postId) {
310
                wp_cache_post_change($postId);
311
            }
312
        }
313
    }
314
}
315