This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
podcast.php
151 lines (119 loc) · 4.41 KB
/
podcast.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
<?php
/**
* Podcast Plugin
* @author Maurice Renck <hello@maurice-renck.de>
* @version 2.0.0
*/
namespace Podcast;
use c;
use field;
use PiwikTracker;
use ssga;
use yaml;
require_once(__DIR__ . '/lib/helper.php');
if(c::get('plugin.podcast.piwik.Base', false)) {
require_once(__DIR__ . '/lib/PiwikTracker.php');
}
if(c::get('plugin.podcast.GA.UA', false)) {
require_once(__DIR__ . '/lib/ss-ga.class.php');
}
class Podcast {
private $site;
private $kirby;
private $page;
public function __construct() {
$this->kirby = kirby();
$this->site = site();
$this->page = page();
$this->kirby->set('template', 'podcastrss', __DIR__ . '/templates/rss.php');
$this->kirby->set('widget', 'podstats', __DIR__ . '/widgets/podstats');
$this->kirby->set('blueprint', 'podcastrss', __DIR__ . '/blueprints/podcastrss.yml');
$this->kirby->set('blueprint', 'episode', __DIR__ . '/blueprints/episode.yml');
$this->kirby->set('snippet', 'podcastplayer', __DIR__ . '/snippets/podcastplayer.php');
$this->parseCategories();
$this->setRoutes();
}
private function parseCategories() {
$categoryList = $this->page->itunesCategories();
field::$methods['itunes'] = function($categoryList) {
$categories = array();
foreach ($categoryList->split(',') as $mainCategory) {
// split category in main and sub category
$currentCategory = explode('/', $mainCategory);
if(!isset($categories[$currentCategory[0]])) {
$categories[$currentCategory[0]] = array();
if(isset($currentCategory[1])) {
$categories[$currentCategory[0]][] = $currentCategory[1];
}
} else {
if(isset($currentCategory[1])) {
$categories[$currentCategory[0]][] = $currentCategory[1];
}
}
}
return $categories;
};
}
private function setRoutes() {
$language = ($this->site->languages() !== null) ? $this->site->language()->path() .'/': '';
kirby()->routes(array(
array(
'pattern' => $language. '(:all)/download/(:any)',
'action' => function($episode, $filename) {
// basic tracking
$helper = new PodcastHelper();
$trackingDate = date('Y-m');
$page = page($episode);
$helper->increaseDownloads($page, $trackingDate);
// advanced tracking with piwik
if(c::get('plugin.podcast.piwik.Base', false)) {
// Get Title of Podcast via related rss-feed
// We know the template-name, so get the rss feed by this name
$podcastRssTemplateName = c::get('plugin.podcast.template', 'podcastrss');
$podcastRelatedRss = $page->parent()->children()->filterBy('intendedTemplate', $podcastRssTemplateName);
$podcastTitle = page($podcastRelatedRss)->title()->raw();
$episodeTitle = $page->title()->raw();
// PiwikTracker::$URL = c::get('plugin.podcast.piwikBase');
$piwikTracker = new PiwikTracker(c::get('plugin.podcast.piwik.Id'), c::get('plugin.podcast.piwik.Base'));
$piwikTracker->setTokenAuth(c::get('plugin.podcast.piwik.Token'));
$piwikTracker->disableSendImageResponse();
$piwikTracker->disableCookieSupport();
$piwikTracker->setUrl($page->url());
$piwikTracker->setIp($_SERVER['REMOTE_ADDR']);
if(c::get('plugin.podcast.piwik.GoalId', false)) {
$piwikTracker->doTrackGoal(c::get('plugin.podcast.piwik.GoalId'), 1);
}
if(c::get('plugin.podcast.piwik.EventName', false)) {
$piwikTracker->doTrackEvent($podcastTitle, c::get('plugin.podcast.piwikEventName', 'download'), $episodeTitle);
}
if(c::get('plugin.podcast.piwik.Action', false)) {
$piwikTracker->doTrackAction($page->url(), 'download');
}
}
if(c::get('plugin.podcast.GA.UA', false)) {
$ssga = new ssga(c::get('plugin.podcast.GA.UA', null), site()->url());
if(c::get('plugin.podcast.GA.pageView', false)) {
// Set a pageview
$ssga->set_page( $page->url() );
$ssga->set_page_title( $episodeTitle );
}
if(c::get('plugin.podcast.GA.eventName', false)) {
// Set an event
$ssga->set_event( $podcastTitle, c::get('plugin.podcast.GA.eventName', 'Episode'), $episodeTitle, 1 );
}
// Send
$ssga->send();
$ssga->reset();
}
$filename = str_replace('.mp3', '', $filename);
$file = page($episode)
->files()
->filterBy('filename', '==', $filename.'.mp3')
->last();
header('Location: ' . $file->url());
}
)
));
}
}
$podcast = new Podcast();