feedRepo = $feedRepo; $this->placements = $placements; } public function register(): void { // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores add_action( 'feednetic/feed_updated', [ $this, 'purge' ] ); } /** * Purges URLs affected by a change to $feedId. * * Falls back to a full-domain purge when the feed has ever been rendered * without a post context (block-theme template parts). */ public function purge( int $feedId ): void { $feed = $this->feedRepo->findById( $feedId ); if ( $feed && ! empty( $feed['is_headless'] ) ) { $this->purgeAll( $feedId ); return; } $pageIds = $this->findPageIds( $feedId ); if ( empty( $pageIds ) ) { return; } foreach ( $pageIds as $pageId ) { $url = get_permalink( $pageId ); if ( ! $url ) { continue; } $this->purgePost( $pageId, $url ); } } /** * Triggers a full-domain cache purge. * Fires feednetic/cache_purge_all so integrations and tests can hook in. */ public function purgeAll( int $feedId ): void { // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores do_action( 'feednetic/cache_purge_all', $feedId ); // WP Rocket. if ( function_exists( 'rocket_clean_domain' ) ) { rocket_clean_domain(); } // LiteSpeed Cache. if ( is_callable( [ 'LiteSpeed_Cache_API', 'purge_all' ] ) ) { call_user_func( [ 'LiteSpeed_Cache_API', 'purge_all' ] ); } elseif ( has_action( 'litespeed_purge_all' ) ) { // LiteSpeed Cache's own public hook (third-party, so not prefixed). // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound do_action( 'litespeed_purge_all' ); } // W3 Total Cache. if ( function_exists( 'w3tc_flush_all' ) ) { w3tc_flush_all(); } // WP Super Cache. if ( function_exists( 'wp_cache_clear_cache' ) ) { wp_cache_clear_cache(); } // SiteGround Optimizer. if ( function_exists( 'sg_cachepress_purge_cache' ) ) { sg_cachepress_purge_cache(); } } // ── Helpers ─────────────────────────────────────────────────────────────── /** * Returns IDs of WP posts/pages on which feed $feedId has been rendered. * Populated at render time via PlacementRegistry::record() and backfilled * once by CleanupJob::backfill(). * * @return int[] */ private function findPageIds( int $feedId ): array { return $this->placements->getForFeed( $feedId ); } /** * Purges a single post/page URL in every detected cache plugin. * Falls back to full-domain purge for plugins with no per-URL API. */ private function purgePost( int $pageId, string $url ): void { // WP Rocket — per-post API available. if ( function_exists( 'rocket_clean_post' ) ) { rocket_clean_post( $pageId ); } elseif ( function_exists( 'rocket_clean_domain' ) ) { rocket_clean_domain(); } // LiteSpeed Cache — per-URL via action hook. if ( has_action( 'litespeed_purge_url' ) ) { // LiteSpeed Cache's own public hook (third-party, so not prefixed). // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound do_action( 'litespeed_purge_url', $url ); } elseif ( is_callable( [ 'LiteSpeed_Cache_API', 'purge_all' ] ) ) { call_user_func( [ 'LiteSpeed_Cache_API', 'purge_all' ] ); } // W3 Total Cache — per-post API. if ( function_exists( 'w3tc_flush_post' ) ) { w3tc_flush_post( $pageId ); } elseif ( function_exists( 'w3tc_flush_all' ) ) { w3tc_flush_all(); } // WP Super Cache — per-URL API. if ( function_exists( 'wpsc_delete_url_cache' ) ) { wpsc_delete_url_cache( $url ); } elseif ( function_exists( 'wp_cache_clear_cache' ) ) { wp_cache_clear_cache(); } // SiteGround Optimizer — no per-URL API; always full purge. if ( function_exists( 'sg_cachepress_purge_cache' ) ) { sg_cachepress_purge_cache(); } } }