Skip to content

Commit a8bd842

Browse files
committed
feat: add clear_all_on_post_update option to CDN page caching subscriber
1 parent 105c766 commit a8bd842

4 files changed

Lines changed: 168 additions & 71 deletions

File tree

src/Configuration/EventManagementConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function modify(Container $container)
4646
// Ymir subscribers
4747
new Subscriber\AdminSubscriber(),
4848
new Subscriber\AssetsSubscriber($container['content_directory_name'], $container['site_url'], $container['assets_url'], $container['ymir_project_type'], $container['uploads_baseurl']),
49-
new Subscriber\ContentDeliveryNetworkPageCachingSubscriber($container['cloudfront_client'], $container['rest_url'], $container['is_page_caching_disabled']),
49+
new Subscriber\ContentDeliveryNetworkPageCachingSubscriber($container['cloudfront_client'], $container['rest_url'], $container['page_caching_options']),
5050
new Subscriber\DisallowIndexingSubscriber($container['ymir_using_vanity_domain']),
5151
new Subscriber\EmailSubscriber($container['email_client'], $container['ymir_is_email_sending_enabled'], $container['ymir_using_vanity_domain']),
5252
new Subscriber\ImageEditorSubscriber($container['console_client'], $container['file_manager']),

src/Configuration/PageCacheConfiguration.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function modify(Container $container)
3030
$container['cloudfront_client'] = $container->service(function (Container $container) {
3131
return new CloudFrontClient($container['ymir_http_client'], getenv('YMIR_DISTRIBUTION_ID'), $container['cloud_provider_key'], $container['cloud_provider_secret']);
3232
});
33-
$container['is_page_caching_disabled'] = $container->service(function (Container $container) {
33+
$container['page_caching_invalidation_disabled'] = $container->service(function (Container $container) {
3434
if (false !== getenv('YMIR_DISABLE_PAGE_CACHING')) {
3535
return (bool) getenv('YMIR_DISABLE_PAGE_CACHING');
3636
} elseif (defined('YMIR_DISABLE_PAGE_CACHING')) {
@@ -39,5 +39,20 @@ public function modify(Container $container)
3939

4040
return parse_url($container['upload_url'], PHP_URL_HOST) !== parse_url(WP_HOME, PHP_URL_HOST);
4141
});
42+
$container['page_caching_clear_all_on_post_update'] = $container->service(function () {
43+
if (false !== getenv('YMIR_CLEAR_ALL_ON_POST_UPDATE')) {
44+
return (bool) getenv('YMIR_CLEAR_ALL_ON_POST_UPDATE');
45+
} elseif (defined('YMIR_CLEAR_ALL_ON_POST_UPDATE')) {
46+
return (bool) YMIR_CLEAR_ALL_ON_POST_UPDATE;
47+
}
48+
49+
return false;
50+
});
51+
$container['page_caching_options'] = $container->service(function (Container $container) {
52+
return [
53+
'invalidation_enabled' => !$container['page_caching_invalidation_disabled'],
54+
'clear_all_on_post_update' => $container['page_caching_clear_all_on_post_update'],
55+
];
56+
});
4257
}
4358
}

src/Subscriber/ContentDeliveryNetworkPageCachingSubscriber.php

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class ContentDeliveryNetworkPageCachingSubscriber extends AbstractEventManagerAw
3232
/**
3333
* Flag whether page caching is disabled or not.
3434
*
35-
* @var bool
35+
* @var array
3636
*/
37-
private $pageCachingDisabled;
37+
private $pageCachingOptions;
3838

3939
/**
4040
* Base URL for the WordPress REST API endpoints.
@@ -46,10 +46,10 @@ class ContentDeliveryNetworkPageCachingSubscriber extends AbstractEventManagerAw
4646
/**
4747
* Constructor.
4848
*/
49-
public function __construct(ContentDeliveryNetworkPageCacheClientInterface $pageCacheClient, string $restUrl, bool $pageCachingDisabled = false)
49+
public function __construct(ContentDeliveryNetworkPageCacheClientInterface $pageCacheClient, string $restUrl, array $pageCachingOptions = [])
5050
{
5151
$this->pageCacheClient = $pageCacheClient;
52-
$this->pageCachingDisabled = $pageCachingDisabled;
52+
$this->pageCachingOptions = $pageCachingOptions;
5353
$this->restBaseUrl = rtrim($restUrl, '/').'/wp/v2';
5454
}
5555

@@ -87,28 +87,64 @@ public function clearCache()
8787
*/
8888
public function clearPost($postId)
8989
{
90-
if ($this->pageCachingDisabled) {
90+
if (empty($this->pageCachingOptions['invalidation_enabled'])) {
91+
return;
92+
} elseif (!empty($this->pageCachingOptions['clear_all_on_post_update'])) {
93+
$this->clearCache();
94+
95+
return;
96+
}
97+
98+
$urlsToClear = $this->eventManager->filter('ymir_page_caching_urls_to_clear', $this->getUrlsToClear($postId), $postId);
99+
100+
if (is_array($urlsToClear) || is_string($urlsToClear)) {
101+
$urlsToClear = new Collection($urlsToClear);
102+
} elseif (!$urlsToClear instanceof Collection) {
91103
return;
92104
}
93105

106+
$urlsToClear->filter(function ($url) {
107+
return is_string($url) && !empty($url);
108+
})->each(function (string $url) {
109+
$this->pageCacheClient->clearUrl($url);
110+
});
111+
}
112+
113+
/**
114+
* Send request to content delivery network to clear all requested URLs from its cache.
115+
*/
116+
public function sendClearRequest()
117+
{
118+
$this->eventManager->execute('ymir_page_caching_send_clear_request');
119+
120+
$this->pageCacheClient->sendClearRequest();
121+
}
122+
123+
/**
124+
* Get all the URLs to clear for the given post ID.
125+
*/
126+
private function getUrlsToClear($postId): Collection
127+
{
94128
$permalink = get_permalink($postId);
95129
$post = get_post($postId);
130+
$urlsToClear = new Collection();
96131

97132
if (!$post instanceof \WP_Post
98133
|| !is_string($permalink)
99134
|| !in_array($post->post_status, ['publish', 'private', 'trash', 'pending', 'draft'], true)
100135
|| in_array($post->post_type, ['nav_menu_item', 'revision'], true)
101136
) {
102-
return;
137+
return $urlsToClear;
103138
}
104139

105140
if ('trash' === $post->post_status) {
106141
$permalink = str_replace('__trashed', '', $permalink);
107142
}
108143

109-
$permalink = rtrim($permalink, '/').'/';
110144
$postType = get_post_type_object($postId);
111-
$urlsToClear = new Collection([$permalink, rtrim(home_url(), '/').'/']);
145+
146+
$urlsToClear[] = rtrim($permalink, '/').'/';
147+
$urlsToClear[] = rtrim(home_url(), '/').'/';
112148

113149
// Custom post archive
114150
if ('page' === get_site_option('show_on_front') && !empty(get_site_option('page_for_posts'))) {
@@ -189,28 +225,6 @@ public function clearPost($postId)
189225
$urlsToClear[] = get_post_type_archive_feed_link($post->post_type);
190226
}
191227

192-
$urlsToClear = $this->eventManager->filter('ymir_page_caching_urls_to_clear', $urlsToClear);
193-
194-
if (is_array($urlsToClear) || is_string($urlsToClear)) {
195-
$urlsToClear = new Collection($urlsToClear);
196-
} elseif (!$urlsToClear instanceof Collection) {
197-
return;
198-
}
199-
200-
$urlsToClear->filter(function ($url) {
201-
return is_string($url) && !empty($url);
202-
})->each(function (string $url) {
203-
$this->pageCacheClient->clearUrl($url);
204-
});
205-
}
206-
207-
/**
208-
* Send request to content delivery network to clear all requested URLs from its cache.
209-
*/
210-
public function sendClearRequest()
211-
{
212-
$this->eventManager->execute('ymir_page_caching_send_clear_request');
213-
214-
$this->pageCacheClient->sendClearRequest();
228+
return $urlsToClear;
215229
}
216230
}

0 commit comments

Comments
 (0)