__( 'Permission denied.', 'ailinkmaster-internal-auto-manager' ) ), 403 ); } } /** * Save module settings. * * @return void */ public static function save_settings() { self::guard(); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- nonce + capability checked in guard(); the payload is sanitized by AILM_LlmsTxt_Settings::update_from_input() $raw = isset( $_POST['settings'] ) && is_array( $_POST['settings'] ) ? wp_unslash( $_POST['settings'] ) : array(); $stored = AILM_LlmsTxt_Settings::update_from_input( $raw ); // Enabling the module triggers an immediate first build. $was_enabled = ! empty( $stored['enabled'] ); if ( $was_enabled ) { $result = AILM_LlmsTxt_Rebuilder::rebuild(); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } } else { // Disabled: remove artifacts so stale files are not served. AILM_LlmsTxt_Rebuilder::remove_artifacts(); } wp_send_json_success( array( 'message' => $was_enabled ? __( 'Settings saved and llms.txt generated.', 'ailinkmaster-internal-auto-manager' ) : __( 'Settings saved. The module is disabled; generated files were removed.', 'ailinkmaster-internal-auto-manager' ), ) ); } /** * Manual rebuild. * * @return void */ public static function rebuild() { self::guard(); // Pressing "Rebuild" is an explicit intent to have llms.txt: // auto-enable the module instead of failing with "disabled". $auto_enabled = false; if ( ! AILM_LlmsTxt_Settings::is_enabled() ) { AILM_LlmsTxt_Settings::set_enabled( true ); $auto_enabled = true; } $result = AILM_LlmsTxt_Rebuilder::rebuild(); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( array( 'message' => $auto_enabled ? __( 'Module enabled and llms.txt generated.', 'ailinkmaster-internal-auto-manager' ) : __( 'llms.txt rebuilt successfully.', 'ailinkmaster-internal-auto-manager' ), 'status' => AILM_LlmsTxt_Rebuilder::get_status(), ) ); } /** * Validate the generated file. * * @return void */ public static function validate() { self::guard(); $content = AILM_LlmsTxt_File_Writer::read( AILM_LlmsTxt_File_Writer::FILENAME_LLMS ); if ( false === $content ) { $content = AILM_LlmsTxt_Server::get_db_copy( AILM_LlmsTxt_File_Writer::FILENAME_LLMS ); } if ( '' === $content ) { wp_send_json_error( array( 'message' => __( 'No llms.txt found. Rebuild first.', 'ailinkmaster-internal-auto-manager' ) ) ); } // Defensive lazy-load: a missing scanner class used to fatal (HTTP 500) // instead of returning a readable error. if ( ! class_exists( 'AILM_LlmsTxt_Scanner' ) ) { require_once plugin_dir_path( __FILE__ ) . 'class-ailm-llmstxt-scanner.php'; } if ( ! class_exists( 'AILM_LlmsTxt_Scanner' ) ) { wp_send_json_error( array( 'message' => __( 'Validation unavailable: scanner class could not be loaded.', 'ailinkmaster-internal-auto-manager' ) ) ); } try { $report = AILM_LlmsTxt_Scanner::scan( $content ); } catch ( \Throwable $e ) { wp_send_json_error( array( 'message' => sprintf( /* translators: %s: error message. */ __( 'Validation failed: %s', 'ailinkmaster-internal-auto-manager' ), $e->getMessage() ), ) ); } wp_send_json_success( array( 'message' => $report['ok'] ? sprintf( /* translators: %d: number of URLs. */ __( 'Validation passed: %d URLs, no issues found.', 'ailinkmaster-internal-auto-manager' ), $report['urls'] ) : __( 'Validation finished with warnings.', 'ailinkmaster-internal-auto-manager' ), 'report' => $report, ) ); } /** * Sync AI summaries from the SaaS server. * * @return void */ public static function sync() { self::guard(); $result = AILM_LlmsTxt_Data::sync(); if ( ! empty( $result['ok'] ) ) { wp_send_json_success( array( 'message' => $result['message'], 'synced' => isset( $result['synced'] ) ? (int) $result['synced'] : 0, ) ); } wp_send_json_error( array( 'message' => $result['message'], 'code' => isset( $result['code'] ) ? $result['code'] : '', ) ); } /** * Status endpoint (files, last rebuild, summaries cache). * * @return void */ public static function status() { self::guard(); wp_send_json_success( array( 'enabled' => AILM_LlmsTxt_Settings::is_enabled(), 'llms_exists' => AILM_LlmsTxt_File_Writer::exists( AILM_LlmsTxt_File_Writer::FILENAME_LLMS ), 'full_exists' => AILM_LlmsTxt_File_Writer::exists( AILM_LlmsTxt_File_Writer::FILENAME_LLMS_FULL ), 'llms_url' => home_url( '/llms.txt' ), 'full_url' => home_url( '/llms-full.txt' ), 'status' => AILM_LlmsTxt_Rebuilder::get_status(), 'summaries' => AILM_LlmsTxt_Data::count_cached(), 'last_sync' => AILM_LlmsTxt_Data::last_sync_at(), 'writable' => self::is_root_writable(), ) ); } /** * Generate a preview draft (does NOT touch the live file). * * @return void */ public static function preview() { self::guard(); if ( ! AILM_LlmsTxt_Settings::is_enabled() ) { AILM_LlmsTxt_Settings::set_enabled( true ); } $result = AILM_LlmsTxt_Rebuilder::rebuild(); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( array( 'message' => __( 'Preview generated. Review the draft below, edit if needed, then press Publish.', 'ailinkmaster-internal-auto-manager' ), 'draft' => AILM_LlmsTxt_Rebuilder::get_draft(), ) ); } /** * Publish the (possibly admin-edited) draft to the live file. * * @return void */ public static function publish() { self::guard(); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- nonce + capability checked in guard(); the draft is sanitized when published $content = isset( $_POST['content'] ) ? (string) wp_unslash( $_POST['content'] ) : ''; // UX: publishing an empty draft must not dead-end the user. The most // common cause is "opened the page, pressed Publish before Generate // preview" (or the draft was never built). Instead of an error, // generate the content now and publish it — the exact outcome the // user expects from the button. if ( '' === trim( $content ) ) { if ( ! AILM_LlmsTxt_Settings::is_enabled() ) { AILM_LlmsTxt_Settings::set_enabled( true ); } $content = AILM_LlmsTxt_Generator::generate(); } if ( '' === trim( $content ) ) { wp_send_json_error( array( 'message' => __( 'Nothing to publish: no posts matched the current settings.', 'ailinkmaster-internal-auto-manager' ) ) ); } $result = AILM_LlmsTxt_Rebuilder::publish( $content ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } $full_published = ! empty( AILM_LlmsTxt_Settings::get( 'generate_full' ) ); wp_send_json_success( array( 'message' => $full_published ? __( 'Published! The live llms.txt and llms-full.txt files have been updated.', 'ailinkmaster-internal-auto-manager' ) : __( 'Published! The live llms.txt file has been updated.', 'ailinkmaster-internal-auto-manager' ), 'url' => home_url( '/llms.txt' ), ) ); } /** * Search published posts by title (select2 AJAX source). * * @return void */ public static function search_posts() { self::guard(); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified by guard() $term = isset( $_POST['q'] ) ? sanitize_text_field( wp_unslash( $_POST['q'] ) ) : ''; if ( '' === $term ) { wp_send_json_success( array( 'results' => array() ) ); } $settings = AILM_LlmsTxt_Settings::get_all(); $post_types = is_array( $settings['post_types'] ) ? $settings['post_types'] : array( 'post', 'page' ); $query = new WP_Query( array( 'post_type' => $post_types, 'post_status' => 'publish', 's' => $term, 'posts_per_page' => 20, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'orderby' => 'title', 'order' => 'ASC', ) ); $results = array(); foreach ( $query->posts as $post ) { $results[] = array( 'id' => (int) $post->ID, 'text' => $post->post_title . ' (' . $post->post_type . ')', ); } wp_send_json_success( array( 'results' => $results ) ); } /** * Prefill the manual selection with top posts by AILM Rank. * * @return void */ public static function prefill_top() { self::guard(); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified by guard() $limit = isset( $_POST['limit'] ) ? absint( $_POST['limit'] ) : 20; $limit = max( 1, min( 100, $limit ) ); $settings = AILM_LlmsTxt_Settings::get_all(); $post_types = is_array( $settings['post_types'] ) ? $settings['post_types'] : array( 'post', 'page' ); $query = new WP_Query( array( 'post_type' => $post_types, 'post_status' => 'publish', 'posts_per_page' => 50, 'no_found_rows' => true, 'update_post_term_cache' => false, 'orderby' => 'date', 'order' => 'DESC', ) ); // Sort by AILM Rank (read postmeta in bulk for the fetched set). $scored = array(); foreach ( $query->posts as $post ) { $pr = get_post_meta( $post->ID, '_ailm_pagerank', true ); $scored[] = array( 'id' => (int) $post->ID, 'pr' => '' !== $pr ? (float) $pr : 0.0 ); } usort( $scored, function ( $a, $b ) { if ( abs( $a['pr'] - $b['pr'] ) > 0.000001 ) { return ( $a['pr'] > $b['pr'] ) ? -1 : 1; } return 0; } ); $top = array_slice( $scored, 0, $limit ); $results = array(); foreach ( $top as $item ) { $post = get_post( $item['id'] ); if ( $post ) { $results[] = array( 'id' => $item['id'], 'text' => $post->post_title . ' (' . $post->post_type . ')', ); } } wp_send_json_success( array( 'message' => sprintf( /* translators: %d: number of posts. */ __( 'Prefilled %d top posts by AILM Rank.', 'ailinkmaster-internal-auto-manager' ), count( $results ) ), 'results' => $results, ) ); } /** * Whether the site root is writable (for status display). * * @return bool */ private static function is_root_writable() { $dir = AILM_LlmsTxt_File_Writer::get_target_dir(); return wp_is_writable( $dir ); } }