Skip to content

Commit f3ca55d

Browse files
committed
fix method to read asseets.json file index & fix default img
1 parent fe49f62 commit f3ca55d

File tree

2 files changed

+53
-52
lines changed

2 files changed

+53
-52
lines changed

inc/Services/Assets.php

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class Assets implements Service {
2020
*/
2121
private $assets_tools;
2222

23+
/**
24+
* @var array $assets_json_index
25+
*/
26+
protected $assets_json_index;
27+
2328
/**
2429
* @param Service_Container $container
2530
*/
@@ -126,32 +131,23 @@ public function get_min_file( string $type ): string {
126131
return '';
127132
}
128133

129-
if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) {
130-
return '';
131-
}
132-
133-
$json = file_get_contents( \get_theme_file_path( '/dist/assets.json' ) ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
134-
$assets = json_decode( $json, true );
135-
136-
if ( empty( $assets ) || JSON_ERROR_NONE !== json_last_error() ) {
137-
return '';
138-
}
134+
$assets = $this->get_assets_json_index_file();
139135

140136
switch ( $type ) {
141137
case 'css':
142-
$file = $assets['app.css'];
138+
$file = $assets['app.css'] ?? '';
143139
break;
144140
case 'editor.css':
145-
$file = $assets['editor.css'];
141+
$file = $assets['editor.css'] ?? '';
146142
break;
147143
case 'login':
148-
$file = $assets['login.css'];
144+
$file = $assets['login.css'] ?? '';
149145
break;
150146
case 'editor.js':
151-
$file = $assets['editor.js'];
147+
$file = $assets['editor.js'] ?? '';
152148
break;
153149
case 'js':
154-
$file = $assets['app.js'];
150+
$file = $assets['app.js'] ?? '';
155151
break;
156152
default:
157153
$file = null;
@@ -170,6 +166,36 @@ public function get_min_file( string $type ): string {
170166
return $file;
171167
}
172168

169+
/**
170+
* Read and get assets json index only once
171+
*
172+
* @return array
173+
*
174+
* @author Léonard Phoumpakka
175+
*/
176+
protected function get_assets_json_index_file(): array {
177+
if ( isset( $this->assets_json_index ) ) {
178+
return $this->assets_json_index;
179+
}
180+
181+
$this->assets_json_index = [];
182+
183+
if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) {
184+
return $this->assets_json_index;
185+
}
186+
187+
$json = file_get_contents( \get_theme_file_path( '/dist/assets.json' ) ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
188+
$assets = json_decode( $json, true );
189+
190+
if ( empty( $assets ) || JSON_ERROR_NONE !== json_last_error() ) {
191+
return $this->assets_json_index;
192+
}
193+
194+
$this->assets_json_index = $assets;
195+
196+
return $this->assets_json_index;
197+
}
198+
173199
/**
174200
* Retrieve data for a compiled asset file.
175201
*

inc/Services/Theme.php

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ public function register( Service_Container $container ): void {}
1818
*/
1919
public function boot( Service_Container $container ): void {
2020
$this->after_setup_theme();
21-
add_filter( 'ari_responsive_image_default_img_path', [ $this, 'set_ari_responsive_image_default_img_path' ] );
22-
add_filter( 'ari_responsive_image_default_img_name', [ $this, 'set_ari_responsive_image_default_img_name' ] );
21+
/**
22+
* @psalm-suppress PossiblyInvalidMethodCall
23+
* @psalm-suppress UndefinedInterfaceMethod
24+
*/
25+
if ( Framework::get_container()->get_service( 'assets' )->is_minified() ) {
26+
add_filter( 'ari_responsive_image_default_img_path', [ $this, 'set_ari_responsive_image_default_img_path' ] );
27+
add_filter( 'ari_responsive_image_default_img_name', [ $this, 'set_ari_responsive_image_default_img_name' ] );
28+
}
2329
}
2430

2531
/**
@@ -86,14 +92,6 @@ private function i18n(): void {
8692
*
8793
*/
8894
public function set_ari_responsive_image_default_img_path( string $attr ): string {
89-
/**
90-
* @psalm-suppress PossiblyInvalidMethodCall
91-
* @psalm-suppress UndefinedInterfaceMethod
92-
*/
93-
if ( ! Framework::get_container()->get_service( 'assets' )->is_minified() ) {
94-
return $attr;
95-
}
96-
9795
return '/dist/';
9896
}
9997

@@ -106,15 +104,7 @@ public function set_ari_responsive_image_default_img_path( string $attr ): strin
106104
*
107105
*/
108106
public function set_ari_responsive_image_default_img_name( string $default_img ): string {
109-
/**
110-
* @psalm-suppress PossiblyInvalidMethodCall
111-
* @psalm-suppress UndefinedInterfaceMethod
112-
*/
113-
if ( ! Framework::get_container()->get_service( 'assets' )->is_minified() ) {
114-
return $default_img;
115-
}
116-
117-
return $this->get_min_image( $default_img );
107+
return $this->get_min_default_image( $default_img );
118108
}
119109

120110
/**
@@ -124,24 +114,9 @@ public function set_ari_responsive_image_default_img_name( string $default_img )
124114
*
125115
* @return string
126116
*
117+
* @author Léonard Phoumpakka
127118
*/
128-
protected function get_min_image( string $original_image ): string {
129-
130-
if ( empty( $original_image ) ) {
131-
return $original_image;
132-
}
133-
134-
if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) {
135-
return $original_image;
136-
}
137-
138-
$json = file_get_contents( \get_theme_file_path( '/dist/assets.json' ) ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
139-
$assets = json_decode( $json, true );
140-
141-
if ( empty( $assets ) || JSON_ERROR_NONE !== json_last_error() ) {
142-
return $original_image;
143-
}
144-
145-
return $assets[ 'assets/' . $original_image ] ?: $original_image;
119+
public function get_min_default_image( string $original_image ): string {
120+
return Framework::get_container()->get_service( 'assets' )->get_min_file( 'assets/' . $original_image );
146121
}
147122
}

0 commit comments

Comments
 (0)