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

199 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 <div class="beyondwords-setting--languages">
96 <select
97 id="beyondwords_languages"
98 name="beyondwords_languages[]"
99 placeholder="Add a language"
100 multiple
101 style="width: 500px;"
102 >
103 <?php foreach ($allLanguages as $language) : ?>
104 <option
105 value="<?php echo esc_attr($language['id']); ?>"
106 <?php selected(in_array($language['id'], $selectedLanguages), true); ?>
107 >
108 <?php echo esc_attr($language['name']); ?>
109 </option>
110 <?php endforeach; ?>
111 </select>
112 <p class="description">
113 <?php
114 printf(
115 /* translators: %s is replaced with the link to the BeyondWords dashboard */
116 esc_html__('The default voice for audio is determined by the project settings in your %s.', 'speechkit'), // phpcs:ignore Generic.Files.LineLength.TooLong
117 sprintf(
118 '<a href="%s" target="_blank">%s</a>',
119 esc_url(Environment::getDashboardUrl()),
120 esc_html__('BeyondWords dashboard', 'speechkit')
121 )
122 );
123 ?>
124 </p>
125 <p class="description">
126 <?php
127 esc_html_e('Add languages here to use voices other than the default project voice.', 'speechkit');
128 ?>
129 </p>
130 <p class="description">
131 <?php
132 esc_html_e('The voices will be available to select on the Post Edit screen.', 'speechkit');
133 ?>
134 </p>
135 </div>
136 <?php
137 }
138
139 /**
140 * Sanitise the setting value.
141 *
142 * @since 4.0.0
143 * @param array $value The submitted value.
144 *
145 * @return void
146 **/
147 public function sanitize($value)
148 {
149 if (empty($value)) {
150 $value = [];
151 }
152
153 return $value;
154 }
155
156 /**
157 * Register the component scripts.
158 *
159 * @since 4.0.0
160 *
161 * @param string $hook Page hook
162 *
163 * @return void
164 */
165 public function enqueueScripts($hook)
166 {
167 // Only enqueue for Post screens
168 if ($hook === 'settings_page_beyondwords' && SettingsUtils::hasApiSettings()) {
169 // Tom Select CSS
170 wp_enqueue_style(
171 'tom-select',
172 'https://cdn.jsdelivr.net/npm/tom-select@2.2.2/dist/css/tom-select.css',
173 false,
174 BEYONDWORDS__PLUGIN_VERSION
175 );
176
177 // Tom Select JS
178 wp_enqueue_script(
179 'tom-select',
180 'https://cdn.jsdelivr.net/npm/tom-select@2.2.2/dist/js/tom-select.complete.min.js',
181 [],
182 null,
183 true
184 );
185
186 // Settings page
187 wp_register_script(
188 'beyondwords-settings--languages',
189 BEYONDWORDS__PLUGIN_URI . 'src/Component/Settings/Languages/settings.js',
190 ['jquery', 'tom-select'],
191 BEYONDWORDS__PLUGIN_VERSION,
192 true
193 );
194
195 wp_enqueue_script('beyondwords-settings--languages');
196 }
197 }
198 }
199