From ba3d252f441717e28f82e9f8c3c265bb541d0622 Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bjaskulski@protonmail.com> Date: Fri, 10 Jan 2025 12:04:38 +0100 Subject: [PATCH] fix: i18n file path Signed-off-by: Bart Jaskulski <bjaskulski@protonmail.com> --- CHANGELOG.md | 6 ++++++ src/Extension/CommonBinding/I18n.php | 10 +++++++++- src/Kernel.php | 23 ++++++++++++++--------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f6634a..3896bae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # wp-init changelog +## [0.10.6] - 2025-01-10 +### Fixed +- Fixed path for loading plugin translations. +### Changed +- Improved _development_ environment detection. Now, in addition to previous methods, adding `dev` suffix to plugin version enabled development environment (no container compilation). + ## [0.10.5] - 2025-01-09 ### Fixed - Fixed condition checking if current environment is development, causing container to always use live version. diff --git a/src/Extension/CommonBinding/I18n.php b/src/Extension/CommonBinding/I18n.php index 0f5cd6e..702eb44 100644 --- a/src/Extension/CommonBinding/I18n.php +++ b/src/Extension/CommonBinding/I18n.php @@ -22,10 +22,18 @@ class I18n implements Hookable { } public function __invoke(): void { + $relative_path = str_replace( + WP_PLUGIN_DIR . '/', + '', + $this->plugin->get_path( + $this->plugin->header()->get( 'DomainPath' ) + ) + ); + \load_plugin_textdomain( $this->plugin->get_slug(), false, - $this->plugin->header()->get( 'DomainPath' ) + $relative_path ); } } diff --git a/src/Kernel.php b/src/Kernel.php index 929881b..14b1994 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -79,7 +79,7 @@ final class Kernel { $container->set( Plugin::class, $plugin ); $container->set( Configuration::class, $this->config ); - $this->prepare_driver( $container )->register_hooks(); + $this->prepare_driver( $container, $plugin )->register_hooks(); } private function get_cache_path( string $path = '' ): string { @@ -88,14 +88,19 @@ final class Kernel { ); } + /** + * Container name in scheme: `<slug>_<version>_container`. + * + * Container is compiled in client environment, so in order to allow graceful upgrade, include version name to the container. Compiled container class is also autoloaded, so it is necessary that name is unique enough to avoid clash with other plugins. + */ private function get_container_name( Plugin $plugin ): string { - return preg_replace( '/[^\w_]/', '_', implode("_", [ $plugin->get_slug(), $plugin->get_version(), 'container' ]) ); + return preg_replace( '/[^\w_]/', '_', implode( '_', [ $plugin->get_slug(), $plugin->get_version(), 'container' ] ) ); } private function initialize_container( Plugin $plugin, bool $use_cache = true ): Container { $original_builder = new DiBuilder(); - if ( $this->is_prod() && $use_cache ) { + if ( $this->is_prod( $plugin ) && $use_cache ) { $original_builder->enableCompilation( $this->get_cache_path(), $this->get_container_name( $plugin ) @@ -124,7 +129,7 @@ final class Kernel { } } - private function prepare_driver( ContainerInterface $container ): HookDriver { + private function prepare_driver( ContainerInterface $container, Plugin $plugin ): HookDriver { $loader = new CompositeBindingLoader(); foreach ( $this->extensions as $extension ) { $loader->add( $extension->bindings( $container ) ); @@ -134,7 +139,7 @@ final class Kernel { new ClusteredLoader( $loader ) ); - if ( $this->is_dev() ) { + if ( $this->is_dev( $plugin ) ) { $loader = new DebugBindingLoader( $loader ); } @@ -156,11 +161,11 @@ final class Kernel { return $driver; } - private function is_dev(): bool { - return $this->config->get( 'debug', false ) || wp_get_environment_type() === 'development'; + private function is_dev( Plugin $plugin ): bool { + return $this->config->get( 'debug', false ) || wp_get_environment_type() === 'development' || str_contains( $plugin->get_version(), 'dev' ); } - private function is_prod(): bool { - return $this->is_dev() === false; + private function is_prod( Plugin $plugin ): bool { + return $this->is_dev( $plugin ) === false; } } -- GitLab