1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
<?php
class Af_Youtube_Thumb extends Plugin {
private $host;
function about() {
return array(null,
"Show Youtube videos as clickable thumbnails",
"fox");
}
function init($host) {
$this->host = $host;
$host->add_hook($host::HOOK_RENDER_ENCLOSURE, $this);
$host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
$host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this);
}
function get_css() {
return file_get_contents(__DIR__ . "/init.css");
}
function hook_render_enclosure($entry, $article_id, $rv) {
$url = $entry["content_url"];
if ($vid_id = UrlHelper::url_to_youtube_vid($url)) {
$thumb_url = htmlspecialchars("https://img.youtube.com/vi/$vid_id/hqdefault.jpg");
$url = htmlspecialchars($url);
return "<a target='_blank' rel='noopener noreferrer' href=\"$url\" title=\"".$this->__("Click to open video")."\">
<div class='youtube-thumb'>
<img class='thumbnail' src=\"$thumb_url\" referrerpolicy='no-referrer'>
<div class='watermark'></div>
</div>
</a>";
}
}
function hook_render_article($article) {
return $this->hook_render_article_cdm($article);
}
/* function hook_render_article_api($row) {
$article = isset($row['headline']) ? $row['headline'] : $row['article'];
return $this->hook_render_article_cdm($article, true);
} */
function hook_render_article_cdm($article, $api_mode = false) {
$doc = new DOMDocument();
$need_saving = false;
if (!empty($article["content"]) && @$doc->loadHTML($article["content"])) {
$xpath = new DOMXPath($doc);
$iframes = $xpath->query("//iframe[@src]");
foreach ($iframes as $iframe) {
$url = $iframe->getAttribute("src");
if ($vid_id = UrlHelper::url_to_youtube_vid($url)) {
$thumb_url = htmlspecialchars("https://img.youtube.com/vi/$vid_id/hqdefault.jpg");
$img = $doc->createElement("img");
$img->setAttribute("src", $thumb_url);
$watermark = $doc->createElement("div");
$watermark->setAttribute("class", "watermark");
$div = $doc->createElement("div");
$div->setAttribute("class", "youtube-thumb");
$div->appendChild($img);
$div->appendChild($watermark);
$a = $doc->createElement("a");
$a->setAttribute("target", "_blank");
$a->setAttribute("href", $url);
$a->appendChild($div);
$parent = $iframe->parentNode;
if ($parent->getAttribute("class") == "embed-responsive")
$parent->setAttribute("class", "");
$parent->replaceChild($a, $iframe);
$need_saving = true;
}
}
}
if ($need_saving) $article["content"] = $doc->saveXML();
return $article;
}
function api_version() {
return 2;
}
}
|