PluginProbe
BeyondWords – AI audio for publishers / 4.2.4
BeyondWords – AI audio for publishers v4.2.4
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 / Languages / Languages.php

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

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