false, 'message' => 'Please enter a valid URL.', ]; } $response = wp_safe_remote_get($url, [ 'timeout' => 20, 'redirection' => 5, 'headers' => [ 'User-Agent' => '360-seo-Optimizer/' . AI_SEO_CRO_VERSION, 'Accept' => 'text/html,application/xhtml+xml', ], ]); if (is_wp_error($response)) { return [ 'success' => false, 'message' => $response->get_error_message(), ]; } $status = wp_remote_retrieve_response_code($response); if ($status < 200 || $status >= 400) { return [ 'success' => false, 'message' => 'The page returned HTTP ' . (int) $status . '.', ]; } $content_type = strtolower((string) wp_remote_retrieve_header($response, 'content-type')); if ($content_type !== '' && strpos($content_type, 'text/html') === false && strpos($content_type, 'application/xhtml+xml') === false) { return [ 'success' => false, 'message' => 'The target URL did not return an HTML document.', ]; } $html = (string) wp_remote_retrieve_body($response); if (trim($html) === '') { return [ 'success' => false, 'message' => 'The page returned an empty response.', ]; } if (strlen($html) > 2000000) { return [ 'success' => false, 'message' => 'The page is too large to scan safely.', ]; } return [ 'success' => true, 'url' => ai_seo_cro_normalize_url($url), 'html' => $html, 'status' => (int) $status, 'content_type' => $content_type, 'headers' => ai_seo_cro_normalize_headers(wp_remote_retrieve_headers($response)), 'html_size' => strlen($html), ]; } /* |-------------------------------------------------------------------------- | Normalize URL |-------------------------------------------------------------------------- */ function ai_seo_cro_normalize_url($url) { if (!$url) { return ''; } $parts = wp_parse_url(trim((string) $url)); if (!$parts || empty($parts['host'])) { return strtolower(rtrim((string) $url, '/')); } $scheme = strtolower($parts['scheme'] ?? 'https'); $host = strtolower($parts['host']); $path = isset($parts['path']) ? preg_replace('#/+#', '/', $parts['path']) : ''; $path = ($path === '/') ? '' : rtrim((string) $path, '/'); $query = isset($parts['query']) ? '?' . $parts['query'] : ''; return $scheme . '://' . $host . $path . $query; } /* |-------------------------------------------------------------------------- | Convert Relative URL to Absolute |-------------------------------------------------------------------------- */ function ai_seo_cro_make_absolute($base, $url) { $base = trim((string) $base); $url = trim((string) $url); if ($url === '') { return ''; } if (preg_match('#^[a-z][a-z0-9+\-.]*:#i', $url)) { return esc_url_raw($url); } if (strpos($url, '//') === 0) { $scheme = wp_parse_url($base, PHP_URL_SCHEME) ?: 'https'; return esc_url_raw($scheme . ':' . $url); } $base_parts = wp_parse_url($base); $scheme = $base_parts['scheme'] ?? 'https'; $host = $base_parts['host'] ?? ''; $port = isset($base_parts['port']) ? ':' . $base_parts['port'] : ''; if ($host === '') { return ''; } if ($url[0] === '#') { return ai_seo_cro_normalize_url($base) . $url; } if ($url[0] === '?') { $path = $base_parts['path'] ?? ''; return esc_url_raw($scheme . '://' . $host . $port . $path . $url); } if ($url[0] === '/') { return esc_url_raw($scheme . '://' . $host . $port . $url); } $path = $base_parts['path'] ?? '/'; $path = preg_replace('#/[^/]*$#', '/', $path); $path = ($path === '') ? '/' : $path; $segments = explode('/', $path . $url); $resolved = []; foreach ($segments as $segment) { if ($segment === '' || $segment === '.') { continue; } if ($segment === '..') { array_pop($resolved); continue; } $resolved[] = $segment; } return esc_url_raw($scheme . '://' . $host . $port . '/' . implode('/', $resolved)); } /* |-------------------------------------------------------------------------- | Check Internal Link |-------------------------------------------------------------------------- */ function ai_seo_cro_is_internal($base, $url) { $base_host = strtolower((string) wp_parse_url($base, PHP_URL_HOST)); $url_host = strtolower((string) wp_parse_url($url, PHP_URL_HOST)); if (!$url_host) { return true; } return $base_host === $url_host; } /* |-------------------------------------------------------------------------- | Check Crawlable Href |-------------------------------------------------------------------------- */ function ai_seo_cro_is_crawlable_href($href) { $href = trim((string) $href); if ($href === '' || $href === '#') { return false; } if ($href[0] === '#') { return false; } return !preg_match('#^(javascript:|mailto:|tel:|sms:|data:)#i', $href); } /* |-------------------------------------------------------------------------- | Get Anchor Text |-------------------------------------------------------------------------- */ function ai_seo_cro_get_anchor_text($node) { if (!$node instanceof DOMElement) { return ''; } $text = trim(preg_replace('/\s+/u', ' ', $node->textContent)); if ($text !== '') { return $text; } $aria_label = trim($node->getAttribute('aria-label')); if ($aria_label !== '') { return $aria_label; } $title = trim($node->getAttribute('title')); if ($title !== '') { return $title; } foreach ($node->getElementsByTagName('img') as $image) { $alt = trim($image->getAttribute('alt')); if ($alt !== '') { return $alt; } } return ''; } /* |-------------------------------------------------------------------------- | Extract Visible Text |-------------------------------------------------------------------------- */ function ai_seo_cro_extract_visible_text($xpath) { if (!$xpath instanceof DOMXPath) { return ''; } $nodes = $xpath->query('//body//text()[normalize-space() != "" and not(ancestor::script) and not(ancestor::style) and not(ancestor::noscript) and not(ancestor::template)]'); if (!$nodes) { return ''; } $chunks = []; foreach ($nodes as $node) { $value = trim(preg_replace('/\s+/u', ' ', $node->nodeValue)); if ($value !== '') { $chunks[] = $value; } } return trim(implode(' ', $chunks)); } /* |-------------------------------------------------------------------------- | Count Words |-------------------------------------------------------------------------- */ function ai_seo_cro_count_words($text) { $text = trim((string) $text); if ($text === '') { return 0; } if (preg_match_all('/[\p{L}\p{N}][\p{L}\p{N}\p{Mn}\p{Pd}\']*/u', $text, $matches)) { return count($matches[0]); } return 0; } /* |-------------------------------------------------------------------------- | Normalize Headers |-------------------------------------------------------------------------- */ function ai_seo_cro_normalize_headers($headers) { if (empty($headers)) { return []; } if (is_object($headers) && method_exists($headers, 'getAll')) { $headers = $headers->getAll(); } elseif (is_object($headers)) { $headers = (array) $headers; } if (!is_array($headers)) { return []; } $normalized = []; foreach ($headers as $key => $value) { $header_key = strtolower((string) $key); $normalized[$header_key] = is_array($value) ? implode(', ', $value) : (string) $value; } return $normalized; } /* |-------------------------------------------------------------------------- | URL / Content Helpers |-------------------------------------------------------------------------- */ function ai_seo_cro_count_query_params($url) { $query = (string) wp_parse_url((string) $url, PHP_URL_QUERY); if ($query === '') { return 0; } parse_str($query, $params); return count($params); } function ai_seo_cro_get_text_to_html_ratio($text, $html) { $html_length = strlen(trim((string) $html)); if ($html_length === 0) { return 0.0; } $text_length = mb_strlen(trim((string) $text)); if ($text_length === 0) { return 0.0; } return round(($text_length / $html_length) * 100, 2); } function ai_seo_cro_has_doctype($html) { return (bool) preg_match('/') !== false; } function ai_seo_cro_validate_url($url) { static $cache = []; $url = esc_url_raw(trim((string) $url)); if ($url === '') { return [ 'ok' => false, 'status' => 0, 'message' => 'Empty URL.', ]; } if (isset($cache[$url])) { return $cache[$url]; } $args = [ 'timeout' => 10, 'redirection' => 3, 'limit_response_size' => 1024, 'sslverify' => !ai_seo_cro_is_local_url($url), 'headers' => [ 'User-Agent' => '360-seo-Optimizer/' . AI_SEO_CRO_VERSION, ], ]; $response = wp_safe_remote_head($url, $args); if (is_wp_error($response) || in_array((int) wp_remote_retrieve_response_code($response), [0, 403, 405, 429, 500, 501], true)) { $response = wp_safe_remote_get($url, $args); } if (is_wp_error($response)) { $result = [ 'ok' => false, 'status' => 0, 'message' => $response->get_error_message(), ]; $cache[$url] = $result; return $result; } $status = (int) wp_remote_retrieve_response_code($response); $result = [ 'ok' => ($status >= 200 && $status < 400), 'status' => $status, 'message' => '', ]; $cache[$url] = $result; return $result; } function ai_seo_cro_get_primary_language($language_code) { $language_code = strtolower(trim((string) $language_code)); if ($language_code === '') { return ''; } if ($language_code === 'x-default') { return 'x-default'; } $parts = preg_split('/[-_]/', $language_code); return $parts[0] ?? ''; } function ai_seo_cro_is_descriptive_anchor_text($text) { $text = strtolower(trim(preg_replace('/\s+/u', ' ', (string) $text))); if ($text === '') { return false; } if (!preg_match('/[\p{L}\p{N}]/u', $text)) { return false; } $generic = [ 'click here', 'here', 'read more', 'learn more', 'view more', 'more', 'details', 'view', 'visit', 'continue', 'continue reading', 'read article', 'read post', 'see more', 'find out more', 'explore more', 'get started', 'start now', 'check it out', 'this page', 'link', 'open', 'go', 'next', ]; return !in_array($text, $generic, true); } /** Check local paths using WordPress without prompting for filesystem credentials. */ function ai_seo_cro_path_is_writable($path) { require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; $filesystem = new WP_Filesystem_Direct(null); return $filesystem->is_writable($path); }