| 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 |
* @since 6.0.0 Make static. |
| 35 |
*/ |
| 36 |
public static function init() |
| 37 |
{ |
| 38 |
add_action('admin_init', [self::class, 'addSetting']); |
| 39 |
add_action('pre_update_option_' . self::OPTION_NAME_CODE, function ($value) { |
| 40 |
Sync::syncOptionToDashboard(self::OPTION_NAME_CODE); |
| 41 |
return $value; |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Add setting. |
| 47 |
* |
| 48 |
* @since 5.0.0 |
| 49 |
* @since 6.0.0 Make static. |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public static function addSetting() |
| 54 |
{ |
| 55 |
add_settings_field( |
| 56 |
'beyondwords-default-language', |
| 57 |
__('Language', 'speechkit'), |
| 58 |
[self::class, 'render'], |
| 59 |
'beyondwords_voices', |
| 60 |
'voices' |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Render setting field. |
| 66 |
* |
| 67 |
* @since 5.0.0 |
| 68 |
* @since 6.0.0 Make static. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
**/ |
| 72 |
public static function render() |
| 73 |
{ |
| 74 |
$options = self::getOptions(); |
| 75 |
|
| 76 |
$current = get_option(self::OPTION_NAME_CODE); |
| 77 |
?> |
| 78 |
<div class="beyondwords-setting__default-language"> |
| 79 |
<select |
| 80 |
id="<?php echo esc_attr(self::OPTION_NAME_CODE) ?>" |
| 81 |
name="<?php echo esc_attr(self::OPTION_NAME_CODE) ?>" |
| 82 |
placeholder="<?php esc_attr_e('Add a language', 'speechkit'); ?>" |
| 83 |
style="width: 250px;" |
| 84 |
autocomplete="off" |
| 85 |
> |
| 86 |
<?php |
| 87 |
foreach ($options as $option) { |
| 88 |
printf( |
| 89 |
'<option value="%s" data-voices=\'%s\' %s>%s</option>', |
| 90 |
esc_attr($option['value']), |
| 91 |
esc_attr($option['voices']), |
| 92 |
selected($option['value'], $current), |
| 93 |
esc_html($option['label']) |
| 94 |
); |
| 95 |
} |
| 96 |
?> |
| 97 |
</select> |
| 98 |
</div> |
| 99 |
<p class="description"> |
| 100 |
<?php |
| 101 |
esc_html_e( |
| 102 |
'Choose the default language of your posts.', |
| 103 |
'speechkit' |
| 104 |
); |
| 105 |
?> |
| 106 |
</p> |
| 107 |
<?php |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get options for the <select> element. |
| 112 |
* |
| 113 |
* @since 5.0.0 |
| 114 |
* @since 6.0.0 Make static. |
| 115 |
* |
| 116 |
* @return string[] Array of options (value, label). |
| 117 |
**/ |
| 118 |
public static function getOptions() |
| 119 |
{ |
| 120 |
$languages = ApiClient::getLanguages(); |
| 121 |
|
| 122 |
if (! is_array($languages)) { |
| 123 |
$languages = []; |
| 124 |
} |
| 125 |
|
| 126 |
return array_map(function ($language) { |
| 127 |
$label = $language['name']; |
| 128 |
|
| 129 |
if (isset($language['accent'])) { |
| 130 |
$label .= ' (' . $language['accent'] . ')'; |
| 131 |
} |
| 132 |
|
| 133 |
return [ |
| 134 |
'value' => $language['code'], |
| 135 |
'label' => $label, |
| 136 |
'voices' => wp_json_encode($language['default_voices']), |
| 137 |
]; |
| 138 |
}, $languages); |
| 139 |
} |
| 140 |
} |
| 141 |
|