Completed
Branch FET/add-copy-attendee-at-admin (b30562)
by
unknown
36:10 queued 27:01
created

AssetManager::getAssetDetails()   B

Complexity

Conditions 7
Paths 27

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 27
nop 3
dl 0
loc 30
rs 8.5066
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\Asset;
8
use EventEspresso\core\domain\values\assets\JavascriptAsset;
9
use EventEspresso\core\domain\values\assets\ManifestFile;
10
use EventEspresso\core\domain\values\assets\StylesheetAsset;
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
     * @param string $version
105
     * @return JavascriptAsset
106
     * @throws DuplicateCollectionIdentifierException
107
     * @throws InvalidDataTypeException
108
     * @throws InvalidEntityException
109
     * @throws DomainException
110
     * @since 4.9.62.p
111
     */
112 View Code Duplication
    public function addJavascript(
113
        $handle,
114
        $source,
115
        array $dependencies = array(),
116
        $load_in_footer = true,
117
        $version = ''
118
    ) {
119
        $asset = new JavascriptAsset(
120
            $handle,
121
            $source,
122
            array_unique($dependencies),
123
            $load_in_footer,
124
            $this->domain,
125
            $version
126
        );
127
        $this->assets->add($asset, $handle);
128
        return $asset;
129
    }
130
131
132
    /**
133
     * Used to register a javascript asset where everything is dynamically derived from the given handle.
134
     *
135
     * @param string       $handle
136
     * @param string|array $extra_dependencies
137
     * @return JavascriptAsset
138
     * @throws DuplicateCollectionIdentifierException
139
     * @throws InvalidDataTypeException
140
     * @throws InvalidEntityException
141
     * @throws DomainException
142
     */
143 View Code Duplication
    public function addJs($handle, $extra_dependencies = [])
144
    {
145
        $details = $this->getAssetDetails(
146
            Asset::TYPE_JS,
147
            $handle,
148
            $extra_dependencies
149
        );
150
        return $this->addJavascript(
151
            $handle,
152
            $this->registry->getJsUrl($this->domain->assetNamespace(), $handle),
153
            $details['dependencies'],
154
            true,
155
            $details['version']
156
        );
157
    }
158
159
160
    /**
161
     * @param string $handle
162
     * @param array  $dependencies
163
     * @param bool   $load_in_footer
164
     * @param string $version
165
     * @return JavascriptAsset
166
     * @throws DomainException
167
     * @throws DuplicateCollectionIdentifierException
168
     * @throws InvalidDataTypeException
169
     * @throws InvalidEntityException
170
     * @since 4.9.71.p
171
     */
172
    public function addVendorJavascript(
173
        $handle,
174
        array $dependencies = array(),
175
        $load_in_footer = true,
176
        $version = ''
177
    ) {
178
        $dev_suffix = wp_scripts_get_suffix('dev');
179
        $vendor_path = $this->domain->pluginUrl() . 'assets/vendor/';
180
        return $this->addJavascript(
181
            $handle,
182
            "{$vendor_path}{$handle}{$dev_suffix}.js",
183
            $dependencies,
184
            $load_in_footer,
185
            $version
186
        );
187
    }
188
189
190
    /**
191
     * @param string $handle
192
     * @param string $source
193
     * @param array  $dependencies
194
     * @param string $media
195
     * @param string $version
196
     * @return StylesheetAsset
197
     * @throws DomainException
198
     * @throws DuplicateCollectionIdentifierException
199
     * @throws InvalidDataTypeException
200
     * @throws InvalidEntityException
201
     * @since 4.9.62.p
202
     */
203 View Code Duplication
    public function addStylesheet(
204
        $handle,
205
        $source,
206
        array $dependencies = array(),
207
        $media = 'all',
208
        $version = ''
209
    ) {
210
        $asset = new StylesheetAsset(
211
            $handle,
212
            $source,
213
            array_unique($dependencies),
214
            $this->domain,
215
            $media,
216
            $version
217
        );
218
        $this->assets->add($asset, $handle);
219
        return $asset;
220
    }
221
222
223
    /**
224
     * Used to register a css asset where everything is dynamically derived from the given handle.
225
     *
226
     * @param string       $handle
227
     * @param string|array $extra_dependencies
228
     * @return StylesheetAsset
229
     * @throws DuplicateCollectionIdentifierException
230
     * @throws InvalidDataTypeException
231
     * @throws InvalidEntityException
232
     * @throws DomainException
233
     */
234 View Code Duplication
    public function addCss($handle, $extra_dependencies = [])
235
    {
236
        $details = $this->getAssetDetails(
237
            Asset::TYPE_CSS,
238
            $handle,
239
            $extra_dependencies
240
        );
241
        return $this->addStylesheet(
242
            $handle,
243
            $this->registry->getCssUrl($this->domain->assetNamespace(), $handle),
244
            $details['dependencies'],
245
            'all',
246
            $details['version']
247
        );
248
    }
249
250
251
    /**
252
     * @param string $handle
253
     * @return bool
254
     * @since 4.9.62.p
255
     */
256
    public function enqueueAsset($handle)
257
    {
258
        if ($this->assets->has($handle)) {
259
            $asset = $this->assets->get($handle);
260
            if ($asset->isRegistered()) {
261
                $asset->enqueueAsset();
262
                return true;
263
            }
264
        }
265
        return false;
266
    }
267
268
269
    /**
270
     * @param string $asset_type
271
     * @param string $handle
272
     * @param array  $extra_dependencies
273
     * @return array
274
     * @since $VID:$
275
     */
276
    private function getAssetDetails($asset_type, $handle, $extra_dependencies = [])
277
    {
278
        $getAssetDetails = '';
279
        switch ($asset_type) {
280
            case Asset::TYPE_JS :
281
                $getAssetDetails = 'getJsAssetDetails';
282
                break;
283
            case Asset::TYPE_CSS :
284
                $getAssetDetails = 'getCssAssetDetails';
285
                break;
286
        }
287
        if ($getAssetDetails === '') {
288
            return ['dependencies' => [], 'version' => ''];
289
        }
290
        $details = $this->registry->$getAssetDetails(
291
            $this->domain->assetNamespace(),
292
            $handle
293
        );
294
        $details['dependencies'] = isset($details['dependencies'])
295
            ? $details['dependencies']
296
            : [];
297
        $details['version'] = isset($details['version'])
298
            ? $details['version']
299
            : '';
300
        $details['dependencies'] = ! empty($extra_dependencies)
301
            ? array_merge($details['dependencies'], (array) $extra_dependencies)
302
            : $details['dependencies'];
303
        return $details;
304
305
    }
306
}
307