| @@ -41,8 +41,41 @@ | ||
| 41 | 41 | */ |
| 42 | 42 | private Database $database; |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | + * Memoised collected performance measurement, and whether it was resolved. | |
| 46 | + * | |
| 47 | + * Two factors read it and both may be asked for on every post in a list, so | |
| 48 | + * the lookup happens once per calculator. `null` is a real answer here — the | |
| 49 | + * separate flag keeps "not looked up yet" distinct from "nothing measured". | |
| 50 | + * | |
| 51 | + * @since 2.3.1 | |
| 52 | + * @var array|null | |
| 53 | + */ | |
| 54 | + private ?array $measured_performance = null; | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * @since 2.3.1 | |
| 58 | + * @var bool | |
| 59 | + */ | |
| 60 | + private bool $measured_performance_resolved = false; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Length bands the editor scores against, in characters. | |
| 64 | + * | |
| 65 | + * Public so every surface that judges a title or description — the editor | |
| 66 | + * score and the Bulk Snippets problem filter — reads one set of numbers. | |
| 67 | + * Before these existed the bands were literals inside the scoring methods, | |
| 68 | + * and a second screen would have had to copy them and drift (#727). | |
| 69 | + * | |
| 70 | + * @since 2.8.0 | |
| 71 | + */ | |
| 72 | + public const TITLE_OPTIMAL_MIN = 35; | |
| 73 | + public const TITLE_OPTIMAL_MAX = 60; | |
| 74 | + public const DESCRIPTION_OPTIMAL_MIN = 120; | |
| 75 | + public const DESCRIPTION_OPTIMAL_MAX = 160; | |
| 76 | + | |
| 77 | + /** | |
| 45 | 78 | * 2025 SEO scoring factors (Q1 2025 Google Algorithm) |
| 46 | 79 | * Based on First Page Sage research and Google's latest updates |
| 47 | 80 | * |
| 48 | 81 | * @var array |
| @@ -273,10 +306,9 @@ | ||
| 273 | 306 | $checks = []; |
| 274 | 307 | foreach ($haystacks as $location => $haystack) { |
| 275 | 308 | $matched = []; |
| 276 | 309 | foreach ($keywords as $keyword) { |
| 277 | - $needle = strtolower(trim($keyword)); | |
| 278 | - if ($needle !== '' && $haystack !== '' && strpos($haystack, $needle) !== false) { | |
| 310 | + if ($this->keyword_matches($haystack, strtolower(trim($keyword)))) { | |
| 279 | 311 | $matched[] = $keyword; |
| 280 | 312 | } |
| 281 | 313 | } |
| 282 | 314 | $checks[$location] = [ |
| @@ -288,8 +320,86 @@ | ||
| 288 | 320 | return $checks; |
| 289 | 321 | } |
| 290 | 322 | |
| 291 | 323 | /** |
| 324 | + * Scripts written without spaces between words. | |
| 325 | + * | |
| 326 | + * @since 2.1.0 | |
| 327 | + * @var string | |
| 328 | + */ | |
| 329 | + private const SCRIPTIO_CONTINUA = '/[\p{Han}\p{Hiragana}\p{Katakana}\p{Thai}\p{Lao}\p{Khmer}\p{Myanmar}]/u'; | |
| 330 | + | |
| 331 | + /** | |
| 332 | + * Whether a keyword appears in a haystack as a word rather than as a | |
| 333 | + * fragment of a longer one. | |
| 334 | + * | |
| 335 | + * The five keyword checks used a plain strpos(), so any substring hit | |
| 336 | + * counted: "test coronavirus" matched "la|test coronavirus|news", "art" | |
| 337 | + * matched "start", "cat" matched "category". The panel then confidently | |
| 338 | + * reported a keyword placement that does not exist (#416). Same class of | |
| 339 | + * problem #71 fixed in the Image SEO rewriter, and the same remedy. | |
| 340 | + * | |
| 341 | + * Both arguments are expected lowercased already. | |
| 342 | + * | |
| 343 | + * @since 2.1.0 | |
| 344 | + * | |
| 345 | + * @param string $haystack Text to search. | |
| 346 | + * @param string $needle Keyword, lowercased and trimmed. | |
| 347 | + * @return bool | |
| 348 | + */ | |
| 349 | + private function keyword_matches(string $haystack, string $needle): bool { | |
| 350 | + if ($needle === '' || $haystack === '') { | |
| 351 | + return false; | |
| 352 | + } | |
| 353 | + | |
| 354 | + if (!$this->supports_word_boundaries($needle)) { | |
| 355 | + return strpos($haystack, $needle) !== false; | |
| 356 | + } | |
| 357 | + | |
| 358 | + $matched = preg_match('/\b' . preg_quote($needle, '/') . '\b/u', $haystack); | |
| 359 | + | |
| 360 | + // PCRE refusing the pattern — invalid UTF-8 in the keyword, a | |
| 361 | + // backtrack limit — must not be reported as a confident "no match". | |
| 362 | + // Fall back to the behaviour this replaced rather than invent a | |
| 363 | + // negative the user cannot explain. | |
| 364 | + if ($matched === false) { | |
| 365 | + return strpos($haystack, $needle) !== false; | |
| 366 | + } | |
| 367 | + | |
| 368 | + return $matched === 1; | |
| 369 | + } | |
| 370 | + | |
| 371 | + /** | |
| 372 | + * Whether \b can express "this keyword, as a word" for this keyword. | |
| 373 | + * | |
| 374 | + * It asserts a transition between a word and a non-word character, which | |
| 375 | + * only means something where words are separated. Two cases where it is | |
| 376 | + * not, both verified against PCRE rather than assumed: | |
| 377 | + * | |
| 378 | + * - the keyword's own edges are not word characters ("c++", "#seo"), so | |
| 379 | + * no boundary can assert there and a real match is lost; | |
| 380 | + * - scripts written without spaces, where the neighbouring characters | |
| 381 | + * are word characters too — "冠状病毒" inside "最新冠状病毒新闻" is a | |
| 382 | + * legitimate match that \b never sees. | |
| 383 | + * | |
| 384 | + * Accented Latin and Cyrillic need no special handling: PHP's /u modifier | |
| 385 | + * turns on Unicode character properties, so "café" correctly does not | |
| 386 | + * match "cafés" and "коронавирус" does not match "коронавирусный". | |
| 387 | + * | |
| 388 | + * @since 2.1.0 | |
| 389 | + * | |
| 390 | + * @param string $needle Keyword, lowercased and trimmed. | |
| 391 | + * @return bool | |
| 392 | + */ | |
| 393 | + private function supports_word_boundaries(string $needle): bool { | |
| 394 | + if (preg_match(self::SCRIPTIO_CONTINUA, $needle)) { | |
| 395 | + return false; | |
| 396 | + } | |
| 397 | + | |
| 398 | + return preg_match('/^\w/u', $needle) === 1 && preg_match('/\w$/u', $needle) === 1; | |
| 399 | + } | |
| 400 | + | |
| 401 | + /** | |
| 292 | 402 | * Compute the SEO score for a single target keyword. |
| 293 | 403 | * |
| 294 | 404 | * @param array $content_data Content analysis data |
| 295 | 405 | * @param array $metadata Post metadata |
| @@ -401,9 +511,9 @@ | ||
| 401 | 511 | $total_score += $technical_result['score']; |
| 402 | 512 | $suggestions = array_merge($suggestions, $technical_result['suggestions']); |
| 403 | 513 | |
| 404 | 514 | try { |
| 405 | - $prioritized_suggestions = $this->prioritize_suggestions($suggestions); | |
| 515 | + $prioritized_suggestions = $this->prioritize_suggestions($suggestions, $scores); | |
| 406 | 516 | $grade = $this->get_grade_from_score($total_score); |
| 407 | 517 | |
| 408 | 518 | return [ |
| 409 | 519 | 'overall_score' => min(100, $total_score), |
| @@ -552,9 +662,9 @@ | ||
| 552 | 662 | $title_length = mb_strlen($title); |
| 553 | 663 | |
| 554 | 664 | // 2025 length optimization (6 points). 60 characters is the recommended |
| 555 | 665 | // maximum for best SERP visibility before Google truncates the title. |
| 556 | - if ($title_length >= 35 && $title_length <= 60) { | |
| 666 | + if ($title_length >= self::TITLE_OPTIMAL_MIN && $title_length <= self::TITLE_OPTIMAL_MAX) { | |
| 557 | 667 | $score += 6; |
| 558 | 668 | } elseif ($title_length >= 25 && $title_length <= 75) { |
| 559 | 669 | $score += 4; |
| 560 | 670 | $suggestions[] = 'Optimize title length to 35-60 characters for better SERP visibility'; |
| @@ -828,27 +938,69 @@ | ||
| 828 | 938 | |
| 829 | 939 | // Consider it a semantic match if 70% of keyword parts are present |
| 830 | 940 | return ($matches / count($keyword_parts)) >= 0.7; |
| 831 | 941 | } |
| 832 | - private function prioritize_suggestions(array $suggestions): array { | |
| 942 | + private function prioritize_suggestions(array $suggestions, array $scores = []): array { | |
| 943 | + // Map each suggestion back to the factor that emitted it, so priority | |
| 944 | + // can rank by the points the factor actually lost instead of keyword- | |
| 945 | + // matching the advice text — which sorted a 2-point title tweak above | |
| 946 | + // a 6-point thin-content loss and contradicted the row's own impact | |
| 947 | + // tag (#408). | |
| 948 | + $by_text = []; | |
| 949 | + foreach ($scores as $factor => $result) { | |
| 950 | + if (!is_array($result) || empty($result['suggestions']) || !is_array($result['suggestions'])) { | |
| 951 | + continue; | |
| 952 | + } | |
| 953 | + $lost = max(0, (float) ($result['max_score'] ?? 0) - (float) ($result['score'] ?? 0)); | |
| 954 | + foreach ($result['suggestions'] as $text) { | |
| 955 | + if (is_string($text) && !isset($by_text[$text])) { | |
| 956 | + $by_text[$text] = ['factor' => (string) $factor, 'lost' => $lost]; | |
| 957 | + } | |
| 958 | + } | |
| 959 | + } | |
| 960 | + | |
| 833 | 961 | $prioritized = []; |
| 834 | - | |
| 962 | + | |
| 835 | 963 | foreach ($suggestions as $suggestion) { |
| 836 | - $priority = $this->determine_suggestion_priority($suggestion); | |
| 964 | + $origin = $by_text[$suggestion] ?? null; | |
| 965 | + | |
| 966 | + // A factor already at full marks loses nothing to this advice — | |
| 967 | + // it was occupying list positions (sometimes at "High") while | |
| 968 | + // recovering zero points. Dropped rather than sorted last. | |
| 969 | + if (null !== $origin && $origin['lost'] <= 0) { | |
| 970 | + continue; | |
| 971 | + } | |
| 972 | + | |
| 973 | + if (null !== $origin) { | |
| 974 | + $priority = $origin['lost'] >= 4 ? 'High' : ($origin['lost'] >= 2 ? 'Medium' : 'Low'); | |
| 975 | + } else { | |
| 976 | + // No factor attached (defensive: a filter-added or legacy | |
| 977 | + // suggestion) — the old keyword map is the fallback. | |
| 978 | + $priority = $this->determine_suggestion_priority($suggestion); | |
| 979 | + } | |
| 980 | + | |
| 837 | 981 | $prioritized[] = [ |
| 838 | 982 | 'text' => $suggestion, |
| 839 | 983 | 'priority' => $priority, |
| 840 | 984 | 'impact' => $this->estimate_impact($suggestion), |
| 841 | 985 | 'effort' => $this->estimate_effort($suggestion), |
| 986 | + 'factor' => $origin['factor'] ?? null, | |
| 987 | + 'points_recoverable' => $origin['lost'] ?? null, | |
| 842 | 988 | ]; |
| 843 | 989 | } |
| 844 | - | |
| 845 | - // Sort by priority (High > Medium > Low) | |
| 990 | + | |
| 991 | + // Biggest recoverable loss first; keyword-mapped stragglers (no | |
| 992 | + // factor) sort within their priority band after the measured rows. | |
| 846 | 993 | usort($prioritized, function($a, $b) { |
| 994 | + $al = $a['points_recoverable'] ?? -1; | |
| 995 | + $bl = $b['points_recoverable'] ?? -1; | |
| 996 | + if ($al !== $bl) { | |
| 997 | + return $bl <=> $al; | |
| 998 | + } | |
| 847 | 999 | $priority_order = ['High' => 3, 'Medium' => 2, 'Low' => 1]; |
| 848 | 1000 | return $priority_order[$b['priority']] - $priority_order[$a['priority']]; |
| 849 | 1001 | }); |
| 850 | - | |
| 1002 | + | |
| 851 | 1003 | return $prioritized; |
| 852 | 1004 | } |
| 853 | 1005 | |
| 854 | 1006 | /** |
| @@ -1505,9 +1657,27 @@ | ||
| 1505 | 1657 | $words = preg_split('/\s+/', trim(strtolower(wp_strip_all_tags($text))), -1, PREG_SPLIT_NO_EMPTY); |
| 1506 | 1658 | $syllables = 0; |
| 1507 | 1659 | |
| 1508 | 1660 | foreach ($words as $word) { |
| 1509 | - $syllables += max(1, preg_match_all('/[aeiouy]+/', $word)); | |
| 1661 | + $word = preg_replace('/[^a-z]/', '', $word); | |
| 1662 | + if ($word === '') { | |
| 1663 | + continue; | |
| 1664 | + } | |
| 1665 | + | |
| 1666 | + $groups = preg_match_all('/[aeiouy]+/', $word); | |
| 1667 | + | |
| 1668 | + // Standard Flesch heuristic: a trailing silent e does not form a | |
| 1669 | + // syllable ("make", "time", "these") — but only when a consonant | |
| 1670 | + // precedes it (a vowel+e ending like "movie" already shares its | |
| 1671 | + // group) and never for consonant-le ("table"), which does count. | |
| 1672 | + // Without this the counter inflated syllables/word by ~0.2-0.3 on | |
| 1673 | + // ordinary prose, driving raw Flesch negative and the UI to a | |
| 1674 | + // clamped "Very Difficult (0)" (#407). | |
| 1675 | + if ($groups > 1 && preg_match('/[^aeiouy]e$/', $word) && !str_ends_with($word, 'le')) { | |
| 1676 | + $groups--; | |
| 1677 | + } | |
| 1678 | + | |
| 1679 | + $syllables += max(1, $groups); | |
| 1510 | 1680 | } |
| 1511 | 1681 | |
| 1512 | 1682 | return $syllables; |
| 1513 | 1683 | } |
| @@ -1791,20 +1961,101 @@ | ||
| 1791 | 1961 | * @param array $content_data Content analysis data |
| 1792 | 1962 | * @return array Scoring result |
| 1793 | 1963 | */ |
| 1794 | 1964 | private function score_mobile_experience(array $content_data): array { |
| 1795 | - // Mobile experience is theme/site-level, not controlled by post content. | |
| 1796 | - // Award full credit (benefit of the doubt) instead of a fixed partial | |
| 1797 | - // that caps every post's ceiling. | |
| 1965 | + $max = $this->scoring_factors['mobile_experience']; | |
| 1966 | + | |
| 1967 | + // Mobile experience is theme/site-level, not controlled by post content | |
| 1968 | + // — but the plugin already measures it. When a mobile Lighthouse score | |
| 1969 | + // has been collected, score against it; the "benefit of the doubt" below | |
| 1970 | + // is for sites nobody has measured, not for sites measured as slow. | |
| 1971 | + $performance_score = $this->measured_performance_score(); | |
| 1972 | + | |
| 1973 | + if ($performance_score === null) { | |
| 1974 | + return [ | |
| 1975 | + 'score' => $max, | |
| 1976 | + 'max_score' => $max, | |
| 1977 | + 'suggestions' => ['Ensure mobile-first design and fast loading on mobile devices'], | |
| 1978 | + 'details' => ['mobile_score' => 'Assumed adequate', 'measured' => false], | |
| 1979 | + ]; | |
| 1980 | + } | |
| 1981 | + | |
| 1982 | + $score = (int) round($max * $performance_score / 100); | |
| 1983 | + | |
| 1798 | 1984 | return [ |
| 1799 | - 'score' => $this->scoring_factors['mobile_experience'], | |
| 1800 | - 'max_score' => $this->scoring_factors['mobile_experience'], | |
| 1801 | - 'suggestions' => ['Ensure mobile-first design and fast loading on mobile devices'], | |
| 1802 | - 'details' => ['mobile_score' => 'Assumed adequate'], | |
| 1985 | + 'score' => $score, | |
| 1986 | + 'max_score' => $max, | |
| 1987 | + 'suggestions' => $score < $max | |
| 1988 | + ? ['Improve mobile page speed: the last PageSpeed run scored ' . $performance_score . '/100 on mobile'] | |
| 1989 | + : [], | |
| 1990 | + 'details' => [ | |
| 1991 | + 'mobile_score' => $performance_score, | |
| 1992 | + 'measured' => true, | |
| 1993 | + 'source' => 'pagespeed_mobile', | |
| 1994 | + ], | |
| 1803 | 1995 | ]; |
| 1804 | 1996 | } |
| 1805 | 1997 | |
| 1806 | 1998 | /** |
| 1999 | + * The last collected mobile Lighthouse score, or null when unmeasured. | |
| 2000 | + * | |
| 2001 | + * Memoised per instance: compute_score() asks twice, and a post-list screen | |
| 2002 | + * scores a page of posts at a time. | |
| 2003 | + * | |
| 2004 | + * Every failure — no performance module, no collected row, an unreadable | |
| 2005 | + * table — resolves to null, which the callers read as "not measured" and | |
| 2006 | + * answer with the full-credit fallback. A site is never penalised for | |
| 2007 | + * ThinkRank being unable to look. | |
| 2008 | + * | |
| 2009 | + * @since 2.3.1 | |
| 2010 | + * @return int|null Score 0-100, or null when nothing has been collected. | |
| 2011 | + */ | |
| 2012 | + private function measured_performance_score(): ?int { | |
| 2013 | + $measurement = $this->measured_performance(); | |
| 2014 | + | |
| 2015 | + if ($measurement === null || !isset($measurement['performance_score'])) { | |
| 2016 | + return null; | |
| 2017 | + } | |
| 2018 | + | |
| 2019 | + $score = $measurement['performance_score']; | |
| 2020 | + | |
| 2021 | + if (!is_numeric($score)) { | |
| 2022 | + return null; | |
| 2023 | + } | |
| 2024 | + | |
| 2025 | + return (int) round(max(0, min(100, (float) $score))); | |
| 2026 | + } | |
| 2027 | + | |
| 2028 | + /** | |
| 2029 | + * The last collected mobile measurement, or null when there is none. | |
| 2030 | + * | |
| 2031 | + * @since 2.3.1 | |
| 2032 | + * @return array|null { core_web_vitals: array, performance_score: float|null } | |
| 2033 | + */ | |
| 2034 | + private function measured_performance(): ?array { | |
| 2035 | + if ($this->measured_performance_resolved) { | |
| 2036 | + return $this->measured_performance; | |
| 2037 | + } | |
| 2038 | + | |
| 2039 | + $this->measured_performance_resolved = true; | |
| 2040 | + | |
| 2041 | + if (!class_exists('ThinkRank\\SEO\\Performance_Monitoring_Manager')) { | |
| 2042 | + return null; | |
| 2043 | + } | |
| 2044 | + | |
| 2045 | + try { | |
| 2046 | + $manager = new \ThinkRank\SEO\Performance_Monitoring_Manager(); | |
| 2047 | + // Mobile deliberately: Google indexes mobile-first, and it is the | |
| 2048 | + // device the mobile_experience factor is named after. | |
| 2049 | + $this->measured_performance = $manager->get_stored_performance_measurement('mobile'); | |
| 2050 | + } catch (\Throwable $e) { | |
| 2051 | + $this->measured_performance = null; | |
| 2052 | + } | |
| 2053 | + | |
| 2054 | + return $this->measured_performance; | |
| 2055 | + } | |
| 2056 | + | |
| 2057 | + /** | |
| 1807 | 2058 | * Score core web vitals - 2025 version (3 points) |
| 1808 | 2059 | * |
| 1809 | 2060 | * @param array $content_data Content analysis data |
| 1810 | 2061 | * @return array Scoring result |
| @@ -1809,20 +2060,98 @@ | ||
| 1809 | 2060 | * @param array $content_data Content analysis data |
| 1810 | 2061 | * @return array Scoring result |
| 1811 | 2062 | */ |
| 1812 | 2063 | private function score_core_web_vitals(array $content_data): array { |
| 1813 | - // Core Web Vitals are a runtime/performance signal, not derivable from | |
| 1814 | - // post content. Award full credit (benefit of the doubt) rather than a | |
| 1815 | - // fixed partial that caps every post's ceiling. | |
| 2064 | + $max = $this->scoring_factors['core_web_vitals']; | |
| 2065 | + | |
| 2066 | + // Not derivable from post content — but it is measured, and the audit | |
| 2067 | + // stores LCP, INP and CLS with a rating each. Score against those when | |
| 2068 | + // they exist; fall back to the benefit of the doubt when they do not. | |
| 2069 | + $rated = $this->measured_vitals_score(); | |
| 2070 | + | |
| 2071 | + if ($rated === null) { | |
| 2072 | + return [ | |
| 2073 | + 'score' => $max, | |
| 2074 | + 'max_score' => $max, | |
| 2075 | + 'suggestions' => ['Optimize Core Web Vitals: LCP, INP, and CLS for better user experience'], | |
| 2076 | + 'details' => ['vitals_status' => 'Assumed adequate', 'measured' => false], | |
| 2077 | + ]; | |
| 2078 | + } | |
| 2079 | + | |
| 2080 | + $score = (int) round($max * $rated['average'] / 100); | |
| 2081 | + | |
| 1816 | 2082 | return [ |
| 1817 | - 'score' => $this->scoring_factors['core_web_vitals'], | |
| 1818 | - 'max_score' => $this->scoring_factors['core_web_vitals'], | |
| 1819 | - 'suggestions' => ['Optimize Core Web Vitals: LCP, INP, and CLS for better user experience'], | |
| 1820 | - 'details' => ['vitals_status' => 'Assumed adequate'], | |
| 2083 | + 'score' => $score, | |
| 2084 | + 'max_score' => $max, | |
| 2085 | + // Gated on the measurement, not the rounded score: two good metrics | |
| 2086 | + // and one needing improvement averages 88.33, which rounds to the | |
| 2087 | + // full 3 of 3 and used to swallow the suggestion naming the metric | |
| 2088 | + // that is actually failing. | |
| 2089 | + 'suggestions' => !empty($rated['failing']) | |
| 2090 | + ? ['Optimize Core Web Vitals: ' . implode(', ', $rated['failing']) . ' below target on mobile'] | |
| 2091 | + : [], | |
| 2092 | + 'details' => [ | |
| 2093 | + 'vitals_status' => $rated['statuses'], | |
| 2094 | + 'measured' => true, | |
| 2095 | + 'source' => 'pagespeed_mobile', | |
| 2096 | + ], | |
| 1821 | 2097 | ]; |
| 1822 | 2098 | } |
| 1823 | 2099 | |
| 1824 | 2100 | /** |
| 2101 | + * Rate the collected Core Web Vitals, or null when none were measured. | |
| 2102 | + * | |
| 2103 | + * Reuses the per-metric score the performance module already assigns | |
| 2104 | + * (good 100, needs improvement 65, poor 30) rather than inventing a second | |
| 2105 | + * scale, so the SEO score and the performance card cannot disagree about | |
| 2106 | + * whether a metric is healthy. | |
| 2107 | + * | |
| 2108 | + * Metrics with no stored value — fcp is not always collected — are skipped | |
| 2109 | + * rather than counted as failures. | |
| 2110 | + * | |
| 2111 | + * @since 2.3.1 | |
| 2112 | + * @return array|null { average: float, statuses: array, failing: string[] } | |
| 2113 | + */ | |
| 2114 | + private function measured_vitals_score(): ?array { | |
| 2115 | + $measurement = $this->measured_performance(); | |
| 2116 | + $vitals = $measurement['core_web_vitals'] ?? null; | |
| 2117 | + | |
| 2118 | + if (!is_array($vitals)) { | |
| 2119 | + return null; | |
| 2120 | + } | |
| 2121 | + | |
| 2122 | + $scores = []; | |
| 2123 | + $statuses = []; | |
| 2124 | + $failing = []; | |
| 2125 | + | |
| 2126 | + // The three Google ranks on. fcp is diagnostic and not a Core Web Vital. | |
| 2127 | + foreach (['lcp', 'inp', 'cls'] as $metric) { | |
| 2128 | + $data = $vitals[$metric] ?? null; | |
| 2129 | + | |
| 2130 | + if (!is_array($data) || !isset($data['value'], $data['score']) || $data['value'] === null) { | |
| 2131 | + continue; | |
| 2132 | + } | |
| 2133 | + | |
| 2134 | + $scores[] = (float) $data['score']; | |
| 2135 | + $statuses[$metric] = $data['status'] ?? 'unknown'; | |
| 2136 | + | |
| 2137 | + if (($data['status'] ?? '') !== 'good') { | |
| 2138 | + $failing[] = strtoupper($metric); | |
| 2139 | + } | |
| 2140 | + } | |
| 2141 | + | |
| 2142 | + if (empty($scores)) { | |
| 2143 | + return null; | |
| 2144 | + } | |
| 2145 | + | |
| 2146 | + return [ | |
| 2147 | + 'average' => array_sum($scores) / count($scores), | |
| 2148 | + 'statuses' => $statuses, | |
| 2149 | + 'failing' => $failing, | |
| 2150 | + ]; | |
| 2151 | + } | |
| 2152 | + | |
| 2153 | + /** | |
| 1825 | 2154 | * Score internal linking - declining importance (1 point) |
| 1826 | 2155 | * |
| 1827 | 2156 | * @param array $content_data Content analysis data |
| 1828 | 2157 | * @return array Scoring result |
| @@ -1852,9 +2181,9 @@ | ||
| 1852 | 2181 | $suggestions = []; |
| 1853 | 2182 | |
| 1854 | 2183 | // Meta description check |
| 1855 | 2184 | $meta_desc = $metadata['description'] ?? ''; |
| 1856 | - if (!empty($meta_desc) && mb_strlen($meta_desc) >= 120 && mb_strlen($meta_desc) <= 160) { | |
| 2185 | + if (!empty($meta_desc) && mb_strlen($meta_desc) >= self::DESCRIPTION_OPTIMAL_MIN && mb_strlen($meta_desc) <= self::DESCRIPTION_OPTIMAL_MAX) { | |
| 1857 | 2186 | $score += 0.5; |
| 1858 | 2187 | } else { |
| 1859 | 2188 | $suggestions[] = 'Add a compelling meta description (120-160 characters)'; |
| 1860 | 2189 | } |