Completed
Branch Gutenberg/master (a5e5d1)
by
unknown
147:31 queued 134:10
created

BlockAssetManager::getEditorScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace EventEspresso\core\services\assets;
4
5
use EventEspresso\core\domain\services\assets\CoreAssetManager;
6
use EventEspresso\core\domain\values\assets\BrowserAsset;
7
use EventEspresso\core\domain\values\assets\JavascriptAsset;
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 BlockAssetRegister
15
 * Abstract parent for classes that handle asset registration for one or more editor blocks that share the same assets
16
 *
17
 * @package EventEspresso\core\services\editor
18
 * @author  Brent Christensen
19
 * @since   $VID:$
20
 */
21
abstract class BlockAssetManager extends AssetManager implements BlockAssetManagerInterface
22
{
23
24
    /**
25
     * @var string $editor_script_handle
26
     */
27
    private $editor_script_handle;
28
29
    /**
30
     * @var string $editor_style_handle
31
     */
32
    private $editor_style_handle;
33
34
    /**
35
     * @var string $script_handle
36
     */
37
    private $script_handle;
38
39
    /**
40
     * @var string $style_handle
41
     */
42
    private $style_handle;
43
44
45
    /**
46
     * @return string
47
     */
48
    public function getEditorScriptHandle()
49
    {
50
        return $this->editor_script_handle;
51
    }
52
53
54
    /**
55
     * @param string $editor_script_handle
56
     */
57
    public function setEditorScriptHandle($editor_script_handle)
58
    {
59
        $this->editor_script_handle = $editor_script_handle;
60
    }
61
62
63
    /**
64
     * @return string
65
     */
66
    public function getEditorStyleHandle()
67
    {
68
        return $this->editor_style_handle;
69
    }
70
71
72
    /**
73
     * @param string $editor_style_handle
74
     */
75
    public function setEditorStyleHandle($editor_style_handle)
76
    {
77
        $this->editor_style_handle = $editor_style_handle;
78
    }
79
80
81
    /**
82
     * @return string
83
     */
84
    public function getScriptHandle()
85
    {
86
        return $this->script_handle;
87
    }
88
89
90
    /**
91
     * @param string $script_handle
92
     */
93
    public function setScriptHandle($script_handle)
94
    {
95
        $this->script_handle = $script_handle;
96
    }
97
98
99
    /**
100
     * @return string
101
     */
102
    public function getStyleHandle()
103
    {
104
        return $this->style_handle;
105
    }
106
107
108
    /**
109
     * @param string $style_handle
110
     */
111
    public function setStyleHandle($style_handle)
112
    {
113
        $this->style_handle = $style_handle;
114
    }
115
116
    /**
117
     * @since $VID:$
118
     * @throws InvalidDataTypeException
119
     * @throws InvalidEntityException
120
     * @throws DuplicateCollectionIdentifierException
121
     */
122
    public function addAssets()
123
    {
124
        $this->addEditorScript($this->getEditorScriptHandle());
125
        $this->addEditorStyle($this->getEditorStyleHandle());
126
        $this->setScriptHandle($this->getScriptHandle());
127
        $this->setStyleHandle($this->getStyleHandle());
128
    }
129
130
131
    /**
132
     * @param       $handle
133
     * @param array $dependencies
134
     * @since $VID:$
135
     * @return JavascriptAsset
136
     * @throws InvalidDataTypeException
137
     * @throws InvalidEntityException
138
     * @throws DuplicateCollectionIdentifierException
139
     */
140 View Code Duplication
    public function addEditorScript($handle, array $dependencies = array())
141
    {
142
        if($this->assets->has($handle)){
143
            return $this->assets->get($handle);
144
        }
145
        return parent::addJavascript(
146
            $handle,
147
            $this->registry->getJsUrl(
148
                $this->domain->assetNamespace(),
149
                $handle
150
            ),
151
            $this->addDefaultBlockScriptDependencies($dependencies)
152
        );
153
    }
154
155
156
    /**
157
     * @param        $handle
158
     * @param array  $dependencies
159
     * @since $VID:$
160
     * @return StylesheetAsset
161
     * @throws InvalidDataTypeException
162
     * @throws InvalidEntityException
163
     * @throws DuplicateCollectionIdentifierException
164
     */
165 View Code Duplication
    public function addEditorStyle($handle, array $dependencies = array())
166
    {
167
        if ($this->assets->has($handle)) {
168
            return $this->assets->get($handle);
169
        }
170
        return parent::addStylesheet(
171
            $handle,
172
            $this->registry->getCssUrl(
173
                $this->domain->assetNamespace(),
174
                $handle
175
            ),
176
            $dependencies
177
        );
178
    }
179
180
181
    /**
182
     * @param       $handle
183
     * @param array $dependencies
184
     * @since $VID:$
185
     * @return JavascriptAsset
186
     * @throws InvalidDataTypeException
187
     * @throws InvalidEntityException
188
     * @throws DuplicateCollectionIdentifierException
189
     */
190 View Code Duplication
    public function addScript($handle, array $dependencies = array())
191
    {
192
        if ($this->assets->has($handle)) {
193
            return $this->assets->get($handle);
194
        }
195
        return parent::addJavascript(
196
            $handle,
197
            $this->registry->getJsUrl(
198
                $this->domain->assetNamespace(),
199
                $handle
200
            ),
201
            $this->addDefaultBlockScriptDependencies($dependencies)
202
        );
203
    }
204
205
206
    /**
207
     * @param        $handle
208
     * @param array  $dependencies
209
     * @since $VID:$
210
     * @return StylesheetAsset
211
     * @throws InvalidDataTypeException
212
     * @throws InvalidEntityException
213
     * @throws DuplicateCollectionIdentifierException
214
     */
215 View Code Duplication
    public function addStyle($handle, array $dependencies = array())
216
    {
217
        if ($this->assets->has($handle)) {
218
            return $this->assets->get($handle);
219
        }
220
        return parent::addStylesheet(
221
            $handle,
222
            $this->registry->getCssUrl(
223
                $this->domain->assetNamespace(),
224
                $handle
225
            ),
226
            $dependencies
227
        );
228
    }
229
230
231
    /**
232
     * @param array $dependencies
233
     * @return array
234
     */
235
    protected function addDefaultBlockScriptDependencies(array $dependencies)
236
    {
237
        $dependencies += array(
238
                CoreAssetManager::JS_HANDLE_EE_JS_CORE,
239
                'wp-blocks',    // Provides useful functions and components for extending the editor
240
                'wp-i18n',      // Provides localization functions
241
                'wp-element',   // Provides React.Component
242
                'wp-components' // Provides many prebuilt components and controls
243
            );
244
        return $dependencies;
245
    }
246
247
248
    /**
249
     * @param string $handle
250
     * @since $VID:$
251
     * @return mixed|null
252
     */
253
    public function getAsset($handle)
254
    {
255
        if ($this->assets->has($handle)) {
256
            return $this->assets->get($handle);
257
        }
258
        return null;
259
    }
260
261
262
    /**
263
     * @return JavascriptAsset|null
264
     */
265
    public function getEditorScript()
266
    {
267
        return $this->getAsset($this->editor_script_handle);
268
    }
269
270
271
    /**
272
     * @return StylesheetAsset|null
273
     */
274
    public function getEditorStyle()
275
    {
276
        return $this->getAsset($this->editor_style_handle);
277
    }
278
279
280
    /**
281
     * @return JavascriptAsset|null
282
     */
283
    public function getScript()
284
    {
285
        return $this->getAsset($this->script_handle);
286
    }
287
288
289
    /**
290
     * @return StylesheetAsset|null
291
     */
292
    public function getStyle()
293
    {
294
        return $this->getAsset($this->style_handle);
295
    }
296
297
298
    /**
299
     * @return  void
300
     */
301
    public function enqueueAssets()
302
    {
303
        $assets = array(
304
            $this->getEditorScript(),
305
            $this->getEditorStyle(),
306
            $this->getScript(),
307
            $this->getStyle(),
308
        );
309
        foreach ($assets as $asset) {
310
            if ($asset instanceof BrowserAsset && $asset->isRegistered()) {
311
                $asset->enqueueAsset();
312
            }
313
        }
314
    }
315
316
}
317