options->full_mode(); $custom = $this->options->full_custom(); if ( self::is_custom_only( $mode, $custom ) ) { return (string) apply_filters( 'agentwelcome_llms_full_txt', $custom . "\n" ); } $max = max( 100000, (int) apply_filters( 'agentwelcome_llms_full_max_bytes', self::MAX_BYTES ) ); $text = self::start( $mode, $custom, SingleLine::from( get_bloginfo( 'name' ) ) ); $left = 0; foreach ( $this->blocks() as $block ) { if ( $left > 0 || strlen( $text ) + strlen( $block ) > $max ) { ++$left; continue; } $text .= $block; } if ( $left > 0 ) { /* translators: %d: number of pages left out. */ $text .= "\n" . sprintf( _n( 'Note: %d page is not included because this file reached its size limit. The full list is in llms.txt.', 'Note: %d pages are not included because this file reached its size limit. The full list is in llms.txt.', $left, 'agentwelcome' ), $left ) . "\n"; } return (string) apply_filters( 'agentwelcome_llms_full_txt', $text ); } /** * Whether the file is only the custom text. An empty text falls back to the generated file. * * @param string $mode One of the LlmsOptions modes. * @param string $custom Custom text. * @return bool True when the custom text is served as written. */ public static function is_custom_only( string $mode, string $custom ): bool { return LlmsOptions::CUSTOM === $mode && '' !== $custom; } /** * The text the generated file starts with: the custom text in Hybrid mode, otherwise the site heading. * * @param string $mode One of the LlmsOptions modes. * @param string $custom Custom text. * @param string $site_name Site name, on one line. * @return string The start of the file, ending with a line break. */ public static function start( string $mode, string $custom, string $site_name ): string { if ( LlmsOptions::HYBRID === $mode && '' !== $custom ) { return $custom . "\n"; } return '# ' . $site_name . "\n"; } /** * The text of each served stored page, in page ID order. * * @return Generator Text of one page at a time. */ private function blocks(): Generator { $cursor = 0; do { $ids = $this->store->ids( $cursor, self::BATCH ); $count = count( $ids ); foreach ( $ids as $id ) { $cursor = $id; $block = $this->block_for( $id ); if ( null !== $block ) { yield $block; } } } while ( self::BATCH === $count ); } /** * Text of one stored page. * * @param int $id Page ID. * @return string|null The text, or null when the page is not served or has no copy. */ private function block_for( int $id ): ?string { $post = get_post( $id ); if ( ! $post instanceof WP_Post || ! $this->eligibility->is_eligible( $post ) ) { return null; } $entry = $this->store->get( $id ); if ( $this->eligibility->hides( $entry ) ) { return null; } $body = null === $entry ? '' : self::strip_front_matter( $entry->markdown ); $url = $this->urls->permalink( $post ); return '' === $body || null === $url ? null : self::block( SingleLine::from( self::title( $post ) ), $url, $body ); } /** * Title of a page: the one stored when it was generated, or the WordPress title. * * @param WP_Post $post The page. * @return string The title. */ private static function title( WP_Post $post ): string { $stored = get_post_meta( $post->ID, PostMeta::TITLE, true ); return is_string( $stored ) && '' !== $stored ? $stored : get_the_title( $post ); } /** * Removes the metadata block at the top of a stored page. * * @param string $markdown Stored Markdown. * @return string The Markdown without the block. */ public static function strip_front_matter( string $markdown ): string { $stripped = preg_replace( self::FRONT_MATTER, '', $markdown, 1 ); return trim( is_string( $stripped ) ? $stripped : $markdown ); } /** * Puts the source line under the page's own top heading, or adds a heading when the body has none. * * @param string $title Page title, used when the body has no heading. * @param string $url Address of the page. * @param string $body Markdown of the page. * @return string The block, starting with a separator. */ public static function block( string $title, string $url, string $body ): string { $source = 'Source: ' . $url; if ( 1 === preg_match( self::TOP_HEADING, $body, $match ) ) { return "\n---\n\n" . $match[1] . "\n\n" . $source . "\n" . ( '' === $match[2] ? '' : "\n" . $match[2] . "\n" ); } return "\n---\n\n# " . ( '' === $title ? $url : $title ) . "\n\n" . $source . "\n\n" . $body . "\n"; } }