PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.5
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.5
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 1 month ago Optimizations.php 1 month ago PurgeCache.php 1 month 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
291 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 }
29
30 /**
31 * AJAX handler when clicking Purge Entire Cache in admin topbar NitroPack menu
32 * Triggered in nitropack/assets/js/admin_bar_menu.min.js
33 * @return void
34 */
35 public function nitropack_purge_entire_cache() {
36 nitropack_verify_ajax_nonce( $_REQUEST );
37 try {
38 if ( nitropack_sdk_purge( null, null, 'Manual purge of all pages' ) ) {
39 NitroPack::getInstance()->getLogger()->notice( 'Manual purge of all pages' );
40 nitropack_json_and_exit( [
41 "type" => "success",
42 "message" => __( 'Success! Cache has been purged successfully!', 'nitropack' )
43 ] );
44 }
45 } catch (\Exception $e) {
46 NitroPack::getInstance()->getLogger()->error( 'Manual purge of all pages. Error: ' . $e );
47 }
48
49 nitropack_json_and_exit( [
50 "type" => "error",
51 "message" => __( 'Error! There was an error and the cache was not purged!', 'nitropack' )
52 ] );
53 }
54
55 /**
56 * AJAX handler when clicking Invalidate Entire Cache in admin topbar NitroPack menu
57 * Triggered in nitropack/assets/js/admin_bar_menu.min.js
58 * @return void
59 */
60 public function nitropack_invalidate_entire_cache() {
61 nitropack_verify_ajax_nonce( $_REQUEST );
62 try {
63 if ( nitropack_sdk_invalidate( NULL, NULL, 'Manual invalidation of all pages' ) ) {
64 NitroPack::getInstance()->getLogger()->notice( 'Manual invalidation of all pages' );
65 nitropack_json_and_exit( array(
66 "type" => "success",
67 "message" => __( 'Cache has been invalidated successfully!', 'nitropack' )
68
69 ) );
70 }
71 } catch (\Exception $e) {
72 NitroPack::getInstance()->getLogger()->error( 'Manual invalidation of all pages. Error: ' . $e );
73 }
74
75 nitropack_json_and_exit( array(
76 "type" => "error",
77 "message" => __( 'There was an error and the cache was not invalidated!', 'nitropack' )
78 ) );
79 }
80 /**
81 * AJAX handler when clicking Purge Cache in Dashboard > NitroPack. Performs light purge (excludes images).
82 * Triggered in nitropack/assets/js/np_settings.min.js -> clearCacheHandler()
83 * @return void
84 */
85 public function nitropack_purge_cache() {
86 nitropack_verify_ajax_nonce( $_REQUEST );
87 try {
88 if ( nitropack_sdk_purge( NULL, NULL, 'Light purge of all caches', \NitroPack\SDK\PurgeType::LIGHT_PURGE ) ) {
89 NitroPack::getInstance()->getLogger()->notice( 'Light purge of all caches' );
90 nitropack_json_and_exit( array(
91 "type" => "success",
92 "message" => __( 'Cache has been purged successfully!', 'nitropack' )
93 ) );
94 }
95 } catch (\Exception $e) {
96 NitroPack::getInstance()->getLogger()->error( 'Light purge of all caches. Error: ' . $e );
97 }
98 nitropack_json_and_exit( array(
99 "type" => "error",
100 "message" => __( 'Error! There was an error and the cache was not purged!', 'nitropack' )
101 ) );
102 }
103 /**
104 * Extended capabilities when purging or invalidating single post cache in a metabox.
105 * @return string[]
106 */
107 private function capabilities_prior_purge() {
108 $canEditorPurge = get_option( 'nitropack-canEditorClearCache' );
109 if ( $canEditorPurge ) {
110 return [ 'editor', 'manage_options' ];
111 } else {
112 return [ 'manage_options' ];
113 }
114 }
115 /**
116 * AJAX Handler when purging a single post cache via meta box.
117 * @return void
118 */
119 public function nitropack_purge_single_cache() {
120
121 $capabilities = $this->capabilities_prior_purge();
122 nitropack_verify_ajax_nonce( $_REQUEST, $capabilities );
123
124 if ( ! empty( $_POST["postId"] ) && is_numeric( $_POST["postId"] ) ) {
125 $postId = $_POST["postId"];
126 $postUrl = ! empty( $_POST["postUrl"] ) ? $_POST["postUrl"] : NULL;
127 $reason = sprintf( "Manual purge of post %s via the WordPress admin panel", $postId );
128 $tag = $postId > 0 ? "single:$postId" : NULL;
129
130 if ( $postUrl ) {
131 if ( is_array( $postUrl ) ) {
132 foreach ( $postUrl as &$url ) {
133 $url = nitropack_sanitize_url_input( $url );
134 }
135 } else {
136 $postUrl = nitropack_sanitize_url_input( $postUrl );
137 $reason = "Manual purge of " . $postUrl;
138 }
139 }
140
141 try {
142 if ( nitropack_sdk_purge( $postUrl, $tag, $reason ) ) {
143 NitroPack::getInstance()->getLogger()->notice( 'Manual purge of post ' . $postId . ' via WordPress.' );
144 nitropack_json_and_exit( array(
145 "type" => "success",
146 "message" => __( 'Success! Cache has been purged successfully!', 'nitropack' )
147 ) );
148 }
149 } catch (\Exception $e) {
150 NitroPack::getInstance()->getLogger()->error( 'Manual purge of post ' . $postId . ' via WordPress. Error: ' . $e );
151 }
152 }
153
154 nitropack_json_and_exit( array(
155 "type" => "error",
156 "message" => __( 'Error! There was an error and the cache was not purged!', 'nitropack' )
157 ) );
158 }
159
160 /**
161 * AJAX handler when invalidating single post cache via metabox.
162 * @return void
163 */
164 public function nitropack_invalidate_single_cache() {
165
166 $capabilities = $this->capabilities_prior_purge();
167 nitropack_verify_ajax_nonce( $_REQUEST, $capabilities );
168
169 if ( ! empty( $_POST["postId"] ) && is_numeric( $_POST["postId"] ) ) {
170 $postId = $_POST["postId"];
171 $postUrl = ! empty( $_POST["postUrl"] ) ? $_POST["postUrl"] : NULL;
172 $reason = sprintf( "Manual invalidation of post %s via the WordPress admin panel", $postId );
173 $tag = $postId > 0 ? "single:$postId" : NULL;
174
175 if ( $postUrl ) {
176 if ( is_array( $postUrl ) ) {
177 foreach ( $postUrl as &$url ) {
178 $url = nitropack_sanitize_url_input( $url );
179 }
180 } else {
181 $postUrl = nitropack_sanitize_url_input( $postUrl );
182 $reason = "Manual invalidation of " . $postUrl;
183 }
184 }
185
186 try {
187 if ( nitropack_sdk_invalidate( $postUrl, $tag, $reason ) ) {
188 NitroPack::getInstance()->getLogger()->notice( 'Manual invalidation of post ' . $postId . ' via WordPress.' );
189 nitropack_json_and_exit( array(
190 "type" => "success",
191 "message" => __( 'Success! Cache has been invalidated successfully!', 'nitropack' )
192 ) );
193 }
194 } catch (\Exception $e) {
195 NitroPack::getInstance()->getLogger()->error( 'Manual invalidation of post ' . $postId . ' via WordPress. Error: ' . $e );
196 }
197 }
198
199 nitropack_json_and_exit( array(
200 "type" => "error",
201 "message" => __( 'Error! There was an error and the cache was not invalidated!', 'nitropack' )
202 ) );
203 }
204
205 /**
206 * AJAX handler when clicking "Delete now" in residual cache message in Dashboard. Deletes 3rd party cache files.
207 * Notification => "We found residual cache files from %s. These files can interfere with the caching process and must be deleted."
208 * @return void
209 */
210 public function nitropack_clear_residual_cache() {
211 nitropack_verify_ajax_nonce( $_REQUEST );
212 $gde = ! empty( $_POST["gde"] ) ? $_POST["gde"] : NULL;
213 if ( $gde && array_key_exists( $gde, ResidualCache::$modules ) ) {
214 $result = call_user_func( array( ResidualCache::$modules[ $gde ], "clearCache" ) ); // This needs to be like this because of compatibility with PHP 5.6
215 if ( ! in_array( false, $result ) ) {
216 NitroPack::getInstance()->getLogger()->notice( 'Manual clearing of residual cache via WordPress.' );
217 nitropack_json_and_exit( array(
218 "type" => "success",
219 "message" => __( 'Success! The residual cache has been cleared successfully!', 'nitropack' )
220 ) );
221 }
222 }
223 nitropack_json_and_exit( array(
224 "type" => "error",
225 "message" => __( 'Error! There was an error clearing the residual cache!', 'nitropack' )
226 ) );
227 }
228
229 /**
230 * Capabilities of cleaning single post cache
231 * @return string[]
232 */
233 public function clean_cache_capabilities() {
234 $canEditorPurge = get_option( 'nitropack-canEditorClearCache' );
235 if ( $canEditorPurge ) {
236 return [ 'editor', 'manage_options' ];
237 } else {
238 return [ 'manage_options' ];
239 }
240 }
241 /** Checks capabilities and adds meta box to post types that can have "single" pages
242 */
243 public function nitropack_meta_box() {
244 $editor = get_option( "nitropack-canEditorClearCache" );
245 $allowed_capabilities = current_user_can( 'manage_options' ) || current_user_can( 'nitropack_meta_box' ) || ( $editor && current_user_can( 'editor' ) );
246 if ( $allowed_capabilities ) {
247 $cptOptimization = CPTOptimization::getInstance();
248 foreach ( $cptOptimization->nitropack_get_cacheable_object_types() as $objectType ) {
249 add_meta_box( 'nitropack_manage_cache_box', 'NitroPack', [ $this, 'nitropack_print_meta_box' ], $objectType, 'side' );
250 }
251 }
252 }
253
254 /** HTML rendered meta boxes. Used for post types that can have "single" pages
255 */
256 public function nitropack_print_meta_box( $post ) {
257 $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>';
258 $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>';
259 $html .= '<p id="nitropack-status-msg" style="display:none;"></p>';
260 echo $html;
261 }
262
263 /**
264 * Add 2 extra links in wp-admin post listing, under each post on hover
265 * @param array $actions
266 * @param mixed $post
267 */
268 public function purge_invalidate_post_links( $actions, $post ) {
269 //chgeck if the CPT is cacheable
270 $CPTOptimization = CPTOptimization::getInstance();
271 $cacheableObjectTypes = $CPTOptimization->nitropack_get_cacheable_object_types();
272 if ( ! in_array( $post->post_type, $cacheableObjectTypes ) ) {
273 return $actions;
274 }
275
276 //check if the user has permissions
277 $editor = get_option( "nitropack-canEditorClearCache" );
278 $allowed_capabilities = current_user_can( 'manage_options' ) || ( $editor && current_user_can( 'editor' ) );
279 if ( ! $allowed_capabilities ) {
280 return $actions;
281 }
282
283 $permalink = get_permalink( $post->ID );
284 if ( ! empty( $permalink ) ) {
285 $actions['nitropack_purge'] = '<a href="#" class="nitropack-purge-single" data-post_id="' . $post->ID . '" data-post_url="' . get_permalink( $post ) . '" ">' . __( 'Purge Cache', 'nitropack' ) . '</a>';
286 $actions['nitropack_invalidate'] = '<a href="#" class="nitropack-invalidate-single" data-post_id="' . $post->ID . '" data-post_url="' . get_permalink( $post ) . '" ">' . __( 'Invalidate Cache', 'nitropack' ) . '</a>';
287 }
288
289 return $actions;
290 }
291 }