Skip to content

Commit

Permalink
feat(slacknotes): when resolving URLs, use effective URL
Browse files Browse the repository at this point in the history
  • Loading branch information
eteubert committed Jan 13, 2019
1 parent f4e78e3 commit 974c7f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
3 changes: 3 additions & 0 deletions js/src/components/Slacknotes.vue
Expand Up @@ -331,6 +331,9 @@ export default {
if (data.title) {
link.title = data.title;
}
if (data.url) {
link.link = data.url;
}
})
},
toggleExclusion: function(link) {
Expand Down
26 changes: 18 additions & 8 deletions lib/modules/slack_shownotes/slack_shownotes.php
Expand Up @@ -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)
Expand Down Expand Up @@ -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();

Expand All @@ -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.