static function getPageSEORating($page, $content) { $template = BigTreeCMS::getTemplate($page["template"]); $tsources = array(); $h1_field = ""; $body_fields = array(); if (is_array($template["resources"])) { foreach ($template["resources"] as $item) { if (isset($item["seo_body"]) && $item["seo_body"]) { $body_fields[] = $item["id"]; } if (isset($item["seo_h1"]) && $item["seo_h1"]) { $h1_field = $item["id"]; } $tsources[$item["id"]] = $item; } } if (!$h1_field && $tsources["page_header"]) { $h1_field = "page_header"; } if (!count($body_fields) && $tsources["page_content"]) { $body_fields[] = "page_content"; } $textStats = new TextStatistics(); $recommendations = array(); $score = 0; // Check if they have a page title. if ($page["title"]) { $score += 5; // They have a title, let's see if it's unique $r = sqlrows(sqlquery("SELECT * FROM bigtree_pages WHERE title = '" . sqlescape($page["title"]) . "' AND id != '" . sqlescape($page["id"]) . "'")); if ($r == 0) { // They have a unique title $score += 5; } else { $recommendations[] = "Your page title should be unique. " . ($r - 1) . " other page(s) have the same title."; } $words = $textStats->word_count($page["title"]); $length = mb_strlen($page["title"]); if ($words >= 4 && $length <= 72) { // Fits the bill! $score += 5; } else { $recommendations[] = "Your page title should be no more than 72 characters and should contain at least 4 words."; } } else { $recommendations[] = "You should enter a page title."; } // Check for meta description if ($page["meta_description"]) { $score += 5; // They have a meta description, let's see if it's no more than 165 characters. if (mb_strlen($page["meta_description"]) <= 165) { $score += 5; } else { $recommendations[] = "Your meta description should be no more than 165 characters. It is currently " . mb_strlen($page["meta_description"]) . " characters."; } } else { $recommendations[] = "You should enter a meta description."; } // Check for an H1 if (!$h1_field || $content[$h1_field]) { $score += 10; } else { $recommendations[] = "You should enter a page header."; } // Check the content! if (!count($body_fields)) { // If this template doesn't for some reason have a seo body resource, give the benefit of the doubt. $score += 65; } else { $regular_text = ""; $stripped_text = ""; foreach ($body_fields as $field) { if (!is_array($content[$field])) { $regular_text .= $content[$field] . " "; $stripped_text .= strip_tags($content[$field]) . " "; } } // Check to see if there is any content if ($stripped_text) { $score += 5; $words = $textStats->word_count($stripped_text); $readability = $textStats->flesch_kincaid_reading_ease($stripped_text); if ($readability < 0) { $readability = 0; } $number_of_links = substr_count($regular_text, "<a "); $number_of_external_links = substr_count($regular_text, 'href="http://'); // See if there are at least 300 words. if ($words >= 300) { $score += 15; } else { $recommendations[] = "You should enter at least 300 words of page content. You currently have " . $words . " word(s)."; } // See if we have any links if ($number_of_links) { $score += 5; // See if we have at least one link per 120 words. if (floor($words / 120) <= $number_of_links) { $score += 5; } else { $recommendations[] = "You should have at least one link for every 120 words of page content. You currently have {$number_of_links} link(s). You should have at least " . floor($words / 120) . "."; } // See if we have any external links. if ($number_of_external_links) { $score += 5; } else { $recommendations[] = "Having an external link helps build Page Rank."; } } else { $recommendations[] = "You should have at least one link in your content."; } // Check on our readability score. if ($readability >= 90) { $score += 20; } else { $read_score = round($readability / 90, 2); $recommendations[] = "Your readability score is " . $read_score * 100 . "%. Using shorter sentences and words with fewer syllables will make your site easier to read by search engines and users."; $score += ceil($read_score * 20); } } else { $recommendations[] = "You should enter page content."; } // Check page freshness $updated = strtotime($page["updated_at"]); $age = time() - $updated - 60 * 24 * 60 * 60; // See how much older it is than 2 months. if ($age > 0) { $age_score = 10 - floor(2 * ($age / (30 * 24 * 60 * 60))); if ($age_score < 0) { $age_score = 0; } $score += $age_score; $recommendations[] = "Your content is around " . ceil(2 + $age / (30 * 24 * 60 * 60)) . " months old. Updating your page more frequently will make it rank higher."; } else { $score += 10; } } $color = "#008000"; if ($score <= 50) { $color = BigTree::colorMesh("#CCAC00", "#FF0000", 100 - 100 * $score / 50); } elseif ($score <= 80) { $color = BigTree::colorMesh("#008000", "#CCAC00", 100 - 100 * ($score - 50) / 30); } return array("score" => $score, "recommendations" => $recommendations, "color" => $color); }