Skip to content

Commit

Permalink
Merge pull request #397 from markjaquith/purge-on-post-status-transition
Browse files Browse the repository at this point in the history
Purge a post from APO when its status transitions to or from "publish"
  • Loading branch information
jacobbednarz committed Mar 22, 2021
2 parents 48a16c3 + ce99b69 commit e39a243
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
8 changes: 6 additions & 2 deletions cloudflare.loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,19 @@
$cloudflarePurgeURLActions = array(
'deleted_post', // Delete a post
'delete_attachment', // Delete an attachment - includes re-uploading
'post_updated', // Update a post
);

$cloudflarePurgeURLActions = apply_filters('cloudflare_purge_url_actions', $cloudflarePurgeURLActions);

foreach ($cloudflarePurgeURLActions as $action) {
add_action($action, array($cloudflareHooks, 'purgeCacheByRelevantURLs'), PHP_INT_MAX, 4);
add_action($action, array($cloudflareHooks, 'purgeCacheByRelevantURLs'), PHP_INT_MAX);
}

/**
* Register action to account for post status changes
*/
add_action('transition_post_status', array($cloudflareHooks, 'purgeCacheOnPostStatusChange'), PHP_INT_MAX, 3);

/**
* Register two new actions which account for comment status before purging cache
*/
Expand Down
38 changes: 11 additions & 27 deletions src/WordPress/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function purgeCacheEverything()
}
}

public function purgeCacheByRelevantURLs($postId, ...$args)
public function purgeCacheByRelevantURLs($postId)
{
if ($this->isPluginSpecificCacheEnabled() || $this->isAutomaticPlatformOptimizationEnabled()) {
$wpDomainList = $this->integrationAPI->getDomainList();
Expand All @@ -135,36 +135,13 @@ public function purgeCacheByRelevantURLs($postId, ...$args)
}
$wpDomain = $wpDomainList[0];

$validPostStatus = array('publish', 'trash', 'private');
$thisPostStatus = get_post_status($postId);

if (get_permalink($postId) != true || !in_array($thisPostStatus, $validPostStatus)) {
return;
}

// We don't need to purge for private posts, except when they were just transitioned from public.
if ('private' === $thisPostStatus) {
// The action that fires on transition of status is "post_updated", which will have the old version
// in the extra args. If that arg is not there we can bail out.
if (! isset($args[1])) {
return;
}
/**
* @var \WP_Post $before Previous version of post.
*/
list(, $before) = $args;

if ('private' === $before->post_status) {
return;
}
}

if (is_int(wp_is_post_autosave($postId)) || is_int(wp_is_post_revision($postId))) {
// Do not purge for autosaves or updates to post revisions.
if (wp_is_post_autosave($postId) || wp_is_post_revision($postId)) {
return;
}

$savedPost = get_post($postId);
if (is_a($savedPost, 'WP_Post') == false) {
if (!is_a($savedPost, 'WP_Post')) {
return;
}

Expand Down Expand Up @@ -354,6 +331,13 @@ public function initAutomaticPlatformOptimization()
}
}

public function purgeCacheOnPostStatusChange($new_status, $old_status, $post)
{
if ('publish' === $new_status || 'publish' === $old_status) {
$this->purgeCacheByRelevantURLs($post->ID);
}
}

public function purgeCacheOnCommentStatusChange($new_status, $old_status, $comment)
{
if (!isset($comment->comment_post_ID) || empty($comment->comment_post_ID)) {
Expand Down

0 comments on commit e39a243

Please sign in to comment.