Revert "Revert "Removed obsolete trailing slashes from void elements""
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / MetaTagHandler.class.php
CommitLineData
d215cdb0
AE
1<?php
2namespace wcf\system;
3use wcf\util\StringUtil;
4
5/**
6 * Handles meta tags.
7 *
8 * @author Alexander Ebert
7d739af0 9 * @copyright 2001-2016 WoltLab GmbH
d215cdb0 10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
f4f05aa5 11 * @package com.woltlab.wcf
d215cdb0
AE
12 * @subpackage system.message
13 * @category Community Framework
14 */
15class MetaTagHandler extends SingletonFactory implements \Countable, \Iterator {
16 /**
17 * current iterator index
18 * @var integer
19 */
20 protected $index = 0;
21
22 /**
23 * list of index to object relation
7a23a706 24 * @var integer[]
d215cdb0
AE
25 */
26 protected $indexToObject = null;
27
d215cdb0
AE
28 /**
29 * list of meta tags
30 * @var array
31 */
058cbd6a 32 protected $objects = [];
d215cdb0
AE
33
34 /**
0fcfe5f6 35 * @inheritDoc
d215cdb0
AE
36 */
37 protected function init() {
d215cdb0
AE
38 // set default tags
39 $this->addTag('description', 'description', WCF::getLanguage()->get(META_DESCRIPTION));
40 $this->addTag('keywords', 'keywords', WCF::getLanguage()->get(META_KEYWORDS));
fa169278 41 $this->addTag('og:site_name', 'og:site_name', WCF::getLanguage()->get(PAGE_TITLE), true);
d215cdb0
AE
42 }
43
44 /**
45 * Adds or replaces a meta tag.
46 *
47 * @param string $identifier
48 * @param string $name
49 * @param string $value
50 * @param boolean $isProperty
51 */
52 public function addTag($identifier, $name, $value, $isProperty = false) {
a63af0ee
N
53 if (!isset($this->objects[$identifier])) {
54 $this->indexToObject[] = $identifier;
55 }
56
058cbd6a 57 $this->objects[$identifier] = [
d215cdb0
AE
58 'isProperty' => $isProperty,
59 'name' => $name,
60 'value' => $value
058cbd6a 61 ];
1299e654
AE
62
63 // replace description if Open Graph Protocol tag was given
64 if ($name == 'og:description') {
65 $this->objects['description']['value'] = $value;
37849b1a 66 }
d215cdb0
AE
67 }
68
69 /**
70 * Removes a meta tag.
71 *
72 * @param string $identifier
73 */
74 public function removeTag($identifier) {
75 if (isset($this->objects[$identifier])) {
76 unset($this->objects[$identifier]);
fa169278
AE
77
78 $this->indexToObject = array_keys($this->objects);
d215cdb0
AE
79 }
80 }
81
82 /**
0fcfe5f6 83 * @inheritDoc
d215cdb0
AE
84 */
85 public function count() {
86 return count($this->objects);
87 }
88
89 /**
0fcfe5f6 90 * @inheritDoc
d215cdb0
AE
91 */
92 public function current() {
93 $tag = $this->objects[$this->indexToObject[$this->index]];
94
e5f9b56c 95 return '<meta ' . ($tag['isProperty'] ? 'property' : 'name') . '="' . $tag['name'] . '" content="' . StringUtil::encodeHTML($tag['value']) . '">';
d215cdb0
AE
96 }
97
98 /**
99 * CAUTION: This methods does not return the current iterator index,
100 * rather than the object key which maps to that index.
59dc0db6 101 *
d215cdb0
AE
102 * @see \Iterator::key()
103 */
104 public function key() {
105 return $this->indexToObject[$this->index];
106 }
107
108 /**
0fcfe5f6 109 * @inheritDoc
d215cdb0
AE
110 */
111 public function next() {
112 ++$this->index;
113 }
114
115 /**
0fcfe5f6 116 * @inheritDoc
d215cdb0
AE
117 */
118 public function rewind() {
119 $this->index = 0;
120 }
121
122 /**
0fcfe5f6 123 * @inheritDoc
d215cdb0
AE
124 */
125 public function valid() {
126 return isset($this->indexToObject[$this->index]);
127 }
128}