PluginProbe
BeyondWords – AI audio for publishers / 4.6.0
BeyondWords – AI audio for publishers v4.6.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.6.0, at src/Component/Settings/Languages/Languages.php

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