-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.php
52 lines (38 loc) · 1.22 KB
/
init.php
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
<?php
class YoutubeFrames extends Plugin {
private $host;
function about() {
return array(1.0,
"Embed Youtube videos using iframes",
"dugite-code");
}
function init($host) {
$this->host = $host;
$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
$host->add_hook($host::HOOK_IFRAME_WHITELISTED, $this);
}
function hook_iframe_whitelisted($src) {
return in_array($src, ["www.youtube.com", "youtube.com", "youtu.be", "youtube-nocookie.com", "www.youtube-nocookie.com"]);
}
function hook_article_filter($article) {
// Basic link matching
if ( strpos($article["link"], "youtube.com") !==false ){
$domDocument = new DOMDocument();
$domElement = $domDocument->createElement("iframe","");
$newnode = $domDocument->appendChild($domElement);
// Get video ID from link
$urlparts = explode("v=", $article["link"]);
$embed_link = "https://www.youtube-nocookie.com/embed/".$urlparts[1];
// Add src attribute
$newnode->setAttribute("src", $embed_link);
$newnode->setAttribute("frameborder", "0");
// Save domDocument as html and replace article content
$article["content"] = $domDocument->saveHTML();
}
// Return article
return $article;
}
function api_version() {
return 2;
}
}