|
10 | 10 |
|
11 | 11 | use Magefan\Blog\Model\Url;
|
12 | 12 | use Magento\Store\Model\ScopeInterface;
|
| 13 | +use Magefan\Blog\Api\ShortContentExtractorInterface; |
13 | 14 |
|
14 | 15 | /**
|
15 | 16 | * Post model
|
@@ -144,6 +145,11 @@ class Post extends \Magento\Framework\Model\AbstractModel implements \Magento\Fr
|
144 | 145 | */
|
145 | 146 | protected $controllerName;
|
146 | 147 |
|
| 148 | + /** |
| 149 | + * @var ShortContentExtractorInterface |
| 150 | + */ |
| 151 | + protected $shortContentExtractor; |
| 152 | + |
147 | 153 | /**
|
148 | 154 | * Initialize dependencies.
|
149 | 155 | *
|
@@ -486,135 +492,18 @@ public function getShortFilteredContent($len = null, $endСharacters = null)
|
486 | 492 | $len = null;
|
487 | 493 | }
|
488 | 494 | /* End fix */
|
489 |
| - |
| 495 | + |
490 | 496 | $key = 'short_filtered_content' . $len;
|
491 | 497 | if (!$this->hasData($key)) {
|
492 | 498 |
|
493 |
| - $isPagebreakDefined = false; |
494 |
| - |
495 | 499 | if ($this->getShortContent()) {
|
496 |
| - $content = $this->filterProvider->getPageFilter()->filter( |
497 |
| - (string) $this->getShortContent() ?: '' |
498 |
| - ); |
| 500 | + $content = (string)$this->getShortContent() ?: ''; |
499 | 501 | } 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() ?: ''; |
514 | 504 | }
|
515 | 505 |
|
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); |
618 | 507 |
|
619 | 508 | $this->setData($key, $content);
|
620 | 509 | }
|
@@ -642,17 +531,26 @@ public function getMetaTitle()
|
642 | 531 | */
|
643 | 532 | public function getMetaDescription()
|
644 | 533 | {
|
645 |
| - $desc = $this->getData('meta_description'); |
646 |
| - if (!$desc) { |
647 |
| - $desc = $this->getData('content'); |
648 |
| - } |
649 | 534 |
|
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); |
653 | 551 | }
|
654 | 552 |
|
655 |
| - return trim($desc); |
| 553 | + return $this->getData($key); |
656 | 554 | }
|
657 | 555 |
|
658 | 556 | /**
|
@@ -1161,4 +1059,17 @@ public function getUpdatedAt()
|
1161 | 1059 | {
|
1162 | 1060 | return $this->getData('update_time');
|
1163 | 1061 | }
|
| 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 | + } |
1164 | 1075 | }
|
0 commit comments