@@ -55,13 +55,11 @@ public function register_assets(): void {
5555 if ( is_admin () ) {
5656 return ;
5757 }
58- $ theme = wp_get_theme ();
5958
60- // Do not add a versioning query param in assets URLs if minified
61- $ version = $ this ->is_minified () ? null : $ theme ->get ( 'Version ' );
59+ $ theme = wp_get_theme ();
6260
63- // Js
64- $ file = $ this ->is_minified () ? $ this -> get_min_file ( 'js ' ) : ' app.js ' ;
61+ // JavaScript
62+ $ file = $ this ->get_min_file ( 'js ' );
6563 $ asset_data = $ this ->get_asset_data ( $ file );
6664 $ this ->assets_tools ->register_script (
6765 'scripts ' ,
@@ -80,53 +78,48 @@ public function register_assets(): void {
8078 ),
8179 );
8280
83- // CSS
84- wp_register_style ( 'theme-style ' , get_stylesheet_uri (), [], $ version );
81+ // Styles
82+ wp_register_style ( 'theme-style ' , get_stylesheet_uri (), [], $ theme -> get ( ' Version ' ) );
8583 }
8684
8785 /**
8886 * Enqueue the scripts
8987 */
9088 public function enqueue_scripts (): void {
91- // JS
89+ // JavaScript
9290 $ this ->assets_tools ->enqueue_script ( 'scripts ' );
9391 }
9492
9593 /**
9694 * Enqueue the styles
9795 */
9896 public function enqueue_styles (): void {
99- // CSS
97+ // Styles
10098 $ this ->assets_tools ->enqueue_style ( 'theme-style ' );
10199 }
102100
103101 /**
104- * The stylesheet uri based on the dev or not constant
102+ * Point the theme stylesheet to the built CSS in `dist/` when `assets.json` is present.
105103 *
106- * @param string $stylesheet_uri
104+ * @param string $stylesheet_uri Default theme stylesheet URI.
107105 *
108106 * @return string
109107 * @author Nicolas Juen
110108 */
111109 public function stylesheet_uri ( string $ stylesheet_uri ): string {
112- if ( $ this ->is_minified () ) {
113- $ file = $ this ->get_min_file ( 'css ' );
114- if ( ! empty ( $ file ) && file_exists ( \get_theme_file_path ( '/dist/ ' . $ file ) ) ) {
115- return \get_theme_file_uri ( '/dist/ ' . $ file );
116- }
117- }
110+ $ file = $ this ->get_min_file ( 'css ' );
118111
119- if ( file_exists ( \get_theme_file_path ( '/dist/app.css ' ) ) ) {
120- return \get_theme_file_uri ( '/dist/app.css ' );
112+ if ( ! empty ( $ file ) && file_exists ( \get_theme_file_path ( '/dist/ ' . $ file ) ) ) {
113+ return \get_theme_file_uri ( '/dist/ ' . $ file );
121114 }
122115
123116 return $ stylesheet_uri ;
124117 }
125118
126119 /**
127- * Return JS/CSS .min file based on assets.json
120+ * Return the compiled asset filename for a type from ` assets.json`.
128121 *
129- * @param string $type
122+ * @param string $type Asset type key (e.g. `js`, `css`, `editor.js`).
130123 *
131124 * @return string
132125 */
@@ -185,7 +178,7 @@ public function get_min_file( string $type ): string {
185178 * Asset data are produced by the webpack dependencies extraction plugin. They contain for each asset the list of
186179 * dependencies use by the asset and a hash representing the current version of the asset.
187180 *
188- * @param string $file The asset name including its extension, eg: app.js, app-min .js
181+ * @param string $file The asset name including its extension, e.g. ` app.js`.
189182 *
190183 * @return array{dependencies: string[], version:string} The asset data if available or an array with the default keys.
191184 */
@@ -202,37 +195,29 @@ public function get_asset_data( string $file ): array {
202195 return $ empty_asset_data ;
203196 }
204197
205- if ( isset ( $ cache_data [ $ file ] ) ) {
206- return $ cache_data [ $ file ];
198+ $ cache_key = $ file ;
199+
200+ if ( isset ( $ cache_data [ $ cache_key ] ) ) {
201+ return $ cache_data [ $ cache_key ];
207202 }
208203
209- $ filename = strtok ( $ file , '. ' );
210- $ file = sprintf ( '/dist/%s.asset.php ' , $ filename );
211- if ( ! file_exists ( \get_theme_file_path ( $ file ) ) ) {
212- $ cache_data [ $ file ] = $ empty_asset_data ;
213- return $ cache_data [ $ file ];
204+ $ filename = strtok ( $ file , '. ' );
205+ $ asset_php = sprintf ( '/dist/%s.asset.php ' , $ filename );
206+ if ( ! file_exists ( \get_theme_file_path ( $ asset_php ) ) ) {
207+ $ cache_data [ $ cache_key ] = $ empty_asset_data ;
208+ return $ cache_data [ $ cache_key ];
214209 }
215210
216- $ cache_data [ $ file ] = require \get_theme_file_path ( $ file );
211+ $ cache_data [ $ cache_key ] = require \get_theme_file_path ( $ asset_php );
217212
218- return $ cache_data [ $ file ];
219- }
220-
221- /**
222- * Check if we are on minified environment.
223- *
224- * @return bool
225- * @author Nicolas JUEN
226- */
227- public function is_minified (): bool {
228- return ( ! defined ( 'SCRIPT_DEBUG ' ) || SCRIPT_DEBUG === false );
213+ return $ cache_data [ $ cache_key ];
229214 }
230215
231216 /**
232217 * Change login CSS URL
233218 * @return string
234219 */
235220 public function login_stylesheet_uri (): string {
236- return $ this -> is_minified () ? 'dist/ ' . $ this ->get_min_file ( 'login ' ) : ' dist/login.css ' ;
221+ return 'dist/ ' . $ this ->get_min_file ( 'login ' );
237222 }
238223}
0 commit comments