settings = get_option('fulgid_aicogu_settings', array()); $this->seo_integrator = new FULGID_AICOGU_SEO_Integrator(); } /** * Analyze site content and return structured data for llms.txt. * * @return string The formatted content for llms.txt. */ public function analyze_site_content() { // Initialize content with site title and description $content = $this->get_site_header(); // Get main content sections $content .= $this->get_main_content(); // Get taxonomies if enabled if (!empty($this->settings['include_taxonomies'])) { $content .= $this->get_taxonomies(); } // Get custom content if set if (!empty($this->settings['custom_content'])) { $content .= "\n\n" . $this->settings['custom_content']; } return $content; } /** * Get site header with title and description. * * @return string */ private function get_site_header() { $site_name = get_bloginfo('name'); $site_description = get_bloginfo('description'); $content = "# {$site_name}\n\n"; $content .= "> {$site_description}\n\n"; // Add site introduction if set if (!empty($this->settings['site_introduction'])) { $content .= $this->settings['site_introduction'] . "\n\n"; } return $content; } /** * Get main content for llms.txt file. * * @return string */ private function get_main_content() { $content = ""; $post_types = $this->get_included_post_types(); foreach ($post_types as $post_type) { $post_type_obj = get_post_type_object($post_type); if (!$post_type_obj) { continue; } // Add section heading for post type $content .= "## " . $post_type_obj->labels->name . "\n\n"; // Get posts for this post type $posts = $this->get_posts_for_post_type($post_type); if (empty($posts)) { $content .= "No content available in this section.\n\n"; continue; } foreach ($posts as $post) { // Skip if this post is excluded if ($this->is_post_excluded($post->ID)) { continue; } // Get post title, URL, and description $title = $post->post_title; $url = get_permalink($post->ID); $description = $this->get_post_description($post); // Add to content $content .= "- [{$title}]({$url}): {$description}\n"; } $content .= "\n"; } return $content; } /** * Get taxonomies content. * * @return string */ private function get_taxonomies() { $content = ""; $taxonomies = !empty($this->settings['taxonomies']) ? $this->settings['taxonomies'] : array('category', 'post_tag'); foreach ($taxonomies as $taxonomy) { $tax_obj = get_taxonomy($taxonomy); if (!$tax_obj) { continue; } // Add section heading for taxonomy $content .= "## " . $tax_obj->labels->name . "\n\n"; // Get terms for this taxonomy $terms = get_terms(array( 'taxonomy' => $taxonomy, 'hide_empty' => true, )); if (empty($terms) || is_wp_error($terms)) { $content .= "No terms available in this taxonomy.\n\n"; continue; } foreach ($terms as $term) { $name = $term->name; $url = get_term_link($term); $description = !empty($term->description) ? $term->description : "Posts categorized as {$name}"; // Add to content $content .= "- [{$name}]({$url}): {$description}\n"; } $content .= "\n"; } return $content; } /** * Get included post types from settings. * * @return array */ private function get_included_post_types() { $default_post_types = array('page', 'post'); return !empty($this->settings['post_types']) ? $this->settings['post_types'] : $default_post_types; } /** * Get posts for a specific post type. * * @param string $post_type The post type. * @return array */ private function get_posts_for_post_type($post_type) { $max_posts = !empty($this->settings['max_posts_per_type']) ? intval($this->settings['max_posts_per_type']) : 20; $args = array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => $max_posts, 'orderby' => 'date', 'order' => 'DESC', ); // Apply SEO filtering if integrated $args = $this->seo_integrator->filter_query_args($args); return get_posts($args); } /** * Check if a post is excluded from llms.txt. * * @param int $post_id The post ID. * @return bool */ private function is_post_excluded($post_id) { // Check if post is in excluded list $excluded_posts = !empty($this->settings['excluded_posts']) ? $this->settings['excluded_posts'] : array(); if (in_array($post_id, $excluded_posts)) { return true; } // Check if post has noindex from SEO plugins if ($this->seo_integrator->is_post_noindex($post_id)) { return true; } return false; } /** * Get description for a post. * * @param WP_Post $post The post object. * @return string */ private function get_post_description($post) { // Check for SEO description first $seo_description = $this->seo_integrator->get_meta_description($post->ID); if (!empty($seo_description)) { // Use wp_strip_all_tags to remove any HTML $description = wp_strip_all_tags($seo_description); } // Fall back to excerpt if available else if (!empty($post->post_excerpt)) { $description = wp_strip_all_tags($post->post_excerpt); } // Generate description from content else { $content = wp_strip_all_tags($post->post_content); // Clean up any extra whitespace or line breaks $content = preg_replace('/\s+/', ' ', $content); $content = trim($content); $max_length = !empty($this->settings['description_length']) ? intval($this->settings['description_length']) : 150; if (strlen($content) > $max_length) { $description = substr($content, 0, $max_length) . '...'; } else { $description = $content; } } // Ensure the description is properly sanitized before returning return sanitize_text_field($description); } }