Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Welcome to WordPress. This is your first post...
XML sitemap listing all llms.txt endpoints, automatically integrated with SEO plugins.

### Individual Post Endpoints (Optional)
Access individual posts via `/post-slug.txt` (disabled by default for performance).
Access individual posts via `/postType-slug.txt` (disabled by default for performance).

## Configuration

Expand Down
3 changes: 2 additions & 1 deletion src/Console/LlmsTxtCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ protected function showInfo(): void
$this->line(' • /llms-full.txt - Complete content');

if (config('llms-txt.individual_posts.enabled', false)) {
$this->line(' • /post-slug.txt - Individual post markdown');
$postTypesPattern = implode('|', config('llms-txt.individual_posts.post_types', ['post', 'page']));
$this->line(" • /({$postTypesPattern})-{slug}.txt - Individual post markdown");
}

if (! config('llms-txt.individual_posts.enabled', false)) {
Expand Down
8 changes: 6 additions & 2 deletions src/ContentFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,22 @@ public function formatSitemap(Collection $posts): string
// Add individual posts if enabled
if (config('llms-txt.individual_posts.enabled', false)) {
$posts->each(function ($post) use (&$output, $baseUrl) {
$postType = get_post_type($post['id']);
if (!in_array($postType, config('llms-txt.individual_posts.post_types'))) {
return;
}
$slug = get_post_field('post_name', $post['id']);
if (! empty($slug)) {
$lastmod = date('Y-m-d\TH:i:s+00:00', strtotime($post['date'] ?? 'now'));

$output[] = ' <url>';
$output[] = " <loc>{$baseUrl}/{$slug}.txt</loc>";
$output[] = " <loc>{$baseUrl}/{$postType}-{$slug}.txt</loc>";
$output[] = " <lastmod>{$lastmod}</lastmod>";
$output[] = ' <changefreq>weekly</changefreq>';
$output[] = ' <priority>0.4</priority>';
$output[] = ' </url>';
}
});
})->filter();
}

$output[] = '</urlset>';
Expand Down
40 changes: 23 additions & 17 deletions src/Http/Controllers/LlmsTxtController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,16 @@ public function sitemap(): Response
{
$cacheTtl = config('llms-txt.cache_ttl', 3600);
$content = Cache::remember('llms_txt_sitemap', $cacheTtl, function () {
if (config('llms-txt.individual_posts.enabled') && !empty(config('llms-txt.individual_posts.post_types'))) {
add_filter('acorn/llms_txt/post_types', [$this, 'getIndividualPostTypes']);
}

$posts = $this->fetcher->getPosts();

if (has_filter('acorn/llms_txt/post_types', [$this, 'getIndividualPostTypes'])) {
remove_filter('acorn/llms_txt/post_types', [$this, 'getIndividualPostTypes']);
}

return $this->formatter->formatSitemap($posts);
});

Expand All @@ -79,17 +87,22 @@ public function sitemap(): Response
->header('X-Robots-Tag', 'noindex');
}

public function individual(string $slug): Response
public function getIndividualPostTypes(): array
{
return config('llms-txt.individual_posts.post_types');
}

public function individual(string $postType, string $slug): Response
{
// Get configuration for individual posts
$postTypes = config('llms-txt.individual_posts.post_types', ['post', 'page']);
$cacheTtl = config('llms-txt.individual_posts.cache_ttl', 3600);

$cacheKey = "llms_txt_individual_{$slug}";
$cacheKey = "llms_txt_individual_{$postType}_{$slug}";

$content = Cache::remember($cacheKey, $cacheTtl, function () use ($postType, $slug) {

$content = Cache::remember($cacheKey, $cacheTtl, function () use ($slug, $postTypes) {
// Find the post by slug
$post = $this->findPostBySlug($slug, $postTypes);
// Find the post by slug and posttype
$post = $this->findPostByPostTypeAndSlug($postType, $slug);

if (! $post) {
return null;
Expand All @@ -113,25 +126,18 @@ public function individual(string $slug): Response
->header('X-Robots-Tag', 'noindex');
}

protected function findPostBySlug(string $slug, array $postTypes): ?\WP_Post
protected function findPostByPostTypeAndSlug(string $postType, string $slug): ?\WP_Post
{
// Validate post types before querying
$validPostTypes = [];
foreach ($postTypes as $postType) {
if (post_type_exists($postType)) {
$validPostTypes[] = $postType;
}
}

if (empty($validPostTypes)) {
error_log('No valid post types for individual post lookup: '.implode(', ', $postTypes));
if (! post_type_exists($postType)) {
error_log('No valid post types for individual post lookup: '.$postType);

return null;
}

$args = [
'name' => sanitize_title($slug),
'post_type' => $validPostTypes,
'post_type' => $postType,
'post_status' => 'publish',
'posts_per_page' => 1,
];
Expand Down
6 changes: 4 additions & 2 deletions src/Providers/LlmsTxtServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ public function boot()
protected function registerIndividualPostRoutes(): void
{
if (config('llms-txt.individual_posts.enabled', false)) {
Route::get('{slug}.txt', [LlmsTxtController::class, 'individual'])
->where('slug', '.*');
$postTypesPattern = implode('|', config('llms-txt.individual_posts.post_types', ['post', 'page']));
Route::get('{postType}-{slug}.txt', [LlmsTxtController::class, 'individual'])
->where('postType', $postTypesPattern)
->where('slug', '.+');
}
}
}