Passed
Push — main ( a0e9c5...805967 )
by Paul
07:42
created

Controller::purge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 17
cp 0
rs 9.7333
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Cache;
4
5
use GeminiLabs\SiteReviews\Commands\CreateReview;
6
use GeminiLabs\SiteReviews\Controllers\Controller as BaseController;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Review;
9
10
class Controller extends BaseController
11
{
12
    /**
13
     * @action admin_footer
14
     * @action wp_footer
15
     */
16
    public function initPurge()
17
    {
18
        glsr()->action('cache/flush');
19
    }
20
21
    /**
22
     * @action site-reviews/cache/flush
23
     */
24
    public function purge()
25
    {
26
        $postIds = glsr()->retrieve('cache/post_ids');
27
        if (is_array($postIds)) {
28
            glsr_log('cache::flushing')->debug($postIds);
29
            glsr()->discard('cache/post_ids');
30
            $postIds = Arr::uniqueInt($postIds);
31
            $this->purgeEnduranceCache($postIds);
32
            $this->purgeHummingbirdCache($postIds);
33
            $this->purgeLitespeedCache($postIds);
34
            $this->purgeNitropackCache($postIds);
35
            $this->purgeSiteGroundCache($postIds);
36
            $this->purgeSwiftPerformanceCache($postIds);
37
            $this->purgeW3TotalCache($postIds);
38
            $this->purgeWPFastestCache($postIds);
39
            $this->purgeWPOptimizeCache($postIds);
40
            $this->purgeWPRocketCache($postIds);
41
            $this->purgeWPSuperCache($postIds);
42
        }
43
    }
44
45
    /**
46
     * @action site-reviews/migration/end
47
     */
48 31
    public function purgeAll()
49
    {
50 31
        glsr()->store('cache/post_ids', []);
51 31
        glsr_log('cache::flush_all');
52
    }
53
54
    /**
55
     * @action site-reviews/review/created
56
     */
57 24
    public function purgeOnCreated(Review $review, CreateReview $command)
58
    {
59 24
        if ($review->is_approved) {
60 24
            $postIds = glsr()->retrieve('cache/post_ids', []);
61 24
            $postIds = array_merge($postIds, $review->assigned_posts, [$command->post_id]);
62 24
            $postIds = Arr::uniqueInt($postIds);
63 24
            glsr()->store('cache/post_ids', $postIds);
64 24
            glsr_log('cache::flush_on_created');
65
        }
66
    }
67
68
    /**
69
     * @action site-reviews/review/transitioned
70
     */
71
    public function purgeOnTransitioned(Review $review, string $new, string $old)
72
    {
73
        $postIds = glsr()->retrieve('cache/post_ids'); // "site-reviews/review/updated" might have already been triggered
74
        if (in_array('publish', [$new, $old]) && is_null($postIds)) {
75
            glsr_log('cache::flush_on_transitioned');
76
            $this->purgeOnUpdated($review);
77
        }
78
    }
79
80
    /**
81
     * @action site-reviews/review/updated
82
     */
83
    public function purgeOnUpdated(Review $review)
84
    {
85
        $postIds = glsr()->retrieve('cache/post_ids', []);
86
        $postIds = array_merge($postIds, $review->assigned_posts);
87
        $postIds = Arr::uniqueInt($postIds);
88
        glsr()->store('cache/post_ids', $postIds);
89
        glsr_log('cache::flush_on_updated');
90
    }
91
92
    /**
93
     * @see https://github.com/bluehost/endurance-page-cache/
94
     */
95
    protected function purgeEnduranceCache(array $postIds = [])
96
    {
97
        // This is a sloppy plugin, the only option we have is to purge the entire cache...
98
        do_action('epc_purge');
99
    }
100
101
    /**
102
     * @see https://premium.wpmudev.org/docs/api-plugin-development/hummingbird-api-docs/#action-wphb_clear_page_cache
103
     */
104
    protected function purgeHummingbirdCache(array $postIds = [])
105
    {
106
        if (empty($postIds)) {
107
            do_action('wphb_clear_page_cache');
108
        }
109
        foreach ($postIds as $postId) {
110
            do_action('wphb_clear_page_cache', $postId);
111
        }
112
    }
113
114
    /**
115
     * @see https://wordpress.org/plugins/litespeed-cache/
116
     */
117
    protected function purgeLitespeedCache(array $postIds = [])
118
    {
119
        if (empty($postIds)) {
120
            do_action('litespeed_purge_all');
121
        }
122
        foreach ($postIds as $postId) {
123
            do_action('litespeed_purge_post', $postId);
124
        }
125
    }
126
127
    /**
128
     * @see https://nitropack.io/
129
     */
130
    protected function purgeNitropackCache(array $postIds = [])
131
    {
132
        if (!function_exists('nitropack_invalidate') || !function_exists('nitropack_get_cacheable_object_types')) {
133
            return;
134
        }
135
        if (!get_option('nitropack-autoCachePurge', 1)) {
136
            return;
137
        }
138
        if (empty($postIds)) {
139
            nitropack_invalidate(null, null, 'Invalidating all pages after updating one or more unassigned reviews');
140
            return;
141
        }
142
        foreach ($postIds as $postId) {
143
            $cacheableTypes = nitropack_get_cacheable_object_types();
144
            $post = get_post($postId);
145
            $postType = $post->post_type ?? 'post';
146
            $postTitle = $post->post_title ?? '';
147
            if (in_array($postType, $cacheableTypes)) {
148
                nitropack_invalidate(null, 'single:'.$postId, sprintf('Invalidating "%s" after updating assigned review', $postTitle));
149
            }
150
        }
151
    }
152
153
    /**
154
     * @see https://wordpress.org/plugins/sg-cachepress/
155
     */
156
    protected function purgeSiteGroundCache(array $postIds = [])
157
    {
158
        if (function_exists('sg_cachepress_purge_everything') && empty($postIds)) {
159
            sg_cachepress_purge_everything();
160
        }
161
        if (function_exists('sg_cachepress_purge_cache')) {
162
            foreach ($postIds as $postId) {
163
                sg_cachepress_purge_cache(get_permalink($postId));
164
            }
165
        }
166
    }
167
168
    /**
169
     * @see https://swiftperformance.io/
170
     */
171
    protected function purgeSwiftPerformanceCache(array $postIds = [])
172
    {
173
        if (!class_exists('Swift_Performance_Cache')) {
174
            return;
175
        }
176
        if (empty($postIds)) {
177
            \Swift_Performance_Cache::clear_all_cache();
178
        } else {
179
            \Swift_Performance_Cache::clear_post_cache_array($postIds);
180
        }
181
    }
182
183
    /**
184
     * @see https://wordpress.org/plugins/w3-total-cache/
185
     */
186
    protected function purgeW3TotalCache(array $postIds = [])
187
    {
188
        if (function_exists('w3tc_flush_all') && empty($postIds)) {
189
            w3tc_flush_all();
190
        }
191
        if (function_exists('w3tc_flush_post')) {
192
            foreach ($postIds as $postId) {
193
                w3tc_flush_post($postId);
194
            }
195
        }
196
    }
197
198
    /**
199
     * @see https://www.wpfastestcache.com/
200
     */
201
    protected function purgeWPFastestCache(array $postIds = [])
202
    {
203
        if (empty($postIds)) {
204
            do_action('wpfc_clear_all_cache');
205
        }
206
        foreach ($postIds as $postId) {
207
            do_action('wpfc_clear_post_cache_by_id', false, $postId);
208
        }
209
    }
210
211
    /**
212
     * @see https://getwpo.com/documentation/#Purging-the-cache-from-an-other-plugin-or-theme
213
     */
214
    protected function purgeWPOptimizeCache(array $postIds = [])
215
    {
216
        if (function_exists('WP_Optimize') && empty($postIds)) {
217
            WP_Optimize()->get_page_cache()->purge();
218
        }
219
        if (class_exists('WPO_Page_Cache')) {
220
            foreach ($postIds as $postId) {
221
                \WPO_Page_Cache::delete_single_post_cache($postId);
222
            }
223
        }
224
    }
225
226
    /**
227
     * @see https://docs.wp-rocket.me/article/93-rocketcleanpost
228
     */
229
    protected function purgeWPRocketCache(array $postIds = [])
230
    {
231
        if (function_exists('rocket_clean_home') && empty($postIds)) {
232
            rocket_clean_home();
233
        }
234
        if (function_exists('rocket_clean_post')) {
235
            foreach ($postIds as $postId) {
236
                rocket_clean_post($postId);
237
            }
238
        }
239
    }
240
241
    /**
242
     * @see https://wordpress.org/plugins/wp-super-cache/
243
     */
244
    protected function purgeWPSuperCache(array $postIds = [])
245
    {
246
        if (function_exists('wp_cache_clear_cache') && empty($postIds)) {
247
            wp_cache_clear_cache();
248
        }
249
        if (function_exists('wp_cache_post_change')) {
250
            foreach ($postIds as $postId) {
251
                wp_cache_post_change($postId);
252
            }
253
        }
254
    }
255
}
256