mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 13:44:42 +08:00
Remove smart button locale
, validate bcp47, add en
as default fallback
This commit is contained in:
parent
845a83acb4
commit
33022f3c47
4 changed files with 9 additions and 55 deletions
|
@ -67,12 +67,16 @@ class ApplicationContextRepository {
|
|||
*/
|
||||
protected function valid_bcp47_code() {
|
||||
$locale = str_replace( '_', '-', get_user_locale() );
|
||||
$parts = explode( '-', $locale );
|
||||
|
||||
if ( count( $parts ) < 3 ) {
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -726,7 +726,6 @@ class SmartButton implements SmartButtonInterface {
|
|||
$params = array(
|
||||
'client-id' => $this->client_id,
|
||||
'currency' => get_woocommerce_currency(),
|
||||
'locale' => $this->valid_locale_code(),
|
||||
'integration-date' => PAYPAL_INTEGRATION_DATE,
|
||||
'components' => implode( ',', $this->components() ),
|
||||
'vault' => $this->can_save_vault_token() ?
|
||||
|
@ -957,20 +956,4 @@ class SmartButton implements SmartButtonInterface {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a PayPal-supported locale code, for example de_DE_formal becomes de_DE.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function valid_locale_code(): string {
|
||||
$locale = get_user_locale();
|
||||
$parts = explode( '_', $locale );
|
||||
|
||||
if ( count( $parts ) < 3 ) {
|
||||
return $locale;
|
||||
}
|
||||
|
||||
return substr( $locale, 0, strrpos( $locale, '_' ) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,8 @@ class ApplicationContextRepositoryTest extends TestCase
|
|||
return [
|
||||
'de-DE' => ['de-DE', 'de-DE'],
|
||||
'de-DE-formal' => ['de-DE-formal', 'de-DE'],
|
||||
'de' => ['de', 'de'],
|
||||
'ceb' => ['ceb', 'en'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Button\Assets;
|
||||
|
||||
use WooCommerce\PayPalCommerce\TestCase;
|
||||
use function Brain\Monkey\Functions\when;
|
||||
|
||||
class SmartButtonTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provider
|
||||
*/
|
||||
public function test_valid_locale_code($input, $output)
|
||||
{
|
||||
$testee = $this->getMockBuilder(SmartButton::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$method = new \ReflectionMethod($testee, 'valid_locale_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'],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue