2 declare(strict_types
=1);
4 use wcf\util\StringUtil
;
9 * @author Alexander Ebert
10 * @copyright 2001-2018 WoltLab GmbH
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12 * @package WoltLabSuite\Core\System\Message
14 class MetaTagHandler
extends SingletonFactory
implements \Countable
, \Iterator
{
16 * current iterator index
22 * list of index to object relation
25 protected $indexToObject = [];
31 protected $objects = [];
36 protected function init() {
38 if ($value = WCF
::getLanguage()->get(META_DESCRIPTION
)) {
39 $this->addTag('description', 'description', $value);
41 if ($value = WCF
::getLanguage()->get(META_KEYWORDS
)) {
42 $this->addTag('keywords', 'keywords', $value);
44 if ($value = WCF
::getLanguage()->get(PAGE_TITLE
)) {
45 $this->addTag('og:site_name', 'og:site_name', $value, true);
48 $this->addTag('og:image', 'og:image', (preg_match('~^https?://~', OG_IMAGE
) ? OG_IMAGE
: WCF
::getPath() . OG_IMAGE
), true);
50 if (FB_SHARE_APP_ID
) {
51 $this->addTag('fb:app_id', 'fb:app_id', FB_SHARE_APP_ID
, true);
56 * Adds or replaces a meta tag.
58 * @param string $identifier
60 * @param string $value
61 * @param boolean $isProperty
63 public function addTag($identifier, $name, $value, $isProperty = false) {
64 if (!isset($this->objects
[$identifier])) {
65 $this->indexToObject
[] = $identifier;
68 $this->objects
[$identifier] = [
69 'isProperty' => $isProperty,
74 // replace description if Open Graph Protocol tag was given
75 if ($name == 'og:description' && $value) {
76 $this->addTag('description', 'description', $value);
83 * @param string $identifier
85 public function removeTag($identifier) {
86 if (isset($this->objects
[$identifier])) {
87 unset($this->objects
[$identifier]);
89 $this->indexToObject
= array_keys($this->objects
);
96 public function count() {
97 return count($this->objects
);
103 public function current() {
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() {
116 return $this->indexToObject
[$this->index
];
122 public function next() {
129 public function rewind() {
136 public function valid() {
137 return isset($this->indexToObject
[$this->index
]);