@@ -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