PluginProbe
BeyondWords – AI audio for publishers / 6.3.0
BeyondWords – AI audio for publishers v6.3.0
7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.7.0 All 43 releases
speechkit / src / Component / Settings / Fields / Language / Language.php

Language.php in BeyondWords – AI audio for publishers 6.3.0, at src/Component/Settings/Fields/Language/Language.php

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