Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
5.88% |
3 / 51 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
Language | |
5.88% |
3 / 51 |
|
0.00% |
0 / 4 |
47.85 | |
0.00% |
0 / 1 |
init | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
1.06 | |||
addSetting | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
render | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
6 | |||
getOptions | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | /** |
6 | * Setting: Language |
7 | * |
8 | * @package Beyondwords\Wordpress |
9 | * @author Stuart McAlpine <stu@beyondwords.io> |
10 | * @since 5.0.0 |
11 | */ |
12 | |
13 | namespace Beyondwords\Wordpress\Component\Settings\Fields\Language; |
14 | |
15 | use Beyondwords\Wordpress\Component\Settings\Sync; |
16 | use Beyondwords\Wordpress\Core\ApiClient; |
17 | |
18 | /** |
19 | * Language |
20 | * |
21 | * @since 5.0.0 |
22 | */ |
23 | class Language |
24 | { |
25 | /** |
26 | * Option name. |
27 | */ |
28 | public const OPTION_NAME_CODE = 'beyondwords_project_language_code'; |
29 | |
30 | /** |
31 | * Constructor |
32 | * |
33 | * @since 5.0.0 |
34 | */ |
35 | public function init() |
36 | { |
37 | add_action('admin_init', array($this, 'addSetting')); |
38 | add_action('pre_update_option_' . self::OPTION_NAME_CODE, function ($value) { |
39 | Sync::syncOptionToDashboard(self::OPTION_NAME_CODE); |
40 | return $value; |
41 | }); |
42 | } |
43 | |
44 | /** |
45 | * Add setting. |
46 | * |
47 | * @since 5.0.0 |
48 | * |
49 | * @return void |
50 | */ |
51 | public function addSetting() |
52 | { |
53 | add_settings_field( |
54 | 'beyondwords-default-language', |
55 | __('Language', 'speechkit'), |
56 | array($this, 'render'), |
57 | 'beyondwords_voices', |
58 | 'voices' |
59 | ); |
60 | } |
61 | |
62 | /** |
63 | * Render setting field. |
64 | * |
65 | * @since 5.0.0 |
66 | * |
67 | * @return void |
68 | **/ |
69 | public function render() |
70 | { |
71 | $options = $this->getOptions(); |
72 | |
73 | $current = get_option(self::OPTION_NAME_CODE); |
74 | ?> |
75 | <div class="beyondwords-setting__default-language"> |
76 | <select |
77 | id="<?php echo esc_attr(self::OPTION_NAME_CODE) ?>" |
78 | name="<?php echo esc_attr(self::OPTION_NAME_CODE) ?>" |
79 | placeholder="<?php esc_attr_e('Add a language', 'speechkit'); ?>" |
80 | style="width: 250px;" |
81 | autocomplete="off" |
82 | > |
83 | <?php |
84 | foreach ($options as $option) { |
85 | printf( |
86 | '<option value="%s" data-voices=\'%s\' %s>%s</option>', |
87 | esc_attr($option['value']), |
88 | esc_attr($option['voices']), |
89 | selected($option['value'], $current), |
90 | esc_html($option['label']) |
91 | ); |
92 | } |
93 | ?> |
94 | </select> |
95 | </div> |
96 | <p class="description"> |
97 | <?php |
98 | esc_html_e( |
99 | 'Choose the default language of your posts.', |
100 | 'speechkit' |
101 | ); |
102 | ?> |
103 | </p> |
104 | <?php |
105 | } |
106 | |
107 | /** |
108 | * Get options for the <select> element. |
109 | * |
110 | * @since 5.0.0 |
111 | * |
112 | * @return string[] Array of options (value, label). |
113 | **/ |
114 | public function getOptions() |
115 | { |
116 | $languages = ApiClient::getLanguages(); |
117 | |
118 | if (! is_array($languages)) { |
119 | $languages = []; |
120 | } |
121 | |
122 | $options = array_map(function ($language) { |
123 | $label = $language['name']; |
124 | |
125 | if (isset($language['accent'])) { |
126 | $label .= ' (' . $language['accent'] . ')'; |
127 | } |
128 | |
129 | return [ |
130 | 'value' => $language['code'], |
131 | 'label' => $label, |
132 | 'voices' => wp_json_encode($language['default_voices']), |
133 | ]; |
134 | }, $languages); |
135 | |
136 | return $options; |
137 | } |
138 | } |