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

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