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 0569ef3
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 20 deletions.
59 changes: 56 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,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 All @@ -67,7 +91,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 +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 ) );

Check failure on line 144 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 = '' ) {
if ( isset( self::AVAILABLE_LANGUAGES[ $lang_locale ] ) ) {
$translated_title = 'title_' . 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 failure on line 172 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
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 );
}
}

0 comments on commit 0569ef3

Please sign in to comment.