3 use wcf\util\StringUtil
;
8 * @author Alexander Ebert
9 * @copyright 2001-2018 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\System\Message
13 class MetaTagHandler
extends SingletonFactory
implements \Countable
, \Iterator
{
15 * current iterator index
21 * list of index to object relation
24 protected $indexToObject = [];
30 protected $objects = [];
35 protected function init() {
37 if ($value = WCF
::getLanguage()->get(META_DESCRIPTION
)) {
38 $this->addTag('description', 'description', $value);
40 if ($value = WCF
::getLanguage()->get(META_KEYWORDS
)) {
41 $this->addTag('keywords', 'keywords', $value);
43 if ($value = WCF
::getLanguage()->get(PAGE_TITLE
)) {
44 $this->addTag('og:site_name', 'og:site_name', $value, true);
47 $this->addTag('og:image', 'og:image', (preg_match('~^https?://~', OG_IMAGE
) ? OG_IMAGE
: WCF
::getPath() . OG_IMAGE
), true);
49 if (FB_SHARE_APP_ID
) {
50 $this->addTag('fb:app_id', 'fb:app_id', FB_SHARE_APP_ID
, true);
55 * Adds or replaces a meta tag.
57 * @param string $identifier
59 * @param string $value
60 * @param boolean $isProperty
62 public function addTag($identifier, $name, $value, $isProperty = false) {
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 * @param string $identifier
84 public function removeTag($identifier) {
85 if (isset($this->objects
[$identifier])) {
86 unset($this->objects
[$identifier]);
88 $this->indexToObject
= array_keys($this->objects
);
95 public function count() {
96 return count($this->objects
);
102 public function current() {
103 $tag = $this->objects
[$this->indexToObject
[$this->index
]];
105 return '<meta ' . ($tag['isProperty'] ?
'property' : 'name') . '="' . $tag['name'] . '" content="' . StringUtil
::encodeHTML($tag['value']) . '">';
109 * CAUTION: This methods does not return the current iterator index,
110 * rather than the object key which maps to that index.
112 * @see \Iterator::key()
114 public function key() {
115 return $this->indexToObject
[$this->index
];
121 public function next() {
128 public function rewind() {
135 public function valid() {
136 return isset($this->indexToObject
[$this->index
]);