| 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 |
protected LlmsTxtSummaryProvider $summary_provider; |
| 14 |
|
| 15 |
public function __construct( LlmsTxtSummaryProvider $summary_provider ) { |
| 16 |
$this->summary_provider = $summary_provider; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_by_post_type( string $post_type, int $limit = self::DEFAULT_LIMIT, int $offset = 0 ): array { |
| 20 |
$limit = apply_filters( 'hostinger_batch_item_limit', $limit ); |
| 21 |
$args = array( |
| 22 |
'post_type' => $post_type, |
| 23 |
'post_status' => 'publish', |
| 24 |
'has_password' => false, |
| 25 |
'fields' => 'ids', |
| 26 |
'posts_per_page' => $limit, |
| 27 |
'offset' => $offset, |
| 28 |
); |
| 29 |
|
| 30 |
return get_posts( $args ); |
| 31 |
} |
| 32 |
|
| 33 |
public function get_items( array $items ): string { |
| 34 |
$content = ''; |
| 35 |
foreach ( $items as $item ) { |
| 36 |
$post = get_post( $item ); |
| 37 |
$title = $post->post_title; |
| 38 |
$permalink = get_permalink( $post ); |
| 39 |
$summary = $this->summary_provider->get_summary( $post ); |
| 40 |
|
| 41 |
$content .= "- [$title]($permalink)"; |
| 42 |
if ( $summary ) { |
| 43 |
$content .= ": $summary"; |
| 44 |
} |
| 45 |
|
| 46 |
$content .= "\n"; |
| 47 |
} |
| 48 |
|
| 49 |
return $content; |
| 50 |
} |
| 51 |
|
| 52 |
public function inject_site_description(): string { |
| 53 |
$description = get_bloginfo( 'description' ); |
| 54 |
|
| 55 |
return $description ? "> $description\n\n" : ''; |
| 56 |
} |
| 57 |
|
| 58 |
public function inject_title(): string { |
| 59 |
$title = get_bloginfo( 'name' ) ? get_bloginfo( 'name' ) : site_url(); |
| 60 |
|
| 61 |
return "# $title\n\n"; |
| 62 |
} |
| 63 |
|
| 64 |
public function inject_signature(): string { |
| 65 |
return "\n\n" . self::HOSTINGER_LLMSTXT_SIGNATURE; |
| 66 |
} |
| 67 |
|
| 68 |
public function inject_mcp_agent_entry(): string { |
| 69 |
$domain = parse_url( site_url(), PHP_URL_HOST ); |
| 70 |
|
| 71 |
return "- [Agent (MCP protocol)](websites-agents.hostinger.com/$domain/mcp)"; |
| 72 |
} |
| 73 |
|
| 74 |
public function inject_items( array $items, string $title ): string { |
| 75 |
if ( empty( $items ) ) { |
| 76 |
return ''; |
| 77 |
} |
| 78 |
|
| 79 |
$content = "\n## $title\n\n"; |
| 80 |
$content .= $this->get_items( $items ); |
| 81 |
|
| 82 |
return $content; |
| 83 |
} |
| 84 |
|
| 85 |
public function inject_optional_entries( array $entries ): string { |
| 86 |
$output = ''; |
| 87 |
|
| 88 |
if ( ! empty( $entries ) ) { |
| 89 |
$output = "\n## Optional\n\n"; |
| 90 |
$output .= implode( "\n", $entries ); |
| 91 |
} |
| 92 |
|
| 93 |
return $output; |
| 94 |
} |
| 95 |
} |
| 96 |
|