Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use translation in block patterns #2414

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions plugins/otter-pro/inc/plugins/class-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ 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';

/**
* Prefix used for translated titles.
*
* @var string
*/
const TRANSLATED_TITLE_PREFIX = 'title_';

/**
* Array mapping WordPress locale codes to available language codes provided by store.
*
* @var array
*/
const AVAILABLE_LANGUAGES = [
'de_DE' => 'de',
'de_DE_formal' => 'de',
];

/**
* Method to define hooks needed.
*
Expand Down Expand Up @@ -57,7 +81,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 Down Expand Up @@ -110,15 +134,44 @@ 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 array The block pattern.
*/
public function prepare_block_pattern( $block_pattern, $lang_locale = '' ) {
if ( isset( self::AVAILABLE_LANGUAGES[ $lang_locale ] ) ) {
$translated_title = self::TRANSLATED_TITLE_PREFIX . self::AVAILABLE_LANGUAGES[ $lang_locale ];
if ( isset( $block_pattern[ $translated_title ] ) ) {
$block_pattern['title'] = $block_pattern[ $translated_title ];
}
}

foreach ( array_keys( $block_pattern ) as $pattern_key ) {
if ( false === strpos( $pattern_key, self::TRANSLATED_TITLE_PREFIX ) ) {
continue;
}

unset( $block_pattern[ $pattern_key ] );
}

return $block_pattern;
}

/**
* Check if the given pattern block has the correct structure.
*
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/plugins/patterns-library/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const Library = ({
const currentCategory = 'favorites' === selectedCategory ? patterns.filter( pattern => getFavorites.includes( pattern.name ) ) : patterns.filter( pattern => pattern.categories.includes( selectedCategory ) );

if ( searchInput ) {
return currentCategory.filter( pattern => pattern.name.toLowerCase().includes( searchInput.toLowerCase() ) );
return currentCategory.filter( pattern => pattern.title.toLowerCase().includes( searchInput.toLowerCase() ) );
}

return currentCategory;
Expand Down
63 changes: 46 additions & 17 deletions tests/test-patterns-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,56 @@ 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 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 );
}
}
Loading