Completed
Push — master ( 237036...650d4b )
by Dominik
02:47
created

Asset::loadFromCdn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Utils;
4
5
class Asset
6
{
7
    const DEFAULT_OPTIONS = [
8
        'dependencies' => [],
9
        'version' => null,
10
        'inFooter' => true,
11
        'media' => 'all'
12
    ];
13
14
    protected static $assetManifest;
15
16
    public static function requireUrl($asset)
17
    {
18
        return self::get('url', $asset);
19
    }
20
21
    public static function requirePath($asset)
22
    {
23
        return self::get('path', $asset);
24
    }
25
26
    public static function register($options)
27
    {
28
        return self::add('register', $options);
29
    }
30
31
    public static function enqueue($options)
32
    {
33
        return self::add('enqueue', $options);
34
    }
35
36
    protected static function get($returnType, $asset)
37
    {
38
        $distPath = get_template_directory() . '/dist';
39
40
        if (!isset(self::$assetManifest)) {
41
            $manifestPath = $distPath . '/rev-manifest.json';
42
            if (is_file($manifestPath)) {
43
                self::$assetManifest = json_decode(file_get_contents($manifestPath), true);
44
            } else {
45
                self::$assetManifest = [];
46
            }
47
        }
48
49
        if (array_key_exists($asset, self::$assetManifest)) {
50
            $assetSuffix = self::$assetManifest[$asset];
51
        } else {
52
            $assetSuffix = $asset;
53
        }
54
55
        if ('path' == $returnType) {
56
            return $distPath . '/' . $assetSuffix;
57
        } else if ('url' == $returnType) {
58
            $distUrl = get_template_directory_uri() . '/dist';
59
            return $distUrl . '/' . $assetSuffix;
60
        }
61
62
        return false;
63
    }
64
65
    protected static function add($funcType, $options)
66
    {
67
        // TODO add cdn functionality
68
        $options = array_merge(self::DEFAULT_OPTIONS, $options);
69
70
        if (!array_key_exists('name', $options)) {
71
            trigger_error('Cannot add asset: Name not provided!', E_USER_WARNING);
72
            return false;
73
        }
74
75
        if (!array_key_exists('path', $options)) {
76
            trigger_error('Cannot add asset: Path not provided!', E_USER_WARNING);
77
            return false;
78
        }
79
80
        $funcName = "wp_{$funcType}_{$options['type']}";
81
        $lastVar = $options['type'] === 'script' ? $options['inFooter'] : $options['media'];
82
83
        // allow external urls
84
        $path = $options['path'];
85
        if (!(StringHelpers::startsWith('http://', $path))
86
            && !(StringHelpers::startsWith('https://', $path))
87
            && !(StringHelpers::startsWith('//', $path))
88
        ) {
89
            $path = Asset::requireUrl($options['path']);
90
        }
91
92
        if (function_exists($funcName)) {
93
            $funcName(
94
                $options['name'],
95
                $path,
96
                $options['dependencies'],
97
                $options['version'],
98
                $lastVar
99
            );
100
101
            return true;
102
        }
103
104
        return false;
105
    }
106
}
107