From df2cfde18d6f5774c2f26316b5324eee768198b9 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Tue, 29 Oct 2024 14:25:12 +0200 Subject: [PATCH] feat: use translation in block patterns --- .../otter-pro/inc/plugins/class-patterns.php | 45 ++++++++++- tests/test-patterns-class.php | 77 +++++++++++++++---- 2 files changed, 102 insertions(+), 20 deletions(-) diff --git a/plugins/otter-pro/inc/plugins/class-patterns.php b/plugins/otter-pro/inc/plugins/class-patterns.php index 08e632234..809341755 100644 --- a/plugins/otter-pro/inc/plugins/class-patterns.php +++ b/plugins/otter-pro/inc/plugins/class-patterns.php @@ -20,6 +20,13 @@ class Patterns { */ protected static $instance = null; + /** + * URL is used to retrieve pattern templates from the Themeisle API. + * + * @var string PATTERNS_ENDPOINT The endpoint URL for Otter patterns. + */ + const PATTERNS_ENDPOINT = 'https://api.themeisle.com/templates-cloud/otter-patterns'; + /** * Method to define hooks needed. * @@ -57,7 +64,7 @@ public function sync_patterns() { 'license_id' => apply_filters( 'product_otter_license_key', 'free' ), 'cache' => gmdate( 'u' ), ), - 'https://api.themeisle.com/templates-cloud/otter-patterns' + self::PATTERNS_ENDPOINT ); $response = ''; @@ -67,7 +74,7 @@ public function sync_patterns() { } else { $response = wp_remote_get( esc_url_raw( $url ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get } - + $response = wp_remote_retrieve_body( $response ); $response = json_decode( $response, true ); @@ -110,15 +117,47 @@ public function register_patterns() { return; } + $user_language = get_user_locale(); + foreach ( $block_patterns as $block_pattern ) { if ( ! version_compare( get_bloginfo( 'version' ), $block_pattern['minimum'], '>=' ) ) { continue; } - register_block_pattern( 'otter-pro/' . $block_pattern['slug'], $block_pattern ); + register_block_pattern( 'otter-pro/' . $block_pattern['slug'], $this->prepare_block_pattern( $block_pattern, $user_language ) ); } } + /** + * Prepare the block pattern for registration. Apply translation if possible. + * + * @param array $block_pattern The block pattern. + * @param string $lang_locale The user locale language code. + * + * @return string The block pattern. + */ + public function prepare_block_pattern( $block_pattern, $lang_locale = '' ) { + $translated_title_prefix = 'title_'; + + if ( ! empty( $lang_locale ) ) { + foreach( array_keys( $block_pattern ) as $pattern_key ) { + if ( false === strpos( $pattern_key, $translated_title_prefix ) ) { + continue; + } + + // Use the translated title if exists. + $lang_suffix = substr( $pattern_key, strlen( $translated_title_prefix ) ); + if ( ! empty( $lang_suffix ) && false !== strpos( $lang_locale, $lang_suffix ) ) { + $block_pattern['title'] = $block_pattern[$pattern_key]; + } + + unset( $block_pattern[$pattern_key] ); + } + } + + return $block_pattern; + } + /** * Check if the given pattern block has the correct structure. * diff --git a/tests/test-patterns-class.php b/tests/test-patterns-class.php index 674577064..985a0b29b 100644 --- a/tests/test-patterns-class.php +++ b/tests/test-patterns-class.php @@ -18,27 +18,70 @@ class TestPatterns extends WP_UnitTestCase { */ public function test_fetch_patterns() { - $json_data = file_get_contents( dirname( dirname( __FILE__ ) ) . '/license.json' ); - $array_data = json_decode( $json_data, true ); + $json_data = file_get_contents( dirname( dirname( __FILE__ ) ) . '/license.json' ); + $array_data = json_decode( $json_data, true ); - $url = add_query_arg( - array( - 'site_url' => get_site_url(), - 'license_id' => $array_data['key'], - 'cache' => time(), - ), - 'https://api.themeisle.com/templates-cloud/otter-patterns' - ); + $url = add_query_arg( + array( + 'site_url' => get_site_url(), + 'license_id' => $array_data['key'], + 'cache' => time(), + ), + ThemeIsle\OtterPro\Plugins\Patterns::PATTERNS_ENDPOINT + ); - $response = wp_remote_get( $url ); - $response = wp_remote_retrieve_body( $response ); + $response = wp_remote_get( $url ); + $response = wp_remote_retrieve_body( $response ); - $this->assertTrue( 2000 < strlen( $response ) ); + $this->assertTrue( 2000 < strlen( $response ) ); - $response = json_decode( $response, true ); + $response = json_decode( $response, true ); - $this->assertIsArray( $response ); + $this->assertIsArray( $response ); - $this->assertArrayHasKey( 'slug', $response[0] ); - } + $this->assertArrayHasKey( 'slug', $response[0] ); + } + + public function test_prepare_block_pattern() { + $patterns_instance = new ThemeIsle\OtterPro\Plugins\Patterns(); + + $block_pattern = array( + 'slug' => 'test-pattern', + 'title' => 'Default Title', + 'title_es' => 'Título en Español', + 'title_fr' => 'Titre en Français', + 'title_de' => 'Titel auf Deutsch', + 'content' => '

Test content

', + 'categories' => array( 'test-category' ), + 'minimum' => '5.0', + ); + + // Test with Spanish locale + $prepared_pattern = $patterns_instance->prepare_block_pattern( $block_pattern, 'es_ES' ); + $this->assertEquals( 'Título en Español', $prepared_pattern['title'] ); + $this->assertArrayNotHasKey( 'title_es', $prepared_pattern ); + $this->assertArrayNotHasKey( 'title_fr', $prepared_pattern ); + $this->assertArrayNotHasKey( 'title_de', $prepared_pattern ); + + // Test with French locale + $prepared_pattern = $patterns_instance->prepare_block_pattern( $block_pattern, 'fr_FR' ); + $this->assertEquals( 'Titre en Français', $prepared_pattern['title'] ); + $this->assertArrayNotHasKey( 'title_es', $prepared_pattern ); + $this->assertArrayNotHasKey( 'title_fr', $prepared_pattern ); + $this->assertArrayNotHasKey( 'title_de', $prepared_pattern ); + + // Test with German locale + $prepared_pattern = $patterns_instance->prepare_block_pattern( $block_pattern, 'de_DE' ); + $this->assertEquals( 'Titel auf Deutsch', $prepared_pattern['title'] ); + $this->assertArrayNotHasKey( 'title_es', $prepared_pattern ); + $this->assertArrayNotHasKey( 'title_fr', $prepared_pattern ); + $this->assertArrayNotHasKey( 'title_de', $prepared_pattern ); + + // Test with default locale (no translation) + $prepared_pattern = $patterns_instance->prepare_block_pattern( $block_pattern, 'en_US' ); + $this->assertEquals( 'Default Title', $prepared_pattern['title'] ); + $this->assertArrayNotHasKey( 'title_es', $prepared_pattern ); + $this->assertArrayNotHasKey( 'title_fr', $prepared_pattern ); + $this->assertArrayNotHasKey( 'title_de', $prepared_pattern ); + } }