-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCategoryOpengraphImage.php
81 lines (61 loc) · 1.93 KB
/
CategoryOpengraphImage.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
<?php
namespace MageSuite\Opengraph\DataProviders;
class CategoryOpengraphImage extends TagProvider implements TagProviderInterface
{
/**
* @var \Magento\Framework\Registry
*/
protected $registry;
/**
* @var \MageSuite\Opengraph\Factory\TagFactoryInterface
*/
protected $tagFactory;
/**
* @var \MageSuite\Opengraph\Helper\Mime
*/
protected $mimeHelper;
protected $tags = [];
public function __construct(
\Magento\Framework\Registry $registry,
\MageSuite\Opengraph\Factory\TagFactoryInterface $tagFactory,
\MageSuite\Opengraph\Helper\Mime $mimeHelper
) {
$this->registry = $registry;
$this->tagFactory = $tagFactory;
$this->mimeHelper = $mimeHelper;
}
public function getTags()
{
$category = $this->registry->registry('current_category');
if (!$category || !$category->getId()) {
return [];
}
$this->addImageTag();
return $this->tags;
}
private function addImageTag()
{
$category = $this->registry->registry('current_category');
$opengraphImage = $category->getOgImage() ?? null;
if (!$opengraphImage) {
return;
}
$imageUrl = $category->getImageUrl('og_image');
if (!$imageUrl) {
return;
}
$tag = $this->tagFactory->getTag('image', $imageUrl);
$this->addTag($tag);
$mimeType = $this->mimeHelper->getMimeType($imageUrl);
if ($mimeType) {
$tag = $this->tagFactory->getTag('image:type', $mimeType);
$this->addTag($tag);
}
$categoryData = array_filter($category->getData());
$title = $categoryData['og_title'] ?? $categoryData['meta_title'] ?? $categoryData['name'] ?? null;
if ($title) {
$tag = $this->tagFactory->getTag('image:alt', $title);
$this->addTag($tag);
}
}
}