Skip to content

Commit 5e51f29

Browse files
committed
Improve addthis sharing on posts list
1 parent 13dc043 commit 5e51f29

File tree

6 files changed

+97
-74
lines changed

6 files changed

+97
-74
lines changed

Block/Amp/Og/Post.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,9 @@ public function getPost()
5656
public function getImage()
5757
{
5858
$image = $this->getPost()->getOgImage();
59+
5960
if (!$image) {
60-
$image = $this->getPost()->getFeaturedImage();
61-
}
62-
if (!$image) {
63-
$content = $this->getContent();
64-
$match = null;
65-
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $match);
66-
if (!empty($match['src'])) {
67-
$image = $match['src'];
68-
}
61+
$image = $this->getPost()->getFirstImage();
6962
}
7063

7164
if ($image) {

Block/Post/AbstractPost.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ abstract class AbstractPost extends \Magento\Framework\View\Element\Template
1717
{
1818

1919
/**
20+
* Deprecated property. Do not use it.
2021
* @var \Magento\Cms\Model\Template\FilterProvider
2122
*/
2223
protected $_filterProvider;
@@ -98,28 +99,7 @@ public function getPost()
9899
*/
99100
public function getShorContent()
100101
{
101-
$content = $this->getContent();
102-
$pageBraker = '<!-- pagebreak -->';
103-
104-
if ($p = mb_strpos($content, $pageBraker)) {
105-
$content = mb_substr($content, 0, $p);
106-
try {
107-
libxml_use_internal_errors(true);
108-
$dom = new \DOMDocument();
109-
$dom->loadHTML('<?xml encoding="UTF-8">' . $content);
110-
$body = $dom->getElementsByTagName('body');
111-
if ( $body && $body->length > 0 ) {
112-
$body = $body->item(0);
113-
$_content = new \DOMDocument;
114-
foreach ($body->childNodes as $child){
115-
$_content->appendChild($_content->importNode($child, true));
116-
}
117-
$content = $_content->saveHTML();
118-
}
119-
} catch (\Exception $e) {}
120-
}
121-
122-
return $content;
102+
return $this->getPost()->getShortFilteredContent();
123103
}
124104

125105
/**
@@ -129,15 +109,7 @@ public function getShorContent()
129109
*/
130110
public function getContent()
131111
{
132-
$post = $this->getPost();
133-
$key = 'filtered_content';
134-
if (!$post->hasData($key)) {
135-
$cotent = $this->_filterProvider->getPageFilter()->filter(
136-
$post->getContent()
137-
);
138-
$post->setData($key, $cotent);
139-
}
140-
return $post->getData($key);
112+
return $this->getPost()->getFilteredContent();
141113
}
142114

143115
/**

Block/Post/View/Opengraph.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,9 @@ public function getPageUrl()
7070
public function getImage()
7171
{
7272
$image = $this->getPost()->getOgImage();
73+
7374
if (!$image) {
74-
$image = $this->getPost()->getFeaturedImage();
75-
}
76-
if (!$image) {
77-
$content = $this->getContent();
78-
$match = null;
79-
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $match);
80-
if (!empty($match['src'])) {
81-
$image = $match['src'];
82-
}
75+
$image = $this->getPost()->getFirstImage();
8376
}
8477

8578
if ($image) {

Block/Widget/Recent.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,29 +131,6 @@ public function getCategory()
131131
*/
132132
public function getShorContent($post)
133133
{
134-
$content = $this->_filterProvider->getPageFilter()->filter(
135-
$post->getContent()
136-
);
137-
$pageBraker = '<!-- pagebreak -->';
138-
139-
if ($p = mb_strpos($content, $pageBraker)) {
140-
$content = mb_substr($content, 0, $p);
141-
try {
142-
libxml_use_internal_errors(true);
143-
$dom = new \DOMDocument();
144-
$dom->loadHTML('<?xml encoding="UTF-8">' . $content);
145-
$body = $dom->getElementsByTagName('body');
146-
if ( $body && $body->length > 0 ) {
147-
$body = $body->item(0);
148-
$_content = new \DOMDocument;
149-
foreach ($body->childNodes as $child){
150-
$_content->appendChild($_content->importNode($child, true));
151-
}
152-
$content = $_content->saveHTML();
153-
}
154-
} catch (\Exception $e) {}
155-
}
156-
157-
return $content;
134+
return $post->getShortFilteredContent();
158135
}
159136
}

Model/Post.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class Post extends \Magento\Framework\Model\AbstractModel
5959
*/
6060
protected $_eventObject = 'blog_post';
6161

62+
/**
63+
* @var \Magento\Cms\Model\Template\FilterProvider
64+
*/
65+
protected $filterProvider;
66+
6267
/**
6368
* @var \Magefan\Blog\Model\Url
6469
*/
@@ -104,6 +109,7 @@ class Post extends \Magento\Framework\Model\AbstractModel
104109
*
105110
* @param \Magento\Framework\Model\Context $context
106111
* @param \Magento\Framework\Registry $registry
112+
* @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
107113
* @param \Magefan\Blog\Model\Url $url
108114
* @param \Magefan\Blog\Model\AuthorFactory $authorFactory
109115
* @param \Magefan\Blog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
@@ -116,6 +122,7 @@ class Post extends \Magento\Framework\Model\AbstractModel
116122
public function __construct(
117123
\Magento\Framework\Model\Context $context,
118124
\Magento\Framework\Registry $registry,
125+
\Magento\Cms\Model\Template\FilterProvider $filterProvider,
119126
Url $url,
120127
\Magefan\Blog\Model\AuthorFactory $authorFactory,
121128
\Magefan\Blog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
@@ -127,6 +134,7 @@ public function __construct(
127134
) {
128135
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
129136

137+
$this->filterProvider = $filterProvider;
130138
$this->_url = $url;
131139
$this->_authorFactory = $authorFactory;
132140
$this->_categoryCollectionFactory = $categoryCollectionFactory;
@@ -222,6 +230,81 @@ public function getFeaturedImage()
222230
return $this->getData('featured_image');
223231
}
224232

233+
/**
234+
* Retrieve first image url
235+
* @return string
236+
*/
237+
public function getFirstImage()
238+
{
239+
if (!$this->hasData('first_image')) {
240+
$image = $this->getFeaturedImage();
241+
if (!$image) {
242+
$content = $this->getFilteredContent();
243+
$match = null;
244+
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $match);
245+
if (!empty($match['src'])) {
246+
$image = $match['src'];
247+
}
248+
}
249+
$this->setData('first_image', $image);
250+
}
251+
252+
return $this->getData('first_image');
253+
}
254+
255+
/**
256+
* Retrieve filtered content
257+
*
258+
* @return string
259+
*/
260+
public function getFilteredContent()
261+
{
262+
$key = 'filtered_content';
263+
if (!$this->hasData($key)) {
264+
$content = $this->filterProvider->getPageFilter()->filter(
265+
$this->getContent()
266+
);
267+
$this->setData($key, $content);
268+
}
269+
return $this->getData($key);
270+
}
271+
272+
/**
273+
* Retrieve short filtered content
274+
*
275+
* @return string
276+
*/
277+
public function getShortFilteredContent()
278+
{
279+
$key = 'short_filtered_content';
280+
if (!$this->hasData($key)) {
281+
$content = $this->getFilteredContent();
282+
$pageBraker = '<!-- pagebreak -->';
283+
284+
if ($p = mb_strpos($content, $pageBraker)) {
285+
$content = mb_substr($content, 0, $p);
286+
try {
287+
libxml_use_internal_errors(true);
288+
$dom = new \DOMDocument();
289+
$dom->loadHTML('<?xml encoding="UTF-8">' . $content);
290+
$body = $dom->getElementsByTagName('body');
291+
if ( $body && $body->length > 0 ) {
292+
$body = $body->item(0);
293+
$_content = new \DOMDocument;
294+
foreach ($body->childNodes as $child){
295+
$_content->appendChild($_content->importNode($child, true));
296+
}
297+
$content = $_content->saveHTML();
298+
}
299+
} catch (\Exception $e) {}
300+
}
301+
302+
$this->setData($key, $content);
303+
}
304+
305+
return $this->getData($key);;
306+
}
307+
225308
/**
226309
* Retrieve meta title
227310
* @return string

view/frontend/templates/post/list/item.phtml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929
</a>
3030
</h2>
3131

32-
<div class="addthis_toolbox addthis_default_style" addthis:url="<?php echo $_postUrl ?>">
32+
<div class="addthis_toolbox addthis_default_style"
33+
addthis:url="<?php echo $_postUrl ?>"
34+
addthis:title="<?php echo $_postName; ?>"
35+
<?php if ($firstImage = $_post->getFirstImage()) { ?>
36+
addthis:media="<?php echo $firstImage ?>"
37+
<?php } ?>>
3338
<a class="addthis_button_facebook"></a>
3439
<a class="addthis_button_twitter"></a>
3540
<a class="addthis_button_email"></a>

0 commit comments

Comments
 (0)