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\Template as TemplateHelper; |
15
|
|
|
use nystudio107\seomatic\base\MetaService; |
16
|
|
|
use nystudio107\seomatic\models\MetaScript; |
17
|
|
|
use nystudio107\seomatic\models\MetaScriptContainer; |
18
|
|
|
use nystudio107\seomatic\Seomatic; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Script service for creating and accessing MetaScript objects in their meta container |
22
|
|
|
* An instance of the service is available via [[`Seomatic::$plugin->script`|`seomatic.script`]] |
23
|
|
|
* |
24
|
|
|
* @author nystudio107 |
25
|
|
|
* @package Seomatic |
26
|
|
|
* @since 3.0.0 |
27
|
|
|
*/ |
28
|
|
|
class Script extends MetaService |
29
|
|
|
{ |
30
|
|
|
// Constants |
31
|
|
|
// ========================================================================= |
32
|
|
|
|
33
|
|
|
// Public Methods |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $key |
38
|
|
|
* @param string $handle |
39
|
|
|
* |
40
|
|
|
* @return null|MetaScript |
41
|
|
|
*/ |
42
|
|
|
public function get(string $key, string $handle = self::GENERAL_HANDLE) |
43
|
|
|
{ |
44
|
|
|
/** @var MetaScript $metaItem */ |
45
|
|
|
$metaItem = Seomatic::$plugin->metaContainers->getMetaItemByKey($key, MetaScriptContainer::CONTAINER_TYPE); |
46
|
|
|
|
47
|
|
|
return $metaItem; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function create(array $config = [], $add = true): MetaScript |
54
|
|
|
{ |
55
|
|
|
$metaItem = MetaScript::create($config); |
56
|
|
|
if ($add && $metaItem !== null) { |
57
|
|
|
$this->add($metaItem); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $metaItem; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
|
|
public function add($metaItem, string $handle = self::GENERAL_HANDLE) |
67
|
|
|
{ |
68
|
|
|
$key = MetaScriptContainer::CONTAINER_TYPE . $handle; |
69
|
|
|
Seomatic::$plugin->metaContainers->addToMetaContainer($metaItem, $key); |
70
|
|
|
|
71
|
|
|
/** @var MetaScript $metaItem */ |
72
|
|
|
return $metaItem; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function render() |
79
|
|
|
{ |
80
|
|
|
$key = MetaScriptContainer::CONTAINER_TYPE; |
81
|
|
|
|
82
|
|
|
return TemplateHelper::raw(Seomatic::$plugin->metaContainers->renderContainersByType($key)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $handle |
87
|
|
|
* |
88
|
|
|
* @return null|MetaScriptContainer |
89
|
|
|
*/ |
90
|
|
|
public function container(string $handle = self::GENERAL_HANDLE) |
91
|
|
|
{ |
92
|
|
|
$key = MetaScriptContainer::CONTAINER_TYPE . $handle; |
93
|
|
|
|
94
|
|
|
return Seomatic::$plugin->metaContainers->getMetaContainer($key); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|