LlmsTxtFileHelper.php
10 months ago
LlmsTxtGenerator.php
2 weeks ago
LlmsTxtParser.php
10 months ago
LlmsTxtParser.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\LlmsTxtGenerator; |
| 4 | |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit; |
| 7 | |
| 8 | class LlmsTxtParser { |
| 9 | |
| 10 | public const DEFAULT_LIMIT = 100; |
| 11 | public const HOSTINGER_LLMSTXT_SIGNATURE = '[comment]: # (Generated by Hostinger Tools Plugin)'; |
| 12 | |
| 13 | public function get_by_post_type( string $post_type, int $limit = self::DEFAULT_LIMIT, int $offset = 0 ): array { |
| 14 | $limit = apply_filters( 'hostinger_batch_item_limit', $limit ); |
| 15 | $args = array( |
| 16 | 'post_type' => $post_type, |
| 17 | 'post_status' => 'publish', |
| 18 | 'fields' => 'ids', |
| 19 | 'posts_per_page' => $limit, |
| 20 | 'offset' => $offset, |
| 21 | ); |
| 22 | |
| 23 | return get_posts( $args ); |
| 24 | } |
| 25 | |
| 26 | public function get_items( array $items ): string { |
| 27 | $content = ''; |
| 28 | foreach ( $items as $item ) { |
| 29 | $post = get_post( $item ); |
| 30 | $title = $post->post_title; |
| 31 | $permalink = get_permalink( $post ); |
| 32 | $excerpt = $this->prepare_excerpt( $post ); |
| 33 | |
| 34 | $content .= "- [$title]($permalink)"; |
| 35 | if ( $excerpt ) { |
| 36 | $content .= ": $excerpt"; |
| 37 | } |
| 38 | |
| 39 | $content .= "\n"; |
| 40 | } |
| 41 | |
| 42 | return $content; |
| 43 | } |
| 44 | |
| 45 | public function inject_site_description(): string { |
| 46 | $description = get_bloginfo( 'description' ); |
| 47 | |
| 48 | return $description ? "> $description\n\n" : ''; |
| 49 | } |
| 50 | |
| 51 | public function inject_title(): string { |
| 52 | $title = get_bloginfo( 'name' ) ? get_bloginfo( 'name' ) : site_url(); |
| 53 | |
| 54 | return "# $title\n\n"; |
| 55 | } |
| 56 | |
| 57 | public function inject_signature(): string { |
| 58 | return "\n\n" . self::HOSTINGER_LLMSTXT_SIGNATURE; |
| 59 | } |
| 60 | |
| 61 | public function inject_mcp_agent_entry(): string { |
| 62 | $domain = parse_url( site_url(), PHP_URL_HOST ); |
| 63 | |
| 64 | return "- [Agent (MCP protocol)](websites-agents.hostinger.com/$domain/mcp)"; |
| 65 | } |
| 66 | |
| 67 | public function inject_items( array $items, string $title ): string { |
| 68 | if ( empty( $items ) ) { |
| 69 | return ''; |
| 70 | } |
| 71 | |
| 72 | $content = "\n## $title\n\n"; |
| 73 | $content .= $this->get_items( $items ); |
| 74 | |
| 75 | return $content; |
| 76 | } |
| 77 | |
| 78 | public function inject_optional_entries( array $entries ): string { |
| 79 | $output = ''; |
| 80 | |
| 81 | if ( ! empty( $entries ) ) { |
| 82 | $output = "\n## Optional\n\n"; |
| 83 | $output .= implode( "\n", $entries ); |
| 84 | } |
| 85 | |
| 86 | return $output; |
| 87 | } |
| 88 | |
| 89 | public function prepare_excerpt( \WP_Post $item ): string { |
| 90 | return str_replace( array( "\r", "\n" ), ' ', strip_tags( wp_trim_excerpt( $item->post_excerpt, $item ) ) ); |
| 91 | } |
| 92 | } |
| 93 |