'; /** * Generate the full llms.txt content. * * @return string */ public static function generate() { $settings = AILM_LlmsTxt_Settings::get_all(); $lines = array(); $lines[] = '# ' . self::site_name(); $lines[] = ''; // The blockquote description is optional: an empty tagline must not // produce a dangling "> " line (seen live on test1.aicoloringpages.pro). $description = self::site_description( $settings ); if ( '' !== $description ) { $lines[] = '> ' . $description; $lines[] = ''; } // Manual sections (sanitized markdown, appended after the intro). $manual = isset( $settings['manual_sections'] ) ? trim( (string) $settings['manual_sections'] ) : ''; if ( '' !== $manual ) { $lines[] = $manual; $lines[] = ''; } // Pinned pages: a short, admin-curated ordered list rendered right // after the intro so LLMs read the "identity" pages (About, Contacts, // Services…) first. Applies to both curation modes. $pinned = self::render_pinned_entries( $settings ); if ( ! empty( $pinned['lines'] ) ) { $lines = array_merge( $lines, $pinned['lines'] ); } // Manual curation: explicit ordered list chosen by the admin. if ( 'manual' === ( $settings['curation_mode'] ?? 'auto' ) ) { // Pinned entries are already listed above — never repeat them. $lines = array_merge( $lines, self::render_manual_entries( $settings, $pinned['ids'] ) ); return implode( "\n", $lines ) . "\n" . self::FOOTER . "\n"; } // Auto mode: sections per post type. $post_types = is_array( $settings['post_types'] ) ? $settings['post_types'] : array(); $exclude = is_array( $settings['exclude_ids'] ) ? array_map( 'absint', $settings['exclude_ids'] ) : array(); // Pinned entries are already listed above — never repeat them below. $exclude = array_values( array_unique( array_merge( $exclude, $pinned['ids'] ) ) ); $max_posts = max( 1, (int) $settings['max_posts'] ); $total = 0; foreach ( $post_types as $post_type ) { if ( $total >= $max_posts ) { break; } $posts = self::query_posts( $post_type, $exclude, $max_posts - $total ); if ( empty( $posts ) ) { continue; } $lines[] = '## ' . self::post_type_label( $post_type ); $lines[] = ''; foreach ( $posts as $post ) { $lines[] = self::render_entry( $post, $settings ); $total++; } $lines[] = ''; } return implode( "\n", $lines ) . "\n" . self::FOOTER . "\n"; } /** * Ordered post IDs selected for the pinned section. * * @param array $settings Module settings. * @return int[] Positive, unique IDs capped at the settings hard cap. */ private static function pinned_ids( array $settings ) { if ( empty( $settings['pinned_pages'] ) || ! is_array( $settings['pinned_pages'] ) ) { return array(); } $ids = array(); foreach ( $settings['pinned_pages'] as $pid ) { // Strict int cast: negative values are dropped, not abs'd. $pid = (int) $pid; if ( $pid > 0 && ! in_array( $pid, $ids, true ) ) { $ids[] = $pid; } if ( count( $ids ) >= AILM_LlmsTxt_Settings::PINNED_MAX ) { break; } } return $ids; } /** * Whether a post must be omitted from the generated file. * * Shared by every renderer (pinned, manual and automatic) so all * sections apply exactly the same exclusion rules. * * @param WP_Post $post Post object. * @return bool */ private static function should_skip( $post ) { if ( self::is_noindex( $post ) ) { return true; } /** * Allow third-party integrations to skip a post from llms.txt. * * @param bool $skip Whether to skip the post. * @param WP_Post $post Post object. */ return (bool) apply_filters( 'ailm_llmstxt_skip_post', false, $post ); } /** * Render the pinned pages section, in the admin-chosen order. * * Entries reuse the standard "- [title](url): description" format so * pinned pages look exactly like every other entry. The heading is only * emitted when at least one pinned page actually renders. * * @param array $settings Module settings. * @return array{lines:string[],ids:int[]} Section lines and the IDs that * were effectively listed. */ private static function render_pinned_entries( array $settings ) { $empty = array( 'lines' => array(), 'ids' => array(), ); $ids = self::pinned_ids( $settings ); if ( empty( $ids ) ) { return $empty; } $title = isset( $settings['pinned_title'] ) ? trim( (string) $settings['pinned_title'] ) : ''; if ( '' === $title ) { $title = __( 'Key Pages', 'ailinkmaster-internal-auto-manager' ); } $title = self::plain_line( $title ); $lines = array(); $listed = array(); foreach ( $ids as $post_id ) { $post = get_post( $post_id ); if ( ! $post || 'publish' !== $post->post_status ) { continue; // Skip deleted/unpublished picks silently. } if ( self::should_skip( $post ) ) { continue; } if ( empty( $listed ) ) { $lines[] = '## ' . $title; $lines[] = ''; } $lines[] = self::render_entry( $post, $settings ); $listed[] = $post_id; } if ( empty( $listed ) ) { return $empty; } $lines[] = ''; return array( 'lines' => $lines, 'ids' => $listed, ); } /** * Render the manual-curation entries (ordered, one flat section). * * @param array $settings Module settings. * @param int[] $exclude Post IDs already rendered elsewhere (pinned). * @return array Lines of markdown. */ private static function render_manual_entries( array $settings, array $exclude = array() ) { $selected = is_array( $settings['selected_posts'] ?? null ) ? array_map( 'absint', $settings['selected_posts'] ) : array(); if ( empty( $selected ) ) { return array(); } $lines = array(); $lines[] = '## ' . __( 'Selected Pages', 'ailinkmaster-internal-auto-manager' ); $lines[] = ''; foreach ( $selected as $post_id ) { if ( in_array( (int) $post_id, $exclude, true ) ) { continue; } $post = get_post( $post_id ); if ( ! $post || 'publish' !== $post->post_status ) { continue; // Skip deleted/unpublished picks silently. } if ( self::should_skip( $post ) ) { continue; } $lines[] = self::render_entry( $post, $settings ); } $lines[] = ''; return $lines; } /** * Render a single "- [title](url): description" entry. * * @param WP_Post $post Post. * @param array $settings Module settings. * @return string */ private static function render_entry( $post, array $settings ) { $url = self::canonical_url( $post ); $title = self::post_title( $post ); $description = self::post_description( $post, $settings ); $entry = '- [' . $title . '](' . $url . ')'; if ( '' !== $description ) { $entry .= ': ' . $description; } return $entry; } /** * Query published posts of one type, prioritized by AILM Rank. * * @param string $post_type Post type slug. * @param array $exclude Excluded post IDs. * @param int $limit Max posts to fetch. * @return WP_Post[] Array of post objects. */ private static function query_posts( $post_type, array $exclude, $limit ) { $args = array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => $limit, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'orderby' => 'date', 'order' => 'DESC', 'fields' => 'all', 'suppress_filters' => false, ); if ( ! empty( $exclude ) ) { $args['post__not_in'] = $exclude; } $query = new WP_Query( $args ); $posts = $query->posts; wp_reset_postdata(); if ( empty( $posts ) ) { return array(); } // Filter noindex / skipped posts, then sort by AILM Rank (desc). $filtered = array(); foreach ( $posts as $post ) { if ( self::should_skip( $post ) ) { continue; } $filtered[] = $post; } usort( $filtered, array( __CLASS__, 'compare_by_rank' ) ); return $filtered; } /** * Sort comparator: higher AILM Rank first, ties by date (newer first). * * @param WP_Post $a First post. * @param WP_Post $b Second post. * @return int */ /** * Read the local AILM Rank score without a hard dependency on the * pagerank module class (read-only postmeta access). * * @param int $post_id Post ID. * @return float */ private static function get_post_pr( $post_id ) { if ( class_exists( 'AILM_PageRank_Engine' ) ) { return AILM_PageRank_Engine::get_post_pr( $post_id ); } $pr = get_post_meta( $post_id, '_ailm_pagerank', true ); return '' !== $pr ? floatval( $pr ) : 0.0; } private static function compare_by_rank( $a, $b ) { $pr_a = self::get_post_pr( $a->ID ); $pr_b = self::get_post_pr( $b->ID ); if ( abs( $pr_a - $pr_b ) > 0.000001 ) { return ( $pr_a > $pr_b ) ? -1 : 1; } return strcmp( $b->post_date, $a->post_date ); } // ------------------------------------------------------------------------- // Field builders. // ------------------------------------------------------------------------- /** * Site name for the H1. * * @return string */ private static function site_name() { /** This filter is documented in wp-includes/general-template.php */ return self::plain_line( get_bloginfo( 'name' ) ); } /** * Decode HTML entities so the plain-text output shows real characters * instead of entity references ("&" → "&", "–" → "–"). * * get_bloginfo(), get_the_title() and post type labels can carry * entities (WordPress stores several values entity-encoded and SEO / * localization filters re-encode them). llms.txt is plain text, not * HTML, so any entity left in place is emitted literally to AI crawlers * (reported: "Your Trusted Source for Crypto News & Analysis" in the * "> " tagline line). * * @param string $text Raw text. * @return string Decoded text. */ public static function decode_entities( $text ) { return (string) html_entity_decode( (string) $text, ENT_QUOTES, 'UTF-8' ); } /** * Single-line plain text: entities decoded and line breaks flattened so * the value is safe inside a one-line Markdown construct (H1, blockquote, * list entry, heading). * * @param string $text Raw text. * @return string Trimmed single-line text. */ public static function plain_line( $text ) { $text = self::decode_entities( $text ); $text = preg_replace( '/[\r\n]+/', ' ', $text ); return trim( (string) $text ); } /** * Site description for the blockquote line. * * Falls back to the site tagline. Both the custom value and the tagline * may carry HTML entities ("&") — decode them so the "> " line shows * real characters (the reported "Crypto News & Analysis" bug). * * @param array $settings Module settings. * @return string */ private static function site_description( array $settings ) { $custom = isset( $settings['site_description'] ) ? trim( (string) $settings['site_description'] ) : ''; if ( '' !== $custom ) { return self::plain_line( $custom ); } return self::plain_line( get_bloginfo( 'description' ) ); } /** * Human-readable label for a post type section. * * @param string $post_type Post type slug. * @return string */ private static function post_type_label( $post_type ) { $object = get_post_type_object( $post_type ); if ( $object && ! empty( $object->labels->name ) ) { return self::plain_line( $object->labels->name ); } return ucfirst( $post_type ); } /** * Post title, flattened to a single line, HTML entities decoded * (– etc.), markdown link syntax escaped. * * Shared helper: used by both this generator (markdown link text) and * AILM_LlmsTxt_FullGenerator (## headings) so the two files never * diverge in title handling. * * @param WP_Post $post Post. * @return string */ public static function post_title( $post ) { $title = wp_strip_all_tags( get_the_title( $post ) ); // get_the_title() returns text with HTML entities (– etc.) — // decode them so the markdown link shows real characters, not numeric // entity references. plain_line() also flattens newlines. $title = self::plain_line( $title ); // Escape markdown link syntax inside the title. $title = str_replace( array( '[', ']' ), array( '\\[', '\\]' ), (string) $title ); return trim( (string) $title ); } /** * Canonical URL for a post. * * Prefers wp_get_canonical_url() when available (respects SEO plugins * that filter get_canonical_url), falls back to get_permalink(). * * @param WP_Post $post Post. * @return string */ private static function canonical_url( $post ) { $url = ''; if ( function_exists( 'wp_get_canonical_url' ) ) { $url = wp_get_canonical_url( $post->ID ); } if ( empty( $url ) ) { $url = get_permalink( $post->ID ); } return (string) $url; } /** * Post description: AI summary (if fresh) or post excerpt. * * @param WP_Post $post Post. * @param array $settings Module settings. * @return string */ private static function post_description( $post, array $settings ) { $description = ''; if ( ! empty( $settings['use_summaries'] ) ) { $description = AILM_LlmsTxt_Data::get_summary( $post->ID, $post->post_content ); } if ( '' === $description ) { $description = wp_strip_all_tags( get_the_excerpt( $post ) ); } return self::plain_line( $description ); } /** * Whether a post is noindex for robots. * * Uses the wp_robots filter on a synthetic request so SEO plugins * (Yoast/RankMath/etc.) that hook wp_robots get a chance to mark it. * * @param WP_Post $post Post. * @return bool True if the post should be excluded. */ private static function is_noindex( $post ) { // NOTE: the global "search engines discouraged" setting (blog_public=0) // intentionally does NOT empty llms.txt. The module is explicitly // enabled by the admin, and blog_public=0 is common on staging/test // sites that still want an llms.txt. Per-post noindex signals below // remain authoritative for individual posts. // Password protected posts are never listed. if ( ! empty( $post->post_password ) ) { return true; } // Sticky attachments / non-viewable post types are filtered by query args. // SEO plugin noindex detection via the wp_robots filter. $robots = self::get_post_robots( $post ); if ( isset( $robots['noindex'] ) ) { return true; } return false; } /** * Compute the wp_robots directives for a post without a real request. * * @param WP_Post $post Post. * @return array Robots directives map. */ private static function get_post_robots( $post ) { // Set up a quasi-front-end context so wp_robots filters behave. $robots = array(); // Temporarily pretend we are on the post's front-end URL. $url = get_permalink( $post->ID ); // Many SEO plugins resolve the current post via query vars; emulate. $original_post_id = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null; $GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- scoped and restored below /** This filter is documented in wp-includes/robots-template.php */ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- applying the core filter is intentional (reads SEO/noindex directives) $robots = apply_filters( 'wp_robots', $robots ); if ( null !== $original_post_id ) { $GLOBALS['post'] = $original_post_id; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited } else { unset( $GLOBALS['post'] ); } return is_array( $robots ) ? $robots : array(); } }