-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
324 lines (275 loc) · 13.9 KB
/
plugin.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
This is the PodlovePlugin plugin.
This file contains the PodlovePlugin plugin.
@package Filmmakers4Future/PodlovePlugin
@version 0.2
@author Paul-Vincent Roll <paul-vincent@filmmakersforfuture.org>
@since 0.2
*/
// ===== DO NOT EDIT HERE =====
// prevent script from getting called directly
if (!defined("URLAUBE")) { die(""); }
class PodlovePlayerPlugin extends BaseSingleton implements Plugin {
// CONSTANTS
// Content
const PODLOVEWEBPLAYER = "[podloveplayer]";
const PODLOVE_AUDIOFILES = "podlove_audioFiles";
const PODLOVE_PODCASTTITLE = "podlove_podcastTitle";
const PODLOVE_PODCASTLINK = "podlove_podcastLink";
const PODLOVE_TITLE = "podlove_title";
const PODLOVE_POSTER = "podlove_poster";
const PODLOVE_DURATION = "podlove_duration";
const PODLOVE_AUDIOSIZES = "podlove_audioSizes";
const PODLOVE_AUDIOTITLES = "podlove_audioTitles";
const PODLOVE_AUDIOMIME = "podlove_audioMimeTypes";
const PODLOVE_DOWNLOADFILES = "podlove_downloadFiles";
const PODLOVE_DOWNLOADSIZES = "podlove_downloadSizes";
const PODLOVE_DOWNLOADTITLES = "podlove_downloadTitles";
const PODLOVE_DOWNLOADMIME = "podlove_downloadMimeTypes";
const PODLOVE_CHAPTERTITLES = "podlove_chapterTitles";
const PODLOVE_CHAPTERSTARTS= "podlove_chapterStarts";
const PODLOVE_CHAPTERLINKS = "podlove_chapterLinks";
const PODLOVE_CHAPTERIMAGES = "podlove_chapterImages";
// CONSTANTS
// config
const PODLOVE_CONFIG_FEED_URL = "PODLOVE_FEED_URL";
const PODLOVE_CONFIG_APPLE_PODCAST_ID = "PODLOVE_APPLE_PODCAST_ID";
const PODLOVE_CONFIG_GOOGLE_PODCAST = "PODLOVE_GOOGLE_PODCAST";
const PODLOVE_CONFIG_CASTBOX_ID = "PODLOVE_CASTBOX_ID";
const PODLOVE_CONFIG_POCKET_CASTS = "PODLOVE_POCKET_CASTS";
const PODLOVE_CONFIG_DEEZER_ID = "PODLOVE_DEEZER_ID";
const PODLOVE_CONFIG_SOUNDCLOUD_ID = "PODLOVE_SOUNDCLOUD_ID";
const PODLOVE_CONFIG_SPOTIFY_ID = "PODLOVE_SPOTIFY_ID";
const PODLOVE_CONFIG_STITCHER_ID = "PODLOVE_STITCHER_ID";
const PODLOVE_CONFIG_YOUTUBE_ID = "PODLOVE_YOUTUBE_ID";
const PODLOVE_CONFIG_ACTIVE_TAB = "PODLOVE_ACTIVE_TAB";
const PODLOVE_CONFIG_EMBEDDING = "PODLOVE_EMBEDDING";
const PODLOVE_CONFIG_THEME_COLORS = "PODLOVE_THEME_COLORS";
const PODLOVE_CONFIG_SHARE_CHANNELS = "PODLOVE_SHARE_CHANNELS";
const PODLOVE_CONFIG_SHARE_PLAYTIME = "PODLOVE_SHARE_PLAYTIME";
// VARIABLES
protected static $PLAYER_BOOTSTRAP_CODE = array();
// HELPER FUNCTIONS
public static function bootstrapPlayer() {
if (!empty(self::$PLAYER_BOOTSTRAP_CODE)){
print(fhtml("<!-- Podlove Webplayer Library -->").NL);
print(fhtml("<script src='".path2uri(__DIR__."/lib/embed.js")."'></script>").NL);
print(fhtml("<!-- Podlove Webplayer Boostraping -->").NL);
print(fhtml("<script>"));
foreach (self::$PLAYER_BOOTSTRAP_CODE as $player) {
print(fhtml($player).NL);
}
print(fhtml("</script>"));
print(NL);
}
}
protected static function configure() {
Plugins::preset(static::PODLOVE_CONFIG_FEED_URL, null);
Plugins::preset(static::PODLOVE_CONFIG_APPLE_PODCAST_ID, null);
Plugins::preset(static::PODLOVE_CONFIG_CASTBOX_ID, null);
Plugins::preset(static::PODLOVE_CONFIG_POCKET_CASTS, null);
Plugins::preset(static::PODLOVE_CONFIG_DEEZER_ID, null);
Plugins::preset(static::PODLOVE_CONFIG_SOUNDCLOUD_ID, null);
Plugins::preset(static::PODLOVE_CONFIG_SPOTIFY_ID, null);
Plugins::preset(static::PODLOVE_CONFIG_STITCHER_ID, null);
Plugins::preset(static::PODLOVE_CONFIG_YOUTUBE_ID, null);
Plugins::preset(static::PODLOVE_CONFIG_ACTIVE_TAB, null);
Plugins::preset(static::PODLOVE_CONFIG_EMBEDDING, "/share.html");
Plugins::preset(static::PODLOVE_CONFIG_SHARE_CHANNELS, ["twitter", "facebook", "whats-app", "mail", "link"]);
Plugins::preset(static::PODLOVE_CONFIG_SHARE_PLAYTIME, true);
}
protected static function getPodlovePlayer($item) {
// preset plugin configuration
static::configure();
$result = value($item, CONTENT);
if (is_string($result)) {
if (value($item, self::PODLOVE_AUDIOFILES) and value($item, self::PODLOVE_AUDIOMIME) and value($item, self::PODLOVE_AUDIOTITLES) and value($item, self::PODLOVE_AUDIOSIZES)) {
# Base player configuration
$config = [
"version" => 5,
"base" => path2uri(__DIR__)."/lib/",
"theme" => [
"tokens" => Plugins::get(static::PODLOVE_CONFIG_THEME_COLORS)
],
"share" => [
"channels" => Plugins::get(static::PODLOVE_CONFIG_SHARE_CHANNELS),
"sharePlaytime" => Plugins::get(static::PODLOVE_CONFIG_SHARE_PLAYTIME),
"outlet" => Plugins::get(static::PODLOVE_CONFIG_EMBEDDING)
]
];
if(Plugins::get(static::PODLOVE_CONFIG_ACTIVE_TAB)){
$config["activeTab"] = Plugins::get(static::PODLOVE_CONFIG_ACTIVE_TAB);
}
# Add subscribe button config
if(Plugins::get(static::PODLOVE_CONFIG_FEED_URL)){
$subscribeButton = [
"feed" => Plugins::get(static::PODLOVE_CONFIG_FEED_URL),
"clients" => [
["id" => "antenna-pod"],
["id" => "beyond-pod"],
["id" => "clementine"],
["id" => "downcast"],
["id" => "gpodder"],
["id" => "itunes"],
["id" => "i-catcher"],
["id" => "instacast"],
["id" => "overcast"],
["id" => "player-fm"],
["id" => "pod-grasp"],
["id" => "podcast-addict"],
["id" => "podcast-republic"],
["id" => "podcat"],
["id" => "podscout"],
["id" => "rss-radio"],
["id" => "rss"]
]
];
if(Plugins::get(static::PODLOVE_CONFIG_APPLE_PODCAST_ID)){
$subscribeButton["clients"][] = ["id" => "apple-podcasts", "service" => Plugins::get(static::PODLOVE_CONFIG_APPLE_PODCAST_ID)];
$subscribeButton["clients"][] = ["id" => "castro", "service" => Plugins::get(static::PODLOVE_CONFIG_APPLE_PODCAST_ID)];
}
if(Plugins::get(static::PODLOVE_CONFIG_CASTBOX_ID)){
$subscribeButton["clients"][] = ["id" => "castbox", "service" => Plugins::get(static::PODLOVE_CONFIG_CASTBOX_ID)];
}
if(Plugins::get(static::PODLOVE_CONFIG_DEEZER_ID)){
$subscribeButton["clients"][] = ["id" => "deezer", "service" => Plugins::get(static::PODLOVE_CONFIG_DEEZER_ID)];
}
if(Plugins::get(static::PODLOVE_CONFIG_SOUNDCLOUD_ID)){
$subscribeButton["clients"][] = ["id" => "soundcloud", "service" => Plugins::get(static::PODLOVE_CONFIG_SOUNDCLOUD_ID)];
}
if(Plugins::get(static::PODLOVE_CONFIG_SPOTIFY_ID)){
$subscribeButton["clients"][] = ["id" => "spotify", "service" => Plugins::get(static::PODLOVE_CONFIG_SPOTIFY_ID)];
}
if(Plugins::get(static::PODLOVE_CONFIG_STITCHER_ID)){
$subscribeButton["clients"][] = ["id" => "stitcher", "service" => Plugins::get(static::PODLOVE_CONFIG_STITCHER_ID)];
}
if(Plugins::get(static::PODLOVE_CONFIG_YOUTUBE_ID)){
$subscribeButton["clients"][] = ["id" => "youtube", "service" => Plugins::get(static::PODLOVE_CONFIG_YOUTUBE_ID)];
}
if(Plugins::get(static::PODLOVE_CONFIG_GOOGLE_PODCAST)){
if (Plugins::get(static::PODLOVE_CONFIG_GOOGLE_PODCAST) === True) {
$subscribeButton["clients"][] = ["id" => "google-podcasts", "service" => Plugins::get(static::PODLOVE_CONFIG_FEED_URL)];
} else {
$subscribeButton["clients"][] = ["id" => "google-podcasts", "service" => Plugins::get(static::PODLOVE_CONFIG_GOOGLE_PODCAST)];
}
}
if(Plugins::get(static::PODLOVE_CONFIG_POCKET_CASTS)){
if (Plugins::get(static::PODLOVE_CONFIG_POCKET_CASTS) === True) {
$subscribeButton["clients"][] = ["id" => "pocket-casts", "service" => Plugins::get(static::PODLOVE_CONFIG_FEED_URL)];
} else {
$subscribeButton["clients"][] = ["id" => "pocket-casts", "service" => Plugins::get(static::PODLOVE_CONFIG_POCKET_CASTS)];
}
}
usort($subscribeButton["clients"], function ($item1, $item2) {
return $item1['id'] <=> $item2['id'];
});
$config["subscribe-button"] = $subscribeButton;
}
// Base episode configuration
$episode = [
"version" => 5,
"show" => [
"title" => value($item, self::PODLOVE_PODCASTTITLE),
"link" => value($item, self::PODLOVE_PODCASTLINK)
],
"title" => value($item, self::PODLOVE_TITLE),
"poster" => value($item, self::PODLOVE_POSTER),
"duration" => value($item, self::PODLOVE_DURATION),
"link" => absoluteurl(value($item, URI))
];
// Parse audio files from markdown
$audioFiles = explode(",", value($item, self::PODLOVE_AUDIOFILES));
$audioSizes = explode(",", value($item, self::PODLOVE_AUDIOSIZES));
$audioTitles = explode(",", value($item, self::PODLOVE_AUDIOTITLES));
$audioMimeTypes = explode(",", value($item, self::PODLOVE_AUDIOMIME));
// Parse download files from markdown (optional)
$downloadFiles = explode(",", (string) value($item, self::PODLOVE_DOWNLOADFILES));
$downloadSizes = explode(",", (string) value($item, self::PODLOVE_DOWNLOADSIZES));
$downloadTitles = explode(",", (string) value($item, self::PODLOVE_DOWNLOADTITLES));
$downloadMimeTypes = explode(",", (string) value($item, self::PODLOVE_DOWNLOADMIME));
// Parse chapters from markdown (optional)
$chapterTitles = explode(",", (string) value($item, self::PODLOVE_CHAPTERTITLES));
$chapterStarts = explode(",", (string) value($item, self::PODLOVE_CHAPTERSTARTS));
$chapterLinks = explode(",", (string) value($item, self::PODLOVE_CHAPTERLINKS));
$chapterImages = explode(",", (string) value($item, self::PODLOVE_CHAPTERIMAGES));
// Create arrays for data
$audio = array();
$files = array();
$chapters = array();
// Add audio files to audio array
foreach($audioFiles as $i => $audioFile) {
$audio[] = ["url" => $audioFile,
"size" => $audioSizes[$i],
"title" => $audioTitles[$i],
"mimeType" => $audioMimeTypes[$i]
];
// Also add the audio files to the download list
$files[] = ["url" => $audioFile,
"size" => $audioSizes[$i],
"title" => "Episode: ".$audioTitles[$i],
"mimeType" => $audioMimeTypes[$i]
];
};
// Add download files to files array
if(value($item, self::PODLOVE_DOWNLOADFILES)){
foreach($downloadFiles as $i => $downloadFile) {
$files[] = ["url" => $downloadFile,
"size" => $downloadSizes[$i],
"title" => $downloadTitles[$i],
"mimeType" => $downloadMimeTypes[$i]
];
};
}
// Add chapters to chapter array
if(value($item, self::PODLOVE_CHAPTERTITLES)){
foreach($chapterTitles as $i => $chapterTitle) {
$chapters[] = ["title" => $chapterTitle,
"start" => $chapterStarts[$i],
"href" => $chapterLinks[$i] ?? null,
"image" => $chapterImages[$i] ?? null
];
};
}
// Add new config arrays to episode array
$episode["audio"] = $audio;
$episode["files"] = $files;
$episode["chapters"] = $chapters;
# Create random id for podlove webplayer div
$divID = "player_".substr(uniqid(rand(),1),0,15);
# Create div for podlove webplayer
$podloveplayer = fhtml("<div id=\"%s\"></div>".NL,
$divID);
# Add player bootstrap code to array to add after body
self::$PLAYER_BOOTSTRAP_CODE[] = 'window.podlovePlayer("#'.$divID.'", '.json_encode($episode, JSON_UNESCAPED_SLASHES).', '.json_encode($config, JSON_UNESCAPED_SLASHES).');';
// replace shortcode with podlove player div
$result = str_ireplace(static::PODLOVEWEBPLAYER, $podloveplayer, $result);
}
}
return $result;
}
// RUNTIME FUNCTIONS
public static function plugin($content) {
$result = $content;
if ($result instanceof Content) {
if ($result->isset(CONTENT)) {
$result->set(CONTENT, static::getPodlovePlayer($result));
}
} else {
if (is_array($result)) {
// iterate through all content items
foreach ($result as $result_item) {
if ($result_item instanceof Content) {
if ($result_item->isset(CONTENT)) {
$result_item->set(CONTENT, static::getPodlovePlayer($result_item));
}
}
}
}
}
return $result;
}
}
// register plugin
Plugins::register(PodlovePlayerPlugin::class, "plugin", FILTER_CONTENT);
Plugins::register(PodlovePlayerPlugin::class, "bootstrapPlayer", AFTER_BODY);