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