ScriptLoader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 8
c 1
b 0
f 1
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A filterScriptLoaderTag() 0 14 4
1
<?php
2
3
namespace Flynt\Utils;
4
5
class ScriptLoader
6
{
7
    /**
8
     * Adds async/defer attributes to enqueued / registered scripts.
9
     *
10
     * If #12009 lands in WordPress, this function can no-op since it would be handled in core.
11
     *
12
     * @link https://core.trac.wordpress.org/ticket/12009
13
     *
14
     * @param string $tag    The script tag.
15
     * @param string $handle The script handle.
16
     * @return string Script HTML string.
17
     */
18
    public function filterScriptLoaderTag($tag, $handle)
19
    {
20
        foreach (['async', 'defer'] as $attr) {
21
            if (!wp_scripts()->get_data($handle, $attr)) {
22
                continue;
23
            }
24
            // Prevent adding attribute when already added in #12009.
25
            if (!preg_match(":\s$attr(=|>|\s):", $tag)) {
26
                $tag = preg_replace(':(?=></script>):', " $attr", $tag, 1);
27
            }
28
            // Only allow async or defer, not both.
29
            break;
30
        }
31
        return $tag;
32
    }
33
}
34