Commit | Line | Data |
---|---|---|
d215cdb0 | 1 | <?php |
a9229942 | 2 | |
d215cdb0 | 3 | namespace wcf\system; |
a9229942 | 4 | |
d215cdb0 AE |
5 | use wcf\util\StringUtil; |
6 | ||
7 | /** | |
8 | * Handles meta tags. | |
a9229942 TD |
9 | * |
10 | * @author Alexander Ebert | |
11 | * @copyright 2001-2019 WoltLab GmbH | |
12 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
d215cdb0 | 13 | */ |
0c4a0aa6 | 14 | final class MetaTagHandler extends SingletonFactory implements \Countable, \Iterator |
a9229942 TD |
15 | { |
16 | /** | |
17 | * current iterator index | |
a9229942 | 18 | */ |
ce3b1557 | 19 | protected int $index = 0; |
a9229942 TD |
20 | |
21 | /** | |
22 | * list of index to object relation | |
23 | * @var int[] | |
24 | */ | |
25 | protected $indexToObject = []; | |
26 | ||
27 | /** | |
28 | * list of meta tags | |
29 | * @var array | |
30 | */ | |
31 | protected $objects = []; | |
32 | ||
33 | /** | |
34 | * @inheritDoc | |
35 | */ | |
ce3b1557 | 36 | protected function init(): void |
a9229942 TD |
37 | { |
38 | // set default tags | |
39 | if ($value = WCF::getLanguage()->get(META_DESCRIPTION)) { | |
40 | $this->addTag('description', 'description', $value); | |
41 | } | |
42 | if ($value = WCF::getLanguage()->get(PAGE_TITLE)) { | |
43 | $this->addTag('og:site_name', 'og:site_name', $value, true); | |
44 | } | |
45 | if (OG_IMAGE) { | |
46 | $this->addTag( | |
47 | 'og:image', | |
48 | 'og:image', | |
49 | (\preg_match('~^https?://~', OG_IMAGE) ? OG_IMAGE : WCF::getPath() . OG_IMAGE), | |
50 | true | |
51 | ); | |
52 | } | |
53 | if (FB_SHARE_APP_ID) { | |
54 | $this->addTag('fb:app_id', 'fb:app_id', FB_SHARE_APP_ID, true); | |
55 | } | |
56 | } | |
57 | ||
58 | /** | |
59 | * Adds or replaces a meta tag. | |
a9229942 | 60 | */ |
ce3b1557 | 61 | public function addTag(string $identifier, string $name, string $value, bool $isProperty = false): void |
a9229942 TD |
62 | { |
63 | if (!isset($this->objects[$identifier])) { | |
64 | $this->indexToObject[] = $identifier; | |
65 | } | |
66 | ||
67 | $this->objects[$identifier] = [ | |
68 | 'isProperty' => $isProperty, | |
69 | 'name' => $name, | |
70 | 'value' => $value, | |
71 | ]; | |
72 | ||
73 | // replace description if Open Graph Protocol tag was given | |
74 | if ($name == 'og:description' && $value) { | |
75 | $this->addTag('description', 'description', $value); | |
76 | } | |
77 | } | |
78 | ||
79 | /** | |
80 | * Removes a meta tag. | |
a9229942 | 81 | */ |
ce3b1557 | 82 | public function removeTag(string $identifier): void |
a9229942 TD |
83 | { |
84 | if (isset($this->objects[$identifier])) { | |
85 | unset($this->objects[$identifier]); | |
86 | ||
87 | $this->indexToObject = \array_keys($this->objects); | |
88 | } | |
89 | } | |
90 | ||
91 | /** | |
92 | * @inheritDoc | |
93 | */ | |
3c3f931d | 94 | public function count(): int |
a9229942 TD |
95 | { |
96 | return \count($this->objects); | |
97 | } | |
98 | ||
99 | /** | |
100 | * @inheritDoc | |
101 | */ | |
ce3b1557 | 102 | public function current(): string |
a9229942 TD |
103 | { |
104 | $tag = $this->objects[$this->indexToObject[$this->index]]; | |
105 | ||
106 | return '<meta ' . ($tag['isProperty'] ? 'property' : 'name') . '="' . $tag['name'] . '" content="' . StringUtil::encodeHTML($tag['value']) . '">'; | |
107 | } | |
108 | ||
109 | /** | |
110 | * CAUTION: This methods does not return the current iterator index, | |
111 | * rather than the object key which maps to that index. | |
112 | * | |
113 | * @see \Iterator::key() | |
114 | */ | |
ce3b1557 | 115 | public function key(): string |
a9229942 TD |
116 | { |
117 | return $this->indexToObject[$this->index]; | |
118 | } | |
119 | ||
120 | /** | |
121 | * @inheritDoc | |
122 | */ | |
ce3b1557 | 123 | public function next(): void |
a9229942 TD |
124 | { |
125 | $this->index++; | |
126 | } | |
127 | ||
128 | /** | |
129 | * @inheritDoc | |
130 | */ | |
ce3b1557 | 131 | public function rewind(): void |
a9229942 TD |
132 | { |
133 | $this->index = 0; | |
134 | } | |
135 | ||
136 | /** | |
137 | * @inheritDoc | |
138 | */ | |
ce3b1557 | 139 | public function valid(): bool |
a9229942 TD |
140 | { |
141 | return isset($this->indexToObject[$this->index]); | |
142 | } | |
d215cdb0 | 143 | } |