false, 'title' => '', 'desc' => '', 'robots' => '', 'canonical' => '', 'h1' => [], 'heads' => [], 'words' => 0, 'text' => '', 'imgs' => 0, 'noalt' => 0, 'noalt_src' => [], 'img_old' => 0, 'int' => [], 'ext' => [], 'intm' => [], 'extm' => [], 'og' => [], 'tw' => '', 'schema' => [], 'lang' => '', 'viewport' => false, 'favicon' => false, 'mixed' => 0, 'block_css' => 0, 'block_js' => 0, 'qheads' => 0, 'first_words' => 0, 'author' => false, 'date' => false, 'consent' => '', 'adsense' => false, 'faq' => false, 'generator' => '', 'amp' => '', 'nodim' => 0, 'hreflang' => 0, 'mx' => []]; if (trim((string) $html) === '' || !class_exists('DOMDocument')) return $f; $doc = new DOMDocument(); $prev = libxml_use_internal_errors(true); $doc->loadHTML('' . $html, LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_COMPACT); libxml_clear_errors(); libxml_use_internal_errors($prev); $x = new DOMXPath($doc); $f['ok'] = true; $host = strtolower((string) wp_parse_url($url, PHP_URL_HOST)); $base = preg_replace('/^www\./', '', $host); $https = stripos($url, 'https://') === 0; $t = $x->query('//head/title'); if ($t->length) $f['title'] = trim(preg_replace('/\s+/u', ' ', $t->item(0)->textContent)); $f['lang'] = $doc->documentElement ? (string) $doc->documentElement->getAttribute('lang') : ''; foreach ($x->query('//meta') as $m) { $n = strtolower($m->getAttribute('name')); $p = strtolower($m->getAttribute('property')); $c = trim($m->getAttribute('content')); if ($n === 'description') $f['desc'] = $c; elseif ($n === 'robots' || $n === 'googlebot') $f['robots'] .= ' ' . strtolower($c); elseif ($n === 'viewport') $f['viewport'] = true; elseif ($n === 'generator') $f['generator'] = $c; elseif ($n === 'twitter:card') $f['tw'] = $c; elseif ($n === 'author' && $c !== '') $f['author'] = true; if (strpos($p, 'og:') === 0) $f['og'][substr($p, 3)] = $c; if ($p === 'article:modified_time' || $p === 'article:published_time') $f['date'] = true; } foreach ($x->query('//link[@rel]') as $l) { $rel = strtolower($l->getAttribute('rel')); $href = $l->getAttribute('href'); if ($rel === 'canonical') $f['canonical'] = $href; elseif (strpos($rel, 'icon') !== false) $f['favicon'] = true; elseif ($rel === 'amphtml') $f['amp'] = $href; elseif ($rel === 'alternate' && $l->getAttribute('hreflang')) $f['hreflang']++; } $f['block_css'] = $x->query('//head/link[@rel="stylesheet"][not(@media="print")]')->length; $f['block_js'] = $x->query('//head/script[@src][not(@async)][not(@defer)][not(@type="module")]')->length; foreach ($x->query('//script[@type="application/ld+json"]') as $s) { $j = json_decode($s->textContent, true); if (is_array($j)) aiotsa_schema_types($j, $f['schema']); } $f['schema'] = array_values(array_unique($f['schema'])); if (in_array('FAQPage', $f['schema'], true)) $f['faq'] = true; $low = strtolower($html); foreach (['cookieyes' => 'CookieYes', 'cookie-law-info' => 'CookieYes', 'complianz' => 'Complianz', 'cookiebot' => 'Cookiebot', 'onetrust' => 'OneTrust', 'iubenda' => 'iubenda', 'fundingchoicesmessages' => 'Google Privacy & Messaging', 'quantcast' => 'Quantcast', 'cmplz' => 'Complianz', 'borlabs' => 'Borlabs Cookie', 'cookie-notice' => 'Cookie Notice', 'termly' => 'Termly', 'usercentrics' => 'Usercentrics', 'real-cookie-banner' => 'Real Cookie Banner'] as $k => $n) if (strpos($low, $k) !== false) { $f['consent'] = $n; break; } $f['adsense'] = strpos($low, 'adsbygoogle') !== false || strpos($low, 'pagead2.googlesyndication') !== false; if ($https) $f['mixed'] = preg_match_all('#<(?:img|script|iframe|link|source|video|audio)\b[^>]+(?:src|href)=["\']http://(?!www\.w3\.org)#i', $html); foreach ($x->query('//img') as $im) { $src = $im->getAttribute('src'); foreach (['data-lazy-src', 'data-src', 'data-original', 'data-srcset'] as $lk) if (($src === '' || strpos($src, 'data:') === 0) && $im->getAttribute($lk) !== '') $src = strtok($im->getAttribute($lk), ' '); if ($src === '' || strpos($src, 'data:') === 0) continue; if ((int) $im->getAttribute('width') === 1 || (int) $im->getAttribute('height') === 1) continue; $f['imgs']++; if (!$im->hasAttribute('alt') || trim($im->getAttribute('alt')) === '') { if ($im->getAttribute('role') !== 'presentation' && $im->getAttribute('aria-hidden') !== 'true') { $f['noalt']++; if (count($f['noalt_src']) < 12) $f['noalt_src'][] = $src; } } if (!$im->hasAttribute('width') || !$im->hasAttribute('height')) $f['nodim']++; if (preg_match('/\.(jpe?g|png|gif|bmp)(\?|$)/i', $src)) $f['img_old']++; } $pickq = function ($qs) use ($x) { $best = null; $len = 0; foreach ($qs as $q) foreach ($x->query($q) as $c) { $l = strlen($c->textContent); if ($l > $len) { $len = $l; $best = $c; } } return [$best, $len]; }; [$cont, $cl] = $pickq(['//*[contains(concat(" ", normalize-space(@class), " "), " wp-block-post-content ")]', '//*[contains(concat(" ", normalize-space(@class), " "), " entry-content ")]', '//*[contains(concat(" ", normalize-space(@class), " "), " post-content ")]', '//*[@itemprop="articleBody"]']); [$wrap, $wl] = $pickq(['//article']); if (!$wrap) [$wrap, $wl] = $pickq(['//main', '//*[@role="main"]']); $main = ($cont && (!$wrap || $cl >= 0.3 * $wl)) ? $cont : $wrap; $bodyMain = false; if (!$main) { $b = $x->query('//body'); $main = $b->length ? $b->item(0) : $doc->documentElement; $bodyMain = true; } $inMain = function ($n) use ($main, $bodyMain) { for ($p = $n->parentNode; $p; $p = $p->parentNode) { if ($p === $main) return true; $t = strtolower($p->nodeName); if ($t === 'nav' || $t === 'aside' || $t === 'footer' || ($bodyMain && $t === 'header') || ($t !== 'body' && $p instanceof DOMElement && preg_match('/\b(comment|related|sidebar|widget|navigation|post-nav)/i', $p->getAttribute('class')))) return false; } return false; }; foreach ($x->query('//a[@href]') as $a) { $h = trim($a->getAttribute('href')); if ($h === '' || $h[0] === '#' || preg_match('#^(mailto:|tel:|javascript:|data:|sms:|whatsapp:)#i', $h)) continue; $abs = WP_Http::make_absolute_url($h, $url); $ah = strtolower((string) wp_parse_url($abs, PHP_URL_HOST)); if ($ah === '' || !preg_match('#^https?://#i', $abs)) continue; $abs = preg_replace('/#.*$/', '', $abs); $k = preg_replace('/^www\./', '', $ah) === $base ? 'int' : 'ext'; if (count($f[$k]) < 300 && !in_array($abs, $f[$k], true)) $f[$k][] = $abs; if ($inMain($a) && count($f[$k . 'm']) < 120 && !in_array($abs, $f[$k . 'm'], true)) $f[$k . 'm'][] = $abs; if (stripos($a->getAttribute('rel'), 'author') !== false) $f['author'] = true; } if ($x->query('//*[contains(@class,"author") or contains(@class,"byline") or @itemprop="author"]')->length) $f['author'] = true; if ($x->query('//time[@datetime]')->length) $f['date'] = true; $f['mx'] = aiotsa_more_parse($x, $main, $html, $url, $f); foreach ($x->query('//script|//style|//noscript|//template|//svg|//iframe') as $n) $rm[] = $n; foreach (($rm ?? []) as $n) if ($n->parentNode) $n->parentNode->removeChild($n); foreach ($x->query('//h1|//h2|//h3|//h4|//h5|//h6') as $h) { $tx = trim(preg_replace('/\s+/u', ' ', $h->textContent)); $lv = (int) substr($h->nodeName, 1); if ($tx === '') continue; if ($lv === 1) $f['h1'][] = $tx; if ($lv > 1 && !$inMain($h)) continue; if (count($f['heads']) < 80) $f['heads'][] = [$lv, mb_substr($tx, 0, 140)]; if ($lv > 1 && (mb_substr($tx, -1) === '?' || preg_match('/^(how|what|why|when|where|which|who|can|does|do|is|are|should)\b/i', $tx))) $f['qheads']++; } if ($bodyMain) { foreach ($x->query('//header|//footer|//nav|//aside|//form') as $n) $rm2[] = $n; foreach (($rm2 ?? []) as $n) if ($n->parentNode) $n->parentNode->removeChild($n); } else { foreach ($x->query('.//nav|.//aside|.//form|.//footer|.//*[contains(@class,"comment")]', $main) as $n) $rm3[] = $n; foreach (($rm3 ?? []) as $n) if ($n->parentNode) $n->parentNode->removeChild($n); } $txt = $main ? trim(preg_replace('/\s+/u', ' ', $main->textContent)) : ''; $f['words'] = aiotsa_wc($txt); $f['text'] = mb_substr($txt, 0, 6000); $fp = $main ? $x->query('.//p[string-length(normalize-space()) > 40]', $main) : null; if ($fp && $fp->length) $f['first_words'] = aiotsa_wc($fp->item(0)->textContent); if (!$f['faq'] && preg_match('/\b(faq|frequently asked)/i', implode(' ', array_column($f['heads'], 1)))) $f['faq'] = 'text'; return $f; } function aiotsa_schema_types($j, &$out, $d = 0) { if ($d > 6 || !is_array($j)) return; if (isset($j['@type'])) foreach ((array) $j['@type'] as $t) if (is_string($t)) $out[] = $t; foreach ($j as $v) if (is_array($v)) aiotsa_schema_types($v, $out, $d + 1); } /** Simple reading-ease style score (0-100) that works for Latin text; other scripts get a sentence-length score. */ function aiotsa_readability($text) { $s = max(1, preg_match_all('/[.!?।]+(\s|$)/u', $text)); $w = max(1, aiotsa_wc($text)); if (preg_match('/[a-z]/i', $text)) { $syl = max(1, preg_match_all('/[aeiouy]+/i', $text)); $v = 206.835 - 1.015 * ($w / $s) - 84.6 * ($syl / $w); } else $v = 100 - max(0, ($w / $s) - 12) * 3; return (int) max(0, min(100, round($v))); }