@@ -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 ' ) ? : 'app.js ' ;
6563 $ asset_data = $ this ->get_asset_data ( $ file );
6664 $ this ->assets_tools ->register_script (
6765 'scripts ' ,
@@ -80,40 +78,39 @@ 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- }
110+ $ file = $ this ->get_min_file ( 'css ' );
111+
112+ if ( ! empty ( $ file ) && file_exists ( \get_theme_file_path ( '/dist/ ' . $ file ) ) ) {
113+ return \get_theme_file_uri ( '/dist/ ' . $ file );
117114 }
118115
119116 if ( file_exists ( \get_theme_file_path ( '/dist/app.css ' ) ) ) {
@@ -124,9 +121,9 @@ public function stylesheet_uri( string $stylesheet_uri ): string {
124121 }
125122
126123 /**
127- * Return JS/CSS .min file based on assets.json
124+ * Return the compiled asset filename for a type from ` assets.json`.
128125 *
129- * @param string $type
126+ * @param string $type Asset type key (e.g. `js`, `css`, `editor.js`).
130127 *
131128 * @return string
132129 */
@@ -185,7 +182,7 @@ public function get_min_file( string $type ): string {
185182 * Asset data are produced by the webpack dependencies extraction plugin. They contain for each asset the list of
186183 * dependencies use by the asset and a hash representing the current version of the asset.
187184 *
188- * @param string $file The asset name including its extension, eg: app.js, app-min .js
185+ * @param string $file The asset name including its extension, e.g. ` app.js`.
189186 *
190187 * @return array{dependencies: string[], version:string} The asset data if available or an array with the default keys.
191188 */
@@ -202,37 +199,42 @@ public function get_asset_data( string $file ): array {
202199 return $ empty_asset_data ;
203200 }
204201
205- if ( isset ( $ cache_data [ $ file ] ) ) {
206- return $ cache_data [ $ file ];
202+ $ cache_key = $ file ;
203+
204+ if ( isset ( $ cache_data [ $ cache_key ] ) ) {
205+ return $ cache_data [ $ cache_key ];
207206 }
208207
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 ];
208+ $ filename = strtok ( $ file , '. ' );
209+ $ asset_php = sprintf ( '/dist/%s.asset.php ' , $ filename );
210+ if ( ! file_exists ( \get_theme_file_path ( $ asset_php ) ) ) {
211+ $ cache_data [ $ cache_key ] = $ empty_asset_data ;
212+ return $ cache_data [ $ cache_key ];
214213 }
215214
216- $ cache_data [ $ file ] = require \get_theme_file_path ( $ file );
215+ $ cache_data [ $ cache_key ] = require \get_theme_file_path ( $ asset_php );
217216
218- return $ cache_data [ $ file ];
217+ return $ cache_data [ $ cache_key ];
219218 }
220219
221220 /**
222- * Check if we are on minified environment .
221+ * Point the login page stylesheet to the built CSS in `dist/` when available .
223222 *
224- * @return bool
225- * @author Nicolas JUEN
223+ * @param string $stylesheet_path Default path relative to the theme root (from `wp_login_page_theme_css`).
224+ *
225+ * @return string Path relative to the theme root.
226226 */
227- public function is_minified (): bool {
228- return ( ! defined ( 'SCRIPT_DEBUG ' ) || SCRIPT_DEBUG === false );
229- }
227+ public function login_stylesheet_uri ( string $ stylesheet_path = '' ): string {
228+ $ file = $ this ->get_min_file ( 'login ' );
230229
231- /**
232- * Change login CSS URL
233- * @return string
234- */
235- public function login_stylesheet_uri (): string {
236- return $ this ->is_minified () ? 'dist/ ' . $ this ->get_min_file ( 'login ' ) : 'dist/login.css ' ;
230+ if ( ! empty ( $ file ) && file_exists ( \get_theme_file_path ( '/dist/ ' . $ file ) ) ) {
231+ return 'dist/ ' . $ file ;
232+ }
233+
234+ if ( file_exists ( \get_theme_file_path ( '/dist/login.css ' ) ) ) {
235+ return 'dist/login.css ' ;
236+ }
237+
238+ return $ stylesheet_path ;
237239 }
238240}
0 commit comments