` * as `array( 'modified' => post_modified_gmt, 'md' => string )` so a stale * entry is detected even if invalidation hooks were bypassed (e.g. direct * DB edits). */ class Cache { const MD_PREFIX = 'ael42a_md_'; const MD_TTL = DAY_IN_SECONDS; const LLMS_TXT = 'ael42a_llms_txt'; const LLMS_FULL = 'ael42a_llms_full'; const VITALS = 'ael42a_vitals'; const VISIBILITY = 'ael42a_visibility'; const LOCAL_VITALS = 'ael42a_local_vitals'; /** * Register invalidation hooks. */ public static function register_hooks() { add_action( 'save_post', array( __CLASS__, 'invalidate_post' ) ); add_action( 'delete_post', array( __CLASS__, 'invalidate_post' ) ); add_action( 'trashed_post', array( __CLASS__, 'invalidate_post' ) ); } /** * Get a cached markdown document for a post. * * @param \WP_Post $post Post. * @return string|null Markdown or null on miss/stale. */ public static function get_markdown( $post ) { $entry = get_transient( self::MD_PREFIX . $post->ID ); if ( ! is_array( $entry ) || ! isset( $entry['modified'], $entry['md'] ) ) { return null; } if ( $entry['modified'] !== $post->post_modified_gmt ) { return null; } return (string) $entry['md']; } /** * Store a markdown document for a post. * * @param \WP_Post $post Post. * @param string $markdown Rendered markdown document. */ public static function set_markdown( $post, $markdown ) { set_transient( self::MD_PREFIX . $post->ID, array( 'modified' => $post->post_modified_gmt, 'md' => $markdown, ), self::MD_TTL ); } /** * Invalidate the cached markdown (and dependent llms.txt caches) for a post. * * @param int $post_id Post ID. */ public static function invalidate_post( $post_id ) { delete_transient( self::MD_PREFIX . (int) $post_id ); delete_transient( self::LLMS_TXT ); delete_transient( self::LLMS_FULL ); } /** * Delete every transient this plugin owns. Used on disconnect/uninstall. */ public static function purge_all() { global $wpdb; delete_transient( self::LLMS_TXT ); delete_transient( self::LLMS_FULL ); delete_transient( self::VITALS ); delete_transient( self::LOCAL_VITALS ); // Per-post markdown transients - direct LIKE delete (bounded prefix). $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( '_transient_' . self::MD_PREFIX ) . '%', $wpdb->esc_like( '_transient_timeout_' . self::MD_PREFIX ) . '%' ) ); } }