Commit | Line | Data |
---|---|---|
d215cdb0 AE |
1 | <?php |
2 | namespace wcf\system; | |
3 | use wcf\util\StringUtil; | |
4 | ||
5 | /** | |
6 | * Handles meta tags. | |
7 | * | |
8 | * @author Alexander Ebert | |
c839bd49 | 9 | * @copyright 2001-2018 WoltLab GmbH |
d215cdb0 | 10 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
e71525e4 | 11 | * @package WoltLabSuite\Core\System\Message |
d215cdb0 AE |
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 | |
7a23a706 | 22 | * @var integer[] |
d215cdb0 | 23 | */ |
ebaf5f56 | 24 | protected $indexToObject = []; |
d215cdb0 | 25 | |
d215cdb0 AE |
26 | /** |
27 | * list of meta tags | |
28 | * @var array | |
29 | */ | |
058cbd6a | 30 | protected $objects = []; |
d215cdb0 AE |
31 | |
32 | /** | |
0fcfe5f6 | 33 | * @inheritDoc |
d215cdb0 AE |
34 | */ |
35 | protected function init() { | |
d215cdb0 | 36 | // set default tags |
ebaf5f56 MW |
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); | |
c5dd767e | 45 | } |
79c3d80a MW |
46 | if (OG_IMAGE) { |
47 | $this->addTag('og:image', 'og:image', (preg_match('~^https?://~', OG_IMAGE) ? OG_IMAGE : WCF::getPath() . OG_IMAGE), true); | |
48 | } | |
ffe5a321 AE |
49 | if (FB_SHARE_APP_ID) { |
50 | $this->addTag('fb:app_id', 'fb:app_id', FB_SHARE_APP_ID, true); | |
51 | } | |
d215cdb0 AE |
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) { | |
a63af0ee N |
63 | if (!isset($this->objects[$identifier])) { |
64 | $this->indexToObject[] = $identifier; | |
65 | } | |
66 | ||
058cbd6a | 67 | $this->objects[$identifier] = [ |
d215cdb0 AE |
68 | 'isProperty' => $isProperty, |
69 | 'name' => $name, | |
70 | 'value' => $value | |
058cbd6a | 71 | ]; |
1299e654 AE |
72 | |
73 | // replace description if Open Graph Protocol tag was given | |
a1434a66 | 74 | if ($name == 'og:description' && $value) { |
e222851a | 75 | $this->addTag('description', 'description', $value); |
37849b1a | 76 | } |
d215cdb0 AE |
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]); | |
fa169278 AE |
87 | |
88 | $this->indexToObject = array_keys($this->objects); | |
d215cdb0 AE |
89 | } |
90 | } | |
91 | ||
92 | /** | |
0fcfe5f6 | 93 | * @inheritDoc |
d215cdb0 AE |
94 | */ |
95 | public function count() { | |
96 | return count($this->objects); | |
97 | } | |
98 | ||
99 | /** | |
0fcfe5f6 | 100 | * @inheritDoc |
d215cdb0 AE |
101 | */ |
102 | public function current() { | |
103 | $tag = $this->objects[$this->indexToObject[$this->index]]; | |
104 | ||
e5f9b56c | 105 | return '<meta ' . ($tag['isProperty'] ? 'property' : 'name') . '="' . $tag['name'] . '" content="' . StringUtil::encodeHTML($tag['value']) . '">'; |
d215cdb0 AE |
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. | |
59dc0db6 | 111 | * |
d215cdb0 AE |
112 | * @see \Iterator::key() |
113 | */ | |
114 | public function key() { | |
115 | return $this->indexToObject[$this->index]; | |
116 | } | |
117 | ||
118 | /** | |
0fcfe5f6 | 119 | * @inheritDoc |
d215cdb0 AE |
120 | */ |
121 | public function next() { | |
122 | ++$this->index; | |
123 | } | |
124 | ||
125 | /** | |
0fcfe5f6 | 126 | * @inheritDoc |
d215cdb0 AE |
127 | */ |
128 | public function rewind() { | |
129 | $this->index = 0; | |
130 | } | |
131 | ||
132 | /** | |
0fcfe5f6 | 133 | * @inheritDoc |
d215cdb0 AE |
134 | */ |
135 | public function valid() { | |
136 | return isset($this->indexToObject[$this->index]); | |
137 | } | |
138 | } |