'/privacy|datenschutz|privacidad|privacidade|confidentialit|privasi|गोपनीयता|ગોપનીયતા/iu', 'about' => '/about|ueber-uns|über-uns|acerca|sobre|qui-sommes|a-propos|tentang|हमारे-बारे|અમારા-વિશે|who-we-are/iu', 'contact' => '/contact|kontakt|contacto|contato|kontak|hubungi|संपर्क|સંપર્ક/iu', 'terms' => '/terms|conditions|\btos\b|agb|terminos|términos|termos|syarat|नियम|શરતો/iu', 'disclaimer' => '/disclaimer|haftungsausschluss|descargo|aviso-legal|penafian|अस्वीकरण|ડિસ્ક્લેમર/iu']; } /** Find the policy and trust pages: the user's own links first, then WordPress, then the home page links. */ function aiotsa_find_pages($A) { $out = []; $given = (array) ($A['opts']['pages'] ?? []); $own = $A['mode'] === 'own'; $kinds = aiotsa_page_kinds(); foreach ($kinds as $k => $re) { $u = aiotsa_norm_url($given[$k] ?? ''); if ($u !== '') $out[$k] = $u; } if ($own) { if (empty($out['privacy'])) { $pp = (int) get_option('wp_page_for_privacy_policy'); if ($pp && get_post_status($pp) === 'publish') $out['privacy'] = get_permalink($pp); } $pages = get_posts(['post_type' => 'page', 'post_status' => 'publish', 'numberposts' => 300, 'orderby' => 'menu_order', 'order' => 'ASC']); foreach ($kinds as $k => $re) { if (!empty($out[$k])) continue; foreach ($pages as $p) if (preg_match($re, $p->post_name . ' ' . $p->post_title)) { $out[$k] = get_permalink($p); break; } } } foreach ($kinds as $k => $re) { if (!empty($out[$k])) continue; foreach (($A['summary']['ctx']['home']['int'] ?? []) as $l) { $path = urldecode((string) wp_parse_url($l, PHP_URL_PATH)); if ($path !== '/' && preg_match($re, $path)) { $out[$k] = $l; break; } } } return $out; } function aiotsa_task_policy(&$A) { $R = &$A['results']; $C = &$A['summary']['ctx']; $found = aiotsa_find_pages($A); $C['pages'] = $found; $own = $A['mode'] === 'own'; $home = array_map(function ($u) { return untrailingslashit(strtolower($u)); }, $C['home']['int'] ?? []); $P = []; foreach ($found as $k => $u) { $r = aiotsa_fetch($u, ['timeout' => 10]); $f = $r['ok'] ? aiotsa_parse($r['body'], $r['url']) : null; $P[$k] = ['url' => $u, 'ok' => (bool) $f, 'words' => $f ? $f['words'] : 0, 'text' => $f ? $f['text'] : '', 'html' => $f ? $r['body'] : '', 'linked' => in_array(untrailingslashit(strtolower($u)), $home, true)]; } $names = ['privacy' => 'Privacy Policy', 'about' => 'About', 'contact' => 'Contact', 'terms' => 'Terms', 'disclaimer' => 'Disclaimer']; $fixnew = $own ? 'Create the page in Pages > Add New and link it in the footer menu.' : 'Create the page and link it in the footer.'; foreach (['privacy' => 'fail', 'about' => 'fail', 'contact' => 'fail', 'terms' => 'warn', 'disclaimer' => 'warn'] as $k => $sev) { $id = 'a_' . $k; $p = $P[$k] ?? null; if (!$p) $R[$id] = aiotsa_r($sev, 'Not found.', [], $fixnew); elseif (!$p['ok']) $R[$id] = aiotsa_r('fail', 'Found at %s, but the page does not load.', [$p['url']], 'Open the page and check it is published.', [], [[$names[$k], $p['url']]]); elseif ($p['words'] < 80 && $k !== 'contact') $R[$id] = aiotsa_r('warn', 'Found, but it has only %d words.', [$p['words']], 'Write a complete page.', [], [[$names[$k], $p['url']]]); elseif (!$p['linked']) $R[$id] = aiotsa_r('warn', 'Found, but the home page does not link to it.', [], 'Add it to the footer menu so visitors and reviewers can find it.', [], [[$names[$k], $p['url']]]); else $R[$id] = aiotsa_r('pass', 'Found (%d words) and linked from the home page.', [$p['words']], '', [], [[$names[$k], $p['url']]]); } if (!empty($P['privacy']['ok'])) { $t = strtolower($P['privacy']['text']); $ck = preg_match('/cookie|कुकी|કૂકી/u', $t); $ad = preg_match('/advertis|adsense|google|anuncio|anúncio|publicit|werbung|iklan|विज्ञापन|જાહેરાત/u', $t); $C['privacy_text'] = mb_substr($P['privacy']['text'], 0, 3000); $R['a_privacy_text'] = $ck && $ad ? aiotsa_r('pass', 'It mentions cookies and advertising.') : aiotsa_r('warn', $ck ? 'It mentions cookies, but not advertising or Google.' : 'It does not mention cookies or advertising.', [], 'Add a paragraph on Google advertising cookies and how visitors can opt out.'); } else $R['a_privacy_text'] = aiotsa_skip('No Privacy Policy page to read.'); if (!empty($P['about']['ok'])) { $w = $P['about']['words']; $R['r_about'] = $w >= 250 ? aiotsa_r('pass', '%d words.', [$w]) : aiotsa_r('warn', 'Only %d words.', [$w], 'Say who runs the site, their experience, and why readers can trust it.'); } else $R['r_about'] = aiotsa_r('fail', 'No About page.', [], 'Create one: who runs the site and why.'); if (!empty($P['contact']['ok'])) { $h = $P['contact']['html']; $ways = []; if (preg_match('/mailto:|[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i', $h)) $ways[] = 'e-mail'; if (stripos($h, '= 2 ? aiotsa_r('pass', '%d profiles linked.', [count($soc)]) : aiotsa_r('warn', '%d profile(s) linked from the home page.', [count($soc)], 'Link your active social profiles in the footer.'); $R['a_consent'] = !empty($hf['consent']) ? aiotsa_r('pass', 'Found: %s.', [$hf['consent']]) : aiotsa_r('warn', 'No cookie consent banner detected on the home page.', [], 'For visitors from Europe, Google requires a certified consent (CMP) banner. Install one, or use Google\'s own in AdSense > Privacy & messaging.'); $n = count($hf['int'] ?? []); $R['a_nav'] = $n >= 8 ? aiotsa_r('pass', 'The home page links to %d pages of the site.', [$n]) : aiotsa_r('warn', 'The home page links to only %d pages of the site.', [$n], 'Add a clear menu with your main categories and pages.'); $https = stripos($C['home_url'] ?? '', 'https://') === 0; $noi = !empty($C['noindex']); $R['a_open'] = $https && !$noi ? aiotsa_r('pass', 'HTTPS is on and search engines can index the site.') : aiotsa_r('fail', $noi ? 'Search engines are told not to index the site.' : 'The site does not use HTTPS.', [], 'Fix this first: see the Technical SEO section.'); foreach ($P as $k => &$p) unset($p['text'], $p['html']); $C['pinfo'] = $P; } function aiotsa_task_wp(&$A) { global $wpdb, $wp_version; $R = &$A['results']; $C = &$A['summary']['ctx']; if (!get_option('blog_public')) $R['t_noindex'] = aiotsa_r('fail', '"Discourage search engines" is switched on in WordPress.', [], 'Go to Settings > Reading and untick it.', [], [['Settings > Reading', admin_url('options-reading.php')]]); $pl = (string) get_option('permalink_structure'); $R['t_permalink'] = $pl === '' ? aiotsa_r('fail', 'Links look like ?p=123.', [], 'Go to Settings > Permalinks and choose "Post name".', [], [['Settings > Permalinks', admin_url('options-permalink.php')]]) : (strpos($pl, '%postname%') !== false ? aiotsa_r('pass', 'Uses the post name.') : aiotsa_r('warn', 'Does not include the post name: %s', [$pl], 'Prefer "Post name". Changing it on an old site needs redirects.')); $up = get_site_transient('update_core'); $new = ''; if (is_object($up) && !empty($up->updates)) foreach ($up->updates as $u) if (($u->response ?? '') === 'upgrade') { $new = $u->current; break; } $php_old = version_compare(PHP_VERSION, '8.1', '<'); $va = [$wp_version, PHP_VERSION, $new]; if ($new && $php_old) $R['c_versions'] = aiotsa_r('warn', 'WordPress %1$s, PHP %2$s. WordPress %3$s is available, and this PHP version no longer gets security fixes.', $va, 'Update WordPress in Dashboard > Updates, and switch to PHP 8.2 or newer in your hosting panel.'); elseif ($new) $R['c_versions'] = aiotsa_r('warn', 'WordPress %1$s, PHP %2$s. WordPress %3$s is available.', $va, 'Update WordPress in Dashboard > Updates.'); elseif ($php_old) $R['c_versions'] = aiotsa_r('warn', 'WordPress %1$s, PHP %2$s. This PHP version no longer gets security fixes.', $va, 'Switch to PHP 8.2 or newer in your hosting panel.'); else $R['c_versions'] = aiotsa_r('pass', 'WordPress %1$s, PHP %2$s. Both are current.', $va); $seo = []; foreach (['rank-math/rank-math.php' => 'Rank Math', 'seo-by-rank-math/rank-math.php' => 'Rank Math', 'wordpress-seo/wp-seo.php' => 'Yoast SEO', 'wordpress-seo-premium/wp-seo-premium.php' => 'Yoast SEO', 'all-in-one-seo-pack/all_in_one_seo_pack.php' => 'All in One SEO', 'wp-seopress/seopress.php' => 'SEOPress', 'autodescription/autodescription.php' => 'The SEO Framework', 'slim-seo/slim-seo.php' => 'Slim SEO'] as $f => $n) if (in_array($f, (array) get_option('active_plugins', []), true)) $seo[$n] = 1; $seo = array_keys($seo); $C['seo_plugins'] = $seo; $R['w_seo'] = count($seo) === 1 ? aiotsa_r('pass', '%s is active.', [$seo[0]]) : (count($seo) > 1 ? aiotsa_r('fail', 'Two SEO plugins are active: %s. They write the same tags twice.', [implode(', ', $seo)], 'Keep one and deactivate the other.') : aiotsa_r('warn', 'No SEO plugin is active.', [], 'Install one (Rank Math or Yoast SEO) to control titles, descriptions, the sitemap and schema.')); $cnt = wp_count_posts('post'); $pgs = wp_count_posts('page'); $pub = (int) ($cnt->publish ?? 0) + max(0, (int) ($pgs->publish ?? 0) - count(aiotsa_find_pages($A)) - 1); $C['posts_pub'] = $pub; $dr = (int) ($cnt->draft ?? 0) + (int) ($cnt->pending ?? 0); $R['a_posts'] = $pub >= 20 ? aiotsa_r('pass', '%d published posts.', [$pub]) : ($pub >= 10 ? aiotsa_r('warn', '%d published posts. 20 or more useful posts is a safer number.', [$pub], 'Publish more complete, original posts before you apply.') : aiotsa_r('fail', 'Only %d published posts.', [$pub], 'Publish at least 15-20 complete, original posts before you apply.')); $R['w_drafts'] = aiotsa_r('info', '%d drafts or pending posts.', [$dr]); $ph = get_posts(['post_type' => ['post', 'page'], 'post_status' => 'publish', 'numberposts' => 20, 's' => '', 'post_name__in' => ['hello-world', 'sample-page']]); $l = []; foreach ($ph as $p) $l[] = [$p->post_title, get_permalink($p), get_edit_post_link($p, 'raw')]; $lorem = $wpdb->get_results($wpdb->prepare("SELECT ID, post_title FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ('post','page') AND post_content LIKE %s LIMIT 10", '%lorem ipsum dolor%')); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ($lorem as $p) $l[] = [$p->post_title . ' (Lorem ipsum)', get_permalink($p->ID), get_edit_post_link($p->ID, 'raw')]; $R['a_placeholder'] = $l ? aiotsa_r('warn', '%d placeholder page(s) are still published.', [count($l)], 'Delete them or replace the text.', [], $l) : aiotsa_r('pass', 'None found.'); $unc = (int) get_option('default_category'); $q = new WP_Query(['post_type' => 'post', 'post_status' => 'publish', 'category__in' => [$unc], 'fields' => 'ids', 'posts_per_page' => 50, 'no_found_rows' => false]); $only = []; foreach ($q->posts as $pid) { if (count(wp_get_post_categories($pid)) === 1) $only[] = [get_the_title($pid), get_permalink($pid), get_edit_post_link($pid, 'raw')]; } $term = get_term($unc); $isunc = $term && !is_wp_error($term) && $term->slug === 'uncategorized'; $R['w_cat'] = ($isunc && $only) ? aiotsa_r('warn', '%d posts are only in "Uncategorized".', [count($only)], 'Give each post a real category.', [], $only) : aiotsa_r('pass', 'Every post has a category.'); $q = new WP_Query(['post_type' => 'post', 'post_status' => 'publish', 'fields' => 'ids', 'posts_per_page' => 50, 'meta_query' => [['key' => '_thumbnail_id', 'compare' => 'NOT EXISTS']]]); $l = []; // phpcs:ignore WordPress.DB.SlowDBQuery foreach ($q->posts as $pid) $l[] = [get_the_title($pid), get_permalink($pid), get_edit_post_link($pid, 'raw')]; $R['w_image'] = $l ? aiotsa_r('warn', '%d posts have no featured image.', [$q->found_posts], 'Add a featured image: it is used in search, social shares and Discover.', [], $l) : aiotsa_r('pass', 'Every post has a featured image.'); $q = new WP_Query(['post_type' => 'post', 'post_status' => 'publish', 'fields' => 'ids', 'posts_per_page' => 50, 'orderby' => 'modified', 'order' => 'ASC', 'date_query' => [['column' => 'post_modified_gmt', 'before' => '12 months ago']]]); $l = []; foreach ($q->posts as $pid) $l[] = [get_the_title($pid), get_permalink($pid), get_edit_post_link($pid, 'raw')]; $R['w_stale'] = $l ? aiotsa_r($q->found_posts > $pub / 2 ? 'warn' : 'info', '%d posts were last updated more than 12 months ago.', [$q->found_posts], 'Review the important ones and update facts, dates and links.', [], $l) : aiotsa_r('pass', 'Every post was updated in the last 12 months.'); $cc = wp_count_comments(); $pend = (int) $cc->moderated; $spam = (int) $cc->spam; $R['w_comments'] = ($pend > 20 || $spam > 200) ? aiotsa_r('warn', '%1$d comments wait for review and %2$d are marked as spam.', [$pend, $spam], 'Approve or delete them, and empty the spam folder.', [], [['Comments', admin_url('edit-comments.php')]]) : aiotsa_r('pass', '%1$d comments wait for review and %2$d are marked as spam.', [$pend, $spam]); } /* ---------- pages and posts ---------- */ function aiotsa_task_posts_plan(&$A) { $batches = []; $scope = $A['opts']['scope'] ?? 'all'; $max = max(1, min(5000, (int) ($A['opts']['max'] ?? 2000))); if ($A['mode'] === 'own') { $args = ['post_type' => post_type_exists('product') ? ['post', 'page', 'product'] : ['post', 'page'], 'post_status' => 'publish', 'fields' => 'ids', 'posts_per_page' => $max, 'orderby' => 'modified', 'order' => 'DESC', 'no_found_rows' => true, 'has_password' => false]; if ($scope === 'changed' && !empty($A['opts']['since'])) $args['date_query'] = [['column' => 'post_modified_gmt', 'after' => $A['opts']['since']]]; $ids = get_posts($args); $front = (int) get_option('page_on_front'); $ids = array_values(array_diff($ids, [$front])); $batches = array_chunk($ids, 4); } elseif ($A['mode'] === 'page') { $batches = [[$A['target']]]; } else $batches = apply_filters('aiotsa_page_batches', [], $A); $add = []; foreach ($batches as $b) $add[] = ['t' => 'posts', 'a' => $b, 's' => 0]; aiotsa_tasks_insert($A, $add); $A['summary']['ctx']['posts_total'] = array_sum(array_map('count', $batches)); } function aiotsa_post_facts_local($p) { $html = '' . esc_html(get_the_title($p)) . '

' . esc_html(get_the_title($p)) . '

' . apply_filters('the_content', $p->post_content) . '
'; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals return aiotsa_parse($html, get_permalink($p)); } function aiotsa_scan_one($A, $ref) { $thin = max(100, (int) aiotsa_settings()['thin']); $pid = 0; $type = 'post'; $local = false; if (is_numeric($ref)) { $p = get_post((int) $ref); if (!$p) return null; $pid = $p->ID; $url = get_permalink($p); $type = $p->post_type; } else $url = (string) $ref; foreach ((array) ($A['summary']['ctx']['pages'] ?? []) as $pu) if (untrailingslashit(strtolower($pu)) === untrailingslashit(strtolower($url))) $type = 'policy'; $r = aiotsa_fetch($url, ['timeout' => 12]); $f = $r['ok'] ? aiotsa_parse($r['body'], $r['url']) : null; if ((!$f || !$f['ok']) && $pid) { $f = aiotsa_post_facts_local(get_post($pid)); $local = true; } if (!$f || !$f['ok']) return ['pid' => $pid, 'url' => $url, 'score' => 0, 'd' => ['t' => $url, 'err' => $r['error'] ?: ('HTTP ' . $r['code']), 'is' => ['load'], 'type' => $type, 'w' => 0, 'int' => [], 'ext' => []]]; $is = []; $sc = 100; $tl = mb_strlen($f['title']); $dl = mb_strlen($f['desc']); if ($tl === 0) { $is[] = 'no_title'; $sc -= 25; } elseif ($tl > 65) { $is[] = 'long_title'; $sc -= 4; } elseif ($tl < 20) { $is[] = 'short_title'; $sc -= 4; } if (!$local) { if ($dl === 0) { $is[] = 'no_desc'; $sc -= 12; } elseif ($dl > 165) { $is[] = 'long_desc'; $sc -= 3; } elseif ($dl < 60) { $is[] = 'short_desc'; $sc -= 3; } } $h1 = count($f['h1']); if ($h1 === 0) { $is[] = 'no_h1'; $sc -= 10; } elseif ($h1 > 1) { $is[] = 'multi_h1'; $sc -= 5; } $prev = 1; $skip = false; foreach ($f['heads'] as [$lv]) { if ($lv > $prev + 1) $skip = true; $prev = $lv; } if ($skip) { $is[] = 'head_skip'; $sc -= 3; } if ($type !== 'policy' && $type !== 'product' && $f['words'] < $thin) { $is[] = 'thin'; $sc -= 20; } elseif ($type === 'post' && $f['words'] < 600) $sc -= 5; if ($f['noalt']) { $is[] = 'noalt'; $sc -= min(10, 2 * $f['noalt']); } if ($type !== 'policy' && count($f['intm']) < 2) { $is[] = 'few_links'; $sc -= 6; } if (!$local && empty($f['og']['title'])) { $is[] = 'no_og'; $sc -= 3; } if (!$local && !$f['schema']) { $is[] = 'no_schema'; $sc -= 4; } if (strpos($f['robots'], 'noindex') !== false) { $is[] = 'noindex'; $sc -= 25; } $read = aiotsa_readability(mb_substr($f['text'], 0, 4000)); if ($f['words'] > 150 && $read < 30) { $is[] = 'hard_read'; $sc -= 4; } $path = (string) wp_parse_url($url, PHP_URL_PATH); if (strlen($path) > 90 || preg_match('/[A-Z_]/', $path)) $is[] = 'long_url'; $d = ['t' => $f['title'] !== '' ? $f['title'] : ($pid ? get_the_title($pid) : $url), 'tl' => $tl, 'ds' => mb_substr($f['desc'], 0, 300), 'dl' => $dl, 'h1' => $h1, 'w' => $f['words'], 'im' => $f['imgs'], 'na' => $f['noalt'], 'nas' => $f['noalt_src'], 'int' => array_slice(array_values(array_unique(array_merge($f['intm'], $f['int']))), 0, 80), 'ext' => array_slice(array_values(array_unique(array_merge($f['extm'], $f['ext']))), 0, 25), 'il' => array_slice($f['intm'], 0, 40), 'ec' => count($f['extm']), 'sch' => $f['schema'], 'og' => !empty($f['og']['title']), 'rd' => $read, 'qh' => $f['qheads'], 'fw' => $f['first_words'], 'faq' => $f['faq'] ? 1 : 0, 'au' => $f['author'] ? 1 : 0, 'dt' => $f['date'] ? 1 : 0, 'is' => $is, 'type' => $type, 'local' => $local ? 1 : 0, 'hd' => array_slice($f['heads'], 0, 25), 'x' => mb_substr($f['text'], 0, 1500), 'cn' => $f['canonical'], 'mx' => (array) $f['mx']]; if ($pid) { $d['mod'] = get_post_modified_time('Y-m-d', true, $pid); $d['pt'] = get_the_title($pid); } return ['pid' => $pid, 'url' => $r['ok'] ? $r['url'] : $url, 'score' => max(0, $sc), 'd' => $d]; } function aiotsa_task_posts(&$A, $refs) { foreach ((array) $refs as $ref) { $o = aiotsa_scan_one($A, $ref); if (!$o) continue; aiotsa_item_add($A['id'], 'post', $o['pid'], $o['url'], $o['score'], $o['d']); if ($o['pid'] && $A['mode'] === 'own') update_post_meta($o['pid'], '_aiotsa_score', ['s' => $o['score'], 'is' => $o['d']['is'], 'a' => $A['id'], 't' => time()]); } $A['summary']['ctx']['posts_done'] = ($A['summary']['ctx']['posts_done'] ?? 0) + count((array) $refs); } function aiotsa_task_links_plan(&$A) { $items = aiotsa_items($A['id'], 'post'); $map = []; $own = aiotsa_origin($A['target']); foreach ($items as $it) { foreach (['int', 'ext'] as $k) foreach (($it['data'][$k] ?? []) as $l) { if (!isset($map[$l])) $map[$l] = ['k' => $k, 'src' => []]; if (count($map[$l]['src']) < 3) $map[$l]['src'][] = $it['url']; } } foreach (($A['summary']['ctx']['home']['int'] ?? []) as $l) if (!isset($map[$l])) $map[$l] = ['k' => 'int', 'src' => [$A['summary']['ctx']['home_url'] ?? $A['target']]]; $scanned = []; foreach ($items as $it) if (empty($it['data']['err'])) $scanned[untrailingslashit($it['url'])] = 1; $int = []; $ext = []; foreach ($map as $l => $m) { if (isset($scanned[untrailingslashit($l)])) continue; if ($m['k'] === 'int') $int[] = $l; else $ext[] = $l; } $pick = array_merge(array_slice($int, 0, (int) ($A['opts']['links_int'] ?? 120)), array_slice($ext, 0, (int) ($A['opts']['links_ext'] ?? 60))); $A['summary']['ctx']['links_total'] = count($pick); $A['summary']['ctx']['links_all'] = count($map); $src = []; foreach ($pick as $l) $src[$l] = $map[$l]['src']; $A['summary']['ctx']['link_src'] = $src; $add = []; foreach (array_chunk($pick, 3) as $b) $add[] = ['t' => 'links', 'a' => $b, 's' => 0]; aiotsa_tasks_insert($A, $add); } function aiotsa_task_links(&$A, $urls) { foreach ((array) $urls as $u) { $r = aiotsa_status($u); $bad = ($r['error'] === '' && $r['code'] >= 400 && !in_array($r['code'], [401, 403, 429, 999], true)) || in_array($r['error'], ['bad_url'], true) || ($r['error'] !== '' && preg_match('/resolve host|could not resolve/i', $r['error'])); if ($bad) aiotsa_item_add($A['id'], 'link', 0, $u, (int) $r['code'], ['src' => $A['summary']['ctx']['link_src'][$u] ?? [], 'err' => $r['error']]); } $A['summary']['ctx']['links_done'] = ($A['summary']['ctx']['links_done'] ?? 0) + count((array) $urls); } /* ---------- finalize: checks that need every page ---------- */ function aiotsa_plist($rows, $own, $extra = null) { $l = []; foreach ($rows as $it) { $lab = $it['data']['pt'] ?? $it['data']['t']; if ($extra) $lab .= ' (' . $extra($it) . ')'; $l[] = [$lab, $it["url"], ($own && $it["ref"]) ? get_edit_post_link($it["ref"], "raw") : "", "", $own ? $it["ref"] : 0]; } return $l; } function aiotsa_task_finalize(&$A) { $R = &$A['results']; $C = &$A['summary']['ctx']; $own = $A['mode'] === 'own'; $single = $A['mode'] === 'page'; $thin = max(100, (int) aiotsa_settings()['thin']); $items = array_values(array_filter(aiotsa_items($A['id'], 'post'), function ($i) { return empty($i['data']['err']); })); $n = count($items); $posts = array_values(array_filter($items, function ($i) { return ($i['data']['type'] ?? 'post') !== 'policy'; })); $np = count($posts); $has = function ($code, $set = null) use ($items) { return array_values(array_filter($set ?? $items, function ($i) use ($code) { return in_array($code, $i['data']['is'], true); })); }; $agg = function ($id, $rows, $total, $warn_at, $fail_at, $msg, $fix, $okmsg, $extra = null) use (&$R, $own) { $c = count($rows); if (!$total) { $R[$id] = aiotsa_skip('No pages were scanned.'); return; } $share = $c / $total; $R[$id] = $c === 0 ? aiotsa_r('pass', $okmsg, [$total]) : aiotsa_r(($share >= $fail_at || $c >= 10 && $share >= $fail_at / 2) ? 'fail' : 'warn', $msg, [$c, $total], $fix, [], aiotsa_plist($rows, $own, $extra)); }; $agg('o_title', array_merge($has('no_title'), $has('long_title'), $has('short_title')), $n, 0, 0.3, '%1$d of %2$d pages have a missing, very short or very long title.', 'Write a clear title of 30-60 characters for each.', 'All %d pages have a title of good length.', function ($i) { return $i['data']['tl'] . ' chars'; }); $agg('o_desc', array_merge($has('no_desc'), $has('long_desc'), $has('short_desc')), $n, 0, 0.25, '%1$d of %2$d pages have a missing, very short or very long meta description.', 'Write 120-155 characters for each.', 'All %d pages have a meta description of good length.', function ($i) { return $i['data']['dl'] ? $i['data']['dl'] . ' chars' : 'missing'; }); $agg('o_h1', array_merge($has('no_h1'), $has('multi_h1')), $n, 0, 0.3, '%1$d of %2$d pages have no H1 or more than one.', 'Use exactly one H1 (the page title) on each page.', 'All %d pages have exactly one H1.', function ($i) { return $i['data']['h1'] . ' H1'; }); $agg('o_heads', $has('head_skip'), $n, 0, 0.6, '%1$d of %2$d pages skip a heading level (for example H2 to H4).', 'Use H3 under H2, and H4 under H3.', 'Headings are in order on all %d pages.'); $na = array_sum(array_map(function ($i) { return $i['data']['na']; }, $items)); $ni = array_sum(array_map(function ($i) { return $i['data']['im']; }, $items)); $C['alt_cov'] = $ni ? (int) round(100 * ($ni - $na) / $ni) : 100; $rows = $has('noalt'); $R['o_alt'] = !$ni ? aiotsa_skip('No images found.') : ($na === 0 ? aiotsa_r('pass', 'All %d images have alt text.', [$ni]) : aiotsa_r($na / $ni > 0.3 ? 'fail' : 'warn', '%1$d of %2$d images have no alt text.', [$na, $ni], 'Describe each image in a few words.', [], aiotsa_plist($rows, $own, function ($i) { return $i['data']['na'] . ' images'; }))); $agg('o_links', $has('few_links', $posts), $np, 0, 0.5, '%1$d of %2$d posts link to fewer than 2 other pages of the site from their text.', 'Add 2-3 links to related posts. See "Internal link ideas" in the Tools tab.', 'All %d posts link to other pages of the site from their text.'); $agg('o_og', $has('no_og'), $n, 0, 0.5, '%1$d of %2$d pages have no Open Graph tags, so shares show no title or image.', 'Turn on social tags in your SEO plugin.', 'All %d pages have Open Graph tags.'); $agg('o_schema', $has('no_schema'), $n, 0, 0.5, '%1$d of %2$d pages have no structured data.', 'Turn on Article schema in your SEO plugin.', 'All %d pages have structured data.'); $agg('o_url', $has('long_url'), $n, 0, 0.6, '%1$d of %2$d pages have a long or untidy URL.', 'Keep new URLs short: 3-5 words, lower case, hyphens.', 'All %d URLs are short and clean.'); $avg = $n ? (int) round(array_sum(array_map(function ($i) { return $i['data']['rd']; }, $items)) / $n) : 0; $C['read_avg'] = $avg; $rows = $has('hard_read'); $R['o_read'] = !$n ? aiotsa_skip('No pages were scanned.') : ($rows ? aiotsa_r('warn', 'Average score %1$d of 100. %2$d pages are hard to read.', [$avg, count($rows)], 'Use shorter sentences and plain words.', [], aiotsa_plist($rows, $own, function ($i) { return (string) $i['data']['rd']; })) : aiotsa_r('pass', 'Average score %d of 100.', [$avg])); // duplicates and near-duplicates $by = []; foreach ($items as $it) { $k = mb_strtolower(trim($it['data']['t'])); if ($k !== '') $by[$k][] = $it; } $dups = []; foreach ($by as $g) if (count($g) > 1) foreach ($g as $it) $dups[] = $it; $R['o_dup'] = $single ? aiotsa_skip('Needs more than one page.') : ($dups ? aiotsa_r('fail', '%d pages share a title with another page.', [count($dups)], 'Give every page its own title.', [], aiotsa_plist($dups, $own)) : aiotsa_r('pass', 'None found.')); $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')); $sig = []; foreach ($posts as $i => $it) { $w = 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) > 2 && !isset($stop[$x]); }); $sig[$i] = array_unique($w); } $pairs = []; $cnt = count($posts); for ($i = 0; $i < $cnt && $cnt <= 600; $i++) for ($j = $i + 1; $j < $cnt; $j++) { $a = $sig[$i]; $b = $sig[$j]; if (count($a) < 3 || count($b) < 3) continue; $in = count(array_intersect($a, $b)); if ($in / min(count($a), count($b)) >= 0.75 && count($pairs) < 30) $pairs[] = [($posts[$i]['data']['pt'] ?? $posts[$i]['data']['t']) . ' <> ' . ($posts[$j]['data']['pt'] ?? $posts[$j]['data']['t']), $posts[$i]['url'], '']; } $R['o_cannibal'] = $single ? aiotsa_skip('Needs more than one page.') : ($pairs ? aiotsa_r('warn', '%d pairs of posts look like they cover the same topic.', [count($pairs)], 'Merge each pair into one stronger post and redirect the other.', [], $pairs) : aiotsa_r('pass', 'None found.')); // thin $rows = $has('thin', $posts); $C['thin'] = count($rows); $C['avg_words'] = $np ? (int) round(array_sum(array_map(function ($i) { return $i['data']['w']; }, $posts)) / $np) : 0; $R['a_thin'] = !$np ? aiotsa_skip('No posts were scanned.') : ($rows ? aiotsa_r(count($rows) / $np >= 0.15 ? 'fail' : 'warn', '%1$d of %2$d posts have fewer than %3$d words.', [count($rows), $np, $thin], 'Expand them, merge them into a fuller post, or unpublish them.', [], aiotsa_plist($rows, $own, function ($i) { return $i['data']['w'] . ' words'; })) : aiotsa_r('pass', 'No post has fewer than %3$d words.', [0, $np, $thin])); if (!$own && !$single) { $est = (int) ($C['sitemap_count'] ?? 0); $R['a_posts'] = $est >= 25 ? aiotsa_r('pass', 'About %d pages in the sitemap.', [$est]) : aiotsa_r('warn', 'About %d pages in the sitemap.', [$est], 'Publish more complete, original posts before you apply.'); } // risky words $bad = '/\b(casino|betting|gambl|porn|xxx|escort|adult video|crack(ed)? (software|apk)|mod apk|torrent|free movie download|nulled|keygen|hack tool|weed|vape|payday loan)\b/i'; $rows = array_values(array_filter($items, function ($i) use ($bad) { return preg_match($bad, $i['data']['t'] . ' ' . mb_substr($i['data']['x'] ?? '', 0, 600)); })); $R['a_risky'] = $rows ? aiotsa_r('warn', '%d pages use words that ad reviewers often flag. Please review them yourself.', [count($rows)], 'Read the AdSense content policies and remove content that breaks them.', [], aiotsa_plist($rows, $own)) : aiotsa_r('pass', 'No flagged words found. This is a word check only, not a policy review.'); // orphans if ($own) { $in = []; foreach ($items as $it) foreach (($it['data']['il'] ?? []) as $l) { $k = untrailingslashit(strtolower($l)); if ($k !== untrailingslashit(strtolower($it['url']))) $in[$k] = ($in[$k] ?? 0) + 1; } foreach (($C['home']['int'] ?? []) as $l) { $k = untrailingslashit(strtolower($l)); $in[$k] = ($in[$k] ?? 0) + 1; } $rows = array_values(array_filter($posts, function ($i) use ($in) { return empty($in[untrailingslashit(strtolower($i['url']))]); })); $C['orphans'] = count($rows); $R['w_orphan'] = !$np ? aiotsa_skip('No posts were scanned.') : ($rows ? aiotsa_r(count($rows) / $np > 0.3 ? 'fail' : 'warn', '%1$d of %2$d posts get no link from the text of any page we scanned (menus do not count).', [count($rows), $np], 'Link to each of them from a related post. See "Internal link ideas" in the Tools tab.', [], aiotsa_plist($rows, $own)) : aiotsa_r('pass', 'Every post gets at least one internal link.')); } // broken links $bl = aiotsa_items($A['id'], 'link'); $l = []; foreach ($bl as $b) $l[] = [$b['url'] . ' (' . ($b['score'] ?: 'no answer') . ')', $b['url'], '', implode(' , ', $b['data']['src'] ?? [])]; $lt = (int) ($C['links_total'] ?? 0); $C['broken'] = count($bl); $R['t_broken'] = !$lt ? aiotsa_skip('No links to test.') : ($bl ? aiotsa_r(count($bl) > 5 ? 'fail' : 'warn', '%1$d of %2$d links we tested are broken.', [count($bl), $lt], 'Open each page shown and fix or remove the link.', [], $l) : aiotsa_r('pass', '%2$d links tested, none broken.', [0, $lt])); // trust + AI content $pc = function ($key) use ($posts, $np) { return $np ? count(array_filter($posts, function ($i) use ($key) { return !empty($i['data'][$key]); })) : 0; }; if ($np) { $a = $pc('au'); $R['r_author'] = $a / $np >= 0.8 ? aiotsa_r('pass', 'Shown on %1$d of %2$d posts.', [$a, $np]) : aiotsa_r('warn', 'Shown on only %1$d of %2$d posts.', [$a, $np], 'Show the author name on posts, linked to an author page with a short bio.'); $a = $pc('dt'); $R['r_dates'] = $a / $np >= 0.8 ? aiotsa_r('pass', 'Shown on %1$d of %2$d posts.', [$a, $np]) : aiotsa_r('warn', 'Shown on only %1$d of %2$d posts.', [$a, $np], 'Show the published or updated date on posts.'); $a = count(array_filter($posts, function ($i) { return ($i['data']['qh'] ?? 0) > 0; })); $R['i_qheads'] = $a / $np >= 0.4 ? aiotsa_r('pass', '%1$d of %2$d posts use question headings.', [$a, $np]) : aiotsa_r('warn', 'Only %1$d of %2$d posts use question headings.', [$a, $np], 'Turn key headings into the questions readers ask, then answer right below.'); $a = count(array_filter($posts, function ($i) { return ($i['data']['fw'] ?? 0) > 90; })); $R['i_answer'] = $a / $np <= 0.3 ? aiotsa_r('pass', 'Most posts get to the point in the first paragraph.') : aiotsa_r('warn', '%1$d of %2$d posts start with a long first paragraph.', [$a, $np], 'Answer the main question in the first 2-3 sentences.'); $a = $pc('faq'); $R['i_faq'] = $a / $np >= 0.2 ? aiotsa_r('pass', '%1$d of %2$d posts have an FAQ section.', [$a, $np]) : aiotsa_r($a ? 'warn' : 'fail', '%1$d of %2$d posts have an FAQ section.', [$a, $np], 'Add a short FAQ block (with FAQ schema) to your main posts.'); $a = count(array_filter($posts, function ($i) { return ($i['data']['ec'] ?? 0) > 0; })); $R['i_sources'] = $a / $np >= 0.5 ? aiotsa_r('pass', '%1$d of %2$d posts link to an outside source.', [$a, $np]) : aiotsa_r('warn', 'Only %1$d of %2$d posts link to an outside source.', [$a, $np], 'Link the source for facts and numbers.'); $a = count(array_filter($posts, function ($i) { return array_intersect($i['data']['sch'] ?? [], ['Article', 'BlogPosting', 'NewsArticle', 'TechArticle', 'Recipe', 'Product', 'HowTo']); })); $R['i_schema'] = $a / $np >= 0.8 ? aiotsa_r('pass', '%1$d of %2$d posts have Article schema.', [$a, $np]) : aiotsa_r('warn', 'Only %1$d of %2$d posts have Article schema.', [$a, $np], 'Turn on Article schema in your SEO plugin.'); } else foreach (['r_author', 'r_dates', 'i_qheads', 'i_answer', 'i_faq', 'i_sources', 'i_schema'] as $k) $R[$k] = aiotsa_skip('No posts were scanned.'); $hw = (int) ($C['home']['words'] ?? 0); $R['i_nojs'] = $hw >= 80 || ($n && $C['avg_words'] >= 150) ? aiotsa_r('pass', 'The text is in the HTML, so AI crawlers can read it.') : aiotsa_r('fail', 'Very little text is in the HTML (%d words on the home page). The content may need JavaScript, which most AI crawlers do not run.', [$hw], 'Render the content on the server.'); $C['counts'] = ['pages' => $n, 'posts' => $np, 'no_desc' => count($has('no_desc')), 'thin' => $C['thin'], 'orphans' => $C['orphans'] ?? 0, 'broken' => $C['broken'], 'noalt' => $na, 'imgs' => $ni, 'faq' => $np ? $pc('faq') : 0]; aiotsa_more_finalize($A, $items, $posts); aiotsa_score($A); unset($C['link_src']); $prev = aiotsa_prev_id($A); if ($prev) { $P = aiotsa_audit_get($prev); $A['summary']['prev'] = ['id' => $prev, 'date' => $P['finished'] ?: $P['created'], 'score' => $P['score'], 'sec' => $P['summary']['sec'] ?? []]; } } /** The last finished audit of the same site, type and depth (a quick check is never compared with a full audit). */ function aiotsa_prev_id($A) { global $wpdb; $ids = $wpdb->get_col($wpdb->prepare("SELECT id FROM %i WHERE mode = %s AND target = %s AND status = 'done' AND id < %d ORDER BY id DESC LIMIT 12", aiotsa_tbl('audits'), $A['mode'], $A['target'], $A['id'])); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $want = ($A['opts']['scope'] ?? 'all') === 'quick' ? 'quick' : 'full'; foreach ($ids as $id) { $P = aiotsa_audit_get((int) $id); if ($P && ((($P['opts']['scope'] ?? 'all') === 'quick' ? 'quick' : 'full') === $want)) return (int) $id; } return 0; } /** Section and overall scores, and the AdSense verdict. */ function aiotsa_score(&$A) { $reg = aiotsa_registry(); $sec = []; $cnt = ['pass' => 0, 'warn' => 0, 'fail' => 0, 'info' => 0, 'skip' => 0]; foreach ($A['results'] as $id => $r) { if (!isset($reg[$id])) continue; [$s, , $w] = $reg[$id]; $st = $r['s']; $cnt[$st] = ($cnt[$st] ?? 0) + 1; if (!isset($sec[$s])) $sec[$s] = ['got' => 0, 'max' => 0, 'pass' => 0, 'warn' => 0, 'fail' => 0]; if ($st === 'pass' || $st === 'warn' || $st === 'fail') { $sec[$s][$st]++; if ($w > 0) { $sec[$s]['max'] += $w; $sec[$s]['got'] += $st === 'pass' ? $w : ($st === 'warn' ? $w * 0.5 : 0); } } } $tot = 0; $wt = 0; $out = []; foreach (aiotsa_sections() as $k => [$name, $w]) { if (empty($sec[$k]) || !$sec[$k]['max']) continue; $v = (int) round(100 * $sec[$k]['got'] / $sec[$k]['max']); $out[$k] = ['score' => $v, 'pass' => $sec[$k]['pass'], 'warn' => $sec[$k]['warn'], 'fail' => $sec[$k]['fail']]; $tot += $v * $w; $wt += $w; } $A['summary']['sec'] = $out; $A['summary']['cnt'] = $cnt; $A['score'] = $wt ? (int) round($tot / $wt) : 0; $crit = 0; foreach (['a_privacy', 'a_about', 'a_contact', 'a_posts', 'a_thin', 'a_open'] as $k) if (($A['results'][$k]['s'] ?? '') === 'fail') $crit++; $as = $out['adsense']['score'] ?? 0; $A['summary']['adsense'] = (!isset($out['adsense']) || $A['mode'] === 'page' || ($A['opts']['scope'] ?? '') === 'quick') ? '' : ($crit === 0 && $as >= 85 ? 'ready' : ($crit <= 1 && $as >= 60 ? 'almost' : 'not')); }