Permalink
Browse files

feat(slacknotes): when resolving URLs, use effective URL

  • Loading branch information...
eteubert committed Jan 13, 2019
1 parent f4e78e3 commit 974c7f8569e940c3dc9b5ee02949208cf422fa87
Showing with 21 additions and 8 deletions.
  1. +3 −0 js/src/components/Slacknotes.vue
  2. +18 −8 lib/modules/slack_shownotes/slack_shownotes.php
@@ -331,6 +331,9 @@ export default {
if (data.title) {
link.title = data.title;
}
if (data.url) {
link.link = data.url;
}
})
},
toggleExclusion: function(link) {
@@ -50,8 +50,8 @@ public function api_resolve_url(\WP_REST_Request $request)
return new \WP_REST_Response(["success" => false]);
}
$title = self::fetch_title_for_url($url);
return new \WP_REST_Response(["title" => $title]);
$response = self::fetch_url_meta($url);
return new \WP_REST_Response($response);
}
public function api_get_messages(\WP_REST_Request $request)
@@ -159,14 +159,14 @@ public function get_messages($channel_id, $date_from, $date_to)
}
/**
* Fetches title for URL.
* Fetches title and effective URL for URL.
*
* Prefers "og:title", falls back to "title".
*
* @param string $url
* @return string
*/
public static function fetch_title_for_url($url)
public static function fetch_url_meta($url)
{
$curl = curl_init();
@@ -188,27 +188,37 @@ public static function fetch_title_for_url($url)
$html = curl_exec($curl);
$err = curl_error($curl);
$effective_url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_close($curl);
$response = [
"url" => $effective_url,
"title" => "",
];
if (!$err) {
$dom = new \DOMDocument;
$loaded = $dom->loadHTML($html, LIBXML_NOERROR);
if (!$loaded) {
return "";
return $response;
}
foreach ($dom->getElementsByTagName('meta') as $node) {
if ($node->getAttribute("property") == "og:title") {
return $node->getAttribute("content");
$response["title"] = $node->getAttribute("content");
return $response;
}
}
foreach ($dom->getElementsByTagName('title') as $node) {
return $node->nodeValue;
$response["title"] = $node->nodeValue;
return $response;
}
}
return "";
return $response;
}
}

0 comments on commit 974c7f8

Please sign in to comment.