0, 'bots' => []] : ['on' => 0, 'bots' => []]; } function aiotsa_robots_text($bots) { $out = "# AI crawlers - rules made with AllInOneTools Site Audit\n"; $known = aiotsa_ai_bots(); foreach ($bots as $b => $v) { if (!isset($known[$b]) || !in_array($v, ['allow', 'block'], true)) continue; $out .= 'User-agent: ' . $b . "\n" . ($v === 'block' ? "Disallow: /\n" : "Allow: /\n") . "\n"; } return rtrim($out) . "\n"; } function aiotsa_robots_presets() { $all = array_keys(aiotsa_ai_bots()); $search = ['OAI-SearchBot', 'ChatGPT-User', 'Claude-SearchBot', 'PerplexityBot']; return ['allow' => array_fill_keys($all, 'allow'), 'block' => array_fill_keys($all, 'block'), 'search' => array_merge(array_fill_keys($all, 'block'), array_fill_keys($search, 'allow'))]; } add_filter('robots_txt', function ($out) { $o = aiotsa_robots_opts(); if (!empty($o['on']) && !empty($o['bots'])) $out = rtrim($out) . "\n\n" . aiotsa_robots_text($o['bots']); return $out; }, 50); function aiotsa_robots_physical() { return file_exists(ABSPATH . 'robots.txt'); } /* ---------- llms.txt ---------- */ function aiotsa_llms_opts() { $o = get_option('aiotsa_llms', []); return is_array($o) ? $o + ['on' => 0, 'text' => ''] : ['on' => 0, 'text' => '']; } function aiotsa_llms_generate($max = 40) { $out = '# ' . wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES) . "\n\n"; $d = trim(wp_specialchars_decode(get_bloginfo('description'), ENT_QUOTES)); if ($d !== '') $out .= '> ' . $d . "\n\n"; $line = function ($p) { $F = aiotsa_seo_fields(); $ds = trim((string) get_post_meta($p->ID, $F['desc'], true)); if ($ds === '' || strpos($ds, '%') !== false) $ds = trim(wp_trim_words(wp_strip_all_tags(strip_shortcodes($p->post_excerpt ?: $p->post_content)), 22, '')); $ds = preg_replace('/\s+/u', ' ', $ds); return '- [' . str_replace(['[', ']'], '', get_the_title($p)) . '](' . get_permalink($p) . ')' . ($ds !== '' ? ': ' . $ds : '') . "\n"; }; $pages = get_posts(['post_type' => 'page', 'post_status' => 'publish', 'numberposts' => 12, 'orderby' => 'menu_order', 'order' => 'ASC', 'has_password' => false]); if ($pages) { $out .= "## Pages\n\n"; foreach ($pages as $p) $out .= $line($p); $out .= "\n"; } $cats = get_categories(['orderby' => 'count', 'order' => 'DESC', 'number' => 6, 'hide_empty' => true]); $used = []; $left = $max; foreach ($cats as $c) { $ps = array_slice(array_values(array_filter(get_posts(['post_type' => 'post', 'post_status' => 'publish', 'numberposts' => 30, 'category' => $c->term_id, 'has_password' => false]), function ($p) use ($used) { return !in_array($p->ID, $used, true); })), 0, 8); if (!$ps || $left <= 0) continue; $out .= '## ' . wp_specialchars_decode($c->name, ENT_QUOTES) . "\n\n"; foreach ($ps as $p) { if ($left-- <= 0) break; $out .= $line($p); $used[] = $p->ID; } $out .= "\n"; } return rtrim($out) . "\n"; } add_action('init', function () { $o = aiotsa_llms_opts(); if (empty($o['on']) || trim($o['text']) === '') return; $path = (string) wp_parse_url(isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '', PHP_URL_PATH); $base = rtrim((string) wp_parse_url(home_url('/'), PHP_URL_PATH), '/'); if ($path !== $base . '/llms.txt') return; status_header(200); header('Content-Type: text/plain; charset=utf-8'); header('X-Robots-Tag: noindex'); echo wp_strip_all_tags($o['text']); exit; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- this is a text/plain file (llms.txt), not HTML: HTML escaping would change characters such as & in the text. Tags are stripped here and when the text is saved. }, 1); /* ---------- internal link ideas (from the latest audit of this site) ---------- */ function aiotsa_link_ideas($limit = 40) { $last = get_option('aiotsa_last'); $aid = is_array($last) ? (int) $last['id'] : 0; if (!$aid) return []; $items = array_values(array_filter(aiotsa_items($aid, 'post'), function ($i) { return empty($i['data']['err']) && ($i['data']['type'] ?? '') !== 'policy' && $i['ref']; })); $stop = array_flip(explode(' ', 'the a an and or of to in for on with how what why best top guide your you is are vs from at by tips ways easy new this that')); $sig = []; $in = []; foreach ($items as $k => $it) { $sig[$k] = array_values(array_unique(array_filter(preg_split('/[^\p{L}\p{N}]+/u', mb_strtolower($it['data']['pt'] ?? $it['data']['t'])), function ($x) use ($stop) { return mb_strlen($x) > 3 && !isset($stop[$x]); }))); foreach ($it['data']['il'] ?? [] as $l) $in[untrailingslashit(strtolower($l))] = ($in[untrailingslashit(strtolower($l))] ?? 0) + 1; } $need = $items; usort($need, function ($a, $b) use ($in) { return ($in[untrailingslashit(strtolower($a['url']))] ?? 0) <=> ($in[untrailingslashit(strtolower($b['url']))] ?? 0); }); $out = []; foreach ($items as $k => $target) { $tu = untrailingslashit(strtolower($target['url'])); $got = $in[$tu] ?? 0; if ($got > 2) continue; $best = []; foreach ($items as $j => $src) { if ($j === $k) continue; $links = array_map(function ($l) { return untrailingslashit(strtolower($l)); }, $src['data']['il'] ?? []); if (in_array($tu, $links, true)) continue; $body = mb_strtolower(($src['data']['x'] ?? '') . ' ' . ($src['data']['pt'] ?? '')); $hit = 0; foreach ($sig[$k] as $w) if (mb_strpos($body, $w) !== false) $hit++; $score = count($sig[$k]) ? $hit / count($sig[$k]) : 0; if ($hit >= 2 && $score >= 0.5) $best[] = [$score, $j]; } rsort($best); foreach (array_slice($best, 0, 2) as [$s, $j]) $out[] = ['to' => $target['data']['pt'] ?? $target['data']['t'], 'to_url' => $target['url'], 'from' => $items[$j]['data']['pt'] ?? $items[$j]['data']['t'], 'from_url' => $items[$j]['url'], 'edit' => get_edit_post_link($items[$j]['ref'], 'raw'), 'inbound' => $got]; if (count($out) >= $limit) break; } return $out; } /** Which SEO plugin stores titles and descriptions on this site. */ function aiotsa_seo_fields() { $act = (array) get_option('active_plugins', []); $has = function ($f) use ($act) { foreach ($act as $a) if (strpos($a, $f) !== false) return true; return false; }; if ($has('rank-math')) return ['name' => 'Rank Math', 'desc' => 'rank_math_description', 'title' => 'rank_math_title']; if ($has('wordpress-seo')) return ['name' => 'Yoast SEO', 'desc' => '_yoast_wpseo_metadesc', 'title' => '_yoast_wpseo_title']; if ($has('wp-seopress')) return ['name' => 'SEOPress', 'desc' => '_seopress_titles_desc', 'title' => '_seopress_titles_title']; if ($has('autodescription')) return ['name' => 'The SEO Framework', 'desc' => '_genesis_description', 'title' => '_genesis_title']; return ['name' => '', 'desc' => '_aiotsa_desc', 'title' => '_aiotsa_title']; }