$post->post_status, 'name' => $post->post_name, 'url' => get_permalink( $post ), ); } } /** * A restore writes the row directly (no transition hooks): if it unpublished the page or put * back another slug, its old address is still cached — purge that one too. */ public static function on_restore( $id ) { self::on_post( $id ); if ( ! is_numeric( $id ) || ! isset( self::$before[ (int) $id ] ) ) { return; } $was = self::$before[ (int) $id ]; unset( self::$before[ (int) $id ] ); $post = get_post( (int) $id ); if ( ! $post || $post->post_status !== $was['status'] || $post->post_name !== $was['name'] ) { self::$urls[ $was['url'] ] = (int) $id; } } /** Unpublishing (or trashing) a page: its public address is no longer what get_permalink() gives. */ public static function on_status( $id, $before = '', $after = '' ) { self::on_post( $id ); $post = get_post( (int) $id ); if ( ! $post || ! is_string( $before ) || '' === $before || ! is_post_status_viewable( $before ) || is_post_publicly_viewable( $post ) ) { return; } $old = clone $post; $old->post_status = $before; if ( 'trash' === $post->post_status ) { $slug = get_post_meta( $post->ID, '_wp_desired_post_slug', true ); if ( is_string( $slug ) && '' !== $slug ) { $old->post_name = $slug; } } $url = get_permalink( $old ); if ( $url ) { self::$urls[ $url ] = $post->ID; } } /** Posts whose content appears on other pages (headers, footers, popups, templates, patterns). */ private static function is_sitewide( \WP_Post $post ) { $types = (array) apply_filters( 'adibridge_sitewide_post_types', array( 'elementor_library', 'wp_template', 'wp_template_part', 'wp_block', 'wp_navigation' ) ); return in_array( $post->post_type, $types, true ) || ! is_post_type_viewable( $post->post_type ); } // ── Purging ───────────────────────────────────────────────────────────────────────── /** Run everything queued, once (a whole-site purge covers every page). */ public static function flush() { self::$ran = array(); if ( ! self::$site && ! self::$posts && ! self::$urls ) { return; } $site = self::$site; $posts = array_keys( self::$posts ); $urls = self::$urls; $pages = array_keys( self::$option_pages ); self::$site = false; self::$posts = array(); self::$urls = array(); self::$option_pages = array(); foreach ( $posts as $id ) { self::purge_post_data( $id ); } if ( $site ) { self::purge_site( $pages ); return; } foreach ( $posts as $id ) { self::purge_url( $id, get_permalink( $id ), true ); } foreach ( $urls as $url => $id ) { self::purge_url( $id, $url, false ); } } /** Run one purge, isolated: a failure is logged, not thrown, and its output is discarded. */ private static function run( $name, callable $purge ) { $level = ob_get_level(); ob_start(); try { $purge(); self::$ran[] = $name; } catch ( \Throwable $e ) { Log::line( "cache purge $name failed: " . Tool_Error::text( $e ) ); } finally { while ( ob_get_level() > $level ) { ob_end_clean(); } } } /** The post's own cached data: WordPress's object cache, Elementor's CSS and element cache. */ private static function purge_post_data( $id ) { self::run( 'wordpress-object-cache', static function () use ( $id ) { clean_post_cache( $id ); } ); if ( class_exists( '\\Elementor\\Core\\Files\\CSS\\Post' ) && 'builder' === get_post_meta( $id, '_elementor_edit_mode', true ) ) { self::run( 'elementor-css', static function () use ( $id ) { \Elementor\Core\Files\CSS\Post::create( $id )->delete(); delete_post_meta( $id, '_elementor_element_cache' ); } ); } } /** * One address in every page cache. $current: it is the post's current address (plugins that * purge by post id are used); otherwise an old address, purged by URL — or, for a plugin with * no URL purge, by clearing that plugin's whole cache. */ private static function purge_url( $id, $url, $current ) { if ( defined( 'LSCWP_V' ) || has_action( 'litespeed_purge_post' ) ) { self::run( 'litespeed', static function () use ( $id, $url, $current ) { if ( $current ) { do_action( 'litespeed_purge_post', $id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's documented purge hook } else { do_action( 'litespeed_purge_url', $url ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's documented purge hook } } ); } if ( function_exists( 'rocket_clean_post' ) ) { self::run( 'wp-rocket', static function () use ( $id, $url, $current ) { if ( $current ) { rocket_clean_post( $id ); } elseif ( function_exists( 'rocket_clean_files' ) ) { rocket_clean_files( array( $url ) ); } elseif ( function_exists( 'rocket_clean_domain' ) ) { rocket_clean_domain(); } else { throw new \RuntimeException( 'no way to purge an old address' ); } } ); } if ( function_exists( 'w3tc_flush_post' ) ) { self::run( 'w3-total-cache', static function () use ( $id, $url, $current ) { if ( $current ) { w3tc_flush_post( $id ); } elseif ( function_exists( 'w3tc_flush_url' ) ) { w3tc_flush_url( $url ); } elseif ( function_exists( 'w3tc_flush_all' ) ) { w3tc_flush_all(); } else { throw new \RuntimeException( 'no way to purge an old address' ); } } ); } if ( function_exists( 'wp_cache_post_change' ) ) { self::run( 'wp-super-cache', static function () use ( $id, $url, $current ) { if ( $current ) { wp_cache_post_change( $id ); } elseif ( function_exists( 'wpsc_delete_url_cache' ) ) { wpsc_delete_url_cache( $url ); } elseif ( function_exists( 'wp_cache_clear_cache' ) ) { wp_cache_clear_cache(); } else { throw new \RuntimeException( 'no way to purge an old address' ); } } ); } if ( isset( $GLOBALS['wp_fastest_cache'] ) && method_exists( $GLOBALS['wp_fastest_cache'], 'singleDeleteCache' ) ) { self::run( 'wp-fastest-cache', static function () use ( $id, $current ) { if ( $current ) { $GLOBALS['wp_fastest_cache']->singleDeleteCache( false, $id ); } else { $GLOBALS['wp_fastest_cache']->deleteCache( true ); } } ); } if ( function_exists( 'sg_cachepress_purge_cache' ) ) { self::run( 'siteground', static function () use ( $url ) { sg_cachepress_purge_cache( $url ); } ); } if ( has_action( 'breeze_clear_all_cache' ) ) { // Breeze has no per-page purge: this clears its whole cache. self::run( 'breeze', static function () { do_action( 'breeze_clear_all_cache' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Breeze's documented purge hook } ); } if ( has_action( 'adibridge_purge_post' ) ) { self::run( 'adibridge_purge_post', static function () use ( $id, $url ) { do_action( 'adibridge_purge_post', $id, $url ); } ); } } /** * Options pages and site-wide templates feed every page (header, footer, popups): clear * everything. * * @param string[] $option_pages Options pages that changed: their option keys leave the object cache. */ private static function purge_site( array $option_pages = array() ) { if ( $option_pages ) { self::run( 'wordpress-object-cache', static function () use ( $option_pages ) { self::forget_options( $option_pages ); } ); } if ( class_exists( '\\Elementor\\Plugin' ) && isset( \Elementor\Plugin::$instance->files_manager ) ) { self::run( 'elementor-css', static function () { \Elementor\Plugin::$instance->files_manager->clear_cache(); } ); } if ( defined( 'LSCWP_V' ) || has_action( 'litespeed_purge_all' ) ) { self::run( 'litespeed', static function () { do_action( 'litespeed_purge_all' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's documented purge hook } ); } if ( function_exists( 'rocket_clean_domain' ) ) { self::run( 'wp-rocket', static function () { rocket_clean_domain(); } ); } if ( function_exists( 'w3tc_flush_all' ) ) { self::run( 'w3-total-cache', static function () { w3tc_flush_all(); } ); } if ( function_exists( 'wp_cache_clear_cache' ) ) { self::run( 'wp-super-cache', static function () { wp_cache_clear_cache(); } ); } if ( isset( $GLOBALS['wp_fastest_cache'] ) && method_exists( $GLOBALS['wp_fastest_cache'], 'deleteCache' ) ) { self::run( 'wp-fastest-cache', static function () { $GLOBALS['wp_fastest_cache']->deleteCache( true ); } ); } if ( function_exists( 'sg_cachepress_purge_everything' ) ) { self::run( 'siteground', static function () { sg_cachepress_purge_everything(); } ); } if ( has_action( 'breeze_clear_all_cache' ) ) { self::run( 'breeze', static function () { do_action( 'breeze_clear_all_cache' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Breeze's documented purge hook } ); } if ( has_action( 'adibridge_purge_site' ) ) { self::run( 'adibridge_purge_site', static function () { do_action( 'adibridge_purge_site' ); } ); } } /** Drop an options page's options (ACF stores _ and __) from the object cache. */ private static function forget_options( array $slugs ) { global $wpdb; $pages = Adapters\Acf::option_pages(); foreach ( $slugs as $slug ) { if ( ! isset( $pages[ $slug ] ) ) { continue; } $prefix = (string) $pages[ $slug ]['post_id']; $prefix = 'option' === $prefix ? 'options' : $prefix; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the option names an ACF options page stores, to purge their cached copies; no API lists them $names = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( $prefix . '_' ) . '%', $wpdb->esc_like( '_' . $prefix . '_' ) . '%' ) ); foreach ( $names as $name ) { wp_cache_delete( $name, 'options' ); } } wp_cache_delete( 'alloptions', 'options' ); wp_cache_delete( 'notoptions', 'options' ); if ( function_exists( 'acf_get_store' ) ) { acf_get_store( 'values' )->reset(); } } /** Run the queued purges and add the ones that ran to a write tool's result. */ public static function report( $result ) { self::flush(); $result['caches_cleared'] = array_values( array_unique( self::$ran ) ); self::$ran = array(); return $result; } }