forked from Automattic/_s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpengine.php
33 lines (28 loc) · 1.09 KB
/
wpengine.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
<?php
/**
* Varnish cache clearing for JSON REST API routes
*
* WP Engine caches all sorts of things to keep your WordPress site up & running.
* One layer of caching is the venerable Varnish. WP Engine's purging code doesn't
* include JSON REST API endpoints however so public requests to them suffer from
* 10 minute delays. This code purges the standard routes when content is updated,
* avoiding such delays.
*
* Access to Varnish isn't publicly exposed, but is possible via the WP Engine plugin
* installed for all customers.
*
* Technique is borrowed from https://github.com/cftp/WPEngine-Clear-URL-Cache
*/
function purge_cached_post( $post_id ){
$varnish = 'localhost';
$hostname = $_SERVER['SERVER_NAME'];
$endpoints = array(
'/'.WP_SOLIDUS_JSON_URL_PREFIX.'/posts',
'/'.WP_SOLIDUS_JSON_URL_PREFIX.'/posts/'.$post_id
);
foreach( $endpoints as &$endpoint ){
error_log( 'PURGE! '.$endpoint );
WpeCommon::http_request_async( 'PURGE', $varnish, 9002, $hostname, $endpoint, array( ), 0 );
}
}
add_action( 'save_post', 'purge_cached_post' );