> */ protected function indexable_sample_pages(RunContext $ctx): array { $out = []; foreach ((array) $ctx->get('sample_pages', []) as $p) { if (!is_array($p)) { continue; } $code = (int) ($p['status_code'] ?? 0); // 4.0.17: 3xx rows are redirects, not content pages. if ($code < 200 || $code >= 300 || !empty($p['robots_noindex'])) { continue; } $out[] = $p; } return $out; } /** * @return array> */ protected function indexable_crawl_rows(RunContext $ctx): array { $rows = (array) $ctx->get('crawl_rows', []); if (empty($rows)) { $rows = (array) $ctx->get('sample_pages', []); } $out = []; foreach ($rows as $p) { if (!is_array($p)) { continue; } $code = (int) ($p['status_code'] ?? 0); // 4.0.17: 3xx rows are redirects, not content pages. if ($code < 200 || $code >= 300 || !empty($p['robots_noindex'])) { continue; } $out[] = $p; } return $out; } /** * @return array> */ protected function schema_nodes(array $page): array { $raw = isset($page['jsonld_raw']) && is_array($page['jsonld_raw']) ? $page['jsonld_raw'] : []; $nodes = []; foreach ($raw as $block) { if (!is_array($block)) { $block = is_string($block) ? json_decode($block, true) : null; } if (!is_array($block)) { continue; } foreach (SchemaResolver::flatten_graph($block) as $node) { $nodes[] = $node; } } return $nodes; } /** * @param array> $pages * @return string[] */ protected function collect_same_as(array $pages): array { $links = []; foreach ($pages as $p) { foreach ($this->schema_nodes($p) as $node) { if (!array_key_exists('sameAs', $node)) { continue; } foreach ($this->as_string_list($node['sameAs']) as $link) { if ($link !== '') { $links[] = $link; } } } } return array_values(array_unique($links)); } /** * @param mixed $value * @return string[] */ protected function as_string_list($value): array { if (is_string($value)) { return [trim($value)]; } if (!is_array($value)) { return []; } $out = []; foreach ($value as $item) { if (is_string($item)) { $out[] = trim($item); } elseif (is_array($item) && isset($item['url']) && is_string($item['url'])) { $out[] = trim($item['url']); } elseif (is_array($item) && isset($item['@id']) && is_string($item['@id'])) { $out[] = trim($item['@id']); } } return array_values(array_filter($out, static function ($v) { return $v !== ''; })); } protected function domain_of(string $url): string { $url = trim($url); if ($url === '') { return ''; } if (strpos($url, '://') === false) { $url = 'https://' . ltrim($url, '/'); } $host = wp_parse_url($url, PHP_URL_HOST); if (!is_string($host) || $host === '') { return ''; } $host = strtolower($host); return preg_replace('/^www\./', '', $host) ?: $host; } /** * @param string[] $needles */ protected function domain_matches(string $domain, array $needles): bool { $domain = strtolower($domain); foreach ($needles as $needle) { $needle = strtolower($needle); if ($needle !== '' && (strpos($domain, $needle) !== false || substr($domain, -strlen($needle)) === $needle)) { return true; } } return false; } /** * @param string[] $types * @param string[] $wanted */ protected function has_any_type(array $types, array $wanted): bool { foreach ($types as $type) { if (!is_string($type)) { continue; } foreach ($wanted as $w) { if (strcasecmp($type, $w) === 0) { return true; } } } return false; } /** * @param string[] $fields */ protected function has_any_field(array $node, array $fields): bool { foreach ($fields as $field) { if (array_key_exists($field, $node) && $node[$field] !== '' && $node[$field] !== null && $node[$field] !== []) { return true; } } return false; } /** * @param string[] $needles */ protected function plugin_active(array $wp, array $needles): bool { $plugins = isset($wp['plugins']) && is_array($wp['plugins']) ? $wp['plugins'] : []; foreach ($plugins as $pl) { if (!is_array($pl)) { continue; } $hay = strtolower((string) ($pl['file'] ?? '') . ' ' . (string) ($pl['name'] ?? '')); foreach ($needles as $needle) { if ($needle !== '' && strpos($hay, strtolower($needle)) !== false) { return true; } } } return false; } /** * @param string[] $needles */ protected function text_has_any(string $text, array $needles): bool { $text = strtolower($text); foreach ($needles as $needle) { if ($needle !== '' && strpos($text, strtolower($needle)) !== false) { return true; } } return false; } }