PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 1.1
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v1.1
1.7.1 1.7.0 1.6.0 1.5.2 trunk 0.1 0.2.0 0.2.1 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 1.0 1.1 1.2 1.3.1 1.4.0 1.4.1 1.5.0 1.5.1 All 26 releases
flywp / includes / Fastcgi_Cache.php

Fastcgi_Cache.php in FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel 1.1, at includes/Fastcgi_Cache.php

192 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // cache key format (without space): $protocal $method $host $path
125 // because of nginx-proxy, all request will be http, so we don't need to check protocal
126 // $method is always GET as we only cache GET request
127 $hash = md5( 'httpGET' . $parsed['host'] . $path );
128
129 $cache_path = self::CACHE_PATH . '/' . substr( $hash, -1 ) . '/' . substr( $hash, -3, 2 ) . '/' . $hash;
130
131 if ( file_exists( $cache_path ) ) {
132 unlink( $cache_path );
133 }
134 }
135
136 /**
137 * Purge homepage cache.
138 *
139 * @return void
140 */
141 public function purge_home_cache() {
142 $this->purge_cache_by_url( home_url() );
143 }
144
145 /**
146 * Purge cache when a post is saved.
147 *
148 * @param int $post_id Post ID
149 * @param object $post Post object
150 *
151 * @return void
152 */
153 public function on_save_post( $post_id, $post ) {
154 if ( $post->post_status !== 'publish' ) {
155 return;
156 }
157
158 if ( $this->get_setting( 'home_created' ) ) {
159 $this->purge_home_cache();
160 }
161
162 $this->purge_cache_by_url( get_permalink( $post ) );
163 }
164
165 /**
166 * Purge cache when a post is trashed.
167 *
168 * @param int $post_id Post ID
169 *
170 * @return void
171 */
172 public function wp_trash_post( $post_id ) {
173 $post = get_post( $post_id );
174
175 if ( $this->get_setting( 'single_modified' ) ) {
176 $this->purge_cache_by_url( get_permalink( $post ) );
177 }
178
179 if ( $this->get_setting( 'home_created' ) && $post->post_status === 'publish' ) {
180 $this->purge_home_cache();
181 }
182 }
183
184 public function purge_taxonomy_cache( $term_id, $taxonomy ) {
185 // :TODO
186 }
187
188 public function purge_archive_cache() {
189 // :TODO
190 }
191 }
192