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