-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache-purge.php
40 lines (33 loc) · 879 Bytes
/
cache-purge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
class CachePurge {
static $instance = array();
public static function get_instance() {
$c = get_called_class();
if ( ! isset( self::$instance[ $c ] ) ) {
self::$instance[ $c ] = new $c;
}
return self::$instance[ $c ];
}
private function __construct() {
add_action( 'transition_post_status', array( $this, 'transition_post_status' ), 10, 3 );
}
public function transition_post_status( $new, $old, $post ) {
if ( 'publish' !== $old && 'publish' !== $new ) {
return;
}
$post = get_post( $post );
$url = get_permalink( $post );
// Purge this URL
$this->purge( $url );
// Purge the front page
$this->purge( home_url( '/' ) );
}
private function purge( $url ) {
wp_remote_get( $url, array(
'timeout' => 0.01,
'blocking' => false,
'headers' => array( 'X-Nginx-Cache-Purge' => '1' )
) );
}
}
CachePurge::get_instance();