1 | <?php |
||
2 | |||
3 | namespace GeminiLabs\Castor; |
||
4 | |||
5 | use GeminiLabs\Castor\Helpers\Utility; |
||
6 | use DomDocument; |
||
7 | |||
8 | class Oembed |
||
9 | { |
||
10 | public $oembed; |
||
11 | public $utility; |
||
12 | |||
13 | public $vimeo = [ |
||
14 | 'api', 'autopause', 'autoplay', 'byline', 'color', 'height', 'loop', 'player_id', |
||
15 | 'portrait', 'title', 'width', |
||
16 | ]; |
||
17 | |||
18 | public $youtube = [ |
||
19 | 'autohide', 'autoplay', 'cc_load_policy', 'color', 'controls', 'disablekb', 'enablejsapi', |
||
20 | 'end', 'fs', 'height', 'hl', 'iv_load_policy', 'list', 'listType', 'loop', 'modestbranding', |
||
21 | 'origin', 'playerapiid', 'playlist', 'playsinline', 'rel', 'showinfo', 'start', 'theme', |
||
22 | 'width', |
||
23 | ]; |
||
24 | |||
25 | public function __construct(Utility $utility) |
||
26 | { |
||
27 | $this->oembed = _wp_oembed_get_object(); |
||
28 | $this->utility = $utility; |
||
29 | } |
||
30 | |||
31 | public function request($url, $args = '') |
||
32 | { |
||
33 | $args = wp_parse_args($args, [ |
||
34 | 'embed_type' => '', |
||
35 | ]); |
||
36 | $request = $this->oembed->fetch($this->oembed->get_provider($url), $url, [ |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
37 | 'width' => 1280, |
||
38 | 'height' => 1280, |
||
39 | ]); |
||
40 | if (false === $request) { |
||
41 | return; |
||
42 | } |
||
43 | if (!empty($args['embed_type']) && $args['embed_type'] != $request->type) { |
||
44 | return; |
||
45 | } |
||
46 | return $this->modifyRequest($request, $args); |
||
47 | } |
||
48 | |||
49 | protected function domLoad($html) |
||
50 | { |
||
51 | $dom = new DomDocument(); |
||
52 | $dom->loadHTML($html); |
||
53 | return $dom; |
||
54 | } |
||
55 | |||
56 | protected function modifyRequest($request, $args) |
||
57 | { |
||
58 | $providerName = strtolower($request->provider_name); |
||
59 | $provider = property_exists($this, $providerName) |
||
60 | ? $this->$providerName |
||
61 | : []; |
||
62 | |||
63 | $method = $this->utility->buildMethodName($providerName.'_request', 'modify'); |
||
64 | |||
65 | if (method_exists($this, $method)) { |
||
66 | return call_user_func([$this, $method], $request, array_intersect_key( |
||
67 | $args, |
||
68 | array_flip($provider) |
||
69 | )); |
||
70 | } |
||
71 | return $request; |
||
72 | } |
||
73 | |||
74 | protected function modifyYoutubeRequest($request, array $args) |
||
75 | { |
||
76 | $html = $this->domLoad($request->html); |
||
77 | $node = $html->getElementsByTagName('iframe')->item(0); |
||
78 | $url = $node->getAttribute('src'); |
||
79 | |||
80 | if (isset($args['fs']) && 0 == $args['fs']) { |
||
81 | $node->removeAttribute('allowfullscreen'); |
||
82 | } |
||
83 | |||
84 | $args['origin'] = urlencode(get_bloginfo('url')); |
||
85 | |||
86 | $node->setAttribute('class', 'video-embed'); |
||
87 | $node->setAttribute('src', |
||
88 | add_query_arg($args, remove_query_arg('feature', $url)) |
||
89 | ); |
||
90 | |||
91 | $request->html = $html->saveHTML($node); |
||
92 | |||
93 | return $request; |
||
94 | } |
||
95 | } |
||
96 |