-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCms.php
111 lines (85 loc) · 2.72 KB
/
Cms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace MageSuite\Opengraph\DataProviders;
class Cms extends TagProvider implements TagProviderInterface
{
const DEFAULT_TYPE = 'article';
const HOMEPAGE_TYPE = 'website';
/**
* @var \Magento\Cms\Api\Data\PageInterface
*/
protected $page;
/**
* @var \Magento\Cms\Api\PageRepositoryInterface
*/
protected $pageRepository;
/**
* @var \Magento\Framework\App\RequestInterface
*/
protected $request;
/**
* @var \MageSuite\Opengraph\Helper\Data
*/
protected $dataHelper;
/**
* @var \MageSuite\Opengraph\Factory\TagFactoryInterface
*/
protected $tagFactory;
protected $tags = [];
public function __construct(
\Magento\Cms\Api\Data\PageInterface $page,
\Magento\Cms\Api\PageRepositoryInterface $pageRepository,
\Magento\Framework\App\RequestInterface $request,
\MageSuite\Opengraph\Helper\Data $dataHelper,
\MageSuite\Opengraph\Factory\TagFactoryInterface $tagFactory
) {
$this->page = $page;
$this->pageRepository = $pageRepository;
$this->request = $request;
$this->dataHelper = $dataHelper;
$this->tagFactory = $tagFactory;
}
public function getTags()
{
if (!$this->page->getIdentifier() && !$this->request->getParam('page_id')) {
return [];
}
$pageData = $this->page->getData();
if (empty($pageData)) {
$page = $this->pageRepository->getById($this->request->getParam('page_id'));
$pageData = $page->getData();
}
$pageData = array_filter($pageData);
$this->addTitleTag($pageData);
$this->addDescriptionTag($pageData);
$this->addTypeTag($pageData);
return $this->tags;
}
private function addTitleTag($pageData)
{
$title = $pageData['og_title'] ?? $pageData['meta_title'] ?? $pageData['title'] ?? null;
if (!$title) {
return;
}
$tag = $this->tagFactory->getTag('title', $title);
$this->addTag($tag);
}
private function addDescriptionTag($pageData)
{
$description = $pageData['og_description'] ?? $pageData['meta_description'] ?? null;
if (!$description) {
return;
}
$tag = $this->tagFactory->getTag('description', $description);
$this->addTag($tag);
}
private function addTypeTag($pageData)
{
$type = $pageData['og_type'] ?? null;
if (!$type) {
$isHomepage = $this->dataHelper->isHomePage();
$type = $isHomepage ? self::HOMEPAGE_TYPE : self::DEFAULT_TYPE;
}
$tag = $this->tagFactory->getTag('type', $type);
$this->addTag($tag);
}
}