| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace SpinupWp; | |
| 3 | +namespace DeliciousBrains\SpinupWp; | |
| 4 | 4 | |
| 5 | -use SpinupWp\Cli\CacheCommands; | |
| 5 | +use DeliciousBrains\SpinupWp\Cli\CacheCommands; | |
| 6 | 6 | use WP_Post; |
| 7 | 7 | |
| 8 | 8 | class Cache { |
| 9 | 9 | |
| @@ -37,29 +37,8 @@ | ||
| 37 | 37 | */ |
| 38 | 38 | public function init() { |
| 39 | 39 | $this->set_cache_path(); |
| 40 | 40 | |
| 41 | - $this->cli->register_command( 'spinupwp cache', CacheCommands::class ); | |
| 42 | - | |
| 43 | - add_action( 'init', array( $this, 'register_admin_bar_menu_items' ) ); | |
| 44 | - add_action( 'spinupwp_purge_object_cache', array( $this, 'purge_object_cache' ) ); | |
| 45 | - add_action( 'spinupwp_purge_page_cache', array( $this, 'purge_page_cache' ) ); | |
| 46 | - add_action( 'spinupwp_purge_url', array( $this, 'purge_url' ) ); | |
| 47 | - add_action( 'admin_init', array( $this, 'handle_manual_purge_action' ) ); | |
| 48 | - add_action( 'transition_post_status', array( $this, 'purge_post_on_update' ), 10, 3 ); | |
| 49 | - add_action( 'delete_post', array( $this, 'purge_post_on_delete' ), 10, 1 ); | |
| 50 | - add_action( 'switch_theme', array( $this, 'purge_page_cache' ) ); | |
| 51 | - add_action( 'comment_post', array( $this, 'purge_post_on_comment' ), 10, 2 ); | |
| 52 | - add_action( 'wp_set_comment_status', array( $this, 'purge_post_by_comment' ) ); | |
| 53 | - add_action( 'upgrader_process_complete', array( $this, 'purge_page_cache_on_shutdown' ) ); | |
| 54 | - } | |
| 55 | - | |
| 56 | - /** | |
| 57 | - * Register cache purge menu items in the admin bar. | |
| 58 | - * | |
| 59 | - * @return void | |
| 60 | - */ | |
| 61 | - public function register_admin_bar_menu_items() { | |
| 62 | 41 | if ( $this->is_object_cache_enabled() && $this->is_page_cache_enabled() ) { |
| 63 | 42 | $this->admin_bar->add_item( __( 'Purge All Caches', 'spinupwp' ), 'purge-all' ); |
| 64 | 43 | } |
| 65 | 44 | |
| @@ -68,13 +47,19 @@ | ||
| 68 | 47 | } |
| 69 | 48 | |
| 70 | 49 | if ( $this->is_page_cache_enabled() ) { |
| 71 | 50 | $this->admin_bar->add_item( __( 'Purge Page Cache', 'spinupwp' ), 'purge-page' ); |
| 51 | + $this->cli->register_command( 'spinupwp cache', CacheCommands::class ); | |
| 72 | 52 | } |
| 73 | 53 | |
| 74 | - if ( $this->is_page_cache_enabled() && ! is_admin() ) { | |
| 75 | - $this->admin_bar->add_item( __( 'Purge this URL', 'spinupwp' ), 'purge-url' ); | |
| 76 | - } | |
| 54 | + add_action( 'spinupwp_purge_object_cache', array( $this, 'purge_object_cache' ) ); | |
| 55 | + add_action( 'spinupwp_purge_page_cache', array( $this, 'purge_page_cache' ) ); | |
| 56 | + add_action( 'admin_init', array( $this, 'handle_manual_purge_action' ) ); | |
| 57 | + add_action( 'transition_post_status', array( $this, 'purge_post_on_update' ), 10, 3 ); | |
| 58 | + add_action( 'delete_post', array( $this, 'purge_post_on_delete' ), 10, 1 ); | |
| 59 | + add_action( 'switch_theme', array( $this, 'purge_page_cache' ) ); | |
| 60 | + add_action( 'comment_post', array( $this, 'purge_post_on_comment' ), 10, 2 ); | |
| 61 | + add_action( 'wp_set_comment_status', array( $this, 'purge_post_by_comment' ) ); | |
| 77 | 62 | } |
| 78 | 63 | |
| 79 | 64 | |
| 80 | 65 | /** |
| @@ -86,9 +71,9 @@ | ||
| 86 | 71 | } |
| 87 | 72 | |
| 88 | 73 | $action = filter_input( INPUT_GET, 'spinupwp_action' ); |
| 89 | 74 | |
| 90 | - if ( ! $action || ! in_array( $action, array( 'purge-all', 'purge-object', 'purge-page', 'purge-url' ) ) ) { | |
| 75 | + if ( ! $action || ! in_array( $action, array( 'purge-all', 'purge-object', 'purge-page' ) ) ) { | |
| 91 | 76 | return; |
| 92 | 77 | } |
| 93 | 78 | |
| 94 | 79 | if ( ! wp_verify_nonce( filter_input( INPUT_GET, '_wpnonce' ), $action ) ) { |
| @@ -95,12 +80,9 @@ | ||
| 95 | 80 | return; |
| 96 | 81 | } |
| 97 | 82 | |
| 98 | 83 | if ( 'purge-all' === $action ) { |
| 99 | - $purge_object_cache = $this->purge_object_cache(); | |
| 100 | - $purge_page_cache = $this->purge_page_cache(); | |
| 101 | - | |
| 102 | - $purge = $purge_object_cache && $purge_page_cache; | |
| 84 | + $purge = $this->purge_object_cache() && $this->purge_page_cache(); | |
| 103 | 85 | $type = 'all'; |
| 104 | 86 | } |
| 105 | 87 | |
| 106 | 88 | if ( 'purge-object' === $action ) { |
| @@ -112,14 +94,8 @@ | ||
| 112 | 94 | $purge = $this->purge_page_cache(); |
| 113 | 95 | $type = 'page'; |
| 114 | 96 | } |
| 115 | 97 | |
| 116 | - if ( 'purge-url' === $action ) { | |
| 117 | - $url = $_SERVER['HTTP_REFERER']; | |
| 118 | - $purge = $this->purge_url( $url ); | |
| 119 | - $type = 'url'; | |
| 120 | - } | |
| 121 | - | |
| 122 | 98 | $redirect_url = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : admin_url(); |
| 123 | 99 | |
| 124 | 100 | wp_safe_redirect( add_query_arg( array( |
| 125 | 101 | 'purge_success' => (int) $purge, |
| @@ -227,15 +203,8 @@ | ||
| 227 | 203 | return false; |
| 228 | 204 | } |
| 229 | 205 | |
| 230 | 206 | /** |
| 231 | - * Purge the page cache on shutdown. | |
| 232 | - */ | |
| 233 | - public function purge_page_cache_on_shutdown() { | |
| 234 | - add_action( 'shutdown', array( $this, 'purge_page_cache' ) ); | |
| 235 | - } | |
| 236 | - | |
| 237 | - /** | |
| 238 | 207 | * Set the base cache path. |
| 239 | 208 | */ |
| 240 | 209 | private function set_cache_path() { |
| 241 | 210 | if ( getenv( 'SPINUPWP_CACHE_PATH' ) ) { |
| @@ -309,69 +278,36 @@ | ||
| 309 | 278 | * |
| 310 | 279 | * @return bool |
| 311 | 280 | */ |
| 312 | 281 | public function purge_url( $url ) { |
| 313 | - $cache_paths = $this->get_cache_paths_for_url( $url ); | |
| 282 | + $path = $this->get_cache_path_for_url( $url ); | |
| 283 | + $result = $this->delete( $path ); | |
| 314 | 284 | |
| 315 | - $all_deleted = true; | |
| 316 | - foreach ( $cache_paths as $path ) { | |
| 317 | - $deleted = $this->delete( $path ); | |
| 318 | - do_action( 'spinupwp_url_purged', $url, $deleted ); | |
| 319 | - $all_deleted &= $deleted; | |
| 320 | - } | |
| 285 | + do_action( 'spinupwp_url_purged', $url, $result ); | |
| 321 | 286 | |
| 322 | - return $all_deleted; | |
| 287 | + return $result; | |
| 323 | 288 | } |
| 324 | 289 | |
| 325 | 290 | /** |
| 326 | - * Gets the cache file paths for a given URL. | |
| 291 | + * Get's the cache file path for a given URL. | |
| 327 | 292 | * |
| 328 | 293 | * Must be using the default Nginx cache options (levels=1:2) |
| 294 | + * and (fastcgi_cache_key "$scheme$request_method$host$request_uri"). | |
| 329 | 295 | * https://www.digitalocean.com/community/tutorials/how-to-setup-fastcgi-caching-with-nginx-on-your-vps#purging-the-cache |
| 330 | 296 | * |
| 331 | 297 | * @param string $url |
| 332 | 298 | * |
| 333 | - * @return array | |
| 299 | + * @return string | |
| 334 | 300 | */ |
| 335 | - private function get_cache_paths_for_url( $url ) { | |
| 336 | - $cache_keys = $this->get_cache_keys_for_url( $url ); | |
| 301 | + private function get_cache_path_for_url( $url ) { | |
| 302 | + $parsed_url = parse_url( trailingslashit( $url ) ); | |
| 303 | + $cache_key = md5( $parsed_url['scheme'] . 'GET' . $parsed_url['host'] . $parsed_url['path'] ); | |
| 304 | + $cache_path = substr( $cache_key, - 1 ) . '/' . substr( $cache_key, - 3, 2 ) . '/' . $cache_key; | |
| 337 | 305 | |
| 338 | - $cache_paths = array(); | |
| 339 | - foreach ( $cache_keys as $key ) { | |
| 340 | - $hashed_key = md5( $key ); | |
| 341 | - $path = substr( $hashed_key, - 1 ) . '/' . substr( $hashed_key, - 3, 2 ) . '/' . $hashed_key; | |
| 342 | - $cache_paths[] = trailingslashit( $this->cache_path ) . $path; | |
| 343 | - } | |
| 344 | - | |
| 345 | - return $cache_paths; | |
| 306 | + return trailingslashit( $this->cache_path ) . $cache_path; | |
| 346 | 307 | } |
| 347 | 308 | |
| 348 | 309 | /** |
| 349 | - * Gets the cache keys for a given URL. | |
| 350 | - * | |
| 351 | - * It defaults to a single key: (fastcgi_cache_key "$scheme$request_method$host$request_uri"), | |
| 352 | - * but it can be modified with spinupwp_cache_key_components filter. | |
| 353 | - * This must match the Nginx configuration. | |
| 354 | - * | |
| 355 | - * @param string $url | |
| 356 | - * | |
| 357 | - * @return array | |
| 358 | - */ | |
| 359 | - private function get_cache_keys_for_url( $url ) { | |
| 360 | - $parsed_url = parse_url( $url ); | |
| 361 | - | |
| 362 | - $cache_keys = array( | |
| 363 | - $parsed_url['scheme'] . 'GET' . $parsed_url['host'] . trailingslashit( $parsed_url['path'] ), | |
| 364 | - $parsed_url['scheme'] . 'GET' . $parsed_url['host'] . untrailingslashit( $parsed_url['path'] ), | |
| 365 | - ); | |
| 366 | - | |
| 367 | - // Allow the cache keys to be modified | |
| 368 | - $cache_keys = apply_filters( 'spinupwp_cache_keys_for_url', $cache_keys, $url ); | |
| 369 | - | |
| 370 | - return $cache_keys; | |
| 371 | - } | |
| 372 | - | |
| 373 | - /** | |
| 374 | 310 | * Delete a file/dir from the local filesystem. |
| 375 | 311 | * |
| 376 | 312 | * @param string $path Absolute path to file |
| 377 | 313 | * @param bool $recursive |
| @@ -378,9 +314,9 @@ | ||
| 378 | 314 | * |
| 379 | 315 | * @return bool |
| 380 | 316 | */ |
| 381 | 317 | private function delete( $path, $recursive = false ) { |
| 382 | - if ( null !== $path && file_exists( $path ) && is_writable( $path ) ) { | |
| 318 | + if ( file_exists( $path ) && is_writable( $path ) ) { | |
| 383 | 319 | require_once( ABSPATH . '/wp-admin/includes/file.php' ); |
| 384 | 320 | |
| 385 | 321 | $context = $path; |
| 386 | 322 | if ( is_file( $path ) ) { |
| @@ -483,5 +419,5 @@ | ||
| 483 | 419 | 'revision', |
| 484 | 420 | 'user_request' |
| 485 | 421 | ) ); |
| 486 | 422 | } |
| 487 | -} | |
| 423 | +} | |