PluginProbe
BeyondWords – AI audio for publishers / 4.5.0
BeyondWords – AI audio for publishers v4.5.0
7.2.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 All 44 releases
speechkit / src / Component / Settings / Languages / Languages.php

Languages.php in BeyondWords – AI audio for publishers 4.5.0, at src/Component/Settings/Languages/Languages.php

226 lines 5.7 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: Languages
7 *
8 * @package Beyondwords\Wordpress
9 * @author Stuart McAlpine <[email protected]>
10 * @since 4.0.0
11 */
12
13 namespace Beyondwords\Wordpress\Component\Settings\Languages;
14
15 use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
16 use Beyondwords\Wordpress\Core\ApiClient;
17 use Beyondwords\Wordpress\Core\Environment;
18
19 /**
20 * Languages setup
21 *
22 * @since 4.0.0
23 */
24 class Languages
25 {
26 public const DEFAULT_LANGUAGES = [];
27
28 /**
29 * API Client.
30 *
31 * @since 3.0.0
32 */
33 private $apiClient;
34
35 /**
36 * Constructor.
37 *
38 * @since 3.0.0
39 */
40 public function __construct($apiClient)
41 {
42 $this->apiClient = $apiClient;
43 }
44
45 /**
46 * Init.
47 *
48 * @since 4.0.0
49 */
50 public function init()
51 {
52 if (SettingsUtils::hasApiSettings()) {
53 add_action('admin_init', array($this, 'registerSetting'));
54 add_action('admin_init', array($this, 'addSettingsField'));
55 add_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
56 }
57 }
58
59 /**
60 * Init setting.
61 *
62 * @since 4.0.0
63 *
64 * @return void
65 */
66 public function registerSetting()
67 {
68 if (! SettingsUtils::hasApiSettings()) {
69 return;
70 }
71
72 register_setting(
73 'beyondwords',
74 'beyondwords_languages',
75 [
76 'default' => '',
77 'sanitize_callback' => array($this, 'sanitize'),
78 ]
79 );
80 }
81
82 /**
83 * Init setting.
84 *
85 * @since 4.0.0
86 *
87 * @return void
88 */
89 public function addSettingsField()
90 {
91 add_settings_field(
92 'beyondwords-languages',
93 __('Languages', 'speechkit'),
94 array($this, 'render'),
95 'beyondwords',
96 'content'
97 );
98 }
99
100 /**
101 * Render setting field.
102 *
103 * @since 4.0.0
104 *
105 * @return void
106 **/
107 public function render()
108 {
109 $allLanguages = $this->apiClient->getLanguages();
110
111 if (! is_array($allLanguages)) {
112 $allLanguages = [];
113 }
114
115 $selectedLanguages = get_option('beyondwords_languages', Languages::DEFAULT_LANGUAGES);
116
117 if (! is_array($selectedLanguages)) {
118 $selectedLanguages = Languages::DEFAULT_LANGUAGES;
119 }
120
121 ?>
122 <div class="beyondwords-setting--languages">
123 <select
124 id="beyondwords_languages"
125 name="beyondwords_languages[]"
126 placeholder="Add a language"
127 multiple
128 style="width: 500px;"
129 >
130 <?php foreach ($allLanguages as $language) : ?>
131 <option
132 value="<?php echo esc_attr($language['id']); ?>"
133 <?php selected(in_array($language['id'], $selectedLanguages), true); ?>
134 >
135 <?php echo esc_attr($language['name']); ?>
136 </option>
137 <?php endforeach; ?>
138 </select>
139 <p class="description">
140 <?php
141 printf(
142 /* translators: %s is replaced with the link to the BeyondWords dashboard */
143 esc_html__('The default voice for audio is determined by the project settings in your %s.', 'speechkit'), // phpcs:ignore Generic.Files.LineLength.TooLong
144 sprintf(
145 '<a href="%s" target="_blank">%s</a>',
146 esc_url(Environment::getDashboardUrl()),
147 esc_html__('BeyondWords dashboard', 'speechkit')
148 )
149 );
150 ?>
151 </p>
152 <p class="description">
153 <?php
154 esc_html_e('Add languages here to use voices other than the default project voice.', 'speechkit');
155 ?>
156 </p>
157 <p class="description">
158 <?php
159 esc_html_e('The voices will be available to select on the Post Edit screen.', 'speechkit');
160 ?>
161 </p>
162 </div>
163 <?php
164 }
165
166 /**
167 * Sanitise the setting value.
168 *
169 * @since 4.0.0
170 * @param array $value The submitted value.
171 *
172 * @return void
173 **/
174 public function sanitize($value)
175 {
176 if (empty($value)) {
177 $value = [];
178 }
179
180 return $value;
181 }
182
183 /**
184 * Register the component scripts.
185 *
186 * @since 4.0.0
187 *
188 * @param string $hook Page hook
189 *
190 * @return void
191 */
192 public function enqueueScripts($hook)
193 {
194 // Only enqueue for Post screens
195 if ($hook === 'settings_page_beyondwords' && SettingsUtils::hasApiSettings()) {
196 // Tom Select CSS
197 wp_enqueue_style(
198 'tom-select',
199 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/tom-select.css',
200 false,
201 BEYONDWORDS__PLUGIN_VERSION
202 );
203
204 // Tom Select JS
205 wp_enqueue_script(
206 'tom-select',
207 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/tom-select.complete.min.js',
208 [],
209 null,
210 true
211 );
212
213 // Settings page
214 wp_register_script(
215 'beyondwords-settings--languages',
216 BEYONDWORDS__PLUGIN_URI . 'src/Component/Settings/Languages/settings.js',
217 ['jquery', 'tom-select'],
218 BEYONDWORDS__PLUGIN_VERSION,
219 true
220 );
221
222 wp_enqueue_script('beyondwords-settings--languages');
223 }
224 }
225 }
226