File tree Expand file tree Collapse file tree 4 files changed +95
-0
lines changed Expand file tree Collapse file tree 4 files changed +95
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * Copyright © 2016 Ihor Vansach ([email protected] ). All rights reserved.
4
+ * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5
+ *
6
+ * Glory to Ukraine! Glory to the heroes!
7
+ */
8
+ namespace Magefan \Blog \Block \Post \View ;
9
+
10
+ use Magento \Store \Model \ScopeInterface ;
11
+
12
+ /**
13
+ * Blog post view rich snippets
14
+ */
15
+ class Richsnippets extends \Magefan \Blog \Block \Post \AbstractPost
16
+ {
17
+ /**
18
+ * @param array
19
+ */
20
+ protected $ _options ;
21
+
22
+ /**
23
+ * Retrieve snipet params
24
+ *
25
+ * @return array
26
+ */
27
+ public function getOptions ()
28
+ {
29
+ if ($ this ->_options === null ) {
30
+ $ post = $ this ->getPost ();
31
+ $ this ->_options = array (
32
+ '@context ' => 'http://schema.org ' ,
33
+ '@type ' => 'BlogPosting ' ,
34
+ '@id ' => $ post ->getPostUrl (),
35
+ 'headline ' => $ post ->getTitle (),
36
+ 'description ' => $ post ->getMetaDescription (),
37
+ 'datePublished ' => $ post ->getPublishDate ('c ' ),
38
+ );
39
+ }
40
+
41
+ return $ this ->_options ;
42
+ }
43
+
44
+ /**
45
+ * Render html output
46
+ *
47
+ * @return string
48
+ */
49
+ protected function _toHtml ()
50
+ {
51
+ return '<script type="application/ld+json"> '
52
+ . json_encode ($ this ->getOptions ())
53
+ . '</script> ' ;
54
+ }
55
+ }
Original file line number Diff line number Diff line change @@ -191,6 +191,10 @@ public function getPostUrl()
191
191
return $ this ->_url ->getUrl ($ this , URL ::CONTROLLER_POST );
192
192
}
193
193
194
+ /**
195
+ * Retrieve featured image url
196
+ * @return string
197
+ */
194
198
public function getFeaturedImage ()
195
199
{
196
200
if (!$ this ->hasData ('featured_image ' )) {
@@ -205,6 +209,25 @@ public function getFeaturedImage()
205
209
return $ this ->getData ('featured_image ' );
206
210
}
207
211
212
+ /**
213
+ * Retrieve meta description
214
+ * @return string
215
+ */
216
+ public function getMetaDescription ()
217
+ {
218
+ $ desc = $ this ->getData ('meta_description ' );
219
+ if (!$ desc ) {
220
+ $ desc = $ this ->getData ('content ' );
221
+ }
222
+
223
+ $ desc = strip_tags ($ desc );
224
+ if (mb_strlen ($ desc ) > 160 ) {
225
+ $ desc = mb_substr ($ desc , 160 );
226
+ }
227
+
228
+ return $ desc ;
229
+ }
230
+
208
231
/**
209
232
* Retrieve post parent categories
210
233
* @return \Magefan\Blog\Model\ResourceModel\Category\Collection
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" ?>
2
+ <!--
3
+ /**
4
+ * Copyright © 2016 Ihor Vansach ([email protected] ). All rights reserved.
5
+ * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
6
+ *
7
+ * Glory to Ukraine! Glory to the heroes!
8
+ */
9
+ -->
10
+ <page xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" xsi : noNamespaceSchemaLocation =" urn:magento:framework:View/Layout/etc/page_configuration.xsd" >
11
+ <body >
12
+ <referenceBlock name =" head.additional" >
13
+ <block class =" Magefan\Blog\Block\Post\View\Richsnippets" name =" blog.post.richsnippets" />
14
+ </referenceBlock >
15
+ </body >
16
+ </page >
Original file line number Diff line number Diff line change 10
10
<page xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" xsi : noNamespaceSchemaLocation =" urn:magento:framework:View/Layout/etc/page_configuration.xsd" >
11
11
<update handle =" blog_default" />
12
12
<update handle =" blog_post_opengraph" />
13
+ <update handle =" blog_post_richsnippets" />
13
14
<body >
14
15
<referenceContainer name =" content" >
15
16
<block class =" Magefan\Blog\Block\Post\View" name =" blog.post" template =" post/view.phtml" >
You can’t perform that action at this time.
0 commit comments