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