Completed
Branch FET/event-question-group-refac... (2cd1c1)
by
unknown
55:42 queued 46:21
created

AssetManager::addJs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\assets;
4
5
use DomainException;
6
use EventEspresso\core\domain\DomainInterface;
7
use EventEspresso\core\domain\values\assets\JavascriptAsset;
8
use EventEspresso\core\domain\values\assets\ManifestFile;
9
use EventEspresso\core\domain\values\assets\StylesheetAsset;
10
use EventEspresso\core\domain\values\assets\VendorJavascriptAsset;
11
use EventEspresso\core\exceptions\InvalidDataTypeException;
12
use EventEspresso\core\exceptions\InvalidEntityException;
13
use EventEspresso\core\services\collections\DuplicateCollectionIdentifierException;
14
15
/**
16
 * Class AssetManager
17
 * Manager class for helping with adding and retrieving Asset objects from an AssetCollection
18
 *
19
 * @package EventEspresso\core\services\assets
20
 * @author  Brent Christensen
21
 * @since   4.9.62.p
22
 */
23
abstract class AssetManager implements AssetManagerInterface
24
{
25
26
    /**
27
     * @var AssetCollection $assets
28
     */
29
    protected $assets;
30
31
    /**
32
     * @var DomainInterface
33
     */
34
    protected $domain;
35
36
    /**
37
     * @var Registry $registry
38
     */
39
    protected $registry;
40
41
42
    /**
43
     * AssetRegister constructor.
44
     *
45
     * @param DomainInterface $domain
46
     * @param AssetCollection $assets
47
     * @param Registry        $registry
48
     */
49
    public function __construct(DomainInterface $domain, AssetCollection $assets, Registry $registry)
50
    {
51
        $this->domain = $domain;
52
        $this->assets = $assets;
53
        $this->registry = $registry;
54
        add_action('wp_enqueue_scripts', array($this, 'addManifestFile'), 0);
55
        add_action('admin_enqueue_scripts', array($this, 'addManifestFile'), 0);
56
        add_action('wp_enqueue_scripts', array($this, 'addAssets'), 2);
57
        add_action('admin_enqueue_scripts', array($this, 'addAssets'), 2);
58
    }
59
60
61
    /**
62
     * @since 4.9.71.p
63
     * @return string
64
     */
65
    public function assetNamespace()
66
    {
67
        return $this->domain->assetNamespace();
68
    }
69
70
71
    /**
72
     * @return void
73
     * @throws DuplicateCollectionIdentifierException
74
     * @throws InvalidDataTypeException
75
     * @throws InvalidEntityException
76
     * @since 4.9.62.p
77
     */
78
    public function addManifestFile()
79
    {
80
        // if a manifest file has already been added for this domain, then just return
81
        if ($this->assets->has($this->domain->assetNamespace())) {
82
            return;
83
        }
84
        $asset = new ManifestFile($this->domain);
85
        $this->assets->add($asset, $this->domain->assetNamespace());
86
    }
87
88
89
    /**
90
     * @return ManifestFile[]
91
     * @since 4.9.62.p
92
     */
93
    public function getManifestFile()
94
    {
95
        return $this->assets->getManifestFiles();
96
    }
97
98
99
    /**
100
     * @param string $handle
101
     * @param string $source
102
     * @param array  $dependencies
103
     * @param bool   $load_in_footer
104
     * @return JavascriptAsset
105
     * @throws DuplicateCollectionIdentifierException
106
     * @throws InvalidDataTypeException
107
     * @throws InvalidEntityException
108
     * @since 4.9.62.p
109
     */
110 View Code Duplication
    public function addJavascript(
111
        $handle,
112
        $source,
113
        array $dependencies = array(),
114
        $load_in_footer = true
115
    ) {
116
        $asset = new JavascriptAsset(
117
            $handle,
118
            $source,
119
            array_unique($dependencies),
120
            $load_in_footer,
121
            $this->domain
122
        );
123
        $this->assets->add($asset, $handle);
124
        return $asset;
125
    }
126
127
128
    /**
129
     * Used to register a javascript asset where everything is dynamically derived from the given handle.
130
     *
131
     * @param string $handle
132
     * @param string|array  $extra_dependencies
133
     * @return JavascriptAsset
134
     * @throws DuplicateCollectionIdentifierException
135
     * @throws InvalidDataTypeException
136
     * @throws InvalidEntityException
137
     */
138 View Code Duplication
    public function addJs($handle, $extra_dependencies = [])
139
    {
140
        $dependencies = $this->registry->getJsDependencies(
141
            $this->domain->assetNamespace(),
142
            $handle
143
        );
144
        $dependencies = ! empty( $extra_dependencies )
145
            ? array_merge(( array ) $extra_dependencies, $dependencies)
146
            : $dependencies;
147
        return $this->addJavascript(
148
            $handle,
149
            $this->registry->getJsUrl($this->domain->assetNamespace(), $handle),
150
            $dependencies
151
        );
152
    }
153
154
155
    /**
156
     * @param string $handle
157
     * @param array  $dependencies
158
     * @param bool   $load_in_footer
159
     * @return JavascriptAsset
160
     * @throws DuplicateCollectionIdentifierException
161
     * @throws InvalidDataTypeException
162
     * @throws InvalidEntityException
163
     * @throws DomainException
164
     * @since 4.9.71.p
165
     */
166
    public function addVendorJavascript(
167
        $handle,
168
        array $dependencies = array(),
169
        $load_in_footer = true
170
    ) {
171
        $dev_suffix = wp_scripts_get_suffix('dev');
172
        $vendor_path = $this->domain->pluginUrl() . 'assets/vendor/';
173
        return $this->addJavascript(
174
            $handle,
175
            "{$vendor_path}{$handle}{$dev_suffix}.js",
176
            $dependencies,
177
            $load_in_footer
178
        );
179
    }
180
181
182
183
    /**
184
     * @param string $handle
185
     * @param string $source
186
     * @param array  $dependencies
187
     * @param string $media
188
     * @return StylesheetAsset
189
     * @throws DuplicateCollectionIdentifierException
190
     * @throws InvalidDataTypeException
191
     * @throws InvalidEntityException
192
     * @since 4.9.62.p
193
     */
194 View Code Duplication
    public function addStylesheet(
195
        $handle,
196
        $source,
197
        array $dependencies = array(),
198
        $media = 'all'
199
    ) {
200
        $asset = new StylesheetAsset(
201
            $handle,
202
            $source,
203
            array_unique($dependencies),
204
            $this->domain,
205
            $media
206
        );
207
        $this->assets->add($asset, $handle);
208
        return $asset;
209
    }
210
211
212
    /**
213
     * Used to register a css asset where everything is dynamically derived from the given handle.
214
     *
215
     * @param string $handle
216
     * @param string|array  $extra_dependencies
217
     * @return StylesheetAsset
218
     * @throws DuplicateCollectionIdentifierException
219
     * @throws InvalidDataTypeException
220
     * @throws InvalidEntityException
221
     */
222 View Code Duplication
    public function addCss($handle, $extra_dependencies = [])
223
    {
224
        $dependencies = $this->registry->getCssDependencies(
225
            $this->domain->assetNamespace(),
226
            $handle
227
        );
228
        $dependencies = ! empty( $extra_dependencies )
229
            ? array_merge(( array ) $extra_dependencies, $dependencies)
230
            : $dependencies;
231
        return $this->addStylesheet(
232
            $handle,
233
            $this->registry->getCssUrl($this->domain->assetNamespace(), $handle),
234
            $dependencies
235
        );
236
    }
237
238
239
    /**
240
     * @param string $handle
241
     * @return bool
242
     * @since 4.9.62.p
243
     */
244
    public function enqueueAsset($handle)
245
    {
246
        if ($this->assets->has($handle)) {
247
            $asset = $this->assets->get($handle);
248
            if ($asset->isRegistered()) {
249
                $asset->enqueueAsset();
250
                return true;
251
            }
252
        }
253
        return false;
254
    }
255
}
256