| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
|
| 7 |
class Fastcgi_Cache { |
| 8 |
|
| 9 |
/** |
| 10 |
* Cache path. |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
const CACHE_PATH = '/var/run/nginx-cache'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Cache option settings key. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
const SETTINGS_KEY = 'flywp_fastcgi_cache'; |
| 22 |
|
| 23 |
public function __construct() { |
| 24 |
add_action( 'save_post', [ $this, 'on_save_post' ], 10, 2 ); |
| 25 |
add_action( 'delete_post', [ $this, 'wp_trash_post' ], 10, 2 ); |
| 26 |
// add_action( 'edited_term', [ $this, 'purge_taxonomy_cache' ], 10, 2 ); |
| 27 |
// add_action( 'wp_update_nav_menu', [ $this, 'purge_home_cache' ], 10, 2 ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Check if the cache is enabled. |
| 32 |
* |
| 33 |
* @return bool |
| 34 |
*/ |
| 35 |
public function enabled() { |
| 36 |
return $this->settings() && $this->settings()['enabled']; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get all cache settings. |
| 41 |
* |
| 42 |
* @return array |
| 43 |
*/ |
| 44 |
public function settings() { |
| 45 |
$default = [ |
| 46 |
'enabled' => true, |
| 47 |
'home_created' => true, |
| 48 |
'home_deleted' => true, |
| 49 |
// 'single_modified' => true, |
| 50 |
// 'single_comment' => true, |
| 51 |
]; |
| 52 |
|
| 53 |
return get_option( self::SETTINGS_KEY, $default ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get a cache setting. |
| 58 |
* |
| 59 |
* @param string $key Setting key |
| 60 |
* |
| 61 |
* @return mixed |
| 62 |
*/ |
| 63 |
public function get_setting( $key ) { |
| 64 |
return isset( $this->settings()[ $key ] ) ? $this->settings()[ $key ] : false; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Purge all cache. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function clear_cache() { |
| 73 |
global $wp_filesystem; |
| 74 |
|
| 75 |
if ( ! is_object( $wp_filesystem ) ) { |
| 76 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 77 |
WP_Filesystem(); |
| 78 |
} |
| 79 |
|
| 80 |
if ( ! $wp_filesystem->exists( self::CACHE_PATH ) ) { |
| 81 |
return new WP_Error( 'cache_not_exists', __( 'Cache directory does not exist.', 'flywp' ) ); |
| 82 |
} |
| 83 |
|
| 84 |
if ( ! $wp_filesystem->is_dir( self::CACHE_PATH ) ) { |
| 85 |
return new WP_Error( 'cache_not_dir', __( 'Cache directory is not a directory.', 'flywp' ) ); |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! $wp_filesystem->is_writable( self::CACHE_PATH ) ) { |
| 89 |
return new WP_Error( 'cache_not_writable', __( 'Cache directory is not writable.', 'flywp' ) ); |
| 90 |
} |
| 91 |
|
| 92 |
$wp_filesystem->rmdir( self::CACHE_PATH, true ); |
| 93 |
$wp_filesystem->mkdir( self::CACHE_PATH ); |
| 94 |
|
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Purge cache by URL. |
| 100 |
* |
| 101 |
* @param string $url URL to purge |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function purge_cache_by_url( $url ) { |
| 106 |
$parsed = wp_parse_url( $url ); |
| 107 |
$path = isset( $parsed['path'] ) ? $parsed['path'] : ''; |
| 108 |
|
| 109 |
// cache key format (without space): $protocal $method $host $path |
| 110 |
// because of nginx-proxy, all request will be http, so we don't need to check protocal |
| 111 |
// $method is always GET as we only cache GET request |
| 112 |
$hash = md5( 'httpGET' . $parsed['host'] . $path ); |
| 113 |
|
| 114 |
$cache_path = self::CACHE_PATH . '/' . substr( $hash, -1 ) . '/' . substr( $hash, -3, 2 ) . '/' . $hash; |
| 115 |
|
| 116 |
if ( file_exists( $cache_path ) ) { |
| 117 |
unlink( $cache_path ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Purge homepage cache. |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function purge_home_cache() { |
| 127 |
$this->purge_cache_by_url( home_url() ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Purge cache when a post is saved. |
| 132 |
* |
| 133 |
* @param int $post_id Post ID |
| 134 |
* @param object $post Post object |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function on_save_post( $post_id, $post ) { |
| 139 |
if ( $post->post_status !== 'publish' ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
if ( $this->get_setting( 'home_created' ) ) { |
| 144 |
$this->purge_home_cache(); |
| 145 |
} |
| 146 |
|
| 147 |
$this->purge_cache_by_url( get_permalink( $post ) ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Purge cache when a post is trashed. |
| 152 |
* |
| 153 |
* @param int $post_id Post ID |
| 154 |
* |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
public function wp_trash_post( $post_id ) { |
| 158 |
$post = get_post( $post_id ); |
| 159 |
|
| 160 |
if ( $this->get_setting( 'single_modified' ) ) { |
| 161 |
$this->purge_cache_by_url( get_permalink( $post ) ); |
| 162 |
} |
| 163 |
|
| 164 |
if ( $this->get_setting( 'home_created' ) && $post->post_status === 'publish' ) { |
| 165 |
$this->purge_home_cache(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
public function purge_taxonomy_cache( $term_id, $taxonomy ) { |
| 170 |
// :TODO |
| 171 |
} |
| 172 |
|
| 173 |
public function purge_archive_cache() { |
| 174 |
// :TODO |
| 175 |
} |
| 176 |
} |
| 177 |
|