Completed
Branch Gutenberg/form-system (bade44)
by
unknown
149:40 queued 140:13
created

AssetManager::addVendorJavascript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 14
rs 9.7998
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 $VID:$
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
    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
            $dependencies,
120
            $load_in_footer,
121
            $this->domain
122
        );
123
        $this->assets->add($asset, $handle);
124
        return $asset;
125
    }
126
127
128
    /**
129
     * @param string $handle
130
     * @param array  $dependencies
131
     * @param bool   $load_in_footer
132
     * @return JavascriptAsset
133
     * @throws DuplicateCollectionIdentifierException
134
     * @throws InvalidDataTypeException
135
     * @throws InvalidEntityException
136
     * @throws DomainException
137
     * @since $VID:$
138
     */
139
    public function addVendorJavascript(
140
        $handle,
141
        array $dependencies = array(),
142
        $load_in_footer = true
143
    ) {
144
        $dev_suffix = wp_scripts_get_suffix('dev');
145
        $vendor_path = $this->domain->pluginUrl() . 'assets/vendor/';
146
        return $this->addJavascript(
147
            $handle,
148
            "{$vendor_path}{$handle}{$dev_suffix}.js",
149
            $dependencies,
150
            $load_in_footer
151
        );
152
    }
153
154
155
156
    /**
157
     * @param string $handle
158
     * @param string $source
159
     * @param array  $dependencies
160
     * @param string $media
161
     * @return StylesheetAsset
162
     * @throws DuplicateCollectionIdentifierException
163
     * @throws InvalidDataTypeException
164
     * @throws InvalidEntityException
165
     * @since 4.9.62.p
166
     */
167
    public function addStylesheet(
168
        $handle,
169
        $source,
170
        array $dependencies = array(),
171
        $media = 'all'
172
    ) {
173
        $asset = new StylesheetAsset(
174
            $handle,
175
            $source,
176
            $dependencies,
177
            $this->domain,
178
            $media
179
        );
180
        $this->assets->add($asset, $handle);
181
        return $asset;
182
    }
183
184
185
    /**
186
     * @param string $handle
187
     * @return bool
188
     * @since 4.9.62.p
189
     */
190
    public function enqueueAsset($handle)
191
    {
192
        if ($this->assets->has($handle)) {
193
            $asset = $this->assets->get($handle);
194
            if ($asset->isRegistered()) {
195
                $asset->enqueueAsset();
196
                return true;
197
            }
198
        }
199
        return false;
200
    }
201
}
202