'GET', 'callback' => array( $this, 'get' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'PUT', 'callback' => array( $this, 'update' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'enabled' => array( 'type' => 'boolean' ), 'intro' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_textarea_field', ), 'post_types' => array( 'type' => 'array' ), 'max_items' => array( 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'include_tax' => array( 'type' => 'boolean' ), 'full_enabled' => array( 'type' => 'boolean' ), 'full_max_items' => array( 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'key_pages' => array( 'type' => 'array' ), 'key_label' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'brand' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'summary' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_textarea_field', ), 'citation' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'contact' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'license' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'link_sitemap' => array( 'type' => 'boolean' ), 'link_feed' => array( 'type' => 'boolean' ), 'declare_lang' => array( 'type' => 'boolean' ), 'extra' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_textarea_field', ), ), ), ) ); // The developer surface: raw output of both files, cache state, and the // hooks that change them. Everything here is answerable from view-source // and the code — putting it one click away is the difference between a // plugin a developer can extend and one they have to reverse-engineer. register_rest_route( self::NAMESPACE, '/llms-txt/raw', array( 'methods' => 'GET', 'callback' => array( $this, 'raw' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'variant' => array( 'type' => 'string', 'default' => 'short', 'enum' => array( 'short', 'full' ), ), ), ) ); // Fetch the file the site really serves and compare it with what this // screen thinks it is publishing. A rewrite rule that never flushed, a // CDN holding a copy from before the last edit, a security plugin // answering /llms.txt with a 404 — none of that is visible from inside // the admin, and all of it means the assistants are reading something // other than what is on this screen. register_rest_route( self::NAMESPACE, '/llms-txt/verify', array( 'methods' => 'POST', 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'llmsverify', 60 ); }, 'callback' => function ( WP_REST_Request $request ) { $full = (bool) $request->get_param( 'full' ); $url = home_url( $full ? '/llms-full.txt' : '/llms.txt' ); $expected = $full ? $this->generator->generate_full() : $this->generator->generate(); $response = wp_remote_get( $url, array( 'timeout' => 15, 'headers' => array( 'Accept' => 'text/plain' ) ) ); if ( is_wp_error( $response ) ) { return new WP_REST_Response( array( 'ok' => false, 'url' => $url, 'reason' => $response->get_error_message() ) ); } $code = (int) wp_remote_retrieve_response_code( $response ); $body = (string) wp_remote_retrieve_body( $response ); $type = (string) wp_remote_retrieve_header( $response, 'content-type' ); if ( $code >= 400 ) { return new WP_REST_Response( array( 'ok' => false, 'url' => $url, 'status' => $code, 'reason' => 404 === $code ? __( 'The URL returns 404. The rewrite rules have probably not been flushed — open Settings → Permalinks and save once.', 'ai-rank-suite' ) : sprintf( /* translators: %d: HTTP status code. */ __( 'The server answered with HTTP %d.', 'ai-rank-suite' ), $code ), ) ); } // Whitespace differences are not drift; a different body is. $same = trim( preg_replace( '/\s+/', ' ', $body ) ?? '' ) === trim( preg_replace( '/\s+/', ' ', $expected ) ?? '' ); return new WP_REST_Response( array( 'ok' => true, 'url' => $url, 'status' => $code, 'bytes' => strlen( $body ), 'expected' => strlen( $expected ), 'type' => $type, // text/html here means WordPress served a page, not // the file — usually a theme 404 template that // happens to return 200. 'plain' => str_contains( strtolower( $type ), 'text/plain' ), 'matches' => $same, 'body' => mb_substr( $body, 0, 6000 ), ) ); }, 'args' => array( 'full' => array( 'type' => 'boolean', 'default' => false ), ), ) ); // The one-click writer: start, one slice, status, cancel. Driven from // the browser one slice at a time, like the audit. $writer = static fn(): Services\Writer => new Services\Writer( Kernel::service( Options::class ) ); register_rest_route( self::NAMESPACE, '/llms-txt/write', array( array( 'methods' => 'GET', 'callback' => static fn() => new WP_REST_Response( $writer()->status() ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'POST', 'callback' => static fn( WP_REST_Request $request ) => new WP_REST_Response( $writer()->start( (bool) $request->get_param( 'overwrite' ) ) ), // A run summarises every page; twenty starts an hour is plenty. 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'llms_write', 20 ); }, 'args' => array( 'overwrite' => array( 'type' => 'boolean', 'default' => false ), ), ), ) ); register_rest_route( self::NAMESPACE, '/llms-txt/write/tick', array( 'methods' => 'POST', 'callback' => static fn() => new WP_REST_Response( $writer()->tick() ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/llms-txt/write/cancel', array( 'methods' => 'POST', 'callback' => static fn() => new WP_REST_Response( $writer()->cancel() ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/llms-txt/regenerate', array( 'methods' => 'POST', 'callback' => array( $this, 'regenerate' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); } /** * Raw output plus the numbers that decide whether a model will read it. */ public function raw( WP_REST_Request $request ): WP_REST_Response { $full = 'full' === (string) $request['variant']; $body = $full ? $this->generator->generate_full() : $this->generator->generate(); $tokens = Generator::tokens( $body ); return new WP_REST_Response( array( 'variant' => $full ? 'full' : 'short', 'url' => home_url( $full ? '/llms-full.txt' : '/llms.txt' ), 'body' => $body, 'bytes' => strlen( $body ), 'tokens' => $tokens, 'lines' => substr_count( $body, "\n" ) + 1, 'links' => substr_count( $body, '](' ), // What Google's Agentic Browsing report will say about this // file, worked out here so the answer arrives before the // report does. 'lighthouse' => Generator::lighthouse( $body ), // Context windows are large now, but retrieval budgets are not: // a file past roughly 100k tokens gets truncated by most // retrievers, and the truncation point is not your choice. 'heavy' => $tokens > 100000, // The generated reference, narrowed to what shapes this file — // a second hand-written list here was the same staleness bug // the generator exists to end. 'hooks' => array_values( array_map( static fn( array $h ) => array( 'name' => $h['hook'], 'type' => $h['type'], 'args' => $h['args'], 'desc' => $h['desc'], ), array_filter( \AiRankSuite\Modules\DevTools\DevToolsModule::hooks(), static fn( array $h ) => str_starts_with( (string) $h['hook'], 'airanksuite/llms_txt/' ) || 'airanksuite/content/raw' === $h['hook'] ) ) ), ) ); } private function options(): Options { return Kernel::service( Options::class ); } public function get(): WP_REST_Response { $settings = $this->options()->get( 'llms_txt', Generator::defaults() ); $preview = $this->generator->generate(); // Pages the customer can pin, and the ones already pinned resolved to // titles so the UI never has to guess what an id refers to. $candidates = array(); foreach ( get_posts( array( 'post_type' => array_values( (array) $settings['post_types'] ) ?: array( 'page', 'post' ), 'post_status' => 'publish', 'posts_per_page' => 200, 'orderby' => 'title', 'order' => 'ASC', 'suppress_filters' => false, ) ) as $post ) { $candidates[] = array( 'id' => $post->ID, 'title' => get_the_title( $post ), 'type' => $post->post_type, ); } return new WP_REST_Response( array( 'settings' => $settings, 'url' => home_url( '/llms.txt' ), 'full_url' => home_url( '/llms-full.txt' ), 'preview' => $preview, 'candidates' => $candidates, 'stats' => array( 'bytes' => strlen( $preview ), 'tokens' => Generator::tokens( $preview ), 'lines' => substr_count( $preview, "\n" ), 'links' => substr_count( $preview, '](' ), 'lighthouse' => Generator::lighthouse( $preview ), ), ) ); } public function update( WP_REST_Request $request ): WP_REST_Response { $defaults = Generator::defaults(); $values = array(); foreach ( array_keys( $defaults ) as $key ) { if ( $request->has_param( $key ) ) { $values[ $key ] = $request->get_param( $key ); } } if ( isset( $values['post_types'] ) ) { $public = get_post_types( array( 'public' => true ) ); $values['post_types'] = array_values( array_intersect( (array) $values['post_types'], $public ) ); } if ( isset( $values['key_pages'] ) ) { // Real published posts only, de-duplicated, order preserved — the // order is the feature, so array_unique must not reindex it away. $ids = array(); foreach ( array_map( 'absint', (array) $values['key_pages'] ) as $id ) { $post = $id > 0 ? get_post( $id ) : null; if ( $post && 'publish' === $post->post_status && ! in_array( $id, $ids, true ) ) { $ids[] = $id; } } $values['key_pages'] = array_slice( $ids, 0, 20 ); } $this->options()->set( 'llms_txt', $values, $defaults ); delete_transient( LlmsTxtModule::CACHE_KEY ); delete_transient( LlmsTxtModule::CACHE_KEY_FULL ); return $this->get(); } public function regenerate(): WP_REST_Response { delete_transient( LlmsTxtModule::CACHE_KEY ); delete_transient( LlmsTxtModule::CACHE_KEY_FULL ); return $this->get(); } }