Skip to content

Commit

Permalink
fix: improvement speed up in admin dashboard (#52gj39)
Browse files Browse the repository at this point in the history
  • Loading branch information
matzeeable committed May 5, 2020
1 parent ce42998 commit 5b036c3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
22 changes: 21 additions & 1 deletion packages/utils/src/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ public function load_script_translation_file($file, $handle, $domain) {
$domain
);

$locale = determine_locale();
if (
$domain === $packageDomain &&
!is_readable($useFile) &&
substr($useFile, 0, strlen($packagePath)) === $packagePath
) {
// Collect data
$locale = $this->getLanguageFromFile($file);
if ($locale === false) {
return $file;
}

$folder = dirname($useFile);
$use = $this->override($locale);
$wp_scripts = wp_scripts();
Expand All @@ -85,6 +89,22 @@ public function load_script_translation_file($file, $handle, $domain) {
return $file;
}

/**
* Obtain language key from a file name.
*
* @param string $file
*/
public function getLanguageFromFile($file) {
$availableLanguages = get_available_languages();
$availableLanguages[] = 'en_US';
preg_match_all('/-(' . join('|', $availableLanguages) . ')-/m', basename($file), $matches, PREG_SET_ORDER, 0);

if (count($matches) === 0) {
return false;
}
return $matches[0][1];
}

/**
* Allow language overrides so for example de_AT uses de_DE to avoid duplicate
* .po files and management. This is for backend PHP files!
Expand Down
76 changes: 71 additions & 5 deletions packages/utils/test/phpunit/LocalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,20 @@ public function testLoadScriptTranslationFile() {
$domain = PHPUNIT_TD;
$parsedPackageInfo = [$file, $path, $domain, $domain];
$handle = 'phpunit-admin';
$currentLocale = 'en_CA';
$currentLocale = 'en_US';

$this->localization->shouldReceive('load_script_translation_file')->passthru();
$this->localization
->shouldReceive('packageInfoParserForOverrides')
->with(Localization::$PACKAGE_INFO_FRONTEND, $file, $domain)
->andReturn($parsedPackageInfo);

WP_Mock::userFunction('determine_locale', ['return' => $currentLocale]);
$this->localization
->shouldReceive('getLanguageFromFile')
->once()
->with($file)
->andReturn($currentLocale);

WP_Mock::userFunction('path_join', ['return' => $path, 'args' => [PHPUNIT_PATH, Mockery::any()]]);

redefine('is_readable', always(false));
Expand All @@ -130,22 +135,46 @@ public function testLoadScriptTranslationFile() {
$this->addToAssertionCount(1);
}

public function testLoadScriptTranslationFileNoMatchingLanguage() {
$file = '/var/www/html/wp-content/plugins/phpunit/public/languages/json/phpunit--phpunit-admin.json';
$path = '/var/www/html/wp-content/plugins/phpunit/public/languages/json';
$domain = PHPUNIT_TD;
$parsedPackageInfo = [$file, $path, $domain, $domain];
$handle = 'phpunit-admin';

$this->localization->shouldReceive('load_script_translation_file')->passthru();
$this->localization
->shouldReceive('packageInfoParserForOverrides')
->with(Localization::$PACKAGE_INFO_FRONTEND, $file, $domain)
->andReturn($parsedPackageInfo);

$this->localization
->shouldReceive('getLanguageFromFile')
->once()
->with($file)
->andReturn(false);
$this->localization->shouldNotReceive('override');

$actual = $this->localization->load_script_translation_file($file, $handle, $domain);

$this->assertEquals($file, $actual);
}

public function testLoadScriptTranslationFileNotMatchingDomain() {
$file = '/var/www/html/wp-content/plugins/phpunit/public/languages/json/phpunit-en_US-phpunit-admin.json';
$path = '/var/www/html/wp-content/plugins/phpunit/public/languages/json';
$domain = PHPUNIT_TD;
$anotherDomain = 'another-domain';
$parsedPackageInfo = [$file, $path, $domain, $domain];
$handle = 'phpunit-admin';
$currentLocale = 'en_CA';
$currentLocale = 'en_US';

$this->localization->shouldReceive('load_script_translation_file')->passthru();
$this->localization
->shouldReceive('packageInfoParserForOverrides')
->with(Localization::$PACKAGE_INFO_FRONTEND, $file, $anotherDomain)
->andReturn($parsedPackageInfo);

WP_Mock::userFunction('determine_locale', ['return' => $currentLocale]);
WP_Mock::userFunction('path_join', ['return' => $path, 'args' => [PHPUNIT_PATH, Mockery::any()]]);

$this->localization->shouldNotReceive('override');
Expand All @@ -169,7 +198,6 @@ public function testLoadScriptTranslationFileNotMatchingPlugin() {
->with(Localization::$PACKAGE_INFO_FRONTEND, $file, $domain)
->andReturn($parsedPackageInfo);

WP_Mock::userFunction('determine_locale', ['return' => $currentLocale]);
WP_Mock::userFunction('path_join', [
'return' => $path,
'args' => [PHPUNIT_PATH, Assets::$PUBLIC_JSON_I18N]
Expand All @@ -184,6 +212,44 @@ public function testLoadScriptTranslationFileNotMatchingPlugin() {
$this->assertEquals($file, $actual);
}

public function testGetLanguageFromFile() {
$should = 'en_US';
$file = '/var/www/html/wp-content/plugins/phpunit/public/languages/json/phpunit-en_US-phpunit-admin.json';

$this->localization->shouldReceive('getLanguageFromFile')->passthru();

WP_Mock::userFunction('get_available_languages', ['times' => 1, 'return' => ['de_DE', 'de_DE_formal']]);

$actual = $this->localization->getLanguageFromFile($file);

$this->assertEquals($should, $actual);
}

public function testGetLanguageFromFileNoMatch() {
$file = '/var/www/html/wp-content/plugins/phpunit/public/languages/json/phpunit--phpunit-admin.json';

$this->localization->shouldReceive('getLanguageFromFile')->passthru();

WP_Mock::userFunction('get_available_languages', ['times' => 1, 'return' => ['de_DE', 'de_DE_formal']]);

$actual = $this->localization->getLanguageFromFile($file);

$this->assertFalse($actual);
}

public function testGetLanguageFromFileNoInstalledLanguages() {
$should = 'en_US';
$file = '/var/www/html/wp-content/plugins/phpunit/public/languages/json/phpunit-en_US-phpunit-admin.json';

$this->localization->shouldReceive('getLanguageFromFile')->passthru();

WP_Mock::userFunction('get_available_languages', ['times' => 1, 'return' => []]);

$actual = $this->localization->getLanguageFromFile($file);

$this->assertEquals($should, $actual);
}

public function testOverrideLoadTextdomain() {
$mofile = '/var/www/html/wp-content/plugins/phpunit/languages/phpunit-en_US.mo';
$domain = PHPUNIT_TD;
Expand Down

0 comments on commit 5b036c3

Please sign in to comment.