/
Barista.php
328 lines (279 loc) · 11 KB
/
Barista.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
class Barista
{
/**
* @var array
*/
private $entry_points;
/**
* @var array
*/
private $manifest;
/**
* @var array
*/
private $styles = [];
/**
* @var array
*/
private $edtr_domains = ['eventSmart'];
public function initialize()
{
add_action('wp_default_scripts', [$this, 'registerScripts']);
add_action('wp_default_styles', [$this, 'registerPackagesStyles']);
add_action('admin_enqueue_scripts', [$this, 'addAssets']);
add_action('admin_enqueue_scripts', [$this, 'enqueueScripts']);
}
/**
* Retrieves a URL to a file in the ee_barista plugin.
*
* @param string $path Relative path of the desired file.
*
* @return string Fully qualified URL pointing to the desired file.
*
* @since 0.0.1
*/
public function url(string $path): string
{
return EE_BARISTA_URL . $path;
}
/**
* @return array
*/
protected function getEntryPoints(): array
{
if (! $this->entry_points) {
$this->entry_points = array_keys($this->getManifest('entrypoints'));
}
return $this->entry_points;
}
/**
* @param string $key
* @return mixed
*/
protected function getManifest($key = 'files')
{
if (! $this->manifest) {
$manifest_path = EE_BARISTA_DIR . 'build/asset-manifest.json';
if (! file_exists($manifest_path)) {
wp_die('No manifest file found. Try yarn dev');
}
// TODO May be use WP File system? ¯\_(ツ)_/¯
$manifest_json = file_get_contents($manifest_path);
$this->manifest = json_decode($manifest_json, true);
}
if (empty($this->manifest[ $key ])) {
wp_die(sprintf('No entry for %1$s found in manifest file.', $key));
}
return $this->manifest[ $key ];
}
/**
* Registers a script according to `wp_register_script`. Honors this request by
* reassigning internal dependency properties of any script handle already
* registered by that name. It does not deregister the original script, to
* avoid losing inline scripts which may have been attached.
*
* @param WP_Scripts $scripts WP_Scripts instance.
* @param string $handle Name of the script. Should be unique.
* @param string $src Full URL of the script, or path of the script relative to the WordPress root
* directory.
* @param array $deps Optional. An array of registered script handles this script depends on.
* Default empty array.
* @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is
* added to the URL as a query string for cache busting purposes. If version is
* set to false, a version number is automatically added equal to current
* installed WordPress version. If set to null, no version is added.
* @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the
* <head>. Default 'false'.
* @since 0.0.1
*/
protected function overrideScript(
WP_Scripts $scripts,
string $handle,
string $src,
array $deps = [],
$ver = false,
$in_footer = false
) {
$script = $scripts->query($handle);
if ($script) {
/*
* In many ways, this is a reimplementation of `wp_register_script` but
* bypassing consideration of whether a script by the given handle had
* already been registered.
*/
// See: `_WP_Dependency::__construct` .
$script->src = $src;
$script->deps = $deps;
$script->ver = $ver;
$script->args = $in_footer;
} else {
$scripts->add($handle, $src, $deps, $ver, $in_footer);
}
$script = $scripts->query($handle);
if ($script) {
/*
* The script's `group` designation is an indication of whether it is
* to be printed in the header or footer. The behavior here defers to
* the arguments as passed. Specifically, group data is not assigned
* for a script unless it is designated to be printed in the footer.
*/
// See: `wp_register_script` .
unset($script->extra['group']);
if ($in_footer) {
$script->add_data('group', 1);
}
}
}
/**
* Registers a style according to `wp_register_style`. Honors this request by
* de-registering any style by the same handler before registration.
*
* @param WP_Styles $styles WP_Styles instance.
* @param string $handle Name of the stylesheet. Should be unique.
* @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress
* root directory.
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on.
* Default empty array.
* @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is
* added to the URL as a query string for cache busting purposes. If version is set
* to false, a version number is automatically added equal to current installed
* WordPress version. If set to null, no version is added.
* @param string $media Optional. The media for which this stylesheet has been defined.
* Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media
* queries like
* '(orientation: portrait)' and '(max-width: 640px)'.
* @since 0.0.1
*
*/
protected function overrideStyle(
WP_Styles $styles,
string $handle,
string $src,
array $deps = [],
$ver = false,
$media = 'all'
) {
$style = $styles->query($handle);
if ($style) {
$styles->remove($handle);
}
// save the handle to use for loading styles for dev env
$this->styles[ $handle ] = true;
$styles->add($handle, $src, $deps, $ver, $media);
}
/**
* Whether a domain depends upon eventEditor domain.
*
* @param string $entry_point
* @return void
*/
private function dependsUponEdtr($entry_point)
{
return in_array($entry_point, $this->edtr_domains, true);
}
/**
* Registers all the WordPress packages scripts that are in the standardized
* `build/` location.
*
* @param WP_Scripts $scripts WP_Scripts instance.
* @since 0.0.1
*/
public function registerScripts(WP_Scripts $scripts)
{
$asset_files = $this->getManifest();
$entry_points = $this->getEntryPoints();
foreach ($entry_points as $entry_point) {
$handle = 'eventespresso-' . $entry_point;
// Get the path from root directory as expected by `$this->url`.
$package_path = 'build' . $asset_files[ $entry_point . '.js' ];
$dependencies = [];
if (! empty($asset_files[ $entry_point . '.php' ])) {
$asset_file = EE_BARISTA_DIR . 'build' . $asset_files[ $entry_point . '.php' ];
$asset = file_exists($asset_file) ? require($asset_file) : null;
$dependencies = isset($asset['dependencies']) ? $asset['dependencies'] : $dependencies;
}
// remove cyclical dependencies, if any
if (($key = array_search($handle, $dependencies, true)) !== false) {
unset($dependencies[ $key ]);
}
if ($this->dependsUponEdtr($entry_point)) {
$dependencies[] = 'eventespresso-eventEditor';
}
$version = isset($asset['version']) ? $asset['version'] : '';
$version = $version === '' && file_exists(EE_BARISTA_DIR . $package_path)
? filemtime(EE_BARISTA_DIR . $package_path)
: espresso_version();
$this->overrideScript(
$scripts,
$handle,
$this->url($package_path),
$dependencies,
$version,
true
);
}
}
/**
* Registers all the packages and domain styles that are in the build folder.
*
* @param WP_Styles $styles WP_Styles instance.
* @since 0.0.1
*/
public function registerPackagesStyles(WP_Styles $styles)
{
$asset_files = $this->getManifest();
$entry_points = $this->getEntryPoints();
foreach ($entry_points as $entry_point) {
$handle = 'eventespresso-' . $entry_point;
$dependencies = []; // May be we need it some day ¯\_(ツ)_/¯
if (! empty($asset_files[ $entry_point . '.css' ])) {
$css_relative_path = 'build' . $asset_files[ $entry_point . '.css' ];
$css_absolute_path = EE_BARISTA_DIR . $css_relative_path;
if (file_exists($css_absolute_path)) {
$version = filemtime($css_absolute_path);
$this->overrideStyle(
$styles,
$handle,
$this->url($css_relative_path),
$dependencies,
$version
);
}
}
}
}
/**
* Enqueues assets that are loaded by exernal plugins/services.
*/
public function enqueueScripts($hook)
{
global $post;
$is_edtr_page = $post && $post->post_type === 'espresso_events';
$is_edtr_page = $is_edtr_page && ($hook == 'post-new.php' || $hook == 'post.php');
if (!$is_edtr_page) {
return;
}
$scripts = ['esEdtrSlots'];
foreach ($scripts as $handle) {
$handle = 'eventespresso-' . $handle;
if (wp_script_is($handle, 'registered')) {
wp_enqueue_script($handle);
}
}
}
public function addAssets()
{
// Enqueue the empty style with all the barista styles as deps.
wp_enqueue_style(
'ee-barista',
plugins_url('styles.css', __FILE__),
array_keys($this->styles)
);
wp_add_inline_script(
'eventespresso-i18n',
sprintf('var baristaAssetsUrl = "%s";', $this->url('build/')),
'before'
);
}
}