From fe49f622011a1c362999517738612aaa8dd1e6f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=CC=81onard=20Phoumpakka?= Date: Mon, 9 Jan 2023 09:41:27 +0100 Subject: [PATCH 1/4] fix ARI default image --- inc/Services/Theme.php | 72 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/inc/Services/Theme.php b/inc/Services/Theme.php index 86958390..da3a627a 100644 --- a/inc/Services/Theme.php +++ b/inc/Services/Theme.php @@ -2,10 +2,10 @@ 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 { /** @@ -18,6 +18,8 @@ public function register( Service_Container $container ): void {} */ public function boot( Service_Container $container ): void { $this->after_setup_theme(); + 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 +76,72 @@ 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 + * + * @param string $attr + * + * @return string + * + */ + public function set_ari_responsive_image_default_img_path( string $attr ): string { + /** + * @psalm-suppress PossiblyInvalidMethodCall + * @psalm-suppress UndefinedInterfaceMethod + */ + if ( ! Framework::get_container()->get_service( 'assets' )->is_minified() ) { + return $attr; + } + + 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 { + /** + * @psalm-suppress PossiblyInvalidMethodCall + * @psalm-suppress UndefinedInterfaceMethod + */ + if ( ! Framework::get_container()->get_service( 'assets' )->is_minified() ) { + return $default_img; + } + + return $this->get_min_image( $default_img ); + } + + /** + * Get minified default_image + * + * @param string $original_image + * + * @return string + * + */ + protected function get_min_image( string $original_image ): string { + + if ( empty( $original_image ) ) { + return $original_image; + } + + if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) { + return $original_image; + } + + $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 $original_image; + } + + return $assets[ 'assets/' . $original_image ] ?: $original_image; + } } From f3ca55d0b4fe9c5f55c307c8376cccc2e0fe4678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=CC=81onard=20Phoumpakka?= Date: Wed, 11 Jan 2023 10:48:47 +0100 Subject: [PATCH 2/4] fix method to read asseets.json file index & fix default img --- inc/Services/Assets.php | 56 ++++++++++++++++++++++++++++++----------- inc/Services/Theme.php | 49 +++++++++--------------------------- 2 files changed, 53 insertions(+), 52 deletions(-) diff --git a/inc/Services/Assets.php b/inc/Services/Assets.php index c6eb96a4..9ebdcbe0 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,23 @@ public function get_min_file( string $type ): string { return ''; } - if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) { - return ''; - } - - $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 ''; - } + $assets = $this->get_assets_json_index_file(); 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 +166,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 da3a627a..66770214 100644 --- a/inc/Services/Theme.php +++ b/inc/Services/Theme.php @@ -18,8 +18,14 @@ public function register( Service_Container $container ): void {} */ public function boot( Service_Container $container ): void { $this->after_setup_theme(); - 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' ] ); + /** + * @psalm-suppress PossiblyInvalidMethodCall + * @psalm-suppress UndefinedInterfaceMethod + */ + if ( Framework::get_container()->get_service( 'assets' )->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' ] ); + } } /** @@ -86,14 +92,6 @@ private function i18n(): void { * */ public function set_ari_responsive_image_default_img_path( string $attr ): string { - /** - * @psalm-suppress PossiblyInvalidMethodCall - * @psalm-suppress UndefinedInterfaceMethod - */ - if ( ! Framework::get_container()->get_service( 'assets' )->is_minified() ) { - return $attr; - } - return '/dist/'; } @@ -106,15 +104,7 @@ public function set_ari_responsive_image_default_img_path( string $attr ): strin * */ public function set_ari_responsive_image_default_img_name( string $default_img ): string { - /** - * @psalm-suppress PossiblyInvalidMethodCall - * @psalm-suppress UndefinedInterfaceMethod - */ - if ( ! Framework::get_container()->get_service( 'assets' )->is_minified() ) { - return $default_img; - } - - return $this->get_min_image( $default_img ); + return $this->get_min_default_image( $default_img ); } /** @@ -124,24 +114,9 @@ public function set_ari_responsive_image_default_img_name( string $default_img ) * * @return string * + * @author Léonard Phoumpakka */ - protected function get_min_image( string $original_image ): string { - - if ( empty( $original_image ) ) { - return $original_image; - } - - if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) { - return $original_image; - } - - $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 $original_image; - } - - return $assets[ 'assets/' . $original_image ] ?: $original_image; + public function get_min_default_image( string $original_image ): string { + return Framework::get_container()->get_service( 'assets' )->get_min_file( 'assets/' . $original_image ); } } From cbc92d8073437fc58d6afc2bbc5538770d6cebf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=CC=81onard=20Phoumpakka?= Date: Mon, 20 Feb 2023 17:14:06 +0100 Subject: [PATCH 3/4] use var for Asset --- inc/Services/Theme.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/inc/Services/Theme.php b/inc/Services/Theme.php index 66770214..52a30f4c 100644 --- a/inc/Services/Theme.php +++ b/inc/Services/Theme.php @@ -8,6 +8,8 @@ class Theme implements Service { + protected $asset; + /** * @param Service_Container $container */ @@ -17,12 +19,13 @@ 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 ( Framework::get_container()->get_service( 'assets' )->is_minified() ) { + 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' ] ); } @@ -117,6 +120,6 @@ public function set_ari_responsive_image_default_img_name( string $default_img ) * @author Léonard Phoumpakka */ public function get_min_default_image( string $original_image ): string { - return Framework::get_container()->get_service( 'assets' )->get_min_file( 'assets/' . $original_image ); + return $this->asset->get_min_file( 'assets/' . $original_image ); } } From e792c414ab82b26430e09a3e40c9af1a7f0fc5d9 Mon Sep 17 00:00:00 2001 From: lphoumpakka Date: Fri, 31 May 2024 17:24:04 +0200 Subject: [PATCH 4/4] add check condition and phpdoc --- inc/Services/Assets.php | 4 ++++ inc/Services/Theme.php | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/inc/Services/Assets.php b/inc/Services/Assets.php index 9ebdcbe0..7e008e24 100644 --- a/inc/Services/Assets.php +++ b/inc/Services/Assets.php @@ -133,6 +133,10 @@ public function get_min_file( string $type ): string { $assets = $this->get_assets_json_index_file(); + if ( empty( $assets ) ) { + return ''; + } + switch ( $type ) { case 'css': $file = $assets['app.css'] ?? ''; diff --git a/inc/Services/Theme.php b/inc/Services/Theme.php index 52a30f4c..e16921f2 100644 --- a/inc/Services/Theme.php +++ b/inc/Services/Theme.php @@ -8,6 +8,9 @@ class Theme implements Service { + /** + * @var Service|bool + */ protected $asset; /** @@ -89,12 +92,9 @@ private function i18n(): void { /** * Set default path for ARI for minified files * - * @param string $attr - * * @return string - * */ - public function set_ari_responsive_image_default_img_path( string $attr ): string { + public function set_ari_responsive_image_default_img_path(): string { return '/dist/'; }