Skip to content

Commit 89d593c

Browse files
committed
Fix issue with meta description and page builder content
1 parent 46b1e85 commit 89d593c

File tree

7 files changed

+305
-139
lines changed

7 files changed

+305
-139
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Api;
10+
11+
interface ShortContentExtractorInterface
12+
{
13+
/**
14+
* Retrieve short filtered content
15+
* @param string$content
16+
* @param mixed $len
17+
* @param mixed $endСharacters
18+
* @return string
19+
* @throws \Exception
20+
*/
21+
public function execute($content, $len = null, $endСharacters = null);
22+
23+
}

Model/Author.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magefan\Blog\Api\AuthorInterface;
1212
use Magento\Framework\Model\AbstractModel;
13+
use Magefan\Blog\Api\ShortContentExtractorInterface;
1314

1415
/**
1516
* Blog author model
@@ -22,6 +23,11 @@ class Author extends AbstractModel implements AuthorInterface
2223
*/
2324
protected $controllerName;
2425

26+
/**
27+
* @var ShortContentExtractorInterface
28+
*/
29+
protected $shortContentExtractor;
30+
2531
/**
2632
* Initialize dependencies.
2733
*
@@ -90,13 +96,15 @@ public function getMetaTitle()
9096
public function getMetaDescription()
9197
{
9298
$desc = $this->getData('meta_description');
99+
93100
if (!$desc) {
94-
$desc = $this->getData('content');
101+
$desc = $this->getShortContentExtractor()->execute($this->getData('content'));
102+
$desc = str_replace(['<p>', '</p>'], [' ', ''], $desc);
95103
}
96104

97105
$desc = strip_tags($desc);
98-
if (mb_strlen($desc) > 300) {
99-
$desc = mb_substr($desc, 0, 300);
106+
if (mb_strlen($desc) > 160) {
107+
$desc = mb_substr($desc, 0, 160);
100108
}
101109

102110
return trim($desc);
@@ -212,4 +220,17 @@ public function isActive()
212220
{
213221
return $this->getIsActive();
214222
}
223+
224+
/**
225+
* @return ShortContentExtractorInterface
226+
*/
227+
public function getShortContentExtractor()
228+
{
229+
if (null === $this->shortContentExtractor) {
230+
$this->shortContentExtractor = \Magento\Framework\App\ObjectManager::getInstance()
231+
->get(ShortContentExtractorInterface::class);
232+
}
233+
234+
return $this->shortContentExtractor;
235+
}
215236
}

Model/Category.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magefan\Blog\Model\Url;
1212
use Magento\Framework\DataObject\IdentityInterface;
13+
use Magefan\Blog\Api\ShortContentExtractorInterface;
1314

1415
/**
1516
* Category model
@@ -81,6 +82,11 @@ class Category extends \Magento\Framework\Model\AbstractModel implements Identit
8182
*/
8283
protected $controllerName;
8384

85+
/**
86+
* @var ShortContentExtractorInterface
87+
*/
88+
protected $shortContentExtractor;
89+
8490
/**
8591
* Initialize dependencies.
8692
*
@@ -389,7 +395,8 @@ public function getMetaDescription()
389395
{
390396
$desc = $this->getData('meta_description');
391397
if (!$desc) {
392-
$desc = $this->getData('content');
398+
$desc = $this->getShortContentExtractor()->execute($this->getData('content'));
399+
$desc = str_replace(['<p>', '</p>'], [' ', ''], $desc);
393400
}
394401

395402
$desc = strip_tags($desc);
@@ -548,4 +555,17 @@ public function duplicate()
548555

549556
return $object->save();
550557
}
558+
559+
/**
560+
* @return ShortContentExtractorInterface
561+
*/
562+
public function getShortContentExtractor()
563+
{
564+
if (null === $this->shortContentExtractor) {
565+
$this->shortContentExtractor = \Magento\Framework\App\ObjectManager::getInstance()
566+
->get(ShortContentExtractorInterface::class);
567+
}
568+
569+
return $this->shortContentExtractor;
570+
}
551571
}

Model/Post.php

+41-130
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magefan\Blog\Model\Url;
1212
use Magento\Store\Model\ScopeInterface;
13+
use Magefan\Blog\Api\ShortContentExtractorInterface;
1314

1415
/**
1516
* Post model
@@ -144,6 +145,11 @@ class Post extends \Magento\Framework\Model\AbstractModel implements \Magento\Fr
144145
*/
145146
protected $controllerName;
146147

148+
/**
149+
* @var ShortContentExtractorInterface
150+
*/
151+
protected $shortContentExtractor;
152+
147153
/**
148154
* Initialize dependencies.
149155
*
@@ -486,135 +492,18 @@ public function getShortFilteredContent($len = null, $endСharacters = null)
486492
$len = null;
487493
}
488494
/* End fix */
489-
495+
490496
$key = 'short_filtered_content' . $len;
491497
if (!$this->hasData($key)) {
492498

493-
$isPagebreakDefined = false;
494-
495499
if ($this->getShortContent()) {
496-
$content = $this->filterProvider->getPageFilter()->filter(
497-
(string) $this->getShortContent() ?: ''
498-
);
500+
$content = (string)$this->getShortContent() ?: '';
499501
} else {
500-
$content = $this->getFilteredContent();
501-
502-
if (!$len) {
503-
$pageBraker = '<!-- pagebreak -->';
504-
$len = mb_strpos($content, $pageBraker);
505-
if ($len) {
506-
$isPagebreakDefined = true;
507-
} else {
508-
$len = (int)$this->scopeConfig->getValue(
509-
'mfblog/post_list/shortcotent_length',
510-
ScopeInterface::SCOPE_STORE
511-
);
512-
}
513-
}
502+
//$content = $this->getFilteredContent();
503+
$content = (string)$this->getContent() ?: '';
514504
}
515505

516-
if ($len) {
517-
518-
if (!$isPagebreakDefined) {
519-
520-
$oLen = $len;
521-
/* Skip <style> tags at the begining of string in calculations */
522-
$sp1 = mb_strpos($content, '<style>');
523-
if (false !== $sp1) {
524-
$stylePattern = "~\<style(.*)\>(.*)\<\/style\>~";
525-
$cc = preg_replace($stylePattern, '', $content); /* remove style tag */
526-
$sp2 = mb_strpos($content, '</style>');
527-
528-
while (false !== $sp1 && false !== $sp2 && $sp1 < $sp2 && $sp2 > $len && $sp1 < $len) {
529-
$len = $oLen + $sp2 + 8;
530-
$sp1 = mb_strpos($content, '<style>', $sp2 + 1);
531-
$sp2 = mb_strpos($content, '</style>', $sp2 + 1);
532-
}
533-
534-
$l = mb_strlen($content);
535-
if ($len < $l) {
536-
$sp2 = mb_strrpos($content, '</style>', $len - $l);
537-
if ($len < $oLen + $sp2 + 8) {
538-
$len = $oLen + $sp2 + 8;
539-
}
540-
}
541-
542-
} else {
543-
$cc = $content;
544-
}
545-
546-
/* Skip long HTML */
547-
$stcc = trim(strip_tags($cc));
548-
//if ($stcc && strlen($stcc) < strlen($cc) / 3) {
549-
if ($stcc && $len < mb_strlen($content)) {
550-
$str = '';
551-
$start = false;
552-
foreach (explode(' ', $stcc) as $s) {
553-
$str .= ($str ? ' ' : '') . $s;
554-
555-
$pos = mb_strpos($content, $str);
556-
if (false !== $pos) {
557-
$start = $pos;
558-
} else {
559-
break;
560-
}
561-
}
562-
563-
if (false !== $start) {
564-
if ($len < $start + $oLen) {
565-
$len = $start + $oLen;
566-
}
567-
}
568-
}
569-
}
570-
571-
/* Do not cut words */
572-
while ($len < strlen($content)
573-
&& !in_array($content[$len], [' ', '<', "\t", "\r", "\n"])) {
574-
$len++;
575-
}
576-
577-
$content = mb_substr($content, 0, $len);
578-
try {
579-
$previousLoaderState = libxml_disable_entity_loader(true);
580-
$previousErrorState = libxml_use_internal_errors(true);
581-
$dom = new \DOMDocument();
582-
$dom->loadHTML('<?xml encoding="UTF-8">' . $content);
583-
libxml_disable_entity_loader($previousLoaderState);
584-
libxml_use_internal_errors($previousErrorState);
585-
586-
$body = $dom->getElementsByTagName('body');
587-
if ($body && $body->length > 0) {
588-
$body = $body->item(0);
589-
$_content = new \DOMDocument;
590-
foreach ($body->childNodes as $child) {
591-
$_content->appendChild($_content->importNode($child, true));
592-
}
593-
$content = $_content->saveHTML();
594-
}
595-
} catch (\Exception $e) {
596-
/* Do nothing, it's OK */
597-
}
598-
}
599-
600-
if ($endСharacters === null) {
601-
$endСharacters = $this->scopeConfig->getValue(
602-
'mfblog/post_list/end_characters',
603-
ScopeInterface::SCOPE_STORE
604-
);
605-
}
606-
607-
if ($len && $endСharacters) {
608-
$trimMask = " \t\n\r\0\x0B,.!?";
609-
if ($p = strrpos($content, '</')) {
610-
$content = trim(substr($content, 0, $p), $trimMask)
611-
. $endСharacters
612-
. substr($content, $p);
613-
} else {
614-
$content = trim($content, $trimMask)
615-
. $endСharacters;
616-
}
617-
}
506+
$content = $this->getShortContentExtractor()->execute($content, $len, $endСharacters);
618507

619508
$this->setData($key, $content);
620509
}
@@ -642,17 +531,26 @@ public function getMetaTitle()
642531
*/
643532
public function getMetaDescription()
644533
{
645-
$desc = $this->getData('meta_description');
646-
if (!$desc) {
647-
$desc = $this->getData('content');
648-
}
649534

650-
$desc = strip_tags($desc);
651-
if (mb_strlen($desc) > 300) {
652-
$desc = mb_substr($desc, 0, 300);
535+
$key = 'filtered_meta_description';
536+
if (!$this->hasData($key)) {
537+
$desc = $this->getData('meta_description');
538+
if (!$desc) {
539+
$desc = $this->getShortFilteredContent();
540+
$desc = str_replace(['<p>', '</p>'], [' ', ''], $desc);
541+
}
542+
543+
$desc = strip_tags($desc);
544+
if (mb_strlen($desc) > 160) {
545+
$desc = mb_substr($desc, 0, 160);
546+
}
547+
548+
$desc = trim($desc);
549+
550+
$this->setData($key, $desc);
653551
}
654552

655-
return trim($desc);
553+
return $this->getData($key);
656554
}
657555

658556
/**
@@ -1161,4 +1059,17 @@ public function getUpdatedAt()
11611059
{
11621060
return $this->getData('update_time');
11631061
}
1062+
1063+
/**
1064+
* @return ShortContentExtractorInterface
1065+
*/
1066+
public function getShortContentExtractor()
1067+
{
1068+
if (null === $this->shortContentExtractor) {
1069+
$this->shortContentExtractor = \Magento\Framework\App\ObjectManager::getInstance()
1070+
->get(ShortContentExtractorInterface::class);
1071+
}
1072+
1073+
return $this->shortContentExtractor;
1074+
}
11641075
}

0 commit comments

Comments
 (0)