From 2e9b9ce90bc246bd2e4bf506c1d8fe3284b55026 Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Sun, 21 Mar 2021 14:15:25 -0400 Subject: [PATCH 1/2] Purge a post from APO when its status transitions to or from "publish" This includes publish => publish transitions (editing a published post), manually publishing a post, unpublishing a post, or WordPress automatically publishing a scheduled post at the appropriate time. fixes #395 --- cloudflare.loader.php | 6 +++++- src/WordPress/Hooks.php | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cloudflare.loader.php b/cloudflare.loader.php index 23bf5ec7..35c054fa 100644 --- a/cloudflare.loader.php +++ b/cloudflare.loader.php @@ -95,7 +95,6 @@ $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); @@ -104,6 +103,11 @@ add_action($action, array($cloudflareHooks, 'purgeCacheByRelevantURLs'), PHP_INT_MAX, 4); } +/** + * 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 */ diff --git a/src/WordPress/Hooks.php b/src/WordPress/Hooks.php index 15cd4325..9d3b6102 100644 --- a/src/WordPress/Hooks.php +++ b/src/WordPress/Hooks.php @@ -354,6 +354,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)) { From ce99b6996329308d5324a3ec8c4b79bdd8585911 Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Mon, 22 Mar 2021 14:26:43 -0400 Subject: [PATCH 2/2] Remove no-longer-necessary post status inspection code Because we are now hooked on `transition_post_status`, which handles the logic of determining when to purge for a post for various post status transitions (or same-to-same "transitions"), `purgeCacheByRelevantURLs()` no longer needs to do this work. see #365 fixes #395 --- cloudflare.loader.php | 2 +- src/WordPress/Hooks.php | 31 ++++--------------------------- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/cloudflare.loader.php b/cloudflare.loader.php index 35c054fa..c72c0480 100644 --- a/cloudflare.loader.php +++ b/cloudflare.loader.php @@ -100,7 +100,7 @@ $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); } /** diff --git a/src/WordPress/Hooks.php b/src/WordPress/Hooks.php index 9d3b6102..20216873 100644 --- a/src/WordPress/Hooks.php +++ b/src/WordPress/Hooks.php @@ -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(); @@ -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; }