diff --git a/inc/Services/Assets.php b/inc/Services/Assets.php index c6eb96a4..7e008e24 100644 --- a/inc/Services/Assets.php +++ b/inc/Services/Assets.php @@ -20,6 +20,11 @@ class Assets implements Service { */ private $assets_tools; + /** + * @var array $assets_json_index + */ + protected $assets_json_index; + /** * @param Service_Container $container */ @@ -126,32 +131,27 @@ public function get_min_file( string $type ): string { return ''; } - if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) { - return ''; - } + $assets = $this->get_assets_json_index_file(); - $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() ) { + if ( empty( $assets ) ) { return ''; } switch ( $type ) { case 'css': - $file = $assets['app.css']; + $file = $assets['app.css'] ?? ''; break; case 'editor.css': - $file = $assets['editor.css']; + $file = $assets['editor.css'] ?? ''; break; case 'login': - $file = $assets['login.css']; + $file = $assets['login.css'] ?? ''; break; case 'editor.js': - $file = $assets['editor.js']; + $file = $assets['editor.js'] ?? ''; break; case 'js': - $file = $assets['app.js']; + $file = $assets['app.js'] ?? ''; break; default: $file = null; @@ -170,6 +170,36 @@ public function get_min_file( string $type ): string { 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. * diff --git a/inc/Services/Theme.php b/inc/Services/Theme.php index 86958390..e16921f2 100644 --- a/inc/Services/Theme.php +++ b/inc/Services/Theme.php @@ -2,12 +2,17 @@ namespace BEA\Theme\Framework\Services; +use BEA\Theme\Framework\Framework; use BEA\Theme\Framework\Service; use BEA\Theme\Framework\Service_Container; - class Theme implements Service { + /** + * @var Service|bool + */ + protected $asset; + /** * @param Service_Container $container */ @@ -17,7 +22,16 @@ public function register( Service_Container $container ): void {} * @param Service_Container $container */ public function boot( Service_Container $container ): void { + $this->asset = $container->get_service( 'assets' ); $this->after_setup_theme(); + /** + * @psalm-suppress PossiblyInvalidMethodCall + * @psalm-suppress UndefinedInterfaceMethod + */ + if ( $this->asset->is_minified() ) { + add_filter( 'ari_responsive_image_default_img_path', [ $this, 'set_ari_responsive_image_default_img_path' ] ); + add_filter( 'ari_responsive_image_default_img_name', [ $this, 'set_ari_responsive_image_default_img_name' ] ); + } } /** @@ -74,4 +88,38 @@ private function i18n(): void { // Load theme texdomain load_theme_textdomain( 'framework-textdomain', \get_theme_file_path( '/languages' ) ); } + + /** + * Set default path for ARI for minified files + * + * @return string + */ + public function set_ari_responsive_image_default_img_path(): string { + return '/dist/'; + } + + /** + * Set ari default image name for minified files + * + * @param string $default_img + * + * @return string + * + */ + public function set_ari_responsive_image_default_img_name( string $default_img ): string { + return $this->get_min_default_image( $default_img ); + } + + /** + * Get minified default_image + * + * @param string $original_image + * + * @return string + * + * @author Léonard Phoumpakka + */ + public function get_min_default_image( string $original_image ): string { + return $this->asset->get_min_file( 'assets/' . $original_image ); + } }