-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAssets.php
More file actions
259 lines (219 loc) · 5.95 KB
/
Assets.php
File metadata and controls
259 lines (219 loc) · 5.95 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
<?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;
/**
* @var array $assets_json_index
*/
protected $assets_json_index;
/**
* @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_print_styles', [ $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();
// Do not add a versioning query param in assets URLs if minified
$version = $this->is_minified() ? null : $theme->get( 'Version' );
// Js
$file = $this->is_minified() ? $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'],
true
);
// CSS
wp_register_style( 'theme-style', get_stylesheet_uri(), [], $version );
}
/**
* Enqueue the scripts
*/
public function enqueue_scripts(): void {
// JS
$this->assets_tools->enqueue_script( 'scripts' );
}
/**
* Enqueue the styles
*/
public function enqueue_styles(): void {
// CSS
$this->assets_tools->enqueue_style( 'theme-style' );
}
/**
* The stylesheet uri based on the dev or not constant
*
* @param string $stylesheet_uri
*
* @return string
* @author Nicolas Juen
*/
public function stylesheet_uri( string $stylesheet_uri ): string {
if ( $this->is_minified() ) {
$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 JS/CSS .min file based on assets.json
*
* @param string $type
*
* @return string
*/
public function get_min_file( string $type ): string {
if ( empty( $type ) ) {
return '';
}
$assets = $this->get_assets_json_index_file();
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 get assets json index only once
*
* @return array
*
* @author Léonard Phoumpakka
*/
protected function get_assets_json_index_file(): array {
if ( isset( $this->assets_json_index ) ) {
return $this->assets_json_index;
}
$this->assets_json_index = [];
if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) {
return $this->assets_json_index;
}
$json = file_get_contents( \get_theme_file_path( '/dist/assets.json' ) ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$assets = json_decode( $json, true );
if ( empty( $assets ) || JSON_ERROR_NONE !== json_last_error() ) {
return $this->assets_json_index;
}
$this->assets_json_index = $assets;
return $this->assets_json_index;
}
/**
* 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, eg: app.js, app-min.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;
}
if ( isset( $cache_data[ $file ] ) ) {
return $cache_data[ $file ];
}
$filename = strtok( $file, '.' );
$file = sprintf( '/dist/%s.asset.php', $filename );
if ( ! file_exists( \get_theme_file_path( $file ) ) ) {
$cache_data[ $file ] = $empty_asset_data;
return $cache_data[ $file ];
}
$cache_data[ $file ] = require \get_theme_file_path( $file );
return $cache_data[ $file ];
}
/**
* Check if we are on minified environment.
*
* @return bool
* @author Nicolas JUEN
*/
public function is_minified(): bool {
return ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false );
}
/**
* Change login CSS URL
* @return string
*/
public function login_stylesheet_uri(): string {
return $this->is_minified() ? 'dist/' . $this->get_min_file( 'login' ) : 'dist/login.css';
}
}