Skip to content

Commit

Permalink
Merge pull request #112 from silverbackstudio/purge-post-url
Browse files Browse the repository at this point in the history
Purge Post URL, Home, Blog and Terms page on update
  • Loading branch information
jwineman committed Oct 20, 2016
2 parents 0c91c2d + cfc5e51 commit 46c12f8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions cloudflare.loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@
// Load Automatic Cache Purge
add_action('switch_theme', array($cloudflareHooks, 'purgeCache'));
add_action('customize_save_after', array($cloudflareHooks, 'purgeCache'));
add_action('save_post', array($cloudflareHooks, 'purgePage'));
}
60 changes: 60 additions & 0 deletions src/WordPress/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,66 @@ public function purgeCache()
}
}

public function purgePage($post_id)
{
if ($this->isPluginSpecificCacheEnabled()) {
$wp_domain_list = $this->integrationAPI->getDomainList();
$wp_domain = $wp_domain_list[0];
if (count($wp_domain) > 0) {
$zoneTag = $this->api->getZoneTag($wp_domain);

$saved_post = get_post( $post_id );

if ( is_a( $saved_post, 'WP_Post' ) == false ) {
return false;
}

if ( wp_is_post_autosave( $saved_post ) || wp_is_post_revision( $saved_post ) || ( 'publish' != get_post_status( $post_id ) ) ) {
return false;
}

$post_url = get_permalink( $saved_post );

$urls = array();

array_push( $urls, $post_url );
array_push( $urls, home_url() );

$post_type = get_post_type( $saved_post );

$taxonomies = get_object_taxonomies( $post_type, 'objects' );

foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){

$terms = get_the_terms( $saved_post, $taxonomy_slug );

if ( !empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term) {

$term_link = get_term_link( $term );

if ( ! is_wp_error( $term_link ) ) {
array_push( $urls, $term_link );
}
}
}
}

if ( ('post' == $post_type) && ( 'page' == get_option('show_on_front') ) && get_option( 'page_for_posts' ) ) {
array_push( $urls, get_permalink( get_option( 'page_for_posts' ) ) );
}

if( is_ssl() ){
$urls = array_merge($urls, array_map( function($url){ return str_replace('https://', 'http://', $url); }, $urls) );
}

if (isset($zoneTag)) {
$this->api->zonePurgeFiles($zoneTag, $urls);
}
}
}
}

protected function isPluginSpecificCacheEnabled()
{
$cacheSettingObject = $this->dataStore->getPluginSetting(\CF\API\Plugin::SETTING_PLUGIN_SPECIFIC_CACHE);
Expand Down
14 changes: 14 additions & 0 deletions src/WordPress/WordPressClientAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ public function zonePurgeCache($zoneId)
return $this->responseOk($response);
}

/**
* @param $zoneId
* @param $files
*
* @return bool
*/
public function zonePurgeFiles($zoneId, $files)
{
$request = new Request('DELETE', 'zones/'.$zoneId.'/purge_cache', array(), array('files' => $files));
$response = $this->callAPI($request);

return $this->responseOk($response);
}

/**
* @param $zoneId
* @param $settingName
Expand Down

0 comments on commit 46c12f8

Please sign in to comment.