*/ public function caches(): array { $caches = array(); if ( function_exists( 'rocket_clean_files' ) ) { $caches['wp_rocket'] = array( 'name' => 'WP Rocket', 'purge' => static function ( int $post_id, string $url ): void { rocket_clean_files( $url ); }, ); } if ( function_exists( 'w3tc_flush_url' ) ) { $caches['w3_total_cache'] = array( 'name' => 'W3 Total Cache', 'purge' => static function ( int $post_id, string $url ): void { w3tc_flush_url( $url ); }, ); } if ( defined( 'LSCWP_V' ) ) { $caches['litespeed_cache'] = array( 'name' => 'LiteSpeed Cache', 'purge' => static function ( int $post_id, string $url ): void { // LiteSpeed Cache's own documented action. do_action( 'litespeed_purge_url', $url ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound }, ); } if ( function_exists( 'wpsc_delete_url_cache' ) ) { $caches['wp_super_cache'] = array( 'name' => 'WP Super Cache', 'purge' => static function ( int $post_id, string $url ): void { wpsc_delete_url_cache( $url ); }, ); } if ( function_exists( 'wpfc_clear_post_cache_by_id' ) ) { $caches['wp_fastest_cache'] = array( 'name' => 'WP Fastest Cache', 'purge' => static function ( int $post_id, string $url ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Every purge function takes the same arguments. wpfc_clear_post_cache_by_id( $post_id ); }, ); } if ( function_exists( 'sg_cachepress_purge_cache' ) ) { $caches['siteground_optimizer'] = array( 'name' => 'SiteGround Optimizer', 'purge' => static function ( int $post_id, string $url ): void { sg_cachepress_purge_cache( $url ); }, ); } /** * Filters the page caches scheduled content purges. * * @param array $caches Active * known caches, by key: a name to show, and a function that purges one post (ID, URL). */ return (array) apply_filters( 'yangra_conditional_scheduled_content_caches', $caches ); } /** * The names of the known caches that are active. * * @return array */ public function names(): array { return array_values( wp_list_pluck( $this->caches(), 'name' ) ); } /** * Purges posts from every known cache, and from WordPress's object cache. * * @param array $post_ids Posts to purge. * @return array{caches: array, ok: bool} The caches that were purged, and * whether every purge worked (none of them failed with an error). */ public function purge( array $post_ids ): array { $caches = $this->caches(); $ok = true; foreach ( $post_ids as $post_id ) { $post_id = (int) $post_id; if ( null === get_post( $post_id ) ) { continue; } clean_post_cache( $post_id ); $url = (string) get_permalink( $post_id ); foreach ( $caches as $key => $cache ) { try { call_user_func( $cache['purge'], $post_id, $url ); } catch ( Throwable $error ) { $ok = false; if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Only when WP_DEBUG_LOG is on. error_log( sprintf( 'Yangra Conditional: purging post %d from %s failed: %s', $post_id, $key, $error->getMessage() ) ); } } } /** * Fires when scheduled content purges a post, so a host or another * plugin can purge its own cache. * * @param int $post_id Post whose page changed. */ do_action( 'yangra_conditional_purge_post', $post_id ); } return array( 'caches' => array_values( wp_list_pluck( $caches, 'name' ) ), 'ok' => $ok, ); } }