1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\Features\CustomPostTypes; |
4
|
|
|
|
5
|
|
|
use Flynt; |
6
|
|
|
|
7
|
|
|
class Translator |
8
|
|
|
{ |
9
|
|
View Code Duplication |
public static function translateConfig($config) |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
$config = apply_filters( |
12
|
|
|
'Flynt/Features/CustomPostTypes/TranslateConfig', |
13
|
|
|
self::translateKeys($config) |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
$config = apply_filters( |
17
|
|
|
"Flynt/Features/CustomPostTypes/TranslateConfig?name={$config['name']}", |
18
|
|
|
$config |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
return $config; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected static function translateKeys($config) |
25
|
|
|
{ |
26
|
|
|
$config['label'] = self::translateNestedValues($config, 'label'); |
27
|
|
|
$config['labels'] = self::translateNestedValues($config, 'labels'); |
28
|
|
|
$config['singular_label'] = self::translateNestedValues($config, 'singular_label'); |
29
|
|
|
$config['description'] = self::translateNestedValues($config, 'description'); |
30
|
|
|
|
31
|
|
|
return $config; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// ...$args works in PHP 5.6+ |
35
|
|
View Code Duplication |
protected static function translateNestedValues(...$args) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
if (count($args) === 0) { |
38
|
|
|
trigger_error('Invalid argument count for translation!', E_USER_WARNING); |
39
|
|
|
return []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (count($args) === 1) { |
43
|
|
|
return $args[0]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$value = Flynt\Helpers::extractNestedDataFromArray($args); |
47
|
|
|
|
48
|
|
|
if (empty($value)) { |
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (is_array($value)) { |
53
|
|
|
// Loop through array and apply translations while keeping keys intact |
54
|
|
|
// NOTE: assuming it's a single dimensional array |
55
|
|
|
return array_reduce(array_keys($value), function ($carry, $key) use ($value) { |
56
|
|
|
return array_merge($carry, [ |
57
|
|
|
$key => _x($value[$key], $key, 'flynt-starter-theme') |
58
|
|
|
]); |
59
|
|
|
}, []); |
60
|
|
|
} else { |
61
|
|
|
$context = array_pop($args); |
62
|
|
|
return _x($value, $context, 'flynt-starter-theme'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.