Oembed::setSrcAsDataAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Utils;
4
5
use DOMDocument;
6
7
class Oembed
8
{
9
    /**
10
     * Sets an oembed source as data attribute.
11
     * Possibility to append query variables to the URL by using additional get parameters.
12
     *
13
     * @since 0.2.0
14
     *
15
     * @param $iframeTagHtml string The oembed iframe HTML tag.
16
     * @param $additionalGetParams array Associative array of key/values of additional query variables to append to the url.
17
     *
18
     * @return string The modified oembed iframe HTML tag.
19
     */
20
    public static function setSrcAsDataAttribute($iframeTagHtml, $additionalGetParams)
21
    {
22
        $output = '';
23
        $Dom = new DOMDocument();
24
        $Dom->loadHTML($iframeTagHtml);
25
        $domNodes = $Dom->getElementsByTagName('iframe');
26
        foreach ($domNodes as $node) {
27
            $src = $node->getAttribute('src');
28
            // add additional get parameters to existing oembed url
29
            $src = add_query_arg($additionalGetParams, $src);
30
            $node->removeAttribute('src');
31
            $node->setAttribute('data-src', $src);
32
            $output .= $Dom->saveHTML($node);
33
        }
34
        return $output;
35
    }
36
}
37