Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| Voice | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
6.01 | |
0.00% |
0 / 1 |
| getOptions | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
6.01 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Setting: Voice |
| 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\Voice; |
| 14 | |
| 15 | use Beyondwords\Wordpress\Core\ApiClient; |
| 16 | |
| 17 | /** |
| 18 | * Voice |
| 19 | * |
| 20 | * @since 5.0.0 |
| 21 | */ |
| 22 | abstract class Voice |
| 23 | { |
| 24 | /** |
| 25 | * Get all options for the current component. |
| 26 | * |
| 27 | * @since 5.0.0 |
| 28 | * @since 5.4.0 |
| 29 | * @since 6.0.0 Make static, handle API errors, and return language_code in each option. |
| 30 | * |
| 31 | * @return string[] Associative array of options. |
| 32 | **/ |
| 33 | public static function getOptions() |
| 34 | { |
| 35 | $voices = null; |
| 36 | $languageCode = get_option('beyondwords_project_language_code'); |
| 37 | if ($languageCode) { |
| 38 | $voices = ApiClient::getVoices($languageCode); |
| 39 | } |
| 40 | |
| 41 | if (! $voices || ! is_array($voices) || empty($voices)) { |
| 42 | return []; |
| 43 | } |
| 44 | |
| 45 | // Filter out any non-array elements (in case of API errors) |
| 46 | $voices = array_filter($voices, 'is_array'); |
| 47 | |
| 48 | if (empty($voices)) { |
| 49 | return []; |
| 50 | } |
| 51 | |
| 52 | return array_map(fn($voice) => [ |
| 53 | 'value' => $voice['id'], |
| 54 | 'label' => $voice['name'], |
| 55 | 'language_code' => $voice['language']['code'] ?? '', |
| 56 | ], $voices); |
| 57 | } |
| 58 | } |