syndicated feeds:

1. properly reset enclosure template variables if there's no enclosures
2. add ARTICLE_OG_IMAGE which sets flavor image for article using common code with article render etc
This commit is contained in:
Andrew Dolgov 2018-12-09 17:07:17 +03:00
parent 1b4567a973
commit 851f62dc4a
1 changed files with 44 additions and 29 deletions

View File

@ -139,18 +139,27 @@ class Handler_Public extends Handler {
$enclosures = Article::get_article_enclosures($line["id"]); $enclosures = Article::get_article_enclosures($line["id"]);
foreach ($enclosures as $e) { if (count($enclosures) > 0) {
$type = htmlspecialchars($e['content_type']); foreach ($enclosures as $e) {
$url = htmlspecialchars($e['content_url']); $type = htmlspecialchars($e['content_type']);
$length = $e['duration'] ? $e['duration'] : 1; $url = htmlspecialchars($e['content_url']);
$length = $e['duration'] ? $e['duration'] : 1;
$tpl->setVariable('ARTICLE_ENCLOSURE_URL', $url, true); $tpl->setVariable('ARTICLE_ENCLOSURE_URL', $url, true);
$tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', $type, true); $tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', $type, true);
$tpl->setVariable('ARTICLE_ENCLOSURE_LENGTH', $length, true); $tpl->setVariable('ARTICLE_ENCLOSURE_LENGTH', $length, true);
$tpl->addBlock('enclosure'); $tpl->addBlock('enclosure');
}
} else {
$tpl->setVariable('ARTICLE_ENCLOSURE_URL', null, true);
$tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', null, true);
$tpl->setVariable('ARTICLE_ENCLOSURE_LENGTH', null, true);
} }
$tpl->setVariable('ARTICLE_OG_IMAGE',
$this->get_article_image($enclosures, $line['content'], $feed_site_url));
$tpl->addBlock('entry'); $tpl->addBlock('entry');
} }
@ -309,6 +318,32 @@ class Handler_Public extends Handler {
} }
private function get_article_image($enclosures, $content, $site_url) {
$og_image = false;
foreach ($enclosures as $enc) {
if (strpos($enc["content_type"], "image/") !== FALSE) {
$og_image = $enc["content_url"];
break;
}
}
if (!$og_image) {
$tmpdoc = new DOMDocument();
if (@$tmpdoc->loadHTML(mb_substr($content, 0, 131070))) {
$tmpxpath = new DOMXPath($tmpdoc);
$first_img = $tmpxpath->query("//img")->item(0);
if ($first_img) {
$og_image = $first_img->getAttribute("src");
}
}
}
return rewrite_relative_url($site_url, $og_image);
}
private function format_article($id, $owner_uid) { private function format_article($id, $owner_uid) {
$pdo = Db::pdo(); $pdo = Db::pdo();
@ -385,27 +420,7 @@ class Handler_Public extends Handler {
$rv .= "</head>"; $rv .= "</head>";
$og_image = false; $og_image = $this->get_article_image($enclosures, $line['content'], $line["site_url"]);
foreach ($enclosures as $enc) {
if (strpos($enc["content_type"], "image/") !== FALSE) {
$og_image = $enc["content_url"];
break;
}
}
if (!$og_image) {
$tmpdoc = new DOMDocument();
if (@$tmpdoc->loadHTML(mb_substr($line["content"], 0, 131070))) {
$tmpxpath = new DOMXPath($tmpdoc);
$first_img = $tmpxpath->query("//img")->item(0);
if ($first_img) {
$og_image = $first_img->getAttribute("src");
}
}
}
if ($og_image) { if ($og_image) {
$rv .= "<meta property=\"og:image\" content=\"" . htmlspecialchars($og_image) . "\"/>"; $rv .= "<meta property=\"og:image\" content=\"" . htmlspecialchars($og_image) . "\"/>";