Oembed::setSrcAsDataAttribute()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 12
c 1
b 1
f 0
dl 0
loc 17
rs 9.8666
cc 3
nc 2
nop 2
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 string $iframeTagHtml The oembed iframe HTML tag.
16
     * @param array $additionalGetParams 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
        if ($iframeTagHtml) {
24
            $Dom = new DOMDocument();
25
            $Dom->loadHTML($iframeTagHtml);
26
            $domNodes = $Dom->getElementsByTagName('iframe');
27
            foreach ($domNodes as $node) {
28
                $src = $node->getAttribute('src');
29
                // add additional get parameters to existing oembed url
30
                $src = add_query_arg($additionalGetParams, $src);
31
                $node->removeAttribute('src');
32
                $node->setAttribute('data-src', $src);
33
                $output .= $Dom->saveHTML($node);
34
            }
35
        }
36
        return $output;
37
    }
38
}
39