settings = get_option('fulgid_aicogu_settings', array());
}
/**
* Generate the llms.txt file with the provided content.
*
* @param string $content The content to write to the file.
* @return bool True on success, false on failure.
*/
public function generate_file($content) {
// Store the content in the database regardless of file method
update_option('fulgid_aicogu_file_content', $content);
// Get file method from settings
$file_method = isset($this->settings['file_method']) ? $this->settings['file_method'] : 'rewrite';
if ('direct' === $file_method) {
return $this->write_file_directly($content);
}
// For rewrite method, just store content in database
// The rewrite rules will serve it dynamically
// Flush rewrite rules to ensure our rule is registered
flush_rewrite_rules();
return true;
}
/**
* Write the llms.txt file directly to the site root.
*
* @param string $content The content to write.
* @return bool True on success, false on failure.
*/
private function write_file_directly($content) {
// Check if we can get the ABSPATH directory
$root_path = ABSPATH;
$file_path = $root_path . 'llms.txt';
// Try to write the file
$result = @file_put_contents($file_path, $content);
if (false === $result) {
// Log error or set admin notice
add_action('admin_notices', array($this, 'file_write_error_notice'));
return false;
}
return true;
}
/**
* Show admin notice for file write errors.
*/
public function file_write_error_notice() {
?>
analyze_site_content();
// Store for future use
update_option('fulgid_aicogu_file_content', $content);
}
// Do not escape here - escaping should be done at the output point
return $content;
}
/**
* Check if the llms.txt file exists and is accessible via URL.
*
* @return array Status information.
*/
public function check_file_status() {
$result = array(
'exists' => false,
'accessible' => false,
'method' => isset($this->settings['file_method']) ? $this->settings['file_method'] : 'rewrite',
'url' => site_url('/llms.txt'),
);
// For direct method, check if file exists in the file system
if ('direct' === $result['method']) {
$root_path = ABSPATH;
$file_path = $root_path . 'llms.txt';
$result['exists'] = file_exists($file_path);
} else {
// For rewrite method, check if content exists in the database
$content = get_option('fulgid_aicogu_file_content', '');
$result['exists'] = !empty($content);
}
// Check if file is accessible via URL
$response = wp_remote_get($result['url']);
$result['accessible'] = !is_wp_error($response) && 200 === wp_remote_retrieve_response_code($response);
return $result;
}
/**
* Get last modification time of the file.
*
* @return int|bool Timestamp or false if file doesn't exist.
*/
public function get_last_modified() {
$file_method = isset($this->settings['file_method']) ? $this->settings['file_method'] : 'rewrite';
if ('direct' === $file_method) {
$root_path = ABSPATH;
$file_path = $root_path . 'llms.txt';
if (file_exists($file_path)) {
return filemtime($file_path);
}
}
// For rewrite method or if direct file doesn't exist, use option update time
return get_option('fulgid_aicogu_file_updated', time());
}
/**
* Add a reference to llms.txt in robots.txt.
*
* @param string $robots_content Current robots.txt content.
* @return string Modified robots.txt content.
*/
public function add_robots_txt_reference($robots_content) {
if (empty($this->settings['add_to_robots']) || 'yes' !== $this->settings['add_to_robots']) {
return $robots_content;
}
$llms_line = "\n# For LLM training, we have implemented an llms.txt file following https://llmstxt.org/ guidelines.";
$llms_line .= "\n# This file helps AI systems better understand our website content.";
$llms_line .= "\nSitemap: " . site_url('/llms.txt');
// Check if reference already exists
if (strpos($robots_content, 'llms.txt') !== false) {
return $robots_content;
}
return $robots_content . $llms_line;
}
/**
* Generate a simple llms-full.txt file with more comprehensive content.
*
* @param string $content The basic llms.txt content.
* @return string The expanded content for llms-full.txt.
*/
public function generate_full_file($content) {
// Check if full file generation is enabled
if (empty($this->settings['generate_full_file']) || 'yes' !== $this->settings['generate_full_file']) {
return $content;
}
// Get content analyzer to generate more comprehensive content
$content_analyzer = new FULGID_AICOGU_Content_Analyzer();
$full_content = $content . "\n\n## Full Content\n\n";
// Add more detailed content for each post type
$post_types = isset($this->settings['post_types']) ? $this->settings['post_types'] : array('post', 'page');
foreach ($post_types as $post_type) {
$post_type_obj = get_post_type_object($post_type);
if (!$post_type_obj) {
continue;
}
$full_content .= "### " . $post_type_obj->labels->name . " Full Content\n\n";
// Get a limited number of posts
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => 10, // Limit to avoid huge files
'orderby' => 'date',
'order' => 'DESC',
);
$posts = get_posts($args);
foreach ($posts as $post) {
$title = $post->post_title;
$url = get_permalink($post->ID);
$content = wp_strip_all_tags($post->post_content);
// Limit content length
$max_length = 1000; // Reasonable limit for full content
if (strlen($content) > $max_length) {
$content = substr($content, 0, $max_length) . '...';
}
$full_content .= "#### {$title}\n\n";
$full_content .= "URL: {$url}\n\n";
$full_content .= "{$content}\n\n";
}
}
// Store the full content
update_option('fulgid_aicogu_full_file_content', $full_content);
return $full_content;
}
/**
* Get the full file content from the database.
*
* @return string The full file content.
*/
public function get_full_file_content() {
$content = get_option('fulgid_aicogu_full_file_content', '');
if (empty($content)) {
// If no content is stored, generate it now
$basic_content = $this->get_file_content();
$content = $this->generate_full_file($basic_content);
}
return $content;
}
}