5b6edc03500f180a03a8964796adb8908dc83e14
5 use wcf\util\StringUtil
;
10 * @author Alexander Ebert
11 * @copyright 2001-2019 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 final class MetaTagHandler
extends SingletonFactory
implements \Countable
, \Iterator
17 * current iterator index
19 protected int $index = 0;
22 * list of index to object relation
25 protected $indexToObject = [];
31 protected $objects = [];
36 protected function init(): void
39 if ($value = WCF
::getLanguage()->get(META_DESCRIPTION
)) {
40 $this->addTag('description', 'description', $value);
42 if ($value = WCF
::getLanguage()->get(PAGE_TITLE
)) {
43 $this->addTag('og:site_name', 'og:site_name', $value, true);
49 (\
preg_match('~^https?://~', OG_IMAGE
) ? OG_IMAGE
: WCF
::getPath() . OG_IMAGE
),
53 if (FB_SHARE_APP_ID
) {
54 $this->addTag('fb:app_id', 'fb:app_id', FB_SHARE_APP_ID
, true);
59 * Adds or replaces a meta tag.
61 public function addTag(string $identifier, string $name, string $value, bool $isProperty = false): void
63 if (!isset($this->objects
[$identifier])) {
64 $this->indexToObject
[] = $identifier;
67 $this->objects
[$identifier] = [
68 'isProperty' => $isProperty,
73 // replace description if Open Graph Protocol tag was given
74 if ($name == 'og:description' && $value) {
75 $this->addTag('description', 'description', $value);
82 public function removeTag(string $identifier): void
84 if (isset($this->objects
[$identifier])) {
85 unset($this->objects
[$identifier]);
87 $this->indexToObject
= \array_keys
($this->objects
);
94 public function count(): int
96 return \
count($this->objects
);
102 public function current(): string
104 $tag = $this->objects
[$this->indexToObject
[$this->index
]];
106 return '<meta ' . ($tag['isProperty'] ?
'property' : 'name') . '="' . $tag['name'] . '" content="' . StringUtil
::encodeHTML($tag['value']) . '">';
110 * CAUTION: This methods does not return the current iterator index,
111 * rather than the object key which maps to that index.
113 * @see \Iterator::key()
115 public function key(): string
117 return $this->indexToObject
[$this->index
];
123 public function next(): void
131 public function rewind(): void
139 public function valid(): bool
141 return isset($this->indexToObject
[$this->index
]);