' . self::escape_blockquote( $settings['site_summary'] ?? '' ); $lines[] = ''; $lines[] = 'Generated by ADCT LLM Index v' . ADCT_LLMS_VERSION . '. Curated index for AI assistants and LLM crawlers.'; $lines[] = ''; $primary = self::build_primary_pages( $settings ); if ( ! empty( $primary ) ) { $lines[] = '## Primary pages'; $lines[] = ''; $lines = array_merge( $lines, self::items_to_lines( $primary ) ); $lines[] = ''; } $order = array( 'brands', 'pages', 'products', 'posts' ); foreach ( $order as $section_key ) { $section = $sections[ $section_key ] ?? array(); if ( empty( $section['enabled'] ) ) { continue; } $items = ADCT_LLMS_Scanner::collect_section( $section_key, $section, $settings, $file ); $items = self::dedupe_items( $items, $primary ); if ( empty( $items ) ) { continue; } $title = trim( (string) ( $section['title'] ?? ucfirst( $section_key ) ) ); $lines[] = '## ' . $title; $lines[] = ''; $lines = array_merge( $lines, self::items_to_lines( $items ) ); $lines[] = ''; } $cpts = $sections['cpts'] ?? array(); foreach ( $cpts as $post_type => $section ) { if ( empty( $section['enabled'] ) ) { continue; } $items = ADCT_LLMS_Scanner::collect_section( $post_type, $section, $settings, $file ); if ( empty( $items ) ) { continue; } $title = trim( (string) ( $section['title'] ?? ucfirst( $post_type ) ) ); $lines[] = '## ' . $title; $lines[] = ''; $lines = array_merge( $lines, self::items_to_lines( $items ) ); $lines[] = ''; } $lines[] = '## Optional'; $lines[] = ''; if ( 'llms' === $file ) { $lines[] = '- [Full index (llms-full.txt)](' . esc_url( home_url( '/llms-full.txt' ) ) . '): Expanded site index with more pages, products, and posts.'; } if ( function_exists( 'wp_sitemaps_get_server' ) || class_exists( 'WPSEO_Sitemaps' ) ) { $lines[] = '- [XML sitemap](' . esc_url( home_url( '/sitemap_index.xml' ) ) . '): Complete sitemap for deep crawling.'; } $lines[] = ''; return implode( "\n", $lines ); } private static function build_primary_pages( array $settings ) { $ids = array(); if ( ! empty( $settings['shop_page_id'] ) ) { $ids[] = absint( $settings['shop_page_id'] ); } $ids = array_merge( $ids, array_map( 'absint', (array) ( $settings['pinned_page_ids'] ?? array() ) ) ); $ids = array_values( array_unique( array_filter( $ids ) ) ); if ( empty( $ids ) ) { return array(); } $query = new WP_Query( array( 'post_type' => 'page', 'post_status' => 'publish', 'post__in' => $ids, 'orderby' => 'post__in', 'posts_per_page' => count( $ids ), 'no_found_rows' => true, ) ); $items = array(); foreach ( $query->posts as $post ) { $items[] = array( 'title' => get_the_title( $post ), 'url' => get_permalink( $post ), 'description' => self::primary_page_description( $post, $settings ), ); } return $items; } private static function primary_page_description( WP_Post $post, array $settings ) { if ( absint( $settings['shop_page_id'] ?? 0 ) === (int) $post->ID ) { return 'Main inventory and shop page.'; } $text = wp_strip_all_tags( has_excerpt( $post ) ? $post->post_excerpt : $post->post_content ); $text = preg_replace( '/\s+/', ' ', trim( $text ) ); if ( strlen( $text ) > 160 ) { $text = substr( $text, 0, 157 ) . '...'; } return $text; } private static function items_to_lines( array $items ) { $lines = array(); foreach ( $items as $item ) { $title = self::escape_markdown_text( $item['title'] ?? '' ); $url = esc_url( $item['url'] ?? '' ); $line = '- [' . $title . '](' . $url . ')'; if ( ! empty( $item['description'] ) ) { $line .= ': ' . self::escape_markdown_text( $item['description'] ); } $lines[] = $line; } return $lines; } private static function dedupe_items( array $items, array $primary ) { if ( empty( $primary ) ) { return $items; } $seen = array(); foreach ( $primary as $row ) { if ( ! empty( $row['url'] ) ) { $seen[ $row['url'] ] = true; } } $clean = array(); foreach ( $items as $item ) { $url = $item['url'] ?? ''; if ( $url && isset( $seen[ $url ] ) ) { continue; } $clean[] = $item; } return $clean; } private static function escape_blockquote( $text ) { $text = wp_strip_all_tags( (string) $text ); $text = preg_replace( '/\s+/', ' ', trim( $text ) ); return str_replace( array( "\r", "\n" ), ' ', $text ); } private static function escape_markdown_text( $text ) { $text = wp_strip_all_tags( (string) $text ); $text = preg_replace( '/\s+/', ' ', trim( $text ) ); return str_replace( array( '[', ']', '(', ')', '\\' ), array( '\\[', '\\]', '\\(', '\\)', '\\\\' ), $text ); } }