Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / MetaTagHandler.class.php
1 <?php
2 namespace wcf\system;
3 use wcf\util\StringUtil;
4
5 /**
6 * Handles meta tags.
7 *
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
12 */
13 class MetaTagHandler extends SingletonFactory implements \Countable, \Iterator {
14 /**
15 * current iterator index
16 * @var integer
17 */
18 protected $index = 0;
19
20 /**
21 * list of index to object relation
22 * @var integer[]
23 */
24 protected $indexToObject = [];
25
26 /**
27 * list of meta tags
28 * @var array
29 */
30 protected $objects = [];
31
32 /**
33 * @inheritDoc
34 */
35 protected function init() {
36 // set default tags
37 if ($value = WCF::getLanguage()->get(META_DESCRIPTION)) {
38 $this->addTag('description', 'description', $value);
39 }
40 if ($value = WCF::getLanguage()->get(META_KEYWORDS)) {
41 $this->addTag('keywords', 'keywords', $value);
42 }
43 if ($value = WCF::getLanguage()->get(PAGE_TITLE)) {
44 $this->addTag('og:site_name', 'og:site_name', $value, true);
45 }
46 if (OG_IMAGE) {
47 $this->addTag('og:image', 'og:image', (preg_match('~^https?://~', OG_IMAGE) ? OG_IMAGE : WCF::getPath() . OG_IMAGE), true);
48 }
49 if (FB_SHARE_APP_ID) {
50 $this->addTag('fb:app_id', 'fb:app_id', FB_SHARE_APP_ID, true);
51 }
52 }
53
54 /**
55 * Adds or replaces a meta tag.
56 *
57 * @param string $identifier
58 * @param string $name
59 * @param string $value
60 * @param boolean $isProperty
61 */
62 public function addTag($identifier, $name, $value, $isProperty = false) {
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.
81 *
82 * @param string $identifier
83 */
84 public function removeTag($identifier) {
85 if (isset($this->objects[$identifier])) {
86 unset($this->objects[$identifier]);
87
88 $this->indexToObject = array_keys($this->objects);
89 }
90 }
91
92 /**
93 * @inheritDoc
94 */
95 public function count() {
96 return count($this->objects);
97 }
98
99 /**
100 * @inheritDoc
101 */
102 public function current() {
103 $tag = $this->objects[$this->indexToObject[$this->index]];
104
105 return '<meta ' . ($tag['isProperty'] ? 'property' : 'name') . '="' . $tag['name'] . '" content="' . StringUtil::encodeHTML($tag['value']) . '">';
106 }
107
108 /**
109 * CAUTION: This methods does not return the current iterator index,
110 * rather than the object key which maps to that index.
111 *
112 * @see \Iterator::key()
113 */
114 public function key() {
115 return $this->indexToObject[$this->index];
116 }
117
118 /**
119 * @inheritDoc
120 */
121 public function next() {
122 ++$this->index;
123 }
124
125 /**
126 * @inheritDoc
127 */
128 public function rewind() {
129 $this->index = 0;
130 }
131
132 /**
133 * @inheritDoc
134 */
135 public function valid() {
136 return isset($this->indexToObject[$this->index]);
137 }
138 }