{$desc}\n\n"; } $out .= "## Pages\n\n"; $q = new WP_Query( array( 'post_type' => array( 'post', 'page' ), 'post_status' => 'publish', 'posts_per_page' => 100, 'no_found_rows' => true, 'orderby' => 'modified', 'order' => 'DESC', ) ); if ( $q->have_posts() ) { foreach ( $q->posts as $post ) { if ( 'noindex' === get_post_meta( $post->ID, '_yoast_wpseo_meta-robots-noindex', true ) ) { continue; } $title = get_the_title( $post ); $url = get_permalink( $post ); $excerpt = wp_strip_all_tags( get_the_excerpt( $post ) ); $excerpt = $excerpt ? ': ' . wp_trim_words( $excerpt, 20, '' ) : ''; $out .= "- [{$title}]({$url}){$excerpt}\n"; } } else { // Demo fallback so the file is never empty on a fresh install. foreach ( AICitare_Data::seed_pages() as $p ) { $out .= "- [{$p['title']}]( " . home_url( $p['url'] ) . " )\n"; } } wp_reset_postdata(); return $out; } /** Add allow/block directives for each AI crawler to robots.txt. */ public function robots_rules( $output, $public ) { $bots = AICitare_Data::get_bots(); $output .= "\n# --- AICitare: AI crawler management ---\n"; $output .= 'Sitemap: ' . home_url( '/llms.txt' ) . "\n"; foreach ( $bots as $bot => $allowed ) { $output .= "\nUser-agent: {$bot}\n"; $output .= $allowed ? "Allow: /\n" : "Disallow: /\n"; } return $output; } /** Detect & log an AI crawler hit on a front-end request. */ public function log_bot_hit() { if ( is_admin() ) { return; } $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; if ( ! $ua ) { return; } $match = ''; foreach ( AICitare_Data::bot_meta() as $b ) { if ( false !== stripos( $ua, $b['id'] ) ) { $match = $b['id']; break; } } if ( ! $match ) { return; } global $wpdb; $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->prefix . 'aicitare_bot_hits', array( 'bot_name' => $match, 'url' => esc_url_raw( ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) ), 'user_agent' => $ua, 'ip' => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '', 'hit_at' => current_time( 'mysql' ), ), array( '%s', '%s', '%s', '%s', '%s' ) ); } /** Recent bot hits for the dashboard; falls back to demo data if empty. */ public static function recent_hits( $limit = 12 ) { global $wpdb; $table = $wpdb->prefix . 'aicitare_bot_hits'; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT bot_name, url, hit_at FROM {$table} ORDER BY hit_at DESC LIMIT %d", $limit ), ARRAY_A ); // phpcs:ignore if ( empty( $rows ) ) { return AICitare_Data::bot_log(); } $bots = AICitare_Data::get_bots(); $out = array(); foreach ( $rows as $r ) { $out[] = array( 'bot' => $r['bot_name'], 'page' => $r['url'], 'time' => human_time_diff( strtotime( $r['hit_at'] ), time() ) . ' ago', 'ok' => ! empty( $bots[ $r['bot_name'] ] ), ); } return $out; } }