1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SEOmatic plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* A turnkey SEO implementation for Craft CMS that is comprehensive, powerful, |
6
|
|
|
* and flexible |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com |
9
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace nystudio107\seomatic\services; |
13
|
|
|
|
14
|
|
|
use craft\helpers\ArrayHelper; |
15
|
|
|
use craft\helpers\Template as TemplateHelper; |
16
|
|
|
use nystudio107\seomatic\base\MetaService; |
17
|
|
|
use nystudio107\seomatic\models\MetaLink; |
18
|
|
|
use nystudio107\seomatic\models\MetaLinkContainer; |
19
|
|
|
use nystudio107\seomatic\Seomatic; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Link service for creating and accessing MetaLink objects in their meta container |
23
|
|
|
* An instance of the service is available via [[`Seomatic::$plugin->link`|`seomatic.link`]] |
24
|
|
|
* |
25
|
|
|
* @author nystudio107 |
26
|
|
|
* @package Seomatic |
27
|
|
|
* @since 3.0.0 |
28
|
|
|
*/ |
29
|
|
|
class Link extends MetaService |
30
|
|
|
{ |
31
|
|
|
// Constants |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
public const DEFAULT_TYPE = null; |
35
|
|
|
|
36
|
|
|
// Public Methods |
37
|
|
|
// ========================================================================= |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
/** |
43
|
|
|
* @param string $key |
44
|
|
|
* @param string $handle |
45
|
|
|
* |
46
|
|
|
* @return null|MetaLink |
47
|
|
|
*/ |
48
|
|
|
public function get(string $key, string $handle = self::GENERAL_HANDLE) |
49
|
|
|
{ |
50
|
|
|
/** @var MetaLink $metaItem */ |
51
|
|
|
$metaItem = Seomatic::$plugin->metaContainers->getMetaItemByKey($key, MetaLinkContainer::CONTAINER_TYPE); |
52
|
|
|
|
53
|
|
|
return $metaItem; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function create(array $config = [], $add = true): MetaLink |
60
|
|
|
{ |
61
|
|
|
$type = self::DEFAULT_TYPE; |
62
|
|
|
if (!empty($config['type'])) { |
63
|
|
|
$type = ArrayHelper::remove($config, 'type'); |
64
|
|
|
} |
65
|
|
|
$metaItem = MetaLink::create($type, $config); |
66
|
|
|
if ($add && $metaItem !== null) { |
67
|
|
|
$this->add($metaItem); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $metaItem; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function add($metaItem, string $handle = self::GENERAL_HANDLE) |
77
|
|
|
{ |
78
|
|
|
$key = MetaLinkContainer::CONTAINER_TYPE . $handle; |
79
|
|
|
Seomatic::$plugin->metaContainers->addToMetaContainer($metaItem, $key); |
80
|
|
|
|
81
|
|
|
/** @var MetaLink $metaItem */ |
82
|
|
|
return $metaItem; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
|
|
public function render() |
89
|
|
|
{ |
90
|
|
|
$key = MetaLinkContainer::CONTAINER_TYPE; |
91
|
|
|
|
92
|
|
|
return TemplateHelper::raw(Seomatic::$plugin->metaContainers->renderContainersByType($key)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $handle |
97
|
|
|
* |
98
|
|
|
* @return null|MetaLinkContainer |
99
|
|
|
*/ |
100
|
|
|
public function container(string $handle = self::GENERAL_HANDLE) |
101
|
|
|
{ |
102
|
|
|
$key = MetaLinkContainer::CONTAINER_TYPE . $handle; |
103
|
|
|
|
104
|
|
|
return Seomatic::$plugin->metaContainers->getMetaContainer($key); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|