Controller::purgeWPFastestCache()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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