|
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
|
|
|
] |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
return $html; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Renders the responsive Google AMP iframe HTML for the live stream chat |
|
103
|
|
|
* |
|
104
|
|
|
* @param int $aspectRatioX |
|
|
|
|
|
|
105
|
|
|
* @param int $aspectRatioY |
|
|
|
|
|
|
106
|
|
|
* |
|
107
|
|
|
* @return \Twig_Markup |
|
108
|
|
|
*/ |
|
109
|
|
|
public function embedChatAmp(int $aspectRatioX = 16, int $aspectRatioY = 9): \Twig_Markup |
|
110
|
|
|
{ |
|
111
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
|
112
|
|
|
'embeds/youtube-live-chat-amp.twig', |
|
113
|
|
|
[ |
|
114
|
|
|
'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100, |
|
115
|
|
|
'iframeUrl' => $this->getYoutubeChatUrl(), |
|
116
|
|
|
] |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
return $html; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Sets the YouTube Channel ID to $channelId |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $channelId |
|
|
|
|
|
|
126
|
|
|
*/ |
|
|
|
|
|
|
127
|
|
|
public function setChannelId(string $channelId) |
|
128
|
|
|
{ |
|
129
|
|
|
YoutubeLiveEmbed::$youtubeChannelId = $channelId; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns whether the stream is currently live |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function isLive(): bool |
|
138
|
|
|
{ |
|
139
|
|
|
return YoutubeLiveEmbed::getInstance()->settings->isLive; |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// Protected Methods |
|
143
|
|
|
// ========================================================================= |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns the URL to the live video YouTube page |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function getYoutubeStreamUrl(): string |
|
151
|
|
|
{ |
|
152
|
|
|
$url = UrlHelper::urlWithParams(self::YOUTUBE_STREAM_URL, [ |
|
|
|
|
|
|
153
|
|
|
'channel' => YoutubeLiveEmbed::$youtubeChannelId, |
|
154
|
|
|
]); |
|
|
|
|
|
|
155
|
|
|
return $url; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns the URL to the live chat YouTube page |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getYoutubeChatUrl(): string |
|
164
|
|
|
{ |
|
165
|
|
|
$url = ''; |
|
166
|
|
|
$videoId = $this->getVideoIdFromLiveStream(); |
|
167
|
|
|
if ($videoId) { |
|
168
|
|
|
$site = Craft::$app->getSites()->currentSite; |
|
169
|
|
|
$domain = parse_url($site->baseUrl, PHP_URL_HOST); |
|
170
|
|
|
$url = UrlHelper::urlWithParams(self::YOUTUBE_CHAT_URL, [ |
|
|
|
|
|
|
171
|
|
|
'v' => $this->getVideoIdFromLiveStream(), |
|
172
|
|
|
'embed_domain' => $domain |
|
173
|
|
|
]); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $url; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Extracts the Video ID of the current live stream video |
|
181
|
|
|
* |
|
182
|
|
|
* @return null|string |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function getVideoIdFromLiveStream() |
|
185
|
|
|
{ |
|
186
|
|
|
$videoId = null; |
|
187
|
|
|
$liveUrl = $this->getYoutubeStreamUrl(); |
|
188
|
|
|
// Fetch the livestream page |
|
189
|
|
|
if ($data = @file_get_contents($liveUrl)) { |
|
190
|
|
|
// Find the video ID in there |
|
191
|
|
|
if (preg_match('/\'VIDEO_ID\': \"(.*?)\"/', $data, $matches)) { |
|
192
|
|
|
$videoId = $matches[1]; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $videoId; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|