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

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

246 lines 7.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 * @since 4.1.0
187 *
188 * @param array $styles Associative array of player styles.
189 */
190 $styles = apply_filters('beyondwords_player_styles', $styles);
191
192 $transientName = sprintf('beyondwords_player_styles[%s]', get_option('beyondwords_project_id'));
193 set_transient($transientName, $styles);
194
195 return $styles;
196 }
197
198 /**
199 * Get the cached Player styles for a project.
200 *
201 * The transient cache should have been set when the plugin settings were
202 * updated.
203 *
204 * @since 4.1.0
205 * @since 4.2.0 Fix: return empty array instead of false
206 *
207 * @param int $projectId BeyondWords Project ID.
208 *
209 * @return string[] Associative array of Player styles and labels.
210 **/
211 public static function getCachedPlayerStyles($projectId = '')
212 {
213 $transientName = sprintf('beyondwords_player_styles[%s]', $projectId);
214
215 $playerStyles = get_transient($transientName);
216
217 if (! is_array($playerStyles)) {
218 return [];
219 }
220
221 return $playerStyles;
222 }
223
224 /**
225 * Register the component scripts.
226 *
227 * @since 4.1.0
228 *
229 * @param string $hook Page hook
230 *
231 * @return void
232 */
233 public function adminEnqueueScripts($hook)
234 {
235 if ($hook === 'settings_page_beyondwords') {
236 wp_enqueue_script(
237 'beyondwords-settings--player-style',
238 BEYONDWORDS__PLUGIN_URI . 'src/Component/Settings/PlayerStyle/settings.js',
239 ['jquery', 'underscore'],
240 BEYONDWORDS__PLUGIN_VERSION,
241 true
242 );
243 }
244 }
245 }
246