PluginProbe
BeyondWords – AI audio for publishers / 4.4.0
BeyondWords – AI audio for publishers v4.4.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 4.7.0 All 43 releases
speechkit / src / Component / Settings / PlayerStyle / PlayerStyle.php

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

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