1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YouTube Live Embed plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* This plugin allows you to embed a YouTube live stream and/or live chat on your webpage |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2019 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\youtubeliveembed\services; |
12
|
|
|
|
13
|
|
|
use nystudio107\youtubeliveembed\YoutubeLiveEmbed; |
14
|
|
|
use nystudio107\youtubeliveembed\helpers\PluginTemplate; |
15
|
|
|
|
16
|
|
|
use Craft; |
17
|
|
|
use craft\base\Component; |
18
|
|
|
use craft\helpers\UrlHelper; |
19
|
|
|
|
20
|
|
|
/** @noinspection MissingPropertyAnnotationsInspection */ |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package YoutubeLiveEmbed |
|
|
|
|
25
|
|
|
* @since 1.0.0 |
|
|
|
|
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class Embed extends Component |
28
|
|
|
{ |
29
|
|
|
// Constants |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
const YOUTUBE_STREAM_URL = 'https://www.youtube.com/embed/live_stream'; |
33
|
|
|
const YOUTUBE_CHAT_URL = 'https://www.youtube.com/live_chat'; |
34
|
|
|
|
35
|
|
|
// Public Methods |
36
|
|
|
// ========================================================================= |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Renders the responsive iframe for the live stream video |
40
|
|
|
* |
41
|
|
|
* @param int $aspectRatioX |
|
|
|
|
42
|
|
|
* @param int $aspectRatioY |
|
|
|
|
43
|
|
|
* |
44
|
|
|
* @return \Twig_Markup |
45
|
|
|
*/ |
46
|
|
|
public function embedStream(int $aspectRatioX = 16, int $aspectRatioY = 9): \Twig_Markup |
47
|
|
|
{ |
48
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
49
|
|
|
'embeds/youtube-live-stream.twig', |
50
|
|
|
[ |
51
|
|
|
'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100, |
52
|
|
|
'iframeUrl' => $this->getYoutubeStreamUrl(), |
53
|
|
|
] |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
return $html; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Renders the responsive Google AMP iframe for the live stream video |
61
|
|
|
* |
62
|
|
|
* @param int $aspectRatioX |
|
|
|
|
63
|
|
|
* @param int $aspectRatioY |
|
|
|
|
64
|
|
|
* |
65
|
|
|
* @return \Twig_Markup |
66
|
|
|
*/ |
67
|
|
|
public function embedStreamAmp(int $aspectRatioX = 16, int $aspectRatioY = 9): \Twig_Markup |
68
|
|
|
{ |
69
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
70
|
|
|
'embeds/youtube-live-stream-amp.twig', |
71
|
|
|
[ |
72
|
|
|
'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100, |
73
|
|
|
'iframeUrl' => $this->getYoutubeStreamUrl(), |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
return $html; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Renders the responsive iframe HTML for the live stream chat |
82
|
|
|
* |
83
|
|
|
* @param int $aspectRatioX |
|
|
|
|
84
|
|
|
* @param int $aspectRatioY |
|
|
|
|
85
|
|
|
* |
86
|
|
|
* @return \Twig_Markup |
87
|
|
|
*/ |
88
|
|
|
public function embedChat(int $aspectRatioX = 16, int $aspectRatioY = 9): \Twig_Markup |
89
|
|
|
{ |
90
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
91
|
|
|
'embeds/youtube-live-chat.twig', |
92
|
|
|
[ |
93
|
|
|
'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100, |
94
|
|
|
'iframeUrl' => $this->getYoutubeChatUrl(), |
95
|
|
|
'embedDomain' => $this->getSiteDomain(), |
96
|
|
|
] |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return $html; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Renders the responsive Google AMP iframe HTML for the live stream chat |
104
|
|
|
* |
105
|
|
|
* @param int $aspectRatioX |
|
|
|
|
106
|
|
|
* @param int $aspectRatioY |
|
|
|
|
107
|
|
|
* |
108
|
|
|
* @return \Twig_Markup |
109
|
|
|
*/ |
110
|
|
|
public function embedChatAmp(int $aspectRatioX = 16, int $aspectRatioY = 9): \Twig_Markup |
111
|
|
|
{ |
112
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
113
|
|
|
'embeds/youtube-live-chat-amp.twig', |
114
|
|
|
[ |
115
|
|
|
'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100, |
116
|
|
|
'iframeUrl' => $this->getYoutubeChatUrl(), |
117
|
|
|
'embedDomain' => $this->getSiteDomain(), |
118
|
|
|
] |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
return $html; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sets the YouTube Channel ID to $channelId |
126
|
|
|
* |
127
|
|
|
* @param string $channelId |
|
|
|
|
128
|
|
|
*/ |
|
|
|
|
129
|
|
|
public function setChannelId(string $channelId) |
130
|
|
|
{ |
131
|
|
|
YoutubeLiveEmbed::$youtubeChannelId = $channelId; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns whether the stream is currently live |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function isLive(): bool |
140
|
|
|
{ |
141
|
|
|
return YoutubeLiveEmbed::getInstance()->settings->isLive; |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Protected Methods |
145
|
|
|
// ========================================================================= |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns the URL to the live video YouTube page |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function getYoutubeStreamUrl(): string |
153
|
|
|
{ |
154
|
|
|
$url = UrlHelper::urlWithParams(self::YOUTUBE_STREAM_URL, [ |
|
|
|
|
155
|
|
|
'channel' => YoutubeLiveEmbed::$youtubeChannelId, |
156
|
|
|
]); |
|
|
|
|
157
|
|
|
return $url; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns the URL to the live chat YouTube page |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
protected function getYoutubeChatUrl(): string |
166
|
|
|
{ |
167
|
|
|
$url = ''; |
168
|
|
|
$videoId = $this->getVideoIdFromLiveStream(); |
169
|
|
|
if ($videoId) { |
170
|
|
|
$url = UrlHelper::urlWithParams(self::YOUTUBE_CHAT_URL, [ |
|
|
|
|
171
|
|
|
'v' => $this->getVideoIdFromLiveStream(), |
172
|
|
|
'embed_domain' => $this->getSiteDomain(), |
173
|
|
|
]); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $url; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns the domain of the host site |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
protected function getSiteDomain() |
186
|
|
|
{ |
187
|
|
|
$site = Craft::$app->getSites()->currentSite; |
188
|
|
|
$domain = parse_url($site->baseUrl, PHP_URL_HOST); |
189
|
|
|
return $domain; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Extracts the Video ID of the current live stream video |
194
|
|
|
* |
195
|
|
|
* @return null|string |
196
|
|
|
*/ |
197
|
|
|
protected function getVideoIdFromLiveStream() |
198
|
|
|
{ |
199
|
|
|
$videoId = null; |
200
|
|
|
$liveUrl = $this->getYoutubeStreamUrl(); |
201
|
|
|
// Fetch the livestream page |
202
|
|
|
if ($data = @file_get_contents($liveUrl)) { |
203
|
|
|
// Find the video ID in there |
204
|
|
|
if (preg_match('/\'VIDEO_ID\': \"(.*?)\"/', $data, $matches)) { |
205
|
|
|
$videoId = $matches[1]; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $videoId; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|