Skip to content

Commit 7615c76

Browse files
committed
Split pronamic_cloudflare_purge_cache action in pronamic_cloudflare_purge_cache_tags and pronamic_cloudflare_purge_cache_everything.
1 parent 658d549 commit 7615c76

File tree

1 file changed

+57
-49
lines changed

1 file changed

+57
-49
lines changed

php/Plugin.php

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public function __construct() {
6262
*/
6363
public function setup() {
6464
\add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 500 );
65-
\add_action( 'pronamic_cloudflare_purge_cache', [ $this, 'purge_cache' ] );
65+
\add_action( 'pronamic_cloudflare_purge_cache_tags', $this->request_purge_cache_tags( ... ) );
66+
\add_action( 'pronamic_cloudflare_purge_everything', $this->request_purge_everything( ... ) );
6667

6768
// Post actions.
6869
\add_action( 'save_post', $this->purge_cache_by_post( ... ), 10, 1 );
@@ -115,7 +116,7 @@ public function admin_bar_menu( WP_Admin_Bar $admin_bar ) {
115116
$url = \add_query_arg(
116117
[
117118
'page' => 'action-scheduler',
118-
's' => 'pronamic_cloudflare_purge_cache',
119+
's' => 'pronamic_cloudflare_purge',
119120
],
120121
\admin_url( 'tools.php' )
121122
);
@@ -162,7 +163,6 @@ private function count_actions() {
162163

163164
$number = $store->query_actions(
164165
[
165-
'hook' => 'pronamic_cloudflare_purge_cache',
166166
'group' => 'pronamic-cloudflare',
167167
'status' => ActionScheduler_Store::STATUS_PENDING,
168168
],
@@ -271,7 +271,7 @@ public function get_current_cache_tags(): array {
271271
* @return void
272272
* @throws \Exception Throws exception if purge cache action fails.
273273
*/
274-
public function purge_cache( $args ) {
274+
private function send_request( $args ) {
275275
$api_email = (string) \get_option( 'pronamic_cloudflare_api_email' );
276276
$api_key = (string) \get_option( 'pronamic_cloudflare_api_key' );
277277
$zone_id = (string) \get_option( 'pronamic_cloudflare_zone_id' );
@@ -318,6 +318,33 @@ public function purge_cache( $args ) {
318318
}
319319
}
320320

321+
/**
322+
* Request purge cache tags.
323+
*
324+
* @param string[] $tags Tags to purge.
325+
* @return void
326+
*/
327+
private function request_purge_cache_tags( array $tags ): void {
328+
$args = [
329+
'tags' => $tags,
330+
];
331+
332+
$this->send_request( $args );
333+
}
334+
335+
/**
336+
* Request purge everything.
337+
*
338+
* @return void
339+
*/
340+
private function request_purge_everything(): void {
341+
$args = [
342+
'purge_everything' => true,
343+
];
344+
345+
$this->send_request( $args );
346+
}
347+
321348
/**
322349
* Purge cache by post.
323350
*
@@ -702,63 +729,44 @@ private function purge_by_tags( $tags ) {
702729
}
703730
}
704731

705-
/**
706-
* Schedule purge cache action.
707-
*
708-
* @param array $args Arguments for purge cache action.
709-
* @return void
710-
*/
711-
private function schedule_purge_cache_action( $args ): void {
712-
if ( 0 === count( $args ) ) {
713-
return;
714-
}
715-
716-
$args = [ $args ];
717-
718-
$scheduled = \as_has_scheduled_action(
719-
'pronamic_cloudflare_purge_cache',
720-
$args,
721-
'pronamic-cloudflare',
722-
);
723-
724-
if ( $scheduled ) {
725-
return;
726-
}
727-
728-
\as_enqueue_async_action(
729-
'pronamic_cloudflare_purge_cache',
730-
$args,
731-
'pronamic-cloudflare',
732-
false
733-
);
734-
}
735-
736732
/**
737733
* Schedule purge cache action on shutdown.
738734
*
739735
* @return void
740736
*/
741737
private function shutdown(): void {
742-
if ( 0 === count( $this->purge_tags ) && false === $this->purge_everything ) {
743-
return;
744-
}
745-
746-
$args = [
747-
'tags' => $this->purge_tags,
748-
];
749-
738+
// Purge everything.
750739
if ( true === $this->purge_everything ) {
751-
$args = [
752-
'purge_everything' => true,
753-
];
754-
740+
// Remove all scheduled purge cache tags actions
741+
// as purge everything action will purge everything.
755742
\as_unschedule_all_actions(
756-
'pronamic_cloudflare_purge_cache',
743+
'pronamic_cloudflare_purge_cache_tags',
757744
null,
758745
'pronamic-cloudflare'
759746
);
747+
748+
\as_enqueue_async_action(
749+
'pronamic_cloudflare_purge_everything',
750+
[],
751+
'pronamic-cloudflare',
752+
true
753+
);
754+
}
755+
756+
// Purge tags.
757+
if ( 0 === count( $this->purge_tags ) || true === $this->purge_everything ) {
758+
return;
760759
}
761760

762-
$this->schedule_purge_cache_action( $args );
761+
$args = [
762+
$this->purge_tags,
763+
];
764+
765+
\as_enqueue_async_action(
766+
'pronamic_cloudflare_purge_cache_tags',
767+
$args,
768+
'pronamic-cloudflare',
769+
true
770+
);
763771
}
764772
}

0 commit comments

Comments
 (0)