@@ -62,7 +62,8 @@ public function __construct() {
62
62
*/
63
63
public function setup () {
64
64
\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 ( ... ) );
66
67
67
68
// Post actions.
68
69
\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 ) {
115
116
$ url = \add_query_arg (
116
117
[
117
118
'page ' => 'action-scheduler ' ,
118
- 's ' => 'pronamic_cloudflare_purge_cache ' ,
119
+ 's ' => 'pronamic_cloudflare_purge ' ,
119
120
],
120
121
\admin_url ( 'tools.php ' )
121
122
);
@@ -162,7 +163,6 @@ private function count_actions() {
162
163
163
164
$ number = $ store ->query_actions (
164
165
[
165
- 'hook ' => 'pronamic_cloudflare_purge_cache ' ,
166
166
'group ' => 'pronamic-cloudflare ' ,
167
167
'status ' => ActionScheduler_Store::STATUS_PENDING ,
168
168
],
@@ -271,7 +271,7 @@ public function get_current_cache_tags(): array {
271
271
* @return void
272
272
* @throws \Exception Throws exception if purge cache action fails.
273
273
*/
274
- public function purge_cache ( $ args ) {
274
+ private function send_request ( $ args ) {
275
275
$ api_email = (string ) \get_option ( 'pronamic_cloudflare_api_email ' );
276
276
$ api_key = (string ) \get_option ( 'pronamic_cloudflare_api_key ' );
277
277
$ zone_id = (string ) \get_option ( 'pronamic_cloudflare_zone_id ' );
@@ -318,6 +318,33 @@ public function purge_cache( $args ) {
318
318
}
319
319
}
320
320
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
+
321
348
/**
322
349
* Purge cache by post.
323
350
*
@@ -702,63 +729,44 @@ private function purge_by_tags( $tags ) {
702
729
}
703
730
}
704
731
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
-
736
732
/**
737
733
* Schedule purge cache action on shutdown.
738
734
*
739
735
* @return void
740
736
*/
741
737
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.
750
739
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.
755
742
\as_unschedule_all_actions (
756
- 'pronamic_cloudflare_purge_cache ' ,
743
+ 'pronamic_cloudflare_purge_cache_tags ' ,
757
744
null ,
758
745
'pronamic-cloudflare '
759
746
);
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 ;
760
759
}
761
760
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
+ );
763
771
}
764
772
}
0 commit comments