|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\View\Embed; |
|
4
|
|
|
|
|
5
|
|
|
use Embed\Extractor; |
|
6
|
|
|
use Embed\Embed; |
|
7
|
|
|
use Embed\EmbedCode; |
|
8
|
|
|
use Embed\Http\Crawler; |
|
9
|
|
|
use Psr\Http\Message\UriInterface; |
|
10
|
|
|
use SilverStripe\Core\Manifest\ModuleResourceLoader; |
|
11
|
|
|
|
|
12
|
|
|
class EmbedContainer implements Embeddable |
|
13
|
|
|
{ |
|
14
|
|
|
private ?Embed $embed = null; |
|
15
|
|
|
|
|
16
|
|
|
private ?Extractor $extractor = null; |
|
17
|
|
|
|
|
18
|
|
|
private ?Crawler $crawler = null; |
|
19
|
|
|
|
|
20
|
|
|
private string $url; |
|
21
|
|
|
|
|
22
|
|
|
private array $options = []; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(string $url) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->url = $url; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return int |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getWidth() |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var EmbedCode $code */ |
|
35
|
|
|
$code = $this->getExtractor()->code; |
|
36
|
|
|
return $code ? ($code->width ?: 100) : 100; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return int |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getHeight() |
|
43
|
|
|
{ |
|
44
|
|
|
/** @var EmbedCode $code */ |
|
45
|
|
|
$code = $this->getExtractor()->code; |
|
46
|
|
|
return $code ? ($code->height ?: 100) : 100; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getPreviewURL() |
|
53
|
|
|
{ |
|
54
|
|
|
$extractor = $this->getExtractor(); |
|
55
|
|
|
|
|
56
|
|
|
// // Use thumbnail url |
|
57
|
|
|
if ($extractor->image) { |
|
58
|
|
|
return $extractor->image; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Use direct image type |
|
62
|
|
|
$method = 'getApi'; |
|
63
|
|
|
if (method_exists($extractor, $method)) { |
|
64
|
|
|
$api = $extractor->$method(); |
|
65
|
|
|
if ($api && method_exists($api, 'url') && $api->url('direct_link')) { |
|
66
|
|
|
return $this->getExtractor()->url; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Default media |
|
71
|
|
|
return ModuleResourceLoader::resourceURL( |
|
72
|
|
|
'silverstripe/asset-admin:client/dist/images/icon_file.png' |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getName() |
|
80
|
|
|
{ |
|
81
|
|
|
$extractor = $this->getExtractor(); |
|
82
|
|
|
if ($extractor->title) { |
|
83
|
|
|
return $extractor->title; |
|
84
|
|
|
} |
|
85
|
|
|
if ($extractor->url instanceof UriInterface) { |
|
|
|
|
|
|
86
|
|
|
return preg_replace('/\?.*/', '', basename($extractor->url->getPath())); |
|
87
|
|
|
} |
|
88
|
|
|
return ''; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getType() |
|
95
|
|
|
{ |
|
96
|
|
|
// doesn't work? |
|
97
|
|
|
return $this->getExtractor()->type; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public function validate() |
|
104
|
|
|
{ |
|
105
|
|
|
return !empty($this->getExtractor()->code->html ?? ''); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function setOptions(array $options): Embeddable |
|
109
|
|
|
{ |
|
110
|
|
|
$this->options = $options; |
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getOptions(): array |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->options; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getEmbed(): Embed |
|
120
|
|
|
{ |
|
121
|
|
|
if (!$this->embed) { |
|
122
|
|
|
$this->embed = new Embed($this->getCrawler()); |
|
123
|
|
|
} |
|
124
|
|
|
return $this->embed; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function getExtractor(): Extractor |
|
128
|
|
|
{ |
|
129
|
|
|
if (!$this->extractor) { |
|
130
|
|
|
$this->extractor = $this->getEmbed()->get($this->url); |
|
131
|
|
|
} |
|
132
|
|
|
return $this->extractor; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function setCrawler(?Crawler $crawler): self |
|
136
|
|
|
{ |
|
137
|
|
|
$this->crawler = $crawler; |
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function getCrawler(): ?Crawler |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->crawler; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|