Completed
Branch Gutenberg/block-manager (11740c)
by
unknown
66:22 queued 53:17
created

AssetManager::enqueueCssSAsset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 3
dl 0
loc 10
c 0
b 0
f 0
cc 3
eloc 5
nop 1
rs 9.4285
1
<?php
2
3
namespace EventEspresso\core\services\assets;
4
5
use EventEspresso\core\domain\DomainInterface;
6
use EventEspresso\core\domain\values\assets\JavascriptAsset;
7
use EventEspresso\core\domain\values\assets\ManifestFile;
8
use EventEspresso\core\domain\values\assets\StylesheetAsset;
9
use EventEspresso\core\exceptions\InvalidDataTypeException;
10
use EventEspresso\core\exceptions\InvalidEntityException;
11
use EventEspresso\core\services\collections\DuplicateCollectionIdentifierException;
12
13
/**
14
 * Class AssetManager
15
 * Manager class for helping with adding and retrieving Asset objects from an AssetCollection
16
 *
17
 * @package EventEspresso\core\services\assets
18
 * @author  Brent Christensen
19
 * @since   $VID:$
20
 */
21
abstract class AssetManager implements AssetManagerInterface
22
{
23
24
    /**
25
     * @var AssetCollection $assets
26
     */
27
    protected $assets;
28
29
    /**
30
     * @var DomainInterface
31
     */
32
    protected $domain;
33
34
    /**
35
     * @var Registry $registry
36
     */
37
    protected $registry;
38
39
40
    /**
41
     * AssetRegister constructor.
42
     *
43
     * @param DomainInterface $domain
44
     * @param AssetCollection $assets
45
     * @param Registry        $registry
46
     */
47
    public function __construct(DomainInterface $domain, AssetCollection $assets, Registry $registry)
48
    {
49
        $this->domain = $domain;
50
        $this->assets = $assets;
51
        $this->registry = $registry;
52
        add_action('wp_enqueue_scripts', array($this, 'addManifestFile'), 0);
53
        add_action('admin_enqueue_scripts', array($this, 'addManifestFile'), 0);
54
        add_action('wp_enqueue_scripts', array($this, 'addAssets'), 2);
55
        add_action('admin_enqueue_scripts', array($this, 'addAssets'), 2);
56
    }
57
58
59
    /**
60
     * @return void
61
     * @throws DuplicateCollectionIdentifierException
62
     * @throws InvalidDataTypeException
63
     * @throws InvalidEntityException
64
     * @since $VID:$
65
     */
66
    public function addManifestFile()
67
    {
68
        // if a manifest file has already been added for this domain, then just return that one
69
        if ($this->assets->has($this->domain->assetNamespace())) {
70
            return;
71
        }
72
        $asset = new ManifestFile($this->domain);
73
        $this->assets->add($asset, $this->domain->assetNamespace());
74
    }
75
76
77
    /**
78
     * @return ManifestFile[]
79
     * @since $VID:$
80
     */
81
    public function getManifestFile()
82
    {
83
        return $this->assets->getManifestFiles();
84
    }
85
86
87
    /**
88
     * @param string $handle
89
     * @param string $source
90
     * @param array  $dependencies
91
     * @param bool   $load_in_footer
92
     * @return JavascriptAsset
93
     * @throws DuplicateCollectionIdentifierException
94
     * @throws InvalidDataTypeException
95
     * @throws InvalidEntityException
96
     * @since $VID:$
97
     */
98
    public function addJavascript(
99
        $handle,
100
        $source,
101
        array $dependencies = array(),
102
        $load_in_footer = true
103
    ) {
104
        $asset = new JavascriptAsset(
105
            $handle,
106
            $source,
107
            $dependencies,
108
            $load_in_footer,
109
            $this->domain
110
        );
111
        $this->assets->add($asset, $handle);
112
        return $asset;
113
    }
114
115
116
    /**
117
     * @return JavascriptAsset[]
118
     * @since $VID:$
119
     */
120
    public function getJavascriptAssets()
121
    {
122
        return $this->assets->getJavascriptAssets();
123
    }
124
125
126
    /**
127
     * @param string $handle
128
     * @param string $source
129
     * @param array  $dependencies
130
     * @param string $media
131
     * @return StylesheetAsset
132
     * @throws DuplicateCollectionIdentifierException
133
     * @throws InvalidDataTypeException
134
     * @throws InvalidEntityException
135
     * @since $VID:$
136
     */
137
    public function addStylesheet(
138
        $handle,
139
        $source,
140
        array $dependencies = array(),
141
        $media = 'all'
142
    ) {
143
        $asset = new StylesheetAsset(
144
            $handle,
145
            $source,
146
            $dependencies,
147
            $this->domain,
148
            $media
149
        );
150
        $this->assets->add($asset, $handle);
151
        return $asset;
152
    }
153
154
155
    /**
156
     * @return StylesheetAsset[]
157
     * @since $VID:$
158
     */
159
    public function getStylesheetAssets()
160
    {
161
        return $this->assets->getStylesheetAssets();
162
    }
163
164
165
    /**
166
     * @param string $handle
167
     * @return bool
168
     * @since $VID:$
169
     */
170
    public function enqueueAsset($handle)
171
    {
172
        if ($this->assets->has($handle)) {
173
            $asset = $this->assets->get($handle);
174
            if ($asset->isRegistered()) {
175
                $asset->enqueueAsset();
176
                return true;
177
            }
178
        }
179
        return false;
180
    }
181
}
182