| 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 |
defined('ABSPATH') || exit; |
| 18 |
|
| 19 |
/** |
| 20 |
* Voice |
| 21 |
* |
| 22 |
* @since 5.0.0 |
| 23 |
*/ |
| 24 |
abstract class Voice |
| 25 |
{ |
| 26 |
/** |
| 27 |
* Get all options for the current component. |
| 28 |
* |
| 29 |
* @since 5.0.0 |
| 30 |
* @since 5.4.0 |
| 31 |
* @since 6.0.0 Make static, handle API errors, and return language_code in each option. |
| 32 |
* |
| 33 |
* @return string[] Associative array of options. |
| 34 |
**/ |
| 35 |
public static function getOptions() |
| 36 |
{ |
| 37 |
$voices = null; |
| 38 |
$languageCode = get_option('beyondwords_project_language_code'); |
| 39 |
if ($languageCode) { |
| 40 |
$voices = ApiClient::getVoices($languageCode); |
| 41 |
} |
| 42 |
|
| 43 |
if (! $voices || ! is_array($voices) || empty($voices)) { |
| 44 |
return []; |
| 45 |
} |
| 46 |
|
| 47 |
// Filter out any non-array elements (in case of API errors) |
| 48 |
$voices = array_filter($voices, 'is_array'); |
| 49 |
|
| 50 |
if (empty($voices)) { |
| 51 |
return []; |
| 52 |
} |
| 53 |
|
| 54 |
return array_map(fn($voice) => [ |
| 55 |
'value' => $voice['id'], |
| 56 |
'label' => $voice['name'], |
| 57 |
'language_code' => $voice['language']['code'] ?? '', |
| 58 |
], $voices); |
| 59 |
} |
| 60 |
} |
| 61 |
|