Skip to content

Commit

Permalink
feat: use translation in block patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Oct 29, 2024
1 parent 9a50667 commit df2cfde
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 20 deletions.
45 changes: 42 additions & 3 deletions plugins/otter-pro/inc/plugins/class-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 = '';
Expand All @@ -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 );

Expand Down Expand Up @@ -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 ) );

Check failure on line 127 in plugins/otter-pro/inc/plugins/class-patterns.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #2 $pattern_properties of function register_block_pattern expects array{title?: string, content?: string, description?: string, viewportWidth?: int, inserter?: bool, categories?: array<string>, keywords?: array<string>, blockTypes?: array<string>, ...}, string given.
}
}

/**
* 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 failure on line 158 in plugins/otter-pro/inc/plugins/class-patterns.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method ThemeIsle\OtterPro\Plugins\Patterns::prepare_block_pattern() should return string but returns array.
}

/**
* Check if the given pattern block has the correct structure.
*
Expand Down
77 changes: 60 additions & 17 deletions tests/test-patterns-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<!-- wp:paragraph --><p>Test content</p><!-- /wp:paragraph -->',
'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 );
}
}

0 comments on commit df2cfde

Please sign in to comment.