Repository navigation
Expand file tree
/
Copy pathAssets.php
More file actions
270 lines (225 loc) · 6.46 KB
/
Copy pathAssets.php
File metadata and controls
270 lines (225 loc) · 6.46 KB
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
<?php
namespace BEA\Theme\Framework\Services;
use BEA\Theme\Framework\Service;
use BEA\Theme\Framework\Service_Container;
use BEA\Theme\Framework\Tools\Assets as Assets_Tools;
use function json_last_error;
use const JSON_ERROR_NONE;
/**
* Class Assets
*
* @package BEA\Theme\Framework
*/
class Assets implements Service {
/**
* @var Assets_Tools
*/
private $assets_tools;
/**
* @param Service_Container $container
*/
public function register( Service_Container $container ): void {
$this->assets_tools = new Assets_Tools();
}
/**
* @param Service_Container $container
*/
public function boot( Service_Container $container ): void {
/**
* Add hooks for the scripts and styles to hook on
*/
add_action( 'wp', [ $this, 'register_assets' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ] );
add_filter( 'stylesheet_uri', [ $this, 'stylesheet_uri' ] );
add_filter( 'wp_login_page_theme_css', [ $this, 'login_stylesheet_uri' ] );
}
/**
* @return string
*/
public function get_service_name(): string {
return 'assets';
}
/**
* Register all the Theme assets
*/
public function register_assets(): void {
if ( is_admin() ) {
return;
}
$theme = wp_get_theme();
// JavaScript
$file = $this->get_min_file( 'js' ) ?: 'app.js';
$asset_data = $this->get_asset_data( $file );
$this->assets_tools->register_script(
'scripts',
'dist/' . $file,
array_merge( [ 'jquery' ], $asset_data['dependencies'] ), // ensure jQuery dependency is set even if not declared explicitly in the JS
$asset_data['version'],
[ 'strategy' => 'defer' ]
);
wp_add_inline_script(
'scripts',
'const THEME_DATA = ' . wp_json_encode(
[
'themeUri' => get_template_directory_uri(),
]
),
);
// Styles
wp_register_style( 'theme-style', get_stylesheet_uri(), [], $theme->get( 'Version' ) );
}
/**
* Enqueue the scripts
*/
public function enqueue_scripts(): void {
// JavaScript
$this->assets_tools->enqueue_script( 'scripts' );
}
/**
* Enqueue the styles
*/
public function enqueue_styles(): void {
// Styles
$this->assets_tools->enqueue_style( 'theme-style' );
}
/**
* Point the theme stylesheet to the built CSS in `dist/` when `assets.json` is present.
*
* @param string $stylesheet_uri Default theme stylesheet URI.
*
* @return string
* @author Nicolas Juen
*/
public function stylesheet_uri( string $stylesheet_uri ): string {
$file = $this->get_min_file( 'css' );
if ( ! empty( $file ) && file_exists( \get_theme_file_path( '/dist/' . $file ) ) ) {
return \get_theme_file_uri( '/dist/' . $file );
}
if ( file_exists( \get_theme_file_path( '/dist/app.css' ) ) ) {
return \get_theme_file_uri( '/dist/app.css' );
}
return $stylesheet_uri;
}
/**
* Return the compiled asset filename for a type from `assets.json`.
*
* @param string $type Asset type key (e.g. `js`, `css`, `editor.js`).
*
* @return string
*/
public function get_min_file( string $type ): string {
if ( empty( $type ) ) {
return '';
}
$assets = $this->get_assets_manifest();
if ( empty( $assets ) ) {
return '';
}
switch ( $type ) {
case 'css':
$file = $assets['app.css'];
break;
case 'editor.css':
$file = $assets['editor.css'];
break;
case 'login':
$file = $assets['login.css'];
break;
case 'editor.js':
$file = $assets['editor.js'];
break;
case 'js':
$file = $assets['app.js'];
break;
default:
$file = null;
break;
}
// Custom type
if ( ! empty( $assets[ $type ] ) ) {
$file = $assets[ $type ];
}
if ( empty( $file ) ) {
return '';
}
return $file;
}
/**
* Read and decode the `dist/assets.json` manifest once per request.
*
* The manifest is requested several times per request (scripts registration,
* `stylesheet_uri` filter, editor assets…): memoize the decoded content to
* avoid repeated filesystem reads and JSON decoding.
*
* @return array<string, string> Manifest entries, or an empty array if unavailable/invalid.
*/
private function get_assets_manifest(): array {
static $manifest = null;
if ( null !== $manifest ) {
return $manifest;
}
$manifest = [];
$manifest_path = \get_theme_file_path( '/dist/assets.json' );
if ( ! file_exists( $manifest_path ) ) {
return $manifest;
}
$json = file_get_contents( $manifest_path ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$assets = json_decode( $json, true );
if ( ! is_array( $assets ) || JSON_ERROR_NONE !== json_last_error() ) {
return $manifest;
}
$manifest = $assets;
return $manifest;
}
/**
* Retrieve data for a compiled asset file.
*
* Asset data are produced by the webpack dependencies extraction plugin. They contain for each asset the list of
* dependencies use by the asset and a hash representing the current version of the asset.
*
* @param string $file The asset name including its extension, e.g. `app.js`.
*
* @return array{dependencies: string[], version:string} The asset data if available or an array with the default keys.
*/
public function get_asset_data( string $file ): array {
static $cache_data;
$empty_asset_data = [
'dependencies' => [],
'version' => '',
];
$file = trim( $file );
if ( empty( $file ) ) {
return $empty_asset_data;
}
$cache_key = $file;
if ( isset( $cache_data[ $cache_key ] ) ) {
return $cache_data[ $cache_key ];
}
$filename = strtok( $file, '.' );
$asset_php = sprintf( '/dist/%s.asset.php', $filename );
if ( ! file_exists( \get_theme_file_path( $asset_php ) ) ) {
$cache_data[ $cache_key ] = $empty_asset_data;
return $cache_data[ $cache_key ];
}
$cache_data[ $cache_key ] = require \get_theme_file_path( $asset_php );
return $cache_data[ $cache_key ];
}
/**
* Point the login page stylesheet to the built CSS in `dist/` when available.
*
* @param string $stylesheet_path Default path relative to the theme root (from `wp_login_page_theme_css`).
*
* @return string Path relative to the theme root.
*/
public function login_stylesheet_uri( string $stylesheet_path = '' ): string {
$file = $this->get_min_file( 'login' );
if ( ! empty( $file ) && file_exists( \get_theme_file_path( '/dist/' . $file ) ) ) {
return 'dist/' . $file;
}
if ( file_exists( \get_theme_file_path( '/dist/login.css' ) ) ) {
return 'dist/login.css';
}
return $stylesheet_path;
}
}