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

Filter PayPal-supported language codes #154

Merged
merged 2 commits into from
May 20, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function current_context(
): ApplicationContext {

$brand_name = $this->settings->has( 'brand_name' ) ? $this->settings->get( 'brand_name' ) : '';
$locale = str_replace( '_', '-', get_user_locale() );
$locale = $this->valid_bcp47_code();
$landingpage = $this->settings->has( 'landing_page' ) ?
$this->settings->get( 'landing_page' ) : ApplicationContext::LANDING_PAGE_NO_PREFERENCE;
$context = new ApplicationContext(
Expand All @@ -59,4 +59,24 @@ public function current_context(
);
return $context;
}

/**
* Returns a PayPal-supported BCP-47 code, for example de-DE-formal becomes de-DE.
*
* @return string
*/
protected function valid_bcp47_code() {
$locale = str_replace( '_', '-', get_user_locale() );

if ( preg_match( '/^[a-z]{2}(?:-[A-Z][a-z]{3})?(?:-(?:[A-Z]{2}))?$/', $locale ) ) {
return $locale;
}

$parts = explode( '-', $locale );
if ( count( $parts ) === 3 ) {
return substr( $locale, 0, strrpos( $locale, '-' ) );
}

return 'en';
}
}
1 change: 0 additions & 1 deletion modules/ppcp-button/src/Assets/class-smartbutton.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,6 @@ private function url(): string {
$params = array(
'client-id' => $this->client_id,
'currency' => get_woocommerce_currency(),
'locale' => get_user_locale(),
'integration-date' => PAYPAL_INTEGRATION_DATE,
'components' => implode( ',', $this->components() ),
'vault' => $this->can_save_vault_token() ?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace WooCommerce\PayPalCommerce\ApiClient\Repository;

use WooCommerce\PayPalCommerce\ApiClient\TestCase;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButton;
use function Brain\Monkey\Functions\when;

class ApplicationContextRepositoryTest extends TestCase
{
/**
* @dataProvider provider
*/
public function test_valid_bcp47_code($input, $output)
{
$testee = $this->getMockBuilder(ApplicationContextRepository::class)
->disableOriginalConstructor()
->getMock();

$method = new \ReflectionMethod($testee, 'valid_bcp47_code');
$method->setAccessible(true);

when('get_user_locale')->justReturn($input);

$this->assertSame($output, $method->invoke($testee));
}

public function provider()
{
return [
'de-DE' => ['de-DE', 'de-DE'],
'de-DE-formal' => ['de-DE-formal', 'de-DE'],
'de' => ['de', 'de'],
'ceb' => ['ceb', 'en'],
];
}
}