| 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 |
* FastCGI cache purge url. |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public function purge_cache_url() { |
| 36 |
return wp_nonce_url( |
| 37 |
add_query_arg( |
| 38 |
[ 'flywp-action' => 'purge-fastcgi-cache' ], |
| 39 |
admin_url( 'index.php?page=flywp' ) |
| 40 |
), |
| 41 |
'fly-fastcgi-purge-cache' |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Check if the cache is enabled. |
| 47 |
* |
| 48 |
* @return bool |
| 49 |
*/ |
| 50 |
public function enabled() { |
| 51 |
return $this->settings() && $this->settings()['enabled']; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get all cache settings. |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
public function settings() { |
| 60 |
$default = [ |
| 61 |
'enabled' => true, |
| 62 |
'home_created' => true, |
| 63 |
'home_deleted' => true, |
| 64 |
// 'single_modified' => true, |
| 65 |
// 'single_comment' => true, |
| 66 |
]; |
| 67 |
|
| 68 |
return get_option( self::SETTINGS_KEY, $default ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get a cache setting. |
| 73 |
* |
| 74 |
* @param string $key Setting key |
| 75 |
* |
| 76 |
* @return mixed |
| 77 |
*/ |
| 78 |
public function get_setting( $key ) { |
| 79 |
return isset( $this->settings()[ $key ] ) ? $this->settings()[ $key ] : false; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Purge all cache. |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function clear_cache() { |
| 88 |
global $wp_filesystem; |
| 89 |
|
| 90 |
if ( ! is_object( $wp_filesystem ) ) { |
| 91 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 92 |
WP_Filesystem(); |
| 93 |
} |
| 94 |
|
| 95 |
if ( ! $wp_filesystem->exists( self::CACHE_PATH ) ) { |
| 96 |
return new WP_Error( 'cache_not_exists', __( 'Cache directory does not exist.', 'flywp' ) ); |
| 97 |
} |
| 98 |
|
| 99 |
if ( ! $wp_filesystem->is_dir( self::CACHE_PATH ) ) { |
| 100 |
return new WP_Error( 'cache_not_dir', __( 'Cache directory is not a directory.', 'flywp' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
if ( ! $wp_filesystem->is_writable( self::CACHE_PATH ) ) { |
| 104 |
return new WP_Error( 'cache_not_writable', __( 'Cache directory is not writable.', 'flywp' ) ); |
| 105 |
} |
| 106 |
|
| 107 |
$wp_filesystem->rmdir( self::CACHE_PATH, true ); |
| 108 |
$wp_filesystem->mkdir( self::CACHE_PATH ); |
| 109 |
|
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Purge cache by URL. |
| 115 |
* |
| 116 |
* @param string $url URL to purge |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function purge_cache_by_url( $url ) { |
| 121 |
$parsed = wp_parse_url( $url ); |
| 122 |
$path = isset( $parsed['path'] ) ? $parsed['path'] : ''; |
| 123 |
|
| 124 |
// Ensure path ends with trailing slash |
| 125 |
if ( substr( $path, -1 ) !== '/' ) { |
| 126 |
$path .= '/'; |
| 127 |
} |
| 128 |
|
| 129 |
// cache key format (without space): $protocal $method $host $path |
| 130 |
// because of nginx-proxy, all request will be http, so we don't need to check protocal |
| 131 |
// $method is always GET as we only cache GET request |
| 132 |
$hash = md5( 'httpGET' . $parsed['host'] . $path ); |
| 133 |
|
| 134 |
$cache_path = self::CACHE_PATH . '/' . substr( $hash, -1 ) . '/' . substr( $hash, -3, 2 ) . '/' . $hash; |
| 135 |
|
| 136 |
if ( file_exists( $cache_path ) ) { |
| 137 |
unlink( $cache_path ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Purge homepage cache. |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
public function purge_home_cache() { |
| 147 |
$this->purge_cache_by_url( home_url() ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Purge cache when a post is saved. |
| 152 |
* |
| 153 |
* @param int $post_id Post ID |
| 154 |
* @param object $post Post object |
| 155 |
* |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
public function on_save_post( $post_id, $post ) { |
| 159 |
if ( $post->post_status !== 'publish' ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
if ( $this->get_setting( 'home_created' ) ) { |
| 164 |
$this->purge_home_cache(); |
| 165 |
} |
| 166 |
|
| 167 |
$this->purge_cache_by_url( get_permalink( $post ) ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Purge cache when a post is trashed. |
| 172 |
* |
| 173 |
* @param int $post_id Post ID |
| 174 |
* |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
public function wp_trash_post( $post_id ) { |
| 178 |
$post = get_post( $post_id ); |
| 179 |
|
| 180 |
if ( $this->get_setting( 'single_modified' ) ) { |
| 181 |
$this->purge_cache_by_url( get_permalink( $post ) ); |
| 182 |
} |
| 183 |
|
| 184 |
if ( $this->get_setting( 'home_created' ) && $post->post_status === 'publish' ) { |
| 185 |
$this->purge_home_cache(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
public function purge_taxonomy_cache( $term_id, $taxonomy ) { |
| 190 |
// :TODO |
| 191 |
} |
| 192 |
|
| 193 |
public function purge_archive_cache() { |
| 194 |
// :TODO |
| 195 |
} |
| 196 |
} |
| 197 |
|