|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SEOmatic plugin for Craft CMS 3.x |
|
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 nystudio107\seomatic\Seomatic; |
|
15
|
|
|
use nystudio107\seomatic\base\SeoElementInterface; |
|
16
|
|
|
use nystudio107\seomatic\seoelements\SeoCategory; |
|
17
|
|
|
use nystudio107\seomatic\seoelements\SeoEntry; |
|
18
|
|
|
use nystudio107\seomatic\seoelements\SeoProduct; |
|
19
|
|
|
|
|
20
|
|
|
use Craft; |
|
21
|
|
|
use craft\base\Component; |
|
22
|
|
|
use craft\events\RegisterComponentTypesEvent; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
26
|
|
|
* @package Seomatic |
|
27
|
|
|
* @since 3.0.0 |
|
|
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
|
|
29
|
|
|
class SeoElements extends Component |
|
30
|
|
|
{ |
|
31
|
|
|
// Constants |
|
32
|
|
|
// ========================================================================= |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @event RegisterComponentTypesEvent The event that is triggered when |
|
36
|
|
|
* registering SeoElement types |
|
37
|
|
|
* |
|
38
|
|
|
* SeoElement types must implement [[SeoElementInterface]] |
|
39
|
|
|
* |
|
40
|
|
|
* ```php |
|
41
|
|
|
* use nystudio107\seomatic\services\SeoElements; |
|
42
|
|
|
* use craft\events\RegisterComponentTypesEvent; |
|
43
|
|
|
* use yii\base\Event; |
|
44
|
|
|
* |
|
45
|
|
|
* Event::on(SeoElements::class, |
|
46
|
|
|
* SeoElements::EVENT_REGISTER_SEO_ELEMENT_TYPES, |
|
47
|
|
|
* function(RegisterComponentTypesEvent $event) { |
|
48
|
|
|
* $event->types[] = MySeoElement::class; |
|
49
|
|
|
* } |
|
50
|
|
|
* ); |
|
51
|
|
|
* ``` |
|
52
|
|
|
*/ |
|
53
|
|
|
const EVENT_REGISTER_SEO_ELEMENT_TYPES = 'registerSeoElementTypes'; |
|
54
|
|
|
|
|
55
|
|
|
const DEFAULT_SEO_ELEMENT_TYPES = [ |
|
56
|
|
|
SeoCategory::class, |
|
57
|
|
|
SeoEntry::class, |
|
58
|
|
|
SeoProduct::class, |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
// Protected Properties |
|
62
|
|
|
// ========================================================================= |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var SeoElementInterface[] indexed by [sourceType] |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $seoElements; |
|
68
|
|
|
|
|
69
|
|
|
// Public Methods |
|
70
|
|
|
// ========================================================================= |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @inheritDoc |
|
74
|
|
|
*/ |
|
75
|
|
|
public function init() |
|
76
|
|
|
{ |
|
77
|
|
|
parent::init(); |
|
78
|
|
|
$this->getAllSeoElementTypes(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $type |
|
|
|
|
|
|
83
|
|
|
* |
|
84
|
|
|
* @return SeoElementInterface|null |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getSeoElementByMetaBundleType(string $type) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->seoElements[$type] ?? null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns all available field type classes. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string[] The available field type classes |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getAllSeoElementTypes(): array |
|
97
|
|
|
{ |
|
98
|
|
|
// Return the memoized version if available |
|
99
|
|
|
if (!empty($this->seoElements)) { |
|
100
|
|
|
return $this->seoElements; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
// Merge in the built-in types with the types defined in the config.php |
|
103
|
|
|
$seoElementTypes = array_unique(array_merge( |
|
104
|
|
|
SeoMatic::$plugin->getSettings()->defaultSeoElementTypes ?? [], |
|
105
|
|
|
self::DEFAULT_SEO_ELEMENT_TYPES |
|
106
|
|
|
), SORT_REGULAR); |
|
107
|
|
|
// Throw an event to allow other modules/plugins to define their own types |
|
108
|
|
|
$event = new RegisterComponentTypesEvent([ |
|
109
|
|
|
'types' => $seoElementTypes, |
|
110
|
|
|
]); |
|
111
|
|
|
$this->trigger(self::EVENT_REGISTER_SEO_ELEMENT_TYPES, $event); |
|
112
|
|
|
// Index the array by META_BUNDLE_TYPE |
|
113
|
|
|
/** @var SeoElementInterface $seoElement */ |
|
|
|
|
|
|
114
|
|
|
foreach ($event->types as $seoElement) { |
|
115
|
|
|
$requiredPlugin = $seoElement::getRequiredPluginHandle(); |
|
116
|
|
|
if ($requiredPlugin === null || Craft::$app->getPlugins()->getPlugin($requiredPlugin)) { |
|
117
|
|
|
$this->seoElements[$seoElement::getMetaBundleType()] = $seoElement; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->seoElements; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|