-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebtor.php
More file actions
91 lines (83 loc) · 2.6 KB
/
webtor.php
File metadata and controls
91 lines (83 loc) · 2.6 KB
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
<?php
/*
* Plugin Name: Webtor Player
* Description: Provides your users the ability to watch torrent-videos online on your website. Popular videos are cached and converted to various formats for optimal playback on mobile devices!
* Version: 0.0.1
* Author: Webtor.io
* Author URI: https://webtor.io
*/
class Webtor {
private $scriptInjected;
private $defaults = array(
'controls' => true,
'width' => '100%',
);
function generateAttrs($args) {
$attrs = array();
foreach ($args as $k => $v) {
if ($k == 'controls') {
if ($v) $attrs[] = 'controls';
} else {
$attrs[] = "$k=\"$v\"";
}
}
return implode(' ', $attrs);
}
function splitArgs($args) {
$attrs = array();
$tracks = array();
foreach ($args as $k => $v) {
if (strpos($k, 'track') === 0) {
$tracks[$k] = $v;
} else {
$attrs[$k] = $v;
}
}
return array($attrs, $tracks);
}
function generateTracks($args) {
$tracks = array();
foreach ($args as $k => $v) {
$parts = explode('-', $k);
$lang = 'en';
if (sizeof($parts) == 2) {
$attr = $parts[1];
} else if (sizeof($parts) == 3) {
$lang = $parts[1];
$attr = $parts[2];
} else {
continue;
}
$tracks[$lang][$attr] = $v;
}
foreach ($tracks as $k => $v) {
$tracks[$k]['srclang'] = $k;
if (!isset($tracks[$k]['label'])) $tracks[$k]['label'] = $k;
}
$res = [];
foreach ($tracks as $t) {
$attrs = $this->generateAttrs($t);
$res[] = "<track $attrs>";
}
return implode('', $res);
}
function shortcode($args) {
$args = array_merge($this->defaults, $args);
if (!isset($args['src'])) {
return '<p>"src" attribute required!</p>';
}
list($args, $tracks) = $this->splitArgs($args);
$attrs = $this->generateAttrs($args);
$tracks = $this->generateTracks($tracks);
$res = "<video $attrs>$tracks</video>";
$res = '<p>'.$res.'</p>';
if (!$this->scriptInjected) {
$res .= '<script src="https://cdn.jsdelivr.net/npm/@webtor/embed-sdk-js/dist/index.min.js" charset="utf-8" async></script>';
$this->scriptInjected = true;
}
return $res;
}
}
$webtor = new Webtor();
add_shortcode('webtor', array($webtor, 'shortcode'));
?>