$vars Query vars. * @return list */ public static function register_query_var( array $vars ): array { $vars[] = 'agentmatica_llms_txt'; return $vars; } public static function maybe_serve(): void { if ( ! self::is_enabled() || ! self::is_llms_txt_request() ) { return; } status_header( 200 ); nocache_headers(); header( 'Content-Type: text/plain; charset=utf-8' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- generated text/plain llms.txt; HTML escaping would corrupt it. echo self::build_body(); exit; } private static function is_llms_txt_request(): bool { if ( get_query_var( 'agentmatica_llms_txt' ) ) { return true; } if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { return false; } $path = (string) wp_parse_url( sanitize_text_field( wp_unslash( (string) $_SERVER['REQUEST_URI'] ) ), PHP_URL_PATH ); if ( $path === '' ) { return false; } $path = '/' . trim( $path, '/' ); if ( $path === '/llms.txt' ) { return true; } $home_path = (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH ); $home_path = ( $home_path === '/' || $home_path === '' ) ? '' : rtrim( $home_path, '/' ); return $home_path !== '' && $path === $home_path . '/llms.txt'; } public static function get_public_url(): string { return Agentmatica_Public_Urls::get_llms_txt_url(); } public static function build_body(): string { $site_name = wp_specialchars_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES ); $tagline = wp_specialchars_decode( (string) get_bloginfo( 'description' ), ENT_QUOTES ); $home_url = home_url( '/' ); $profile_url = Agentmatica_Public_Urls::get_ucp_profile_url(); $mcp_url = Agentmatica_Public_Urls::get_mcp_endpoint_url(); $has_wc = class_exists( 'WooCommerce' ); $intro = sprintf( /* translators: 1: site name, 2: home URL */ __( '%1$s (%2$s) runs **Agentmatica MCP Connector** — a UCP MCP server for AI visitor chat with native WordPress content tools%3$s.', 'agentmatica-mcp-connector' ), $site_name, $home_url, $has_wc ? __( ' and WooCommerce catalog, cart, and checkout', 'agentmatica-mcp-connector' ) : '' ); $lines = array(); $lines[] = '# ' . $site_name; $lines[] = ''; if ( $tagline !== '' ) { $lines[] = '> ' . $tagline; $lines[] = ''; } $lines[] = $intro; $lines[] = ''; $lines[] = __( 'Key facts:', 'agentmatica-mcp-connector' ); $lines[] = '- ' . sprintf( /* translators: %s: UCP version */ __( 'UCP version: %s', 'agentmatica-mcp-connector' ), Agentmatica_Ucp_Profile::UCP_VERSION ); $lines[] = '- ' . __( 'MCP transport: JSON-RPC 2.0 over POST (Bearer token required)', 'agentmatica-mcp-connector' ); $lines[] = '- ' . __( 'Integration token: WordPress Settings → Agentmatica (prefix amk_wp_)', 'agentmatica-mcp-connector' ); if ( Agentmatica_Public_Urls::uses_pretty_permalinks() ) { $lines[] = '- ' . __( 'REST routing: pretty permalinks (/wp-json/…)', 'agentmatica-mcp-connector' ); } else { $lines[] = '- ' . __( 'REST routing: plain permalinks (index.php?rest_route=…)', 'agentmatica-mcp-connector' ); } $lines[] = ''; $lines[] = '## Commerce Protocol (UCP)'; $lines[] = ''; $lines[] = __( 'Agentmatica MCP Connector', 'agentmatica-mcp-connector' ); $lines[] = '- GET `' . $profile_url . '`'; $lines[] = '- POST `' . $mcp_url . '`'; $lines[] = '- ' . __( 'Auth: `Authorization: Bearer `', 'agentmatica-mcp-connector' ); $lines[] = ''; $lines[] = '## Store MCP (WordPress UCP)'; $lines[] = ''; $lines[] = '- Endpoint: `' . $mcp_url . '`'; $lines[] = '- Discovery profile: `' . $profile_url . '`'; $lines[] = '- ' . __( 'Connect in Agentmatica: paste the MCP endpoint and integration token under Integrations (leave WordPress username blank).', 'agentmatica-mcp-connector' ); $lines[] = ''; $lines[] = '### Tools'; $lines[] = ''; $lines[] = __( 'WordPress content:', 'agentmatica-mcp-connector' ); $lines[] = '- `agentmatica_site_info`, `agentmatica_pages_list`, `agentmatica_posts_list`, `agentmatica_content_get`'; if ( $has_wc ) { $lines[] = ''; $lines[] = __( 'WooCommerce catalog & checkout (UCP):', 'agentmatica-mcp-connector' ); $lines[] = '- `search_catalog`, `lookup_catalog`, `get_product`'; $lines[] = '- `create_cart`, `update_cart`, `get_cart`, `create_checkout`'; $lines[] = ''; $lines[] = __( 'WooCommerce order support (UCP):', 'agentmatica-mcp-connector' ); $lines[] = '- `customer_verify`, `orders_list`, `order_get`, `verify_customer_token`'; $lines[] = '- `cancel_woo_order`, `update_woo_order_address`, `request_woo_return` (when enabled in plugin settings)'; } $lines[] = ''; $lines[] = '## Agentmatica platform'; $lines[] = ''; $lines[] = __( 'Agentmatica is the AI website agent platform that connects to this MCP server:', 'agentmatica-mcp-connector' ); $lines[] = '- Product: https://agentmatica.com'; $lines[] = '- WordPress integration: https://agentmatica.com/docs/integrations/wordpress'; $lines[] = '- Sign up: https://agentmatica.com/signup'; $lines[] = ''; $lines[] = self::build_key_pages_section( $home_url, $has_wc ); return implode( "\n", $lines ) . "\n"; } private static function build_key_pages_section( string $home_url, bool $has_wc ): string { $lines = array(); $lines[] = '## Key pages'; $lines[] = ''; $lines[] = '- [Home](' . $home_url . ')'; if ( $has_wc && function_exists( 'wc_get_page_permalink' ) ) { $shop_url = wc_get_page_permalink( 'shop' ); if ( is_string( $shop_url ) && $shop_url !== '' ) { $lines[] = '- [Shop](' . $shop_url . ')'; } } $seen_urls = array( $home_url => true ); foreach ( self::collect_key_page_entries() as $entry ) { if ( isset( $seen_urls[ $entry['url'] ] ) ) { continue; } $seen_urls[ $entry['url'] ] = true; $lines[] = '- [' . $entry['title'] . '](' . $entry['url'] . ')'; } return implode( "\n", $lines ); } /** * Pages for llms.txt: theme menus, policy/legal pages, then menu_order fallback. * * @return list */ private static function collect_key_page_entries(): array { $entries = array(); $seen_urls = array(); $add = static function ( string $title, string $url ) use ( &$entries, &$seen_urls ): void { if ( $url === '' || isset( $seen_urls[ $url ] ) ) { return; } $seen_urls[ $url ] = true; $entries[] = array( 'title' => $title, 'url' => $url, ); }; foreach ( self::collect_nav_menu_entries() as $entry ) { $add( $entry['title'], $entry['url'] ); } foreach ( self::collect_policy_page_entries() as $entry ) { $add( $entry['title'], $entry['url'] ); } $pages = get_posts( array( 'post_type' => 'page', 'post_status' => 'publish', 'posts_per_page' => 10, 'orderby' => 'menu_order', 'order' => 'ASC', ) ); foreach ( $pages as $page ) { if ( ! $page instanceof WP_Post ) { continue; } $url = get_permalink( $page ); if ( ! is_string( $url ) || $url === '' ) { continue; } $add( wp_specialchars_decode( get_the_title( $page ), ENT_QUOTES ), $url ); } return $entries; } /** * @return list */ private static function collect_nav_menu_entries(): array { $entries = array(); $seen = array(); $locations = get_nav_menu_locations(); if ( ! is_array( $locations ) ) { return $entries; } foreach ( array_values( $locations ) as $menu_id ) { if ( ! $menu_id ) { continue; } $items = wp_get_nav_menu_items( (int) $menu_id ); if ( ! is_array( $items ) ) { continue; } foreach ( $items as $item ) { if ( ! $item instanceof WP_Post ) { continue; } $url = isset( $item->url ) ? (string) $item->url : ''; if ( $url === '' || isset( $seen[ $url ] ) ) { continue; } $seen[ $url ] = true; $entries[] = array( 'title' => wp_specialchars_decode( (string) $item->title, ENT_QUOTES ), 'url' => $url, ); } } return $entries; } /** * Common policy/legal pages — matched by slug so header/footer links are indexed. * * @return list */ private static function collect_policy_page_entries(): array { $entries = array(); $seen = array(); $slug_patterns = array( 'return-policy', 'returns', 'refund-policy', 'refunds', 'privacy-policy', 'privacy', 'shipping-policy', 'shipping', 'terms-of-service', 'terms', 'terms-and-conditions', ); foreach ( $slug_patterns as $slug ) { $page = get_page_by_path( $slug ); if ( ! $page instanceof WP_Post || $page->post_status !== 'publish' ) { continue; } $url = get_permalink( $page ); if ( ! is_string( $url ) || $url === '' || isset( $seen[ $url ] ) ) { continue; } $seen[ $url ] = true; $entries[] = array( 'title' => wp_specialchars_decode( get_the_title( $page ), ENT_QUOTES ), 'url' => $url, ); } return $entries; } }