PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 0.2.1
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v0.2.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 0.2.1, at includes/Fastcgi_Cache.php

155 lines 3.9 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 * 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 trashed.
132 *
133 * @param int $post_id Post ID
134 *
135 * @return void
136 */
137 public function wp_trash_post( $post_id ) {
138 $post = get_post( $post_id );
139
140 if ( $this->get_setting( 'single_modified' ) ) {
141 $this->purge_cache_by_url( get_permalink( $post ) );
142 }
143
144 if ( $this->get_setting( 'home_created' ) && $post->post_status === 'publish' ) {
145 $this->purge_home_cache();
146 }
147 }
148
149 public function purge_taxonomy_cache( $term_id, $taxonomy ) {
150 }
151
152 public function purge_archive_cache() {
153 }
154 }
155