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

BlockAssetManager::addDefaultBlockStyleDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\assets;
4
5
use EventEspresso\core\domain\entities\editor\BlockInterface;
6
use EventEspresso\core\domain\services\assets\CoreAssetManager;
7
use EventEspresso\core\domain\values\assets\BrowserAsset;
8
use EventEspresso\core\domain\values\assets\JavascriptAsset;
9
use EventEspresso\core\domain\values\assets\StylesheetAsset;
10
use EventEspresso\core\exceptions\InvalidDataTypeException;
11
use EventEspresso\core\exceptions\InvalidEntityException;
12
use EventEspresso\core\services\collections\DuplicateCollectionIdentifierException;
13
14
/**
15
 * Class BlockAssetRegister
16
 * Abstract parent for classes that handle asset registration for one or more editor blocks that share the same assets
17
 *
18
 * @package EventEspresso\core\services\editor
19
 * @author  Brent Christensen
20
 * @since   4.9.71.p
21
 */
22
abstract class BlockAssetManager extends AssetManager implements BlockAssetManagerInterface
23
{
24
25
    /**
26
     * @var string $editor_script_handle
27
     */
28
    private $editor_script_handle;
29
30
    /**
31
     * @var string $editor_style_handle
32
     */
33
    private $editor_style_handle;
34
35
    /**
36
     * @var string $script_handle
37
     */
38
    private $script_handle;
39
40
    /**
41
     * @var string $style_handle
42
     */
43
    private $style_handle;
44
45
46
    /**
47
     * @return string
48
     */
49
    public function getEditorScriptHandle()
50
    {
51
        return $this->editor_script_handle;
52
    }
53
54
55
    /**
56
     * @param string $editor_script_handle
57
     */
58 View Code Duplication
    public function setEditorScriptHandle($editor_script_handle)
59
    {
60
        if(strpos($editor_script_handle, BlockInterface::NAME_SPACE . '-') !== 0) {
61
            $editor_script_handle = BlockInterface::NAME_SPACE . '-' . $editor_script_handle;
62
        }
63
        $this->editor_script_handle = $editor_script_handle;
64
    }
65
66
67
    /**
68
     * @return string
69
     */
70
    public function getEditorStyleHandle()
71
    {
72
        return $this->editor_style_handle;
73
    }
74
75
76
    /**
77
     * @param string $editor_style_handle
78
     */
79 View Code Duplication
    public function setEditorStyleHandle($editor_style_handle)
80
    {
81
        if (strpos($editor_style_handle, BlockInterface::NAME_SPACE . '-') !== 0) {
82
            $editor_style_handle = BlockInterface::NAME_SPACE . '-' . $editor_style_handle;
83
        }
84
        $this->editor_style_handle = $editor_style_handle;
85
    }
86
87
88
    /**
89
     * @return string
90
     */
91
    public function getScriptHandle()
92
    {
93
        return $this->script_handle;
94
    }
95
96
97
    /**
98
     * @param string $script_handle
99
     */
100 View Code Duplication
    public function setScriptHandle($script_handle)
101
    {
102
        if (strpos($script_handle, BlockInterface::NAME_SPACE . '-') !== 0) {
103
            $script_handle = BlockInterface::NAME_SPACE . '-' . $script_handle;
104
        }
105
        $this->script_handle = $script_handle;
106
    }
107
108
109
    /**
110
     * @return string
111
     */
112
    public function getStyleHandle()
113
    {
114
        return $this->style_handle;
115
    }
116
117
118
    /**
119
     * @param string $style_handle
120
     */
121 View Code Duplication
    public function setStyleHandle($style_handle)
122
    {
123
        if (strpos($style_handle, BlockInterface::NAME_SPACE . '-') !== 0) {
124
            $style_handle = BlockInterface::NAME_SPACE . '-' . $style_handle;
125
        }
126
        $this->style_handle = $style_handle;
127
    }
128
129
    /**
130
     * @since 4.9.71.p
131
     * @throws InvalidDataTypeException
132
     * @throws InvalidEntityException
133
     * @throws DuplicateCollectionIdentifierException
134
     */
135
    public function addAssets()
136
    {
137
        $this->addEditorScript($this->getEditorScriptHandle());
138
        $this->addEditorStyle($this->getEditorStyleHandle());
139
        $this->addScript($this->getScriptHandle());
140
        $this->addStyle($this->getStyleHandle());
141
    }
142
143
144
    /**
145
     * @param       $handle
146
     * @param array $dependencies
147
     * @since 4.9.71.p
148
     * @return JavascriptAsset
149
     * @throws InvalidDataTypeException
150
     * @throws InvalidEntityException
151
     * @throws DuplicateCollectionIdentifierException
152
     */
153 View Code Duplication
    public function addEditorScript($handle, array $dependencies = array())
154
    {
155
        if ($this->assets->hasJavascriptAsset($handle)){
156
            return $this->assets->getJavascriptAsset($handle);
157
        }
158
        return $this->addJs($handle, $dependencies)->setRequiresTranslation();
159
    }
160
161
162
    /**
163
     * @param        $handle
164
     * @param array  $dependencies
165
     * @since 4.9.71.p
166
     * @return StylesheetAsset
167
     * @throws InvalidDataTypeException
168
     * @throws InvalidEntityException
169
     * @throws DuplicateCollectionIdentifierException
170
     */
171 View Code Duplication
    public function addEditorStyle($handle, array $dependencies = array())
172
    {
173
        if ($this->assets->hasStylesheetAsset($handle)) {
174
            return $this->assets->getStylesheetAsset($handle);
175
        }
176
        return $this->addCss($handle, $dependencies);
177
    }
178
179
180
    /**
181
     * @param       $handle
182
     * @param array $dependencies
183
     * @since 4.9.71.p
184
     * @return JavascriptAsset
185
     * @throws InvalidDataTypeException
186
     * @throws InvalidEntityException
187
     * @throws DuplicateCollectionIdentifierException
188
     */
189 View Code Duplication
    public function addScript($handle, array $dependencies = array())
190
    {
191
        if ($this->assets->hasJavascriptAsset($handle)) {
192
            return $this->assets->getJavascriptAsset($handle);
193
        }
194
        return $this->addJs($handle, $dependencies)->setRequiresTranslation();
195
    }
196
197
198
    /**
199
     * @param        $handle
200
     * @param array  $dependencies
201
     * @since 4.9.71.p
202
     * @return StylesheetAsset
203
     * @throws InvalidDataTypeException
204
     * @throws InvalidEntityException
205
     * @throws DuplicateCollectionIdentifierException
206
     */
207 View Code Duplication
    public function addStyle($handle, array $dependencies = array())
208
    {
209
        if ($this->assets->hasStylesheetAsset($handle)) {
210
            return $this->assets->getStylesheetAsset($handle);
211
        }
212
        return $this->addCss($handle, $dependencies);
213
    }
214
215
216
    /**
217
     * @return JavascriptAsset|null
218
     */
219
    public function getEditorScript()
220
    {
221
        return $this->assets->getJavascriptAsset($this->editor_script_handle);
222
    }
223
224
225
    /**
226
     * @return StylesheetAsset|null
227
     */
228
    public function getEditorStyle()
229
    {
230
        return $this->assets->getStylesheetAsset($this->editor_style_handle);
231
    }
232
233
234
    /**
235
     * @return JavascriptAsset|null
236
     */
237
    public function getScript()
238
    {
239
        return $this->assets->getJavascriptAsset($this->script_handle);
240
    }
241
242
243
    /**
244
     * @return StylesheetAsset|null
245
     */
246
    public function getStyle()
247
    {
248
        return $this->assets->getStylesheetAsset($this->style_handle);
249
    }
250
251
252
    /**
253
     * @return  void
254
     */
255
    public function enqueueAssets()
256
    {
257
        $assets = array(
258
            $this->getEditorScript(),
259
            $this->getEditorStyle(),
260
            $this->getScript(),
261
            $this->getStyle(),
262
        );
263
        foreach ($assets as $asset) {
264
            if ($asset instanceof BrowserAsset && $asset->isRegistered()) {
265
                $asset->enqueueAsset();
266
            }
267
        }
268
    }
269
270
}
271