ttrss-yframes/init.php

54 lines
1.5 KiB
PHP
Raw Normal View History

2021-02-19 17:40:24 +01:00
<?php
class YFrames extends Plugin {
2021-02-19 21:13:55 +01:00
private $host;
2021-02-19 17:40:24 +01:00
2021-02-19 21:13:55 +01:00
function about() {
return array(1.0,
"iFrames for Youtube feeds",
"phga");
}
2021-02-19 17:40:24 +01:00
2021-02-19 21:13:55 +01:00
function init($host) {
$this->host = $host;
2021-02-19 17:40:24 +01:00
2021-02-19 21:13:55 +01:00
$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
2021-02-19 21:12:30 +01:00
$host->add_hook($host::HOOK_IFRAME_WHITELISTED, $this);
}
2021-02-19 17:40:24 +01:00
2021-02-19 21:12:30 +01:00
function hook_iframe_whitelisted($src) {
return in_array($src, ["www.youtube.com", "youtube.com", "youtu.be"]);
}
2021-02-19 17:40:24 +01:00
2021-02-19 21:12:30 +01:00
function hook_article_filter($article) {
2021-02-19 17:40:24 +01:00
2021-02-19 21:12:30 +01:00
// $debug = json_encode($article);
// Match allowed links and extract "video ID"
if (preg_match_all("/.*(youtube|youtu)\.(com|be)\/watch\?v=([\w\d_-]+).*/i",
$article["link"], $matches, PREG_PATTERN_ORDER)) {
2021-02-19 17:40:24 +01:00
2021-02-19 21:13:55 +01:00
if ($matches[3][0]) {
$link = "https://www.youtube.com/embed/" . $matches[3][0];
$attributes = "width='819' height='460' class='youtube-player' allowfullscreen";
$yframe = "<iframe src='$link' $attributes></iframe>";
2021-02-19 17:40:24 +01:00
2021-02-19 21:13:55 +01:00
$article["content"] = $yframe;
}
}
2021-02-19 21:04:13 +01:00
2021-02-19 21:13:55 +01:00
$description = str_replace("\n", "<br/>", $article["enclosures"][0][3]);
if (strlen($description) > 0) {
$article["content"] .= "<br/>" . $description ;
} else {
$article["content"] .= "<br/>No video description available.";
}
2021-02-19 17:40:24 +01:00
2021-02-19 21:13:55 +01:00
return $article;
}
2021-02-19 17:40:24 +01:00
2021-02-19 21:13:55 +01:00
function api_version() {
return 2;
}
2021-02-19 17:40:24 +01:00
}