1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Ajde_Embed_Youtube extends Ajde_Embed |
4
|
|
|
{ |
5
|
|
|
public function convertUrlToEmbed() |
6
|
|
|
{ |
7
|
|
|
if (substr($this->_code, 0, 7) == 'http://' || substr($this->_code, 0, 8) == 'https://') { |
8
|
|
|
$ytid = $this->_getYoutubeId(); |
9
|
|
|
$this->_code = '<iframe width="425" height="700" src="http://www.youtube.com/embed/'.$ytid.'" frameborder="0" allowfullscreen></iframe>'; |
10
|
|
|
} |
11
|
|
|
} |
12
|
|
|
|
13
|
|
View Code Duplication |
public function getCode() |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$this->convertUrlToEmbed(); |
16
|
|
|
$this->_setHeight(); |
17
|
|
|
$this->_setWidth(); |
18
|
|
|
$this->_code = str_replace('" frameborder', '?rel=0&wmode=transparent&autohide=1" frameborder', |
19
|
|
|
$this->_code); |
20
|
|
|
|
21
|
|
|
return $this->_code; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function _getYoutubeId() |
25
|
|
|
{ |
26
|
|
|
if (substr($this->_code, 0, 15) == 'http://youtu.be') { |
27
|
|
|
return substr($this->_code, 16, 11); |
28
|
|
View Code Duplication |
} else { |
|
|
|
|
29
|
|
|
if (substr($this->_code, 0, 7) == 'http://' || substr($this->_code, 0, 8) == 'https://') { |
30
|
|
|
parse_str(parse_url($this->_code, PHP_URL_QUERY), $querystringArray); |
31
|
|
|
|
32
|
|
|
return $querystringArray['v']; |
33
|
|
|
} else { |
34
|
|
|
$matches = []; |
35
|
|
|
preg_match('%embed\/(.+?)[\/\?\"]%', $this->_code, $matches); |
36
|
|
|
|
37
|
|
|
return isset($matches[1]) ? substr($matches[1], 0, 11) : null; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getThumbnail() |
43
|
|
|
{ |
44
|
|
|
$ytid = $this->_getYoutubeId(); |
45
|
|
|
if ($ytid) { |
46
|
|
|
$fullres = 'http://img.youtube.com/vi/'.$ytid.'/maxresdefault.jpg'; |
47
|
|
|
$ch = curl_init($fullres); |
48
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
49
|
|
|
curl_exec($ch); |
50
|
|
|
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
51
|
|
|
curl_close($ch); |
52
|
|
|
if ($http_status == 200) { |
53
|
|
|
return $fullres; |
54
|
|
|
} else { |
55
|
|
|
return 'http://img.youtube.com/vi/'.$ytid.'/0.jpg'; |
56
|
|
|
} |
57
|
|
|
} else { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.