Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Limit title and meta description length based on SEO recommendations

givanz520d4f965115
diff --git a/app/controller/content/post.php b/app/controller/content/post.php
index dcfe9c9..01b8611 100755
--- a/app/controller/content/post.php
+++ b/app/controller/content/post.php
@@ -32,6 +32,7 @@ use Vvveb\System\Core\FrontController;
use Vvveb\System\Event;
use Vvveb\System\Locale;
use Vvveb\System\User\Admin;
+use function Vvveb\truncateWords;
class Post extends Base {
public $type = 'post';
@@ -52,6 +53,8 @@ class Post extends Base {
$slug = $this->request->get['slug'] ?? '';
$type = $this->request->get['type'] ?? null;
$created_at = $this->request->get['created_at'] ?? ''; //revision preview
+ $content = [];
+ $languageContent = [];
//check for custom post or product
if ($type) {
@@ -81,7 +84,6 @@ class Post extends Base {
$class = __NAMESPACE__ . '\\' . ucfirst($this->type); //__CLASS__ is always Post
list($content, $language, $slug) = Event :: trigger($class,__FUNCTION__, $content, $language, $slug);
- $languageContent = [];
if ($content) {
if (isset($content[$language])) {
@@ -139,8 +141,27 @@ class Post extends Base {
}
$languageContent['title'] = $languageContent['name'];
- if (isset($this->global['site']['description']['title'])) {
- $languageContent['title'] = $languageContent['title'] . ' - ' . $this->global['site']['description']['title'];
+
+ //make sure title and desc don't exceed recommended seo limits
+ $titleLen = strlen($languageContent['title']);
+ if ($titleLen > 70) {
+ $languageContent['title'] = truncateWords($languageContent['title'], 70);
+ } else {
+ if (isset($this->global['site']['description']['title']) &&
+ ($siteTitleLen = strlen($this->global['site']['description']['title'])) &&
+ ($titleLen + $siteTitleLen) < 70) {
+ $languageContent['title'] = $languageContent['title'] . ' - ' . $this->global['site']['description']['title'];
+ }
+ }
+
+ if ($languageContent['meta_description']) {
+ $metaLen = strlen($languageContent['meta_description']);
+ if ($metaLen > 70) {
+ $languageContent['meta_description'] = truncateWords($languageContent['meta_description'], 160);
+ }
+ } else {
+ $excerpt = ($languageContent['excerpt'] ?? '') ?: $languageContent['content'];
+ $languageContent['meta_description'] = truncateWords(strip_tags($excerpt), 160);
}
list($content, $languageContent, $language, $slug) = Event :: trigger($class, __FUNCTION__ . ':after', $content, $languageContent, $language, $slug);
@@ -169,11 +190,10 @@ class Post extends Base {
return $this->notFound(true, ['message' => $error, 'title' => $error]);
}
}
-
- $this->view->post = $languageContent;
- $this->view->content = $content;
}
+ $this->view->post = $languageContent;
+ $this->view->content = $content;
$this->view->default_comment_status = $this->global['site']['default_comment_status'] ?? false;
$this->view->anonymous_comments = $this->global['site']['anonymous_comments'] ?? false;
}
diff --git a/app/controller/product/product.php b/app/controller/product/product.php
index d89cad7..baa42be 100755
--- a/app/controller/product/product.php
+++ b/app/controller/product/product.php
@@ -32,6 +32,7 @@ use Vvveb\System\Core\FrontController;
use Vvveb\System\Event;
use Vvveb\System\Locale;
use Vvveb\System\User\Admin;
+use function Vvveb\truncateWords;
class Product extends Base {
public $type = 'product';
@@ -60,6 +61,8 @@ class Product extends Base {
$slug = $this->request->get['slug'] ?? '';
$type = $this->request->get['type'] ?? '';
$created_at = $this->request->get['created_at'] ?? ''; //revision preview
+ $content = [];
+ $languageContent = [];
//check for custom post or product
if ($type) {
@@ -89,7 +92,6 @@ class Product extends Base {
$class = __NAMESPACE__ . '\\' . ucfirst($this->type); //__CLASS__ is always Product
list($content, $language, $slug) = Event :: trigger($class,__FUNCTION__, $content, $language, $slug);
- $languageContent = [];
if ($content) {
if (isset($content[$language])) {
@@ -151,6 +153,28 @@ class Product extends Base {
$languageContent['title'] = $languageContent['title'] . ' - ' . $this->global['site']['description']['title'];
}
+ //make sure title and desc don't exceed recommended seo limits
+ $titleLen = strlen($languageContent['title']);
+ if ($titleLen > 70) {
+ $languageContent['title'] = truncateWords($languageContent['title'], 70);
+ } else {
+ if (isset($this->global['site']['description']['title']) &&
+ ($siteTitleLen = strlen($this->global['site']['description']['title'])) &&
+ ($titleLen + $siteTitleLen) < 70) {
+ $languageContent['title'] = $languageContent['title'] . ' - ' . $this->global['site']['description']['title'];
+ }
+ }
+
+ if ($languageContent['meta_description']) {
+ $metaLen = strlen($languageContent['meta_description']);
+ if ($metaLen > 70) {
+ $languageContent['meta_description'] = truncateWords($languageContent['meta_description'], 160);
+ }
+ } else {
+ $excerpt = ($languageContent['excerpt'] ?? '') ?: $languageContent['content'];
+ $languageContent['meta_description'] = truncateWords(strip_tags($excerpt), 160);
+ }
+
list($content, $languageContent, $language, $slug) = Event :: trigger($class, __FUNCTION__ . ':after', $content, $languageContent, $language, $slug);
} else {
//check for custom post or product
diff --git a/app/template/common.tpl b/app/template/common.tpl
index 4809b47..b6961cb 100755
--- a/app/template/common.tpl
+++ b/app/template/common.tpl
@@ -91,6 +91,6 @@ head > link[hreflang]|after = <?php
}
?>
-head > title = <?php echo htmlspecialchars($this->global['site']['description']['title'] ?? '@@__innerText__@@');?>
-head > meta[name="description"]|content = <?php echo htmlspecialchars($this->global['site']['description']['meta-description'] ?? '@@__content__@@');?>
-head > meta[name="keywords"]|content = <?php echo htmlspecialchars($this->global['site']['description']['meta-keywords'] ?? '@@__content__@@');?>
+head > title = <?php echo htmlspecialchars($this->global['site']['description']['title'] ?? '');?>
+head > meta[name="description"]|content = <?php echo htmlspecialchars($this->global['site']['description']['meta-description'] ?? '@@macro Escape("@@__content__@@")@@');?>
+head > meta[name="keywords"]|content = <?php echo htmlspecialchars($this->global['site']['description']['meta-keywords'] ?? '@@macro Escape("@@__content__@@")@@');?>
diff --git a/system/functions.php b/system/functions.php
index f4f20fd..e179bc9 100755
--- a/system/functions.php
+++ b/system/functions.php
@@ -1360,7 +1360,7 @@ function dd(...$variables) {
echo highlight_string("<?php\n" . var_export($variable, true), true);
}
- die(0);
+ die();
}
function encrypt($key, $value, $cipher = 'aes256', $digest = 'sha256', $tag = null, $aad = '', $tag_length = 16) {
@@ -1519,7 +1519,28 @@ function checkPhpSyntax($source) {
}
function truncateWords($text, $limit) {
- return preg_replace('/((\w+\W*){' . ($limit - 1) . '}(\w+))(.*)/m', '${1}', $text);
+ $parts = preg_split('/(\s+)/ms', substr($text, 0, $limit * 2), 0, PREG_SPLIT_DELIM_CAPTURE);
+ $partsCount = count($parts);
+
+ $length = 0;
+ $lastPart = 0;
+ for (; $lastPart < $partsCount; ++$lastPart) {
+ $partlen = strlen($parts[$lastPart]);
+ $length += $partlen;
+ if ($length >= $limit) {
+ //don't include less than two letter last word or spaces
+ $partlen = strlen($parts[$lastPart - 1]);
+ while (($partlen < 3) && $lastPart > 3) {
+ $lastPart--;
+ $partlen = strlen($parts[$lastPart - 1]);
+ }
+ break;
+ }
+ }
+
+ return implode(array_slice($parts, 0, $lastPart));
+
+ //return preg_replace('/((\w+\W*){' . ($limit - 1) . '}(\w+))(.*)/m', '${1}', $text);
}
/**