Unfurl standalone Urls
authorjoshuaruesweg <ruesweg@woltlab.com>
Tue, 23 Feb 2021 10:56:01 +0000 (11:56 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Tue, 16 Mar 2021 15:19:12 +0000 (16:19 +0100)
wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeProcessor.class.php
wcfsetup/install/files/lib/system/html/node/HtmlNodeUnfurlLink.class.php [new file with mode: 0644]

index 87723fa8ad9d653686a4323626749952bb1096ff..9ac6dbba611006ff8c1ab0224b2d849e8c865a86 100644 (file)
@@ -2,10 +2,12 @@
 
 namespace wcf\system\html\input\node;
 
+use wcf\data\unfurl\url\UnfurlUrlAction;
 use wcf\system\bbcode\BBCodeHandler;
 use wcf\system\event\EventHandler;
 use wcf\system\html\node\AbstractHtmlNodeProcessor;
 use wcf\system\html\node\HtmlNodePlainLink;
+use wcf\system\html\node\HtmlNodeUnfurlLink;
 use wcf\system\html\node\IHtmlNode;
 use wcf\util\DOMUtil;
 use wcf\util\StringUtil;
@@ -742,5 +744,11 @@ class HtmlInputNodeProcessor extends AbstractHtmlNodeProcessor
         }
 
         EventHandler::getInstance()->fireAction($this, 'convertPlainLinks');
+
+        foreach ($this->plainLinks as $plainLink) {
+            if ($plainLink->isPristine()) {
+                HtmlNodeUnfurlLink::setUnfurl($plainLink);
+            }
+        }
     }
 }
diff --git a/wcfsetup/install/files/lib/system/html/node/HtmlNodeUnfurlLink.class.php b/wcfsetup/install/files/lib/system/html/node/HtmlNodeUnfurlLink.class.php
new file mode 100644 (file)
index 0000000..74ff262
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+namespace wcf\system\html\node;
+
+use wcf\data\unfurl\url\UnfurlUrlAction;
+
+/**
+ * Helper class to unfurl link objects.
+ *
+ * @author             Joshua Ruesweg
+ * @copyright   2001-2021 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Html\Node
+ * @since      5.4
+ */
+class HtmlNodeUnfurlLink extends HtmlNodePlainLink
+{
+    public const UNFURL_URL_ID_ATTRIBUTE_NAME = "unfurl-url-id";
+
+    /**
+     * Marks a link element with the UnfurlUrlID.
+     */
+    public static function setUnfurl(HtmlNodePlainLink $link): void
+    {
+        if ($link->isStandalone()) {
+            $object = new UnfurlUrlAction([], 'findOrCreate', [
+                'data' => [
+                    'url' => $link->href,
+                ],
+            ]);
+            $returnValues = $object->executeAction();
+            $link->topLevelParent->firstChild->setAttribute(self::UNFURL_URL_ID_ATTRIBUTE_NAME, $returnValues['returnValues']->urlID);
+        }
+    }
+}