| @@ -27,8 +27,23 @@ | ||
| 27 | 27 | // add_action( 'wp_update_nav_menu', [ $this, 'purge_home_cache' ], 10, 2 ); |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 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 | + /** | |
| 31 | 46 | * Check if the cache is enabled. |
| 32 | 47 | * |
| 33 | 48 | * @return bool |
| 34 | 49 | */ |
| @@ -105,8 +120,13 @@ | ||
| 105 | 120 | public function purge_cache_by_url( $url ) { |
| 106 | 121 | $parsed = wp_parse_url( $url ); |
| 107 | 122 | $path = isset( $parsed['path'] ) ? $parsed['path'] : ''; |
| 108 | 123 | |
| 124 | + // Ensure path ends with trailing slash | |
| 125 | + if ( substr( $path, -1 ) !== '/' ) { | |
| 126 | + $path .= '/'; | |
| 127 | + } | |
| 128 | + | |
| 109 | 129 | // cache key format (without space): $protocal $method $host $path |
| 110 | 130 | // because of nginx-proxy, all request will be http, so we don't need to check protocal |
| 111 | 131 | // $method is always GET as we only cache GET request |
| 112 | 132 | $hash = md5( 'httpGET' . $parsed['host'] . $path ); |
| @@ -127,8 +147,28 @@ | ||
| 127 | 147 | $this->purge_cache_by_url( home_url() ); |
| 128 | 148 | } |
| 129 | 149 | |
| 130 | 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 | + /** | |
| 131 | 171 | * Purge cache when a post is trashed. |
| 132 | 172 | * |
| 133 | 173 | * @param int $post_id Post ID |
| 134 | 174 | * |
| @@ -146,9 +186,11 @@ | ||
| 146 | 186 | } |
| 147 | 187 | } |
| 148 | 188 | |
| 149 | 189 | public function purge_taxonomy_cache( $term_id, $taxonomy ) { |
| 190 | + // :TODO | |
| 150 | 191 | } |
| 151 | 192 | |
| 152 | 193 | public function purge_archive_cache() { |
| 194 | + // :TODO | |
| 153 | 195 | } |
| 154 | 196 | } |