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 / PlayerStyle / PlayerStyle.php

PlayerStyle.php in BeyondWords – AI audio for publishers 4.6.0, at src/Component/Settings/PlayerStyle/PlayerStyle.php

273 lines 8.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: Player style
7 *
8 * @package Beyondwords\Wordpress
9 * @author Stuart McAlpine <[email protected]>
10 * @since 4.1.0
11 */
12
13 namespace Beyondwords\Wordpress\Component\Settings\PlayerStyle;
14
15 use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
16
17 /**
18 * PlayerStyle setup
19 *
20 * @since 4.1.0
21 */
22 class PlayerStyle
23 {
24 public const STANDARD = 'standard';
25
26 public const SMALL = 'small';
27
28 public const LARGE = 'large';
29
30 public const VIDEO = 'video';
31
32 /**
33 * API client, required to check whether video is enabled or not.
34 */
35 private $apiClient;
36
37 /**
38 * Constructor
39 */
40 public function __construct($apiClient)
41 {
42 $this->apiClient = $apiClient;
43 }
44
45 /**
46 * Constructor
47 */
48 public function init()
49 {
50 add_action('admin_init', array($this, 'addSettingsField'));
51 add_action('admin_enqueue_scripts', array($this, 'adminEnqueueScripts'));
52 }
53
54 /**
55 * Add settings field.
56 *
57 * @since 4.5.0
58 *
59 * @return void
60 */
61 public function addSettingsField()
62 {
63 if (! SettingsUtils::hasApiSettings()) {
64 return;
65 }
66
67 register_setting(
68 'beyondwords',
69 'beyondwords_player_style',
70 [
71 'default' => PlayerStyle::STANDARD,
72 ]
73 );
74
75 add_settings_field(
76 'beyondwords-player-style',
77 __('Player style', 'speechkit'),
78 array($this, 'render'),
79 'beyondwords',
80 'player'
81 );
82 }
83
84 /**
85 * Render setting field.
86 *
87 * @since 4.1.0
88 *
89 * @return void
90 **/
91 public function render()
92 {
93 $currentStyle = get_option('beyondwords_player_style', PlayerStyle::STANDARD);
94 $playerStyles = $this->getPlayerStyles();
95 ?>
96 <div class="beyondwords-setting--player--player-style">
97 <select name="beyondwords_player_style">
98 <?php
99 foreach ($playerStyles as $item) {
100 $disabled = isset($item['disabled']) ? $item['disabled'] : false;
101
102 printf(
103 '<option value="%s" %s %s>%s</option>',
104 esc_attr($item['value']),
105 selected($item['value'], $currentStyle),
106 disabled($disabled, true),
107 esc_html($item['label'])
108 );
109 }
110 ?>
111 </select>
112 </div>
113 <p class="description">
114 <?php
115 printf(
116 /* translators: %s is replaced with the "playerStyle setting" link */
117 esc_html__('The default player style (%s) for the audio player. This can be overridden for each post.', 'speechkit'), // phpcs:ignore Generic.Files.LineLength.TooLong
118 sprintf(
119 '<a href="https://github.com/beyondwords-io/player/blob/main/doc/player-settings.md" target="_blank" rel="nofollow">%s</a>', // phpcs:ignore Generic.Files.LineLength.TooLong
120 esc_html__('playerStyle setting', 'speechkit')
121 )
122 );
123 ?>
124 </p>
125 <?php
126 }
127
128 /**
129 * Get all Player styles for the current project.
130 *
131 * @since 4.1.0
132 *
133 * @return string[] Associative array of Player styles and labels.
134 **/
135 public function getPlayerStyles()
136 {
137 $styles = [
138 PlayerStyle::STANDARD => [
139 'value' => PlayerStyle::STANDARD,
140 'label' => __('Standard', 'speechkit'),
141 ],
142 PlayerStyle::SMALL => [
143 'value' => PlayerStyle::SMALL,
144 'label' => __('Small', 'speechkit'),
145 ],
146 PlayerStyle::LARGE => [
147 'value' => PlayerStyle::LARGE,
148 'label' => __('Large', 'speechkit'),
149 ],
150 PlayerStyle::VIDEO => [
151 'value' => PlayerStyle::VIDEO,
152 'label' => __('Video', 'speechkit'),
153 'disabled' => true,
154 ],
155 ];
156
157 /**
158 * Which player style is the default?
159 * This is used to preselect the default option.
160 */
161 $defaultPlayerStyle = get_option('beyondwords_player_style', PlayerStyle::STANDARD);
162
163 if (isset($styles[$defaultPlayerStyle])) {
164 $styles[$defaultPlayerStyle]['default'] = true;
165 }
166
167 /**
168 * Is video enabled for this project?
169 * If not, the video <option> will have the "disabled" attribute.
170 */
171 $projectId = get_option('beyondwords_project_id');
172 $playerVideoSettings = $this->apiClient->getVideoSettings($projectId);
173
174 if (
175 is_array($playerVideoSettings)
176 && array_key_exists('enabled', $playerVideoSettings)
177 && $playerVideoSettings['enabled']
178 ) {
179 unset($styles[PlayerStyle::VIDEO]['disabled']);
180 }
181
182 /**
183 * Filters the player styles – the "Player style" `<select>` options
184 * presented on the plugin settings page and post edit screens.
185 *
186 * Each player style is an associative array with the following keys:
187 * - string `label` The option label e.g. "Standard"
188 * - string `value` The option value e.g. "standard"
189 * - boolean `disabled` (Optional) Is this option disabled?
190 * - boolean `default` (Optional) Is this the default player style, assigned in the plugin settings?
191 *
192 * Scheduled for removal in plugin version 5.0.0.
193 *
194 * @since 4.1.0
195 *
196 * @deprecated 4.3.0 Replaced with beyondwords_settings_player_styles.
197 *
198 * @param array $styles Associative array of player styles.
199 */
200 $styles = apply_filters('beyondwords_player_styles', $styles);
201
202 /**
203 * Filters the player styles – the "Player style" `<select>` options
204 * presented on the plugin settings page and post edit screens.
205 *
206 * Each player style is an associative array with the following keys:
207 * - string `label` The option label e.g. "Standard"
208 * - string `value` The option value e.g. "standard"
209 * - boolean `disabled` (Optional) Is this option disabled?
210 * - boolean `default` (Optional) Is this the default player style, assigned in the plugin settings?
211 *
212 * @since 4.1.0 Introduced as beyondwords_player_styles.
213 * @since 4.3.0 Renamed from beyondwords_player_styles to beyondwords_settings_player_styles.
214 *
215 * @param array $styles Associative array of player styles.
216 */
217 $styles = apply_filters('beyondwords_settings_player_styles', $styles);
218
219 $transientName = sprintf('beyondwords_player_styles[%s]', get_option('beyondwords_project_id'));
220 set_transient($transientName, $styles);
221
222 return $styles;
223 }
224
225 /**
226 * Get the cached Player styles for a project.
227 *
228 * The transient cache should have been set when the plugin settings were
229 * updated.
230 *
231 * @since 4.1.0
232 * @since 4.2.0 Fix: return empty array instead of false
233 *
234 * @param int $projectId BeyondWords Project ID.
235 *
236 * @return string[] Associative array of Player styles and labels.
237 **/
238 public static function getCachedPlayerStyles($projectId = '')
239 {
240 $transientName = sprintf('beyondwords_player_styles[%s]', $projectId);
241
242 $playerStyles = get_transient($transientName);
243
244 if (! is_array($playerStyles)) {
245 return [];
246 }
247
248 return $playerStyles;
249 }
250
251 /**
252 * Register the component scripts.
253 *
254 * @since 4.1.0
255 *
256 * @param string $hook Page hook
257 *
258 * @return void
259 */
260 public function adminEnqueueScripts($hook)
261 {
262 if ($hook === 'settings_page_beyondwords') {
263 wp_enqueue_script(
264 'beyondwords-settings--player-style',
265 BEYONDWORDS__PLUGIN_URI . 'src/Component/Settings/PlayerStyle/settings.js',
266 ['jquery', 'underscore'],
267 BEYONDWORDS__PLUGIN_VERSION,
268 true
269 );
270 }
271 }
272 }
273