AutoPurge.php
4 months ago
BeaverBuilder.php
4 months ago
CPTOptimization.php
4 months ago
CacheWarmup.php
4 months ago
CartCache.php
4 months ago
Components.php
4 months ago
EditorClearCache.php
4 months ago
GeneratePreview.php
7 months ago
HTMLCompression.php
4 months ago
Logger.php
4 months ago
OptimizationLevel.php
4 months ago
Optimizations.php
4 months ago
PurgeCache.php
4 months ago
Shortcodes.php
4 months ago
StockRefresh.php
4 months ago
Subscription.php
4 months ago
SystemReport.php
4 months ago
TestMode.php
4 months ago
PurgeCache.php
294 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/view/javascript/admin_bar_menu.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/view/javascript/admin_bar_menu.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/view/javascript/np_settings.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 | * Triggered in nitropack/view/javascript/metabox.js |
| 106 | * @return string[] |
| 107 | */ |
| 108 | private function capabilities_prior_purge() { |
| 109 | $canEditorPurge = get_option( 'nitropack-canEditorClearCache' ); |
| 110 | if ( $canEditorPurge ) { |
| 111 | return [ 'editor', 'manage_options' ]; |
| 112 | } else { |
| 113 | return [ 'manage_options' ]; |
| 114 | } |
| 115 | } |
| 116 | /** |
| 117 | * AJAX Handler when purging a single post cache via meta box |
| 118 | * Triggered in nitropack/view/javascript/metabox.js |
| 119 | * @return void |
| 120 | */ |
| 121 | public function nitropack_purge_single_cache() { |
| 122 | |
| 123 | $capabilities = $this->capabilities_prior_purge(); |
| 124 | nitropack_verify_ajax_nonce( $_REQUEST, $capabilities ); |
| 125 | |
| 126 | if ( ! empty( $_POST["postId"] ) && is_numeric( $_POST["postId"] ) ) { |
| 127 | $postId = $_POST["postId"]; |
| 128 | $postUrl = ! empty( $_POST["postUrl"] ) ? $_POST["postUrl"] : NULL; |
| 129 | $reason = sprintf( "Manual purge of post %s via the WordPress admin panel", $postId ); |
| 130 | $tag = $postId > 0 ? "single:$postId" : NULL; |
| 131 | |
| 132 | if ( $postUrl ) { |
| 133 | if ( is_array( $postUrl ) ) { |
| 134 | foreach ( $postUrl as &$url ) { |
| 135 | $url = nitropack_sanitize_url_input( $url ); |
| 136 | } |
| 137 | } else { |
| 138 | $postUrl = nitropack_sanitize_url_input( $postUrl ); |
| 139 | $reason = "Manual purge of " . $postUrl; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | try { |
| 144 | if ( nitropack_sdk_purge( $postUrl, $tag, $reason ) ) { |
| 145 | NitroPack::getInstance()->getLogger()->notice( 'Manual purge of post ' . $postId . ' via WordPress.' ); |
| 146 | nitropack_json_and_exit( array( |
| 147 | "type" => "success", |
| 148 | "message" => __( 'Success! Cache has been purged successfully!', 'nitropack' ) |
| 149 | ) ); |
| 150 | } |
| 151 | } catch (\Exception $e) { |
| 152 | NitroPack::getInstance()->getLogger()->error( 'Manual purge of post ' . $postId . ' via WordPress. Error: ' . $e ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | nitropack_json_and_exit( array( |
| 157 | "type" => "error", |
| 158 | "message" => __( 'Error! There was an error and the cache was not purged!', 'nitropack' ) |
| 159 | ) ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * AJAX handler when invalidating single post cache via metabox |
| 164 | * Triggered in nitropack/view/javascript/metabox.js |
| 165 | * @return void |
| 166 | */ |
| 167 | public function nitropack_invalidate_single_cache() { |
| 168 | |
| 169 | $capabilities = $this->capabilities_prior_purge(); |
| 170 | nitropack_verify_ajax_nonce( $_REQUEST, $capabilities ); |
| 171 | |
| 172 | if ( ! empty( $_POST["postId"] ) && is_numeric( $_POST["postId"] ) ) { |
| 173 | $postId = $_POST["postId"]; |
| 174 | $postUrl = ! empty( $_POST["postUrl"] ) ? $_POST["postUrl"] : NULL; |
| 175 | $reason = sprintf( "Manual invalidation of post %s via the WordPress admin panel", $postId ); |
| 176 | $tag = $postId > 0 ? "single:$postId" : NULL; |
| 177 | |
| 178 | if ( $postUrl ) { |
| 179 | if ( is_array( $postUrl ) ) { |
| 180 | foreach ( $postUrl as &$url ) { |
| 181 | $url = nitropack_sanitize_url_input( $url ); |
| 182 | } |
| 183 | } else { |
| 184 | $postUrl = nitropack_sanitize_url_input( $postUrl ); |
| 185 | $reason = "Manual invalidation of " . $postUrl; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | try { |
| 190 | if ( nitropack_sdk_invalidate( $postUrl, $tag, $reason ) ) { |
| 191 | NitroPack::getInstance()->getLogger()->notice( 'Manual invalidation of post ' . $postId . ' via WordPress.' ); |
| 192 | nitropack_json_and_exit( array( |
| 193 | "type" => "success", |
| 194 | "message" => __( 'Success! Cache has been invalidated successfully!', 'nitropack' ) |
| 195 | ) ); |
| 196 | } |
| 197 | } catch (\Exception $e) { |
| 198 | NitroPack::getInstance()->getLogger()->error( 'Manual invalidation of post ' . $postId . ' via WordPress. Error: ' . $e ); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | nitropack_json_and_exit( array( |
| 203 | "type" => "error", |
| 204 | "message" => __( 'Error! There was an error and the cache was not invalidated!', 'nitropack' ) |
| 205 | ) ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * AJAX handler when clicking "Delete now" in residual cache message in Dashboard. Deletes 3rd party cache files. |
| 210 | * Notification => "We found residual cache files from %s. These files can interfere with the caching process and must be deleted." |
| 211 | * @return void |
| 212 | */ |
| 213 | public function nitropack_clear_residual_cache() { |
| 214 | nitropack_verify_ajax_nonce( $_REQUEST ); |
| 215 | $gde = ! empty( $_POST["gde"] ) ? $_POST["gde"] : NULL; |
| 216 | if ( $gde && array_key_exists( $gde, ResidualCache::$modules ) ) { |
| 217 | $result = call_user_func( array( ResidualCache::$modules[ $gde ], "clearCache" ) ); // This needs to be like this because of compatibility with PHP 5.6 |
| 218 | if ( ! in_array( false, $result ) ) { |
| 219 | NitroPack::getInstance()->getLogger()->notice( 'Manual clearing of residual cache via WordPress.' ); |
| 220 | nitropack_json_and_exit( array( |
| 221 | "type" => "success", |
| 222 | "message" => __( 'Success! The residual cache has been cleared successfully!', 'nitropack' ) |
| 223 | ) ); |
| 224 | } |
| 225 | } |
| 226 | nitropack_json_and_exit( array( |
| 227 | "type" => "error", |
| 228 | "message" => __( 'Error! There was an error clearing the residual cache!', 'nitropack' ) |
| 229 | ) ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Capabilities of cleaning single post cache |
| 234 | * @return string[] |
| 235 | */ |
| 236 | public function clean_cache_capabilities() { |
| 237 | $canEditorPurge = get_option( 'nitropack-canEditorClearCache' ); |
| 238 | if ( $canEditorPurge ) { |
| 239 | return [ 'editor', 'manage_options' ]; |
| 240 | } else { |
| 241 | return [ 'manage_options' ]; |
| 242 | } |
| 243 | } |
| 244 | /** Checks capabilities and adds meta box to post types that can have "single" pages |
| 245 | */ |
| 246 | public function nitropack_meta_box() { |
| 247 | $editor = get_option( "nitropack-canEditorClearCache" ); |
| 248 | $allowed_capabilities = current_user_can( 'manage_options' ) || current_user_can( 'nitropack_meta_box' ) || ( $editor && current_user_can( 'editor' ) ); |
| 249 | if ( $allowed_capabilities ) { |
| 250 | $cptOptimization = CPTOptimization::getInstance(); |
| 251 | foreach ( $cptOptimization->nitropack_get_cacheable_object_types() as $objectType ) { |
| 252 | add_meta_box( 'nitropack_manage_cache_box', 'NitroPack', [ $this, 'nitropack_print_meta_box' ], $objectType, 'side' ); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** HTML rendered meta boxes. Used for post types that can have "single" pages |
| 258 | */ |
| 259 | public function nitropack_print_meta_box( $post ) { |
| 260 | $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>'; |
| 261 | $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>'; |
| 262 | $html .= '<p id="nitropack-status-msg" style="display:none;"></p>'; |
| 263 | echo $html; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Add 2 extra links in wp-admin post listing, under each post on hover |
| 268 | * @param array $actions |
| 269 | * @param mixed $post |
| 270 | */ |
| 271 | public function purge_invalidate_post_links( $actions, $post ) { |
| 272 | //chgeck if the CPT is cacheable |
| 273 | $CPTOptimization = CPTOptimization::getInstance(); |
| 274 | $cacheableObjectTypes = $CPTOptimization->nitropack_get_cacheable_object_types(); |
| 275 | if ( ! in_array( $post->post_type, $cacheableObjectTypes ) ) { |
| 276 | return $actions; |
| 277 | } |
| 278 | |
| 279 | //check if the user has permissions |
| 280 | $editor = get_option( "nitropack-canEditorClearCache" ); |
| 281 | $allowed_capabilities = current_user_can( 'manage_options' ) || ( $editor && current_user_can( 'editor' ) ); |
| 282 | if ( ! $allowed_capabilities ) { |
| 283 | return $actions; |
| 284 | } |
| 285 | |
| 286 | $permalink = get_permalink( $post->ID ); |
| 287 | if ( ! empty( $permalink ) ) { |
| 288 | $actions['nitropack_purge'] = '<a href="#" class="nitropack-purge-single" data-post_id="' . $post->ID . '" data-post_url="' . get_permalink( $post ) . '" ">' . __( 'Purge Cache', 'nitropack' ) . '</a>'; |
| 289 | $actions['nitropack_invalidate'] = '<a href="#" class="nitropack-invalidate-single" data-post_id="' . $post->ID . '" data-post_url="' . get_permalink( $post ) . '" ">' . __( 'Invalidate Cache', 'nitropack' ) . '</a>'; |
| 290 | } |
| 291 | |
| 292 | return $actions; |
| 293 | } |
| 294 | } |