-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathHtmlOutputUnfurlUrlNode.class.php
More file actions
97 lines (84 loc) · 2.99 KB
/
HtmlOutputUnfurlUrlNode.class.php
File metadata and controls
97 lines (84 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace wcf\system\html\output\node;
use wcf\data\unfurl\url\UnfurlUrl;
use wcf\system\html\AbstractHtmlProcessor;
use wcf\system\html\node\AbstractHtmlNodeProcessor;
use wcf\system\html\node\HtmlNodePlainLink;
use wcf\system\html\node\HtmlNodeUnfurlLink;
use wcf\system\html\output\HtmlOutputProcessor;
use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
/**
* Node class to replace unfurled urls in the output.
*
* @author Joshua Ruesweg
* @copyright 2001-2021 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 5.4
*/
class HtmlOutputUnfurlUrlNode extends AbstractHtmlOutputNode
{
/**
* @var string[]
*/
private static $disableUnfurlingForContext = ['com.woltlab.wcf.user.signature'];
/**
* @inheritDoc
*/
protected $tagName = 'a';
/**
* @inheritDoc
*/
public function process(array $elements, AbstractHtmlNodeProcessor $htmlNodeProcessor)
{
if ($this->outputType !== 'text/html') {
return;
}
$htmlProcessor = $htmlNodeProcessor->getHtmlProcessor();
if (
$htmlProcessor instanceof AbstractHtmlProcessor
&& \in_array($htmlProcessor->getContext()['objectType'], self::$disableUnfurlingForContext)
) {
return;
}
/** @var \DOMElement $element */
foreach ($elements as $element) {
$attribute = (int)$element->getAttribute(HtmlNodeUnfurlLink::UNFURL_URL_ID_ATTRIBUTE_NAME);
if (
!empty($attribute)
&& MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.unfurlUrl', $attribute) !== null
) {
$enableUgc = true;
$processor = $htmlNodeProcessor->getHtmlProcessor();
if ($processor instanceof HtmlOutputProcessor) {
$enableUgc = $processor->enableUgc;
}
[$nodeIdentifier, $tagName] = $htmlNodeProcessor->getWcfNodeIdentifier();
$htmlNodeProcessor->addNodeData($this, $nodeIdentifier, [
'urlId' => $attribute,
'enableUgc' => $enableUgc,
]);
if ($this->getUnfurlUrl($attribute)->isPlainUrl()) {
$htmlNodeProcessor->renameTag($element, $tagName);
} else {
$htmlNodeProcessor->renameTag(HtmlNodePlainLink::splitAtLink($element), $tagName);
}
}
}
}
/**
* @inheritDoc
*/
public function replaceTag(array $data)
{
return $this->getUnfurlUrl($data['urlId'])->render($data['enableUgc']);
}
/**
* @since 6.0
*/
protected function getUnfurlUrl(int $id): UnfurlUrl
{
$object = MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.unfurlUrl', $id);
\assert($object instanceof UnfurlUrl);
return $object;
}
}