Skip to content

Commit d2ce78c

Browse files
committed
Add accelerated mobile pages for blog using Plumrocket Amp Extension for M2
1 parent 9215de8 commit d2ce78c

32 files changed

+1008
-58
lines changed

Block/Amp/Ldjson/Post.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
9+
namespace Magefan\Blog\Block\Amp\Ldjson;
10+
11+
/**
12+
* Blog post list ldJson block
13+
*/
14+
class Post extends \Magefan\Blog\Block\Post\View\Richsnippets
15+
{
16+
/**
17+
* Retrieve page structure structure data in JSON
18+
*
19+
* @return string
20+
*/
21+
public function getJson()
22+
{
23+
$json = parent::getOptions();
24+
return str_replace('\/', '/', json_encode($json));
25+
}
26+
}

Block/Amp/Ldjson/PostList.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
9+
namespace Magefan\Blog\Block\Amp\Ldjson;
10+
11+
/**
12+
* Blog post list ldJson block
13+
*/
14+
class PostList extends \Plumrocket\Amp\Block\Page\Head\Ldjson\Cms
15+
{
16+
/**
17+
* Retrieve page structure structure data in JSON
18+
*
19+
* @return string
20+
*/
21+
public function getJson()
22+
{
23+
$time = time();
24+
if (!$this->_cmsPage->getCreationTime()) {
25+
$this->_cmsPage->setCreationTime(
26+
date('Y-m-01 00:00:00', $time - 86400 * 150)
27+
);
28+
}
29+
30+
if (!$this->_cmsPage->getUpdateTime()) {
31+
$this->_cmsPage->setUpdateTime(
32+
date('Y-m-01 00:00:00', $time)
33+
);
34+
}
35+
36+
if (!$this->_cmsPage->getTitle()) {
37+
$this->_cmsPage->setTitle(
38+
$this->pageConfig->getTitle()->get()
39+
);
40+
}
41+
42+
return parent::getJson();
43+
}
44+
}

Block/Amp/Og/Post.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\Amp\Og;
9+
10+
use Magento\Store\Model\ScopeInterface;
11+
12+
/**
13+
* Blog post opengraph for amp
14+
*/
15+
class Post extends \Plumrocket\Amp\Block\Page\Head\Og\AbstractOg
16+
{
17+
/**
18+
* Retrieve open graph params
19+
*
20+
* @return array
21+
*/
22+
public function getOgParams()
23+
{
24+
$params = parent::getOgParams();
25+
$post = $this->getPost();
26+
27+
return array_merge($params, [
28+
'type' => $post->getOgType() ?: 'article',
29+
'url' => $this->_helper->getCanonicalUrl($post->getPostUrl()),
30+
'image' => (string)$post->getImageUrl(),
31+
]);
32+
}
33+
34+
/**
35+
* Retrieve current post
36+
*
37+
* @return \Magefan\Blog\Model\Post
38+
*/
39+
public function getPost()
40+
{
41+
return $this->_coreRegistry->registry('current_blog_post');
42+
}
43+
44+
/**
45+
* Retrieve page main image
46+
*
47+
* @return string | null
48+
*/
49+
public function getImage()
50+
{
51+
$image = $this->getPost()->getOgImage();
52+
if (!$image) {
53+
$image = $this->getPost()->getFeaturedImage();
54+
}
55+
if (!$image) {
56+
$content = $this->getContent();
57+
$match = null;
58+
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $match);
59+
if (!empty($match['src'])) {
60+
$image = $match['src'];
61+
}
62+
}
63+
64+
if ($image) {
65+
return $this->stripTags($image);
66+
}
67+
68+
}
69+
70+
}

Block/Post/View/Richsnippets.php

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function getOptions()
3030
$post = $this->getPost();
3131

3232
$logoBlock = $this->getLayout()->getBlock('logo');
33+
if (!$logoBlock) {
34+
$logoBlock = $this->getLayout()->getBlock('amp.logo');
35+
}
3336

3437
$this->_options = [
3538
'@context' => 'http://schema.org',

Helper/Image.php

+33-57
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,19 @@
1212
*/
1313
class Image extends AbstractHelper
1414
{
15-
protected $_width;
16-
protected $_height;
17-
1815
/**
1916
* Default quality value (for JPEG images only).
2017
*
2118
* @var int
2219
*/
2320
protected $_quality = 100;
24-
protected $_keepAspectRatio = false;
25-
protected $_keepFrame = false;
21+
protected $_keepAspectRatio = true;
22+
protected $_keepFrame = true;
2623
protected $_keepTransparency = true;
2724
protected $_constrainOnly = true;
2825
protected $_backgroundColor = [255, 255, 255];
2926
protected $_baseFile;
30-
protected $_isBaseFilePlaceholder;
3127
protected $_newFile;
32-
protected $_processor;
33-
protected $_destinationSubdir;
34-
protected $_angle;
35-
protected $_watermarkFile;
36-
protected $_watermarkPosition;
37-
protected $_watermarkWidth;
38-
protected $_watermarkHeight;
39-
protected $_watermarkImageOpacity = 0;
4028

4129
public function __construct(
4230
\Magento\Framework\App\Helper\Context $context,
@@ -57,64 +45,52 @@ public function init($baseFile)
5745
return $this;
5846
}
5947

60-
public function getImageProcessor()
48+
public function resize($width, $height = null)
6149
{
62-
//if (!$this->_processor) {
63-
$filename = $this->_baseFile ? $this->_mediaDirectory->getAbsolutePath($this->_baseFile) : null;
64-
$this->_processor = $this->_imageFactory->create($filename);
65-
//}
66-
$this->_processor->keepAspectRatio($this->_keepAspectRatio);
67-
$this->_processor->keepFrame($this->_keepFrame);
68-
$this->_processor->keepTransparency($this->_keepTransparency);
69-
$this->_processor->constrainOnly($this->_constrainOnly);
70-
$this->_processor->backgroundColor($this->_backgroundColor);
71-
$this->_processor->quality($this->_quality);
72-
$this->_processor->resize($this->_width, $this->_height);
73-
return $this->_processor;
74-
}
75-
76-
public function saveFile()
77-
{
78-
$filename = $this->_mediaDirectory->getAbsolutePath($this->_newFile);
79-
$this->getImageProcessor()->save($filename);
80-
//$this->_coreFileStorageDatabase->saveFile($filename);
50+
if ($this->_baseFile){
51+
$path = 'blog/cache/' . $width . 'x' . $height;
52+
$this->_newFile = $path. '/' . $this->_baseFile;
53+
if (true || !$this->fileExists($this->_newFile)) {
54+
$this->resizeBaseFile($width, $height);
55+
}
56+
}
8157
return $this;
8258
}
8359

84-
protected function _fileExists($filename)
60+
protected function resizeBaseFile($width, $height)
8561
{
86-
if ($this->_mediaDirectory->isFile($filename)) {
87-
return true;
88-
} else {
89-
return false;
62+
if (!$this->fileExists($this->_baseFile)) {
63+
$this->_baseFile = null;
64+
return $this;
9065
}
91-
}
9266

93-
public function isCached()
94-
{
95-
if (is_string($this->_newFile)) {
96-
return $this->_fileExists($this->_newFile);
97-
}
67+
$processor = $this->_imageFactory->create(
68+
$this->_mediaDirectory->getAbsolutePath($this->_baseFile)
69+
);
70+
$processor->keepAspectRatio($this->_keepAspectRatio);
71+
$processor->keepFrame($this->_keepFrame);
72+
$processor->keepTransparency($this->_keepTransparency);
73+
$processor->constrainOnly($this->_constrainOnly);
74+
$processor->backgroundColor($this->_backgroundColor);
75+
$processor->quality($this->_quality);
76+
$processor->resize($width, $height);
77+
78+
$newFile = $this->_mediaDirectory->getAbsolutePath($this->_newFile);
79+
$processor->save($newFile);
80+
unset($processor);
81+
82+
return $this;
9883
}
9984

100-
public function resize($width,$height = null)
85+
protected function fileExists($filename)
10186
{
102-
if($this->_baseFile){
103-
$this->_width = $width;
104-
$this->_height = $height;
105-
$path = 'blog/cache/'.$width.'x'.$height;
106-
$this->_newFile = $path. '/' . $this->_baseFile;
107-
if(!$this->isCached()){
108-
$this->saveFile();
109-
}
110-
}
111-
return $this;
87+
return $this->_mediaDirectory->isFile($filename);
11288
}
11389

11490
public function __toString()
11591
{
11692
$url = "";
117-
if($this->_baseFile){
93+
if ($this->_baseFile){
11894
$url = $this->_storeManager->getStore()->getBaseUrl(
11995
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
12096
) . $this->_newFile;
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
9+
namespace Magefan\Blog\Plugin\Plumrocket\Amp;
10+
11+
/**
12+
* Plugin for helper data (amp extension by Plumrocket)
13+
*/
14+
class HelperDataPlugin
15+
{
16+
/**
17+
* Add magefan blog actions to allowed list
18+
* @param \Plumrocket\Amp\Helper\Data $helper
19+
* @param array $allowedPages
20+
* @return array
21+
*/
22+
public function afterGetAllowedPages(\Plumrocket\Amp\Helper\Data $helper, $allowedPages)
23+
{
24+
foreach ($allowedPages as &$value) {
25+
if (strpos($value, 'magefan_blog_') === 0) {
26+
$value = str_replace('magefan_blog_', 'blog_', $value);
27+
}
28+
}
29+
30+
return $allowedPages;
31+
}
32+
33+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
9+
namespace Magefan\Blog\Plugin\Plumrocket\Amp;
10+
11+
/**
12+
* Plugin for source page (amp extension by Plumrocket)
13+
*/
14+
class SourcePagePlugin
15+
{
16+
/**
17+
* Add magefan blog pages to soruce
18+
* @param \Plumrocket\Amp\Model\System\Config\Source\Page $page
19+
* @param array $pages
20+
* @return array
21+
*/
22+
public function afterToArray(\Plumrocket\Amp\Model\System\Config\Source\Page $page, $pages)
23+
{
24+
$pages['magefan_blog_index_index'] = __('Blog Main Page');
25+
$pages['magefan_blog_post_view'] = __('Blog Post Pages');
26+
$pages['magefan_blog_category_view'] = __('Blog Category Pages');
27+
$pages['magefan_blog_category_view'] = __('Blog Category Pages');
28+
$pages['magefan_blog_archive_view'] = __('Blog Archive Pages');
29+
$pages['magefan_blog_author_view'] = __('Blog Author Pages');
30+
$pages['magefan_blog_tag_view'] = __('Blog Tag Pages');
31+
32+
return $pages;
33+
}
34+
35+
}

etc/di.xml

+9
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@
3333
<argument name="imageUploader" xsi:type="object">Magefan\Blog\ImageUpload</argument>
3434
</arguments>
3535
</type>
36+
<type name="Plumrocket\Amp\Helper\Data">
37+
<plugin name="plumrocket_amp_helper_data_magefan_blog"
38+
type="Magefan\Blog\Plugin\Plumrocket\Amp\HelperDataPlugin" sortOrder="10"/>
39+
</type>
40+
41+
<type name="Plumrocket\Amp\Model\System\Config\Source\Page">
42+
<plugin name="plumrocket_amp_source_page_magefan_blog"
43+
type="Magefan\Blog\Plugin\Plumrocket\Amp\SourcePagePlugin" sortOrder="10"/>
44+
</type>
3645
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 layout="pramp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
11+
<update handle="amp_blog_default"/>
12+
<update handle="amp_blog_post_list"/>
13+
</page>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 layout="pramp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
11+
<update handle="amp_blog_default"/>
12+
<update handle="amp_blog_post_list"/>
13+
</page>

0 commit comments

Comments
 (0)