Oembed::request()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 9.9332
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
It seems like $this->oembed->get_provider($url) can also be of type false; however, parameter $provider of WP_oEmbed::fetch() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        $request = $this->oembed->fetch(/** @scrutinizer ignore-type */ $this->oembed->get_provider($url), $url, [
Loading history...
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