get_error_message(); } } // Draft is always refreshed so the admin can review and publish. // The DB copy doubles as the fallback live file when the site root // is not writable. self::store_draft( $content ); AILM_LlmsTxt_Server::store_db_copy( AILM_LlmsTxt_File_Writer::FILENAME_LLMS, $content ); // 2. llms-full.txt (optional). The full generator stores its own DB // copy fallback when the write fails, so this never aborts the // rebuild (Generate preview / Regenerate must keep working). if ( ! empty( $settings['generate_full'] ) ) { $full_result = AILM_LlmsTxt_FullGenerator::generate(); if ( is_wp_error( $full_result ) ) { $warning = $full_result->get_error_message(); } } self::update_status( true, $warning ); self::clear_dirty_flags(); if ( ! $force ) { self::release_lock(); } return true; } /** * Publish the (admin-edited) draft to the live file. * * @param string $content Content to publish (already sanitized by caller: * plain text, no HTML tags allowed). * @return true|WP_Error */ public static function publish( $content ) { if ( ! AILM_LlmsTxt_Settings::is_enabled() ) { return new WP_Error( 'llmstxt_disabled', __( 'The llms.txt module is disabled.', 'ailinkmaster-internal-auto-manager' ) ); } // Hard sanitization: llms.txt is plain text, strip everything HTML-ish. $content = preg_replace( '@<\s*/?\s*[a-zA-Z][^>]*>@', '', (string) $content ); $content = str_replace( "\0", '', (string) $content ); // Store the draft + DB copy FIRST: when the site root is not // writable, the DB copy becomes the live file (served by // AILM_LlmsTxt_Server's template_redirect fallback), so the // published content must not be lost on a write failure. self::store_draft( $content ); AILM_LlmsTxt_Server::store_db_copy( AILM_LlmsTxt_File_Writer::FILENAME_LLMS, $content ); $warning = ''; $result = AILM_LlmsTxt_File_Writer::write( AILM_LlmsTxt_File_Writer::FILENAME_LLMS, $content ); if ( is_wp_error( $result ) ) { // Fallback serving keeps the published content live. $warning = $result->get_error_message(); } // llms-full.txt: publish together with llms.txt when its generation // is enabled — one button publishes both files. The full generator // refreshes its DB copy itself; write_live forces the physical file. $settings = AILM_LlmsTxt_Settings::get_all(); if ( ! empty( $settings['generate_full'] ) ) { $full_result = AILM_LlmsTxt_FullGenerator::generate( true ); if ( is_wp_error( $full_result ) ) { $warning = $full_result->get_error_message(); } } self::update_status( true, $warning ); return true; } /** * Get the current draft content ('' when never generated). * * @return string */ public static function get_draft() { return (string) get_option( self::DRAFT_OPTION, '' ); } /** * Store the draft content. * * @param string $content Draft content. * @return void */ private static function store_draft( $content ) { update_option( self::DRAFT_OPTION, (string) $content, false ); } /** * Mark a post dirty on status transitions (publish, unpublish, trash). * * @param string $new_status New status. * @param string $old_status Old status. * @param WP_Post $post Post. * @return void */ public static function mark_dirty_on_transition( $new_status, $old_status, $post ) { if ( wp_is_post_autosave( $post->ID ) || wp_is_post_revision( $post->ID ) ) { return; } // Only transitions involving 'publish' change the file contents. if ( 'publish' !== $new_status && 'publish' !== $old_status ) { return; } $settings = AILM_LlmsTxt_Settings::get_all(); $post_types = is_array( $settings['post_types'] ) ? $settings['post_types'] : array(); if ( ! in_array( $post->post_type, $post_types, true ) ) { return; } if ( empty( $settings['auto_rebuild'] ) ) { return; } update_post_meta( $post->ID, self::DIRTY_META, 1 ); } /** * Permanent post deletion: remove the post from the SaaS index and * the local summaries cache. * * Runs on before_delete_post. Guarded by the module's post types so * unrelated deletions (attachments, CPTs the module ignores) do not * fire SaaS requests. The summaries cache cleanup only matters when * summaries are in use, but forgetting a missing post is a no-op, so * it runs unconditionally within the module's scope. * * @param int $post_id Post ID being permanently deleted. * @return void */ public static function handle_post_deletion( $post_id ) { if ( ! AILM_LlmsTxt_Settings::is_enabled() ) { return; } $post = get_post( $post_id ); if ( ! $post ) { return; } $settings = AILM_LlmsTxt_Settings::get_all(); $post_types = is_array( $settings['post_types'] ) ? $settings['post_types'] : array(); if ( ! in_array( $post->post_type, $post_types, true ) ) { return; } AILM_LlmsTxt_Data::delete_server_document( (int) $post_id ); } /** * Cron handler: rebuild when dirty posts exist. * * @return void */ public static function handle_rebuild_cron() { if ( ! AILM_LlmsTxt_Settings::is_enabled() ) { return; } $settings = AILM_LlmsTxt_Settings::get_all(); if ( empty( $settings['auto_rebuild'] ) ) { return; } if ( ! self::has_dirty_posts() ) { return; } self::rebuild( true ); } /** * Daily cron: refresh the summaries cache (single paginated sync). * * @return void */ public static function handle_daily_sync() { if ( ! AILM_LlmsTxt_Settings::is_enabled() ) { return; } $settings = AILM_LlmsTxt_Settings::get_all(); if ( empty( $settings['use_summaries'] ) ) { return; } AILM_LlmsTxt_Data::sync(); } /** * Whether any post is flagged dirty. * * @return bool */ public static function has_dirty_posts() { $dirty = get_posts( array( 'post_type' => 'any', 'post_status' => 'any', 'posts_per_page' => 1, 'fields' => 'ids', 'meta_key' => self::DIRTY_META, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => 1, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value 'no_found_rows' => true, ) ); return ! empty( $dirty ); } /** * Remove all dirty flags after a successful rebuild. * * @return void */ public static function clear_dirty_flags() { delete_post_meta_by_key( self::DIRTY_META ); } /** * Unschedule module cron events (deactivation/uninstall path). * * @return void */ public static function unschedule_crons() { $timestamp = wp_next_scheduled( self::CRON_HOOK ); if ( $timestamp ) { wp_unschedule_event( $timestamp, self::CRON_HOOK ); } $timestamp = wp_next_scheduled( self::CRON_SYNC_HOOK ); if ( $timestamp ) { wp_unschedule_event( $timestamp, self::CRON_SYNC_HOOK ); } } /** * Remove generated files + DB copies + dirty flags (disable/uninstall). * * @return void */ public static function remove_artifacts() { AILM_LlmsTxt_File_Writer::delete( AILM_LlmsTxt_File_Writer::FILENAME_LLMS ); AILM_LlmsTxt_File_Writer::delete( AILM_LlmsTxt_File_Writer::FILENAME_LLMS_FULL ); AILM_LlmsTxt_Server::clear_db_copies(); self::clear_dirty_flags(); delete_option( self::STATUS_OPT ); delete_option( self::DRAFT_OPTION ); } // ------------------------------------------------------------------------- // Status + lock helpers. // ------------------------------------------------------------------------- /** * Read the last rebuild status. * * @return array{ok:bool,last_run:string,message:string} */ public static function get_status() { $defaults = array( 'ok' => false, 'last_run' => '', 'message' => '', ); $status = get_option( self::STATUS_OPT, array() ); if ( ! is_array( $status ) ) { $status = array(); } return wp_parse_args( $status, $defaults ); } /** * Persist the rebuild status. * * @param bool $ok Success flag. * @param string $message Error message. * @return void */ private static function update_status( $ok, $message = '' ) { update_option( self::STATUS_OPT, array( 'ok' => (bool) $ok, 'last_run' => gmdate( 'Y-m-d H:i:s' ), 'message' => (string) $message, ), false ); } /** * Acquire the rebuild lock (10-minute TTL). * * @return bool */ private static function acquire_lock() { if ( get_transient( self::LOCK_TRANSIENT ) ) { return false; } set_transient( self::LOCK_TRANSIENT, 1, 10 * MINUTE_IN_SECONDS ); return true; } /** * Release the rebuild lock. * * @return void */ private static function release_lock() { delete_transient( self::LOCK_TRANSIENT ); } }