generator = $generator; $this->settings = $settings; } /** * Register the route and hooks. */ public function register(): void { add_rewrite_rule( '^llms\.txt$', 'index.php?' . self::QUERY_VAR . '=1', 'top' ); add_filter( 'query_vars', [ $this, 'addQueryVars' ] ); add_action( 'pre_get_posts', [ $this, 'intercept' ], 1 ); } /** * Add the llms query var. * * @param string[] $vars Query vars. * @return string[] The result. */ public function addQueryVars( array $vars ): array { $vars[] = self::QUERY_VAR; return $vars; } /** * Serve llms.txt when requested. * * @param WP_Query $query Main query. */ public function intercept( WP_Query $query ): void { if ( ! $query->is_main_query() ) { return; } if ( empty( get_query_var( self::QUERY_VAR ) ) ) { return; } if ( ! headers_sent() ) { foreach ( self::responseHeaders() as $header ) { header( $header ); } nocache_headers(); } echo $this->content(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- raw Markdown response body. $this->finishRender(); } /** * Response headers for the Markdown document. * * The nosniff header stops a browser from MIME sniffing the Markdown * body into another type, matching the XSL stylesheet response. * * @return string[] The result. */ public static function responseHeaders(): array { return [ 'Content-Type: text/markdown; charset=UTF-8', 'X-Robots-Tag: noindex, follow', 'X-Content-Type-Options: nosniff', ]; } /** * Build or fetch the cached llms.txt document. * * @return string The result. */ public function content(): string { $validator = (string) get_option( self::VALIDATOR_OPTION, '' ); // The generated sections depend on the post types filter, and a // callback may depend on the request, the visitor or the time. The // document is shared by every visitor, so while a callback is // registered it is rebuilt per request instead of being cached. $filterRegistered = function_exists( 'has_filter' ) && has_filter( 'rankkernel_llms_post_types' ) !== false; if ( ! $filterRegistered ) { $cached = get_transient( self::TRANSIENT ); if ( is_array( $cached ) && isset( $cached['validator'], $cached['md'] ) && (string) $cached['validator'] === $validator ) { return (string) $cached['md']; } } $curated = (string) $this->settings->get( 'content', '' ); // A curated document always wins. Only when the operator has written // nothing does the generator fall back to the automatic sections, // because an empty curated field with 7 published posts produced a // 13 byte document that told crawlers the site had no content. if ( '' === trim( $curated ) ) { $curated = $this->generator->autoSections(); } $markdown = $this->generator->render( (string) get_bloginfo( 'name' ), (string) $this->settings->get( 'summary', '' ), $curated ); if ( ! $filterRegistered ) { set_transient( self::TRANSIENT, [ 'validator' => $validator, 'md' => $markdown, ], 0 ); } return $markdown; } /** * Bump the validator so the next request rebuilds the document. */ public static function invalidate(): void { update_option( self::VALIDATOR_OPTION, (string) time() . '-' . (string) wp_rand(), false ); } /** * Terminate the request unless running under tests. */ private function finishRender(): void { if ( ! defined( 'RANKKERNEL_TESTING' ) ) { exit; } } }