PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / trunk
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization vtrunk
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / classes / WordPress / Settings / PurgeCache.php
nitropack / classes / WordPress / Settings Last commit date
AutoPurge.php 4 months ago BeaverBuilder.php 4 months ago CPTOptimization.php 1 month ago CacheWarmup.php 1 month ago CartCache.php 4 months ago Components.php 1 month ago EditorClearCache.php 1 month ago GeneratePreview.php 7 months ago HTMLCompression.php 3 months ago Logger.php 4 months ago OptimizationLevel.php 3 days ago Optimizations.php 1 month ago PurgeCache.php 3 days ago Shortcodes.php 4 months ago StockRefresh.php 2 months ago Subscription.php 4 months ago SystemReport.php 4 months ago TestMode.php 1 month ago
PurgeCache.php
338 lines
1 <?php
2
3 namespace NitroPack\WordPress\Settings;
4 use NitroPack\WordPress\NitroPack;
5 use NitroPack\Integration\Plugin\RC as ResidualCache;
6
7 /**
8 * Ajax handlers when purging or invalidating the NitroPack cache
9 */
10 class PurgeCache {
11 public function __construct() {
12 //admin topbar menu
13 add_action( 'wp_ajax_nitropack_purge_entire_cache', [ $this, 'nitropack_purge_entire_cache' ] );
14 add_action( 'wp_ajax_nitropack_invalidate_entire_cache', [ $this, 'nitropack_invalidate_entire_cache' ] );
15 //dashboard
16 add_action( 'wp_ajax_nitropack_purge_cache', [ $this, 'nitropack_purge_cache' ] );
17 add_action( 'wp_ajax_nitropack_clear_residual_cache', [ $this, 'nitropack_clear_residual_cache' ] );
18 //metaboxes
19 add_action( 'wp_ajax_nitropack_purge_single_cache', [ $this, 'nitropack_purge_single_cache' ] );
20 add_action( 'wp_ajax_nitropack_invalidate_single_cache', [ $this, 'nitropack_invalidate_single_cache' ] );
21
22 /* Action Links to Purge/Invalidate cache */
23 //add links under page and post for purging and invalidating cache
24 add_filter( 'post_row_actions', [ $this, 'purge_invalidate_post_links' ], 10, 2 );
25 add_filter( 'page_row_actions', [ $this, 'purge_invalidate_post_links' ], 10, 2 );
26 //metaboxes
27 add_action( 'add_meta_boxes', [ $this, 'nitropack_meta_box' ] );
28 //purge/invalidate entire cache when permalink structure or front page is changed
29 add_action( 'permalink_structure_changed', [ $this, 'nitropack_permalink_structure_changed_handler' ], 10, 2 );
30 add_action( 'update_option_show_on_front', [ $this, 'nitropack_frontpage_changed_handler' ], 10, 2 );
31 add_action( 'update_option_page_on_front', [ $this, 'nitropack_frontpage_changed_handler' ], 10, 2 );
32 add_action( 'update_option_page_for_posts', [ $this, 'nitropack_frontpage_changed_handler' ], 10, 2 );
33 }
34
35 /**
36 * AJAX handler when clicking Purge Entire Cache in admin topbar NitroPack menu
37 * Triggered in nitropack/assets/js/admin_bar_menu.min.js
38 * @return void
39 */
40 public function nitropack_purge_entire_cache() {
41 nitropack_verify_ajax_nonce( $_REQUEST );
42 try {
43 if ( nitropack_sdk_purge( null, null, 'Manual purge of all pages' ) ) {
44 NitroPack::getInstance()->getLogger()->notice( 'Manual purge of all pages' );
45 nitropack_json_and_exit( [
46 "type" => "success",
47 "message" => __( 'Success! Cache has been purged successfully!', 'nitropack' )
48 ] );
49 }
50 } catch (\Exception $e) {
51 NitroPack::getInstance()->getLogger()->error( 'Manual purge of all pages. Error: ' . $e );
52 }
53
54 nitropack_json_and_exit( [
55 "type" => "error",
56 "message" => __( 'Error! There was an error and the cache was not purged!', 'nitropack' )
57 ] );
58 }
59
60 /**
61 * AJAX handler when clicking Invalidate Entire Cache in admin topbar NitroPack menu
62 * Triggered in nitropack/assets/js/admin_bar_menu.min.js
63 * @return void
64 */
65 public function nitropack_invalidate_entire_cache() {
66 nitropack_verify_ajax_nonce( $_REQUEST );
67 try {
68 if ( nitropack_sdk_invalidate( NULL, NULL, 'Manual invalidation of all pages' ) ) {
69 NitroPack::getInstance()->getLogger()->notice( 'Manual invalidation of all pages' );
70 nitropack_json_and_exit( array(
71 "type" => "success",
72 "message" => __( 'Cache has been invalidated successfully!', 'nitropack' )
73
74 ) );
75 }
76 } catch (\Exception $e) {
77 NitroPack::getInstance()->getLogger()->error( 'Manual invalidation of all pages. Error: ' . $e );
78 }
79
80 nitropack_json_and_exit( array(
81 "type" => "error",
82 "message" => __( 'There was an error and the cache was not invalidated!', 'nitropack' )
83 ) );
84 }
85 /**
86 * AJAX handler when clicking Purge Cache in Dashboard > NitroPack. Performs light purge (excludes images).
87 * Triggered in nitropack/assets/js/np_settings.min.js -> clearCacheHandler()
88 * @return void
89 */
90 public function nitropack_purge_cache() {
91 nitropack_verify_ajax_nonce( $_REQUEST );
92 try {
93 if ( nitropack_sdk_purge( NULL, NULL, 'Light purge of all caches', \NitroPack\SDK\PurgeType::LIGHT_PURGE ) ) {
94 NitroPack::getInstance()->getLogger()->notice( 'Light purge of all caches' );
95 nitropack_json_and_exit( array(
96 "type" => "success",
97 "message" => __( 'Cache has been purged successfully!', 'nitropack' )
98 ) );
99 }
100 } catch (\Exception $e) {
101 NitroPack::getInstance()->getLogger()->error( 'Light purge of all caches. Error: ' . $e );
102 }
103 nitropack_json_and_exit( array(
104 "type" => "error",
105 "message" => __( 'Error! There was an error and the cache was not purged!', 'nitropack' )
106 ) );
107 }
108 /**
109 * Extended capabilities when purging or invalidating single post cache in a metabox.
110 * @return string[]
111 */
112 private function capabilities_prior_purge() {
113 $canEditorPurge = get_option( 'nitropack-canEditorClearCache' );
114 if ( $canEditorPurge ) {
115 return [ 'editor', 'manage_options' ];
116 } else {
117 return [ 'manage_options' ];
118 }
119 }
120 /**
121 * AJAX Handler when purging a single post cache via meta box.
122 * @return void
123 */
124 public function nitropack_purge_single_cache() {
125
126 $capabilities = $this->capabilities_prior_purge();
127 nitropack_verify_ajax_nonce( $_REQUEST, $capabilities );
128
129 if ( ! empty( $_POST["postId"] ) && is_numeric( $_POST["postId"] ) ) {
130 $postId = $_POST["postId"];
131 $postUrl = ! empty( $_POST["postUrl"] ) ? $_POST["postUrl"] : NULL;
132 $reason = sprintf( "Manual purge of post %s via the WordPress admin panel", $postId );
133 $tag = $postId > 0 ? "single:$postId" : NULL;
134
135 if ( $postUrl ) {
136 if ( is_array( $postUrl ) ) {
137 foreach ( $postUrl as &$url ) {
138 $url = nitropack_sanitize_url_input( $url );
139 }
140 } else {
141 $postUrl = nitropack_sanitize_url_input( $postUrl );
142 $reason = "Manual purge of " . $postUrl;
143 }
144 }
145
146 try {
147 if ( nitropack_sdk_purge( $postUrl, $tag, $reason ) ) {
148 NitroPack::getInstance()->getLogger()->notice( 'Manual purge of post ' . $postId . ' via WordPress.' );
149 nitropack_json_and_exit( array(
150 "type" => "success",
151 "message" => __( 'Success! Cache has been purged successfully!', 'nitropack' )
152 ) );
153 }
154 } catch (\Exception $e) {
155 NitroPack::getInstance()->getLogger()->error( 'Manual purge of post ' . $postId . ' via WordPress. Error: ' . $e );
156 }
157 }
158
159 nitropack_json_and_exit( array(
160 "type" => "error",
161 "message" => __( 'Error! There was an error and the cache was not purged!', 'nitropack' )
162 ) );
163 }
164
165 /**
166 * AJAX handler when invalidating single post cache via metabox.
167 * @return void
168 */
169 public function nitropack_invalidate_single_cache() {
170
171 $capabilities = $this->capabilities_prior_purge();
172 nitropack_verify_ajax_nonce( $_REQUEST, $capabilities );
173
174 if ( ! empty( $_POST["postId"] ) && is_numeric( $_POST["postId"] ) ) {
175 $postId = $_POST["postId"];
176 $postUrl = ! empty( $_POST["postUrl"] ) ? $_POST["postUrl"] : NULL;
177 $reason = sprintf( "Manual invalidation of post %s via the WordPress admin panel", $postId );
178 $tag = $postId > 0 ? "single:$postId" : NULL;
179
180 if ( $postUrl ) {
181 if ( is_array( $postUrl ) ) {
182 foreach ( $postUrl as &$url ) {
183 $url = nitropack_sanitize_url_input( $url );
184 }
185 } else {
186 $postUrl = nitropack_sanitize_url_input( $postUrl );
187 $reason = "Manual invalidation of " . $postUrl;
188 }
189 }
190
191 try {
192 if ( nitropack_sdk_invalidate( $postUrl, $tag, $reason ) ) {
193 NitroPack::getInstance()->getLogger()->notice( 'Manual invalidation of post ' . $postId . ' via WordPress.' );
194 nitropack_json_and_exit( array(
195 "type" => "success",
196 "message" => __( 'Success! Cache has been invalidated successfully!', 'nitropack' )
197 ) );
198 }
199 } catch (\Exception $e) {
200 NitroPack::getInstance()->getLogger()->error( 'Manual invalidation of post ' . $postId . ' via WordPress. Error: ' . $e );
201 }
202 }
203
204 nitropack_json_and_exit( array(
205 "type" => "error",
206 "message" => __( 'Error! There was an error and the cache was not invalidated!', 'nitropack' )
207 ) );
208 }
209
210 /**
211 * AJAX handler when clicking "Delete now" in residual cache message in Dashboard. Deletes 3rd party cache files.
212 * Notification => "We found residual cache files from %s. These files can interfere with the caching process and must be deleted."
213 * @return void
214 */
215 public function nitropack_clear_residual_cache() {
216 nitropack_verify_ajax_nonce( $_REQUEST );
217 $gde = ! empty( $_POST["gde"] ) ? $_POST["gde"] : NULL;
218 if ( $gde && array_key_exists( $gde, ResidualCache::$modules ) ) {
219 $result = call_user_func( array( ResidualCache::$modules[ $gde ], "clearCache" ) ); // This needs to be like this because of compatibility with PHP 5.6
220 if ( ! in_array( false, $result ) ) {
221 NitroPack::getInstance()->getLogger()->notice( 'Manual clearing of residual cache via WordPress.' );
222 nitropack_json_and_exit( array(
223 "type" => "success",
224 "message" => __( 'Success! The residual cache has been cleared successfully!', 'nitropack' )
225 ) );
226 }
227 }
228 nitropack_json_and_exit( array(
229 "type" => "error",
230 "message" => __( 'Error! There was an error clearing the residual cache!', 'nitropack' )
231 ) );
232 }
233
234 /**
235 * Capabilities of cleaning single post cache
236 * @return string[]
237 */
238 public function clean_cache_capabilities() {
239 $canEditorPurge = get_option( 'nitropack-canEditorClearCache' );
240 if ( $canEditorPurge ) {
241 return [ 'editor', 'manage_options' ];
242 } else {
243 return [ 'manage_options' ];
244 }
245 }
246 /** Checks capabilities and adds meta box to post types that can have "single" pages
247 */
248 public function nitropack_meta_box() {
249 $editor = get_option( "nitropack-canEditorClearCache" );
250 $allowed_capabilities = current_user_can( 'manage_options' ) || current_user_can( 'nitropack_meta_box' ) || ( $editor && current_user_can( 'editor' ) );
251 if ( $allowed_capabilities ) {
252 $cptOptimization = CPTOptimization::getInstance();
253 foreach ( $cptOptimization->nitropack_get_cacheable_object_types() as $objectType ) {
254 add_meta_box( 'nitropack_manage_cache_box', 'NitroPack', [ $this, 'nitropack_print_meta_box' ], $objectType, 'side' );
255 }
256 }
257 }
258
259 /** HTML rendered meta boxes. Used for post types that can have "single" pages
260 */
261 public function nitropack_print_meta_box( $post ) {
262 $html = '<p><a class="button nitropack-invalidate-single" data-post_id="' . $post->ID . '" data-post_url="' . get_permalink( $post ) . '" style="width:100%;text-align:center;padding: 3px 0;">Invalidate cache</a></p>';
263 $html .= '<p><a class="button nitropack-purge-single" data-post_id="' . $post->ID . '" data-post_url="' . get_permalink( $post ) . '" style="width:100%;text-align:center;padding: 3px 0;">Purge cache</a></p>';
264 $html .= '<p id="nitropack-status-msg" style="display:none;"></p>';
265 echo $html;
266 }
267
268 /**
269 * Add 2 extra links in wp-admin post listing, under each post on hover
270 * @param array $actions
271 * @param mixed $post
272 */
273 public function purge_invalidate_post_links( $actions, $post ) {
274 //chgeck if the CPT is cacheable
275 $CPTOptimization = CPTOptimization::getInstance();
276 $cacheableObjectTypes = $CPTOptimization->nitropack_get_cacheable_object_types();
277 if ( ! in_array( $post->post_type, $cacheableObjectTypes ) ) {
278 return $actions;
279 }
280
281 //check if the user has permissions
282 $editor = get_option( "nitropack-canEditorClearCache" );
283 $allowed_capabilities = current_user_can( 'manage_options' ) || ( $editor && current_user_can( 'editor' ) );
284 if ( ! $allowed_capabilities ) {
285 return $actions;
286 }
287
288 $permalink = get_permalink( $post->ID );
289 if ( ! empty( $permalink ) ) {
290 $actions['nitropack_purge'] = '<a href="#" class="nitropack-purge-single" data-post_id="' . $post->ID . '" data-post_url="' . get_permalink( $post ) . '" ">' . __( 'Purge Cache', 'nitropack' ) . '</a>';
291 $actions['nitropack_invalidate'] = '<a href="#" class="nitropack-invalidate-single" data-post_id="' . $post->ID . '" data-post_url="' . get_permalink( $post ) . '" ">' . __( 'Invalidate Cache', 'nitropack' ) . '</a>';
292 }
293
294 return $actions;
295 }
296 /**
297 * Purge entire cache when permalink structure is changed.
298 *
299 * @param string $old_permalink_structure The previous permalink structure.
300 * @param string $permalink_structure The new permalink structure.
301 *
302 * @return void
303 */
304 public function nitropack_permalink_structure_changed_handler( $old_permalink_structure, $permalink_structure ) {
305
306 if ( $old_permalink_structure != $permalink_structure && get_option( "nitropack-autoCachePurge", 1 ) ) {
307 $msg = 'The permalink structure is changed. Purging the cache for the home page.';
308 $url = get_home_url();
309
310 nitropack_sdk_purge( $url, null, $msg );
311
312 // run warmup
313 if ( null !== $nitro = get_nitropack_sdk() ) {
314 $nitro->getApi()->runWarmup();
315 }
316 }
317 }
318 /**
319 * Purge entire cache when front page is changed.
320 *
321 * @param array $old_value An array of previous settings values.
322 * @param array $value An array of submitted settings values.
323 *
324 * @return void
325 */
326 public function nitropack_frontpage_changed_handler( $old_value, $value ) {
327
328 if ( $old_value !== $value ) {
329 $msg = 'The front page is changed';
330 $url = get_home_url();
331
332 nitropack_sdk_purge( $url, null, $msg ); // purge entire cache
333
334 }
335 }
336
337
338 }