DAY_IN_SECONDS;
if ($force_refresh) {
ai_ready_force_refresh_sections();
update_option('ai_ready_last_section_refresh', time());
}
$custom_content = get_option('ai_ready_custom_content', '');
if (!empty($custom_content)) {
return apply_filters('ai_ready_llms_txt_content', $custom_content);
}
$lines = [];
$site_title_option = get_option('ai_ready_site_title', false);
if ($site_title_option === false || empty($site_title_option)) {
$site_title_from_bloginfo = get_bloginfo('name');
$site_title = $site_title_from_bloginfo;
} else {
$site_title = $site_title_option;
}
$site_title = is_string($site_title) ? $site_title : (string)$site_title;
$lines[] = "# {$site_title}";
$lines[] = "";
$site_summary = get_option('ai_ready_site_summary', '');
if (empty($site_summary)) {
$site_summary = get_option('ai_ready_llms_summary', '');
}
if (empty($site_summary)) {
$site_summary = get_bloginfo('description');
}
if (!empty($site_summary)) {
$lines[] = "> {$site_summary}";
$lines[] = "";
}
$additional_notes = get_option('ai_ready_llms_notes', '');
if (empty($additional_notes)) {
$auto_notes = ai_ready_generate_auto_notes();
if (!empty($auto_notes)) {
$lines[] = trim($auto_notes);
$lines[] = "";
}
} else {
$lines[] = trim($additional_notes);
$lines[] = "";
}
ai_ready_init_llms_defaults();
$sections = get_option('ai_ready_llms_sections', []);
$needs_upgrade = false;
if (empty($sections) || $force_refresh) {
$needs_upgrade = true;
} elseif (count($sections) === 1 && isset($sections[0]['name']) && strtolower($sections[0]['name']) === 'docs') {
$links = $sections[0]['links'] ?? [];
if (count($links) <= 1) {
$needs_upgrade = true;
}
}
if ($needs_upgrade) {
$sections = ai_ready_get_default_sections();
update_option('ai_ready_llms_sections', $sections);
}
foreach ($sections as $section) {
if (empty($section['name']) || empty($section['links'])) {
continue;
}
$section_name = isset($section['name']) ? (string)$section['name'] : '';
$lines[] = "## {$section_name}";
$lines[] = "";
foreach ($section['links'] as $link) {
if (empty($link['title']) || empty($link['url'])) {
continue;
}
$link_title = isset($link['title']) ? (string)$link['title'] : '';
$link_url = isset($link['url']) ? (string)$link['url'] : '';
$link_description = isset($link['description']) ? (string)$link['description'] : '';
$description = !empty($link_description) ? ": {$link_description}" : '';
$lines[] = "- [{$link_title}]({$link_url}){$description}";
}
$lines[] = "";
}
$content = implode("\n", $lines);
return apply_filters('ai_ready_llms_txt_content', $content);
} catch (Exception $e) {
return "# " . __('Error', 'ai-ready-core') . "\n\n" . __('An error occurred while generating llms.txt content.', 'ai-ready-core');
}
}
/**
* Get default sections for llms.txt
*
* Creates sensible default sections when no custom sections are configured
* Automatically detects and includes relevant content following the llms.txt spec
*
* @return array Default sections array
*/
if (!function_exists('ai_ready_get_default_sections')) {
function ai_ready_get_default_sections() {
$sections = [];
$direct_pages = ai_ready_get_pages_direct();
if (!empty($direct_pages)) {
$categorized_pages = ai_ready_categorize_sitemap_pages($direct_pages);
foreach ($categorized_pages as $category => $pages) {
if (empty($pages)) continue;
$section_name = ucfirst($category);
$section_links = [];
if ($category === 'docs') {
$section_links[] = [
'title' => __('Home', 'ai-ready-core'),
'url' => home_url(),
'description' => __('Main website homepage', 'ai-ready-core')
];
}
foreach ($pages as $page) {
$description = ai_ready_generate_page_description($page, $category);
$section_links[] = [
'title' => $page['title'],
'url' => $page['url'],
'description' => $description
];
}
if (!empty($section_links)) {
$sections[] = [
'name' => $section_name,
'links' => $section_links
];
}
}
if (isset($categorized_pages['optional'])) {
$optional_index = array_search('Optional', array_column($sections, 'name'));
if ($optional_index !== false) {
array_unshift($sections[$optional_index]['links'],
[
'title' => __('Sitemap', 'ai-ready-core'),
'url' => home_url('/sitemap.xml'),
'description' => __('Complete site structure for navigation', 'ai-ready-core')
],
[
'title' => __('RSS Feed', 'ai-ready-core'),
'url' => get_feed_link(),
'description' => __('Syndicated content updates', 'ai-ready-core')
]
);
}
}
} else {
ai_ready_refresh_sitemap();
$sitemap_pages = ai_ready_get_pages_from_sitemap();
if (!empty($sitemap_pages)) {
$categorized_pages = ai_ready_categorize_sitemap_pages($sitemap_pages);
} else {
$sections = ai_ready_get_default_sections_manual();
}
}
return $sections;
}
}
/**
* Get pages directly from WordPress database (most reliable method)
*
* @return array Array of page data
*/
function ai_ready_get_pages_direct() {
$pages = [];
$wp_pages = get_pages([
'post_status' => 'publish',
'number' => 100,
'sort_column' => 'menu_order, post_title'
]);
foreach ($wp_pages as $page) {
$page_data = [
'id' => $page->ID,
'title' => $page->post_title,
'url' => get_permalink($page),
'type' => 'page',
'slug' => $page->post_name,
'content_length' => strlen(wp_strip_all_tags($page->post_content))
];
$pages[] = $page_data;
}
$wp_posts = get_posts([
'post_status' => 'publish',
'numberposts' => 10,
'post_type' => 'post'
]);
foreach ($wp_posts as $post) {
$content_length = strlen(wp_strip_all_tags($post->post_content));
if ($content_length > 500) {
$pages[] = [
'id' => $post->ID,
'title' => $post->post_title,
'url' => get_permalink($post),
'type' => 'post',
'slug' => $post->post_name,
'content_length' => $content_length
];
}
}
return $pages;
}
/**
* Refresh WordPress sitemaps to ensure they're up to date
*/
function ai_ready_refresh_sitemap() {
if (function_exists('wp_sitemaps_get_server')) {
$sitemaps = wp_sitemaps_get_server();
if ($sitemaps && method_exists($sitemaps, 'registry')) {
wp_cache_flush();
}
}
if (function_exists('wpseo_invalidate_sitemap_cache')) {
wpseo_invalidate_sitemap_cache();
}
if (class_exists('RankMath\Sitemap\Cache_Watcher')) {
do_action('rank_math/sitemap/invalidate_cache');
}
if (function_exists('aioseo')) {
if (method_exists(aioseo()->sitemap, 'clearCache')) {
aioseo()->sitemap->clearCache();
}
}
}
/**
* Generate appropriate description for a page based on its category
*
* @param array $page Page data
* @param string $category Page category
* @return string Description text
*/
function ai_ready_generate_page_description($page, $category) {
$slug = strtolower($page['slug']);
$title = strtolower($page['title']);
if (strpos($slug, 'about') !== false || strpos($title, 'about') !== false) {
return __('Information about this organization', 'ai-ready-core');
}
if (strpos($slug, 'mission') !== false || strpos($title, 'mission') !== false) {
return __('Organization mission and vision', 'ai-ready-core');
}
if (strpos($slug, 'service') !== false || strpos($title, 'service') !== false) {
return __('Services and products offered', 'ai-ready-core');
}
if (strpos($slug, 'products') !== false || strpos($title, 'products') !== false) {
return __('Products offered', 'ai-ready-core');
}
if (strpos($slug, 'contact') !== false || strpos($title, 'contact') !== false) {
return __('Contact information and methods', 'ai-ready-core');
}
if (strpos($slug, 'faq') !== false || strpos($title, 'faq') !== false) {
return __('Frequently asked questions and support', 'ai-ready-core');
}
if (strpos($slug, 'portfolio') !== false || strpos($title, 'portfolio') !== false) {
return 'Examples of work and projects';
}
if (strpos($slug, 'resource') !== false || strpos($title, 'resource') !== false) {
return 'Additional resources and documentation';
}
if (strpos($slug, 'privacy') !== false || strpos($title, 'privacy') !== false) {
return 'Privacy policy and data handling practices';
}
if (strpos($slug, 'terms') !== false || strpos($title, 'terms') !== false) {
return 'Terms of service and usage conditions';
}
$extracted_description = ai_ready_extract_page_description($page);
if ($extracted_description) {
return $extracted_description;
}
switch ($category) {
case 'docs':
return 'Important site information';
case 'content':
return $page['content_length'] > 1000 ? 'Detailed content and information' : 'Important content';
case 'resources':
return 'Resources and additional materials';
case 'optional':
return 'Additional information and resources';
default:
return 'Website content';
}
}
/**
* Extract meaningful description from page content
*
* @param array $page Page data with ID
* @return string|false Extracted description or false if none found
*/
function ai_ready_extract_page_description($page) {
if (empty($page['id'])) {
return false;
}
$post = get_post($page['id']);
if (!$post) {
return false;
}
if (!empty($post->post_excerpt)) {
$excerpt = wp_strip_all_tags($post->post_excerpt);
$excerpt = preg_replace('/\s+/', ' ', $excerpt);
$excerpt = trim($excerpt);
if (strlen($excerpt) > 20 && strlen($excerpt) <= 150) {
return $excerpt;
} elseif (strlen($excerpt) > 150) {
return substr($excerpt, 0, 147) . '...';
}
}
$content = $post->post_content;
if (empty($content)) {
return false;
}
$content = do_shortcode($content);
$content = wp_strip_all_tags($content);
$content = preg_replace('/\s+/', ' ', $content);
$content = trim($content);
if (empty($content)) {
return false;
}
$first_sentence = ai_ready_extract_first_sentence($content);
if ($first_sentence && strlen($first_sentence) >= 20 && strlen($first_sentence) <= 200) {
return $first_sentence;
}
if (strlen($content) > 120) {
$truncated = substr($content, 0, 117);
$last_space = strrpos($truncated, ' ');
if ($last_space > 80) {
$truncated = substr($truncated, 0, $last_space);
}
return $truncated . '...';
} elseif (strlen($content) >= 20) {
return $content;
}
return false;
}
/**
* Extract the first meaningful sentence from text
*
* @param string $text Content text
* @return string|false First sentence or false if none found
*/
function ai_ready_extract_first_sentence($text) {
$sentences = preg_split('/[.!?]+/', $text, 3);
if (!empty($sentences[0])) {
$first_sentence = trim($sentences[0]);
if (strlen($first_sentence) >= 20 && str_word_count($first_sentence) >= 4) {
return $first_sentence;
}
if (!empty($sentences[1]) && strlen($first_sentence) < 60) {
$combined = trim($first_sentence . '. ' . trim($sentences[1]));
if (strlen($combined) <= 200) {
return $combined;
}
}
}
return false;
}
/**
* Fallback manual page discovery (original method)
*/
function ai_ready_get_default_sections_manual() {
$sections = [];
$docs_links = [];
$docs_links[] = [
'title' => 'Home',
'url' => home_url(),
'description' => 'Main website homepage'
];
$find_page = function($variants, $title_variants = []) {
foreach ($variants as $variant) {
$page = get_page_by_path($variant);
if ($page) {
return $page;
}
}
if (!empty($title_variants)) {
$pages = get_pages([
'post_status' => 'publish',
'number' => 10,
'sort_column' => 'post_title'
]);
foreach ($pages as $page) {
$page_title = strtolower($page->post_title);
foreach ($title_variants as $variant) {
if (strpos($page_title, strtolower($variant)) !== false) {
return $page;
}
}
}
}
return null;
};
$about_page = $find_page(
['about', 'about-us', 'who-we-are', 'our-story', 'company', 'mission', 'vision'],
['about', 'who we are', 'our story', 'mission', 'vision']
);
if ($about_page) {
$docs_links[] = [
'title' => $about_page->post_title,
'url' => get_permalink($about_page),
'description' => 'Information about this organization'
];
}
$mission_page = $find_page(
['mission', 'vision', 'mission-vision', 'mission-statement', 'our-mission'],
['mission', 'vision', 'mission statement', 'our mission']
);
if ($mission_page && (!$about_page || $mission_page->ID !== $about_page->ID)) {
$docs_links[] = [
'title' => $mission_page->post_title,
'url' => get_permalink($mission_page),
'description' => 'Organization mission and vision'
];
}
$service_page = $find_page(
['services', 'products', 'offerings', 'solutions', 'what-we-do', 'capabilities'],
['services', 'products', 'offerings', 'solutions', 'what we do', 'capabilities']
);
if ($service_page) {
$docs_links[] = [
'title' => $service_page->post_title,
'url' => get_permalink($service_page),
'description' => 'Services and products offered'
];
}
$faq_page = $find_page(
['faq', 'help', 'support', 'frequently-asked-questions', 'knowledge-base', 'faqs'],
['faq', 'faqs', 'help', 'support', 'frequently asked questions', 'knowledge base']
);
if ($faq_page) {
$docs_links[] = [
'title' => $faq_page->post_title,
'url' => get_permalink($faq_page),
'description' => 'Frequently asked questions and support'
];
}
$contact_page = $find_page(
['contact', 'contact-us', 'get-in-touch', 'reach-us', 'connect'],
['contact', 'contact us', 'get in touch', 'reach us', 'connect']
);
if ($contact_page) {
$docs_links[] = [
'title' => $contact_page->post_title,
'url' => get_permalink($contact_page),
'description' => 'Contact information and methods'
];
}
if (!empty($docs_links)) {
$sections[] = [
'name' => 'Docs',
'links' => $docs_links
];
}
$optional_links = [];
$optional_links[] = [
'title' => 'Sitemap',
'url' => home_url('/sitemap.xml'),
'description' => 'Complete site structure for navigation'
];
$optional_links[] = [
'title' => 'RSS Feed',
'url' => get_feed_link(),
'description' => 'Syndicated content updates'
];
$privacy_page_id = get_option('wp_page_for_privacy_policy');
if ($privacy_page_id) {
$optional_links[] = [
'title' => get_the_title($privacy_page_id),
'url' => get_permalink($privacy_page_id),
'description' => 'Data handling and privacy practices'
];
}
if (!empty($optional_links)) {
$sections[] = [
'name' => 'Optional',
'links' => $optional_links
];
}
return $sections;
}
/**
* Get social media links from theme customizer or common sources
*
* @return array Array of social media links
*/
function ai_ready_get_social_links() {
$social_links = [];
$social_options = [
'facebook' => 'Facebook',
'twitter' => 'Twitter',
'linkedin' => 'LinkedIn',
'instagram' => 'Instagram',
'youtube' => 'YouTube',
'github' => 'GitHub'
];
foreach ($social_options as $platform => $label) {
$url = get_theme_mod($platform . '_url') ?:
get_theme_mod('social_' . $platform) ?:
get_option($platform . '_url') ?:
get_option('social_' . $platform);
if ($url && filter_var($url, FILTER_VALIDATE_URL)) {
$social_links[] = [
'title' => $label,
'url' => $url,
'description' => 'Follow on ' . $label
];
}
}
return $social_links;
}
/**
* Generate automatic contextual notes for llms.txt
*
* @return string Auto-generated notes
*/
if (!function_exists('ai_ready_generate_auto_notes')) {
function ai_ready_generate_auto_notes() {
$notes = [];
try {
$site_url = home_url();
$admin_email = get_option('admin_email');
$post_count_obj = wp_count_posts('post');
$post_count = (is_object($post_count_obj) && isset($post_count_obj->publish)) ? (int)$post_count_obj->publish : 0;
$page_count_obj = wp_count_posts('page');
$page_count = (is_object($page_count_obj) && isset($page_count_obj->publish)) ? (int)$page_count_obj->publish : 0;
if ($post_count > 0 && $page_count > 0) {
$notes[] = sprintf("This website contains %d published articles and %d static pages.", $post_count, $page_count);
} elseif ($post_count > 0) {
$notes[] = sprintf("This is a blog-focused website with %d published articles.", $post_count);
} elseif ($page_count > 0) {
$notes[] = sprintf("This is a static website with %d main pages.", $page_count);
}
$active_plugins = get_option('active_plugins', []);
if (!is_array($active_plugins)) {
$active_plugins = [];
}
$features = [];
if (get_option('users_can_register')) {
$features[] = "user registration";
}
if (get_option('default_comment_status') === 'open') {
$features[] = "commenting system";
}
if (in_array('woocommerce/woocommerce.php', $active_plugins) || class_exists('WooCommerce')) {
$features[] = "e-commerce functionality";
}
if (in_array('polylang/polylang.php', $active_plugins) ||
in_array('sitepress-multilingual-cms/sitepress.php', $active_plugins)) {
$features[] = "multilingual content";
}
if (in_array('learnpress/learnpress.php', $active_plugins) ||
in_array('learndash/learndash.php', $active_plugins)) {
$features[] = "learning management system";
}
if (!empty($features)) {
$notes[] = "Key features: " . implode(', ', $features) . ".";
}
if (function_exists('wp_get_theme')) {
$theme = wp_get_theme();
$theme_name = '';
if (is_object($theme) && method_exists($theme, 'get')) {
$theme_name = $theme->get('Name');
if (!is_string($theme_name)) {
$theme_name = '';
}
}
if (!empty($theme_name)) {
$theme_name_lower = strtolower($theme_name);
if (strpos($theme_name_lower, 'business') !== false ||
strpos($theme_name_lower, 'corporate') !== false) {
$notes[] = "This appears to be a business or corporate website.";
} elseif (strpos($theme_name_lower, 'portfolio') !== false) {
$notes[] = "This appears to be a portfolio website showcasing work and projects.";
} elseif ($post_count > $page_count * 2) {
$notes[] = "This is primarily a content-driven site with regular blog updates.";
}
}
}
if (function_exists('get_posts')) {
$recent_post = get_posts(['numberposts' => 1, 'post_status' => 'publish']);
if (!empty($recent_post) && is_array($recent_post) && isset($recent_post[0])) {
$post = $recent_post[0];
if (is_object($post) && isset($post->post_date)) {
$last_updated = strtotime($post->post_date);
if ($last_updated !== false) {
$days_ago = floor((time() - $last_updated) / (60 * 60 * 24));
if ($days_ago < 7) {
$notes[] = "Content is updated regularly (last updated within the past week).";
} elseif ($days_ago < 30) {
$notes[] = "Content is updated periodically (last updated within the past month).";
}
}
}
}
}
} catch (Exception $e) {
return "This is a WordPress website.";
}
return implode(" ", $notes);
}
}
/**
* Get available pages for section building
*
* Helper function to get a list of published pages that could be included
* in llms.txt sections. Useful for admin interface.
*
* @return array Array of page objects with ID, title, and URL
*/
function ai_ready_get_available_pages() {
$pages = get_pages([
'post_status' => 'publish',
'number' => 50,
'sort_column' => 'menu_order, post_title'
]);
$available_pages = [];
foreach ($pages as $page) {
$available_pages[] = [
'id' => $page->ID,
'title' => $page->post_title,
'url' => get_permalink($page),
'slug' => $page->post_name
];
}
return $available_pages;
}
/**
* Validate llms.txt content format
*
* Checks if the generated content follows the llms.txt specification
*
* @param string $content The llms.txt content to validate
* @return array Validation results with 'valid' boolean and 'errors' array
*/
function ai_ready_validate_llms_txt($content) {
$errors = [];
$lines = explode("\n", $content);
$first_line = '';
foreach ($lines as $line) {
if (!empty(trim($line))) {
$first_line = trim($line);
break;
}
}
if (!preg_match('/^# .+/', $first_line)) {
$errors[] = 'Content must start with an H1 title (# Title)';
}
$has_section = false;
foreach ($lines as $line) {
if (preg_match('/^## .+/', trim($line))) {
$has_section = true;
break;
}
}
if (!$has_section) {
$errors[] = 'Content must have at least one section (## Section Name)';
}
$in_section = false;
foreach ($lines as $line) {
$trimmed = trim($line);
if (preg_match('/^## .+/', $trimmed)) {
$in_section = true;
continue;
}
if ($in_section && preg_match('/^- /', $trimmed)) {
if (!preg_match('/^- \[.+\]\(.+\)/', $trimmed)) {
$errors[] = 'Section links must use markdown format: - [Title](URL)';
break;
}
}
}
return [
'valid' => empty($errors),
'errors' => $errors
];
}
add_action('ai_ready_core_activated', 'ai_ready_init_llms_defaults');
/**
* Public wrapper matching spec name.
* Delegates to the enhanced generator.
*
* @return string
*/
function ai_ready_generate_llms_txt() {
return ai_ready_generate_llms_txt_enhanced();
}
/**
* Get pages from sitemap for comprehensive page discovery
*
* @return array Array of page objects with URL, title, and type
*/
function ai_ready_get_pages_from_sitemap() {
$pages = [];
$sitemap_url = home_url('/sitemap.xml');
$sitemap_urls = [
home_url('/wp-sitemap.xml'),
home_url('/sitemap.xml'),
home_url('/sitemap_index.xml'),
home_url('/post-sitemap.xml'),
home_url('/page-sitemap.xml'),
];
foreach ($sitemap_urls as $url) {
$sitemap_content = ai_ready_fetch_sitemap($url);
if ($sitemap_content) {
$discovered_pages = ai_ready_parse_sitemap($sitemap_content);
$pages = array_merge($pages, $discovered_pages);
}
}
$unique_pages = [];
$seen_urls = [];
foreach ($pages as $page) {
if (!in_array($page['url'], $seen_urls)) {
$unique_pages[] = $page;
$seen_urls[] = $page['url'];
}
}
return $unique_pages;
}
/**
* Fetch sitemap content
*
* @param string $url Sitemap URL
* @return string|false Sitemap content or false on failure
*/
function ai_ready_fetch_sitemap($url) {
$response = wp_remote_get($url, [
'timeout' => 10,
'headers' => [
'User-Agent' => 'AI-Ready-Plugin/1.0'
]
]);
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
return wp_remote_retrieve_body($response);
}
if (strpos($url, '/wp-sitemap.xml') !== false) {
$sitemap_content = ai_ready_generate_local_sitemap();
if ($sitemap_content) {
return $sitemap_content;
}
}
return false;
}
/**
* Generate a basic sitemap from WordPress data
*
* @return string Simple XML sitemap
*/
function ai_ready_generate_local_sitemap() {
$pages = get_pages([
'post_status' => 'publish',
'number' => 100
]);
$posts = get_posts([
'post_status' => 'publish',
'numberposts' => 50,
'post_type' => 'post'
]);
$xml = '' . "\n";
$xml .= '' . "\n";
$xml .= '' . home_url() . '' . "\n";
foreach ($pages as $page) {
$xml .= '' . get_permalink($page) . '' . "\n";
}
foreach ($posts as $post) {
$xml .= '' . get_permalink($post) . '' . "\n";
}
$xml .= '';
return $xml;
}
/**
* Parse sitemap XML content
*
* @param string $xml_content Sitemap XML content
* @return array Array of discovered pages
*/
function ai_ready_parse_sitemap($xml_content) {
$pages = [];
if (strpos($xml_content, '(.*?)<\/loc>/', $xml_content, $matches);
if (!empty($matches[1])) {
foreach ($matches[1] as $sitemap_url) {
if (strpos($sitemap_url, 'page-sitemap') !== false ||
strpos($sitemap_url, 'wp-sitemap-pages') !== false) {
$sub_sitemap = ai_ready_fetch_sitemap($sitemap_url);
if ($sub_sitemap) {
$sub_pages = ai_ready_parse_sitemap_urlset($sub_sitemap);
$pages = array_merge($pages, $sub_pages);
}
}
}
}
return $pages;
}
/**
* Parse sitemap urlset (actual page URLs)
*
* @param string $xml_content Sitemap XML with urlset
* @return array Array of page data
*/
function ai_ready_parse_sitemap_urlset($xml_content) {
$pages = [];
preg_match_all('/(.*?)<\/loc>/', $xml_content, $matches);
if (!empty($matches[1])) {
foreach ($matches[1] as $url) {
if (ai_ready_is_relevant_page_url($url)) {
$page_data = ai_ready_get_page_info_from_url($url);
if ($page_data) {
$pages[] = $page_data;
}
}
}
}
return $pages;
}
/**
* Check if a URL represents a relevant page for llms.txt
*
* @param string $url Page URL
* @return bool True if relevant
*/
function ai_ready_is_relevant_page_url($url) {
$home_url = trailingslashit(home_url());
if (strpos($url, $home_url) !== 0) {
return false;
}
$skip_patterns = [
'/wp-admin/',
'/wp-content/',
'/feed/',
'/comment/',
'/trackback/',
'/xmlrpc.php',
'?',
'#',
'/category/',
'/tag/',
'/author/',
'/date/',
'/search/',
'/attachment/'
];
foreach ($skip_patterns as $pattern) {
if (strpos($url, $pattern) !== false) {
return false;
}
}
return true;
}
/**
* Get page information from URL
*
* @param string $url Page URL
* @return array|false Page data or false if not found
*/
function ai_ready_get_page_info_from_url($url) {
$post_id = url_to_postid($url);
if ($post_id) {
$post = get_post($post_id);
if ($post && $post->post_status === 'publish') {
return [
'id' => $post->ID,
'title' => $post->post_title,
'url' => $url,
'type' => $post->post_type,
'slug' => $post->post_name,
'content_length' => strlen(wp_strip_all_tags($post->post_content))
];
}
}
$path = wp_parse_url($url, PHP_URL_PATH);
$slug = trim($path, '/');
return [
'id' => 0,
'title' => ucwords(str_replace(['-', '_'], ' ', basename($slug))),
'url' => $url,
'type' => 'unknown',
'slug' => $slug,
'content_length' => 0
];
}
/**
* Force refresh of llms.txt sections by clearing cached options
*/
function ai_ready_force_refresh_sections() {
delete_option('ai_ready_llms_sections');
}
/**
* Categorize pages by type/purpose for llms.txt sections
*
* @param array $pages Array of page data from sitemap
* @return array Categorized pages
*/
function ai_ready_categorize_sitemap_pages($pages) {
$categories = [
'docs' => [],
'content' => [],
'resources' => [],
'optional' => []
];
foreach ($pages as $page) {
$slug = strtolower($page['slug']);
$title = strtolower($page['title']);
$category = ai_ready_determine_page_category($slug, $title, $page);
if (!$category) {
$category = 'content';
}
if ($category && isset($categories[$category])) {
$categories[$category][] = $page;
} else {
$categories['content'][] = $page;
}
}
return $categories;
}
/**
* Determine which category a page belongs to
*
* @param string $slug Page slug
* @param string $title Page title (lowercase)
* @param array $page_data Full page data
* @return string Category name (always returns a category)
*/
function ai_ready_determine_page_category($slug, $title, $page_data) {
$docs_keywords = [
'about', 'mission', 'vision', 'who-we-are', 'our-story', 'company',
'services', 'products', 'offerings', 'solutions', 'what-we-do',
'contact', 'contact-us', 'get-in-touch', 'reach-us',
'faq', 'help', 'support', 'frequently-asked-questions'
];
foreach ($docs_keywords as $keyword) {
if (strpos($slug, $keyword) !== false || strpos($title, $keyword) !== false) {
return 'docs';
}
}
$resource_keywords = [
'resources', 'downloads', 'documentation', 'docs', 'guides',
'tutorials', 'library', 'portfolio', 'work', 'projects',
'case-studies', 'examples', 'showcase'
];
foreach ($resource_keywords as $keyword) {
if (strpos($slug, $keyword) !== false || strpos($title, $keyword) !== false) {
return 'resources';
}
}
if ($page_data['type'] === 'post' || $page_data['content_length'] > 1000) {
return 'content';
}
if ($page_data['type'] === 'page') {
return 'docs';
}
return 'optional';
}
/**
* Check if llms.txt is enabled
*
* @return bool True if llms.txt is enabled, false otherwise
*/
function ai_ready_is_llms_enabled() {
if (get_option('ai_ready_llms_disabled', false)) {
return false;
}
$has_title = !empty(get_option('ai_ready_site_title', ''));
$has_summary = !empty(get_option('ai_ready_site_summary', ''));
$has_sections = !empty(get_option('ai_ready_llms_sections', []));
$has_custom = !empty(get_option('ai_ready_custom_content', ''));
return true;
}