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

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

297 lines 8.8 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 Version
7 *
8 * @package Beyondwords\Wordpress
9 * @author Stuart McAlpine <[email protected]>
10 * @since 4.0.0
11 */
12
13 namespace Beyondwords\Wordpress\Component\Settings\PlayerVersion;
14
15 use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
16
17 /**
18 * PlayerVersion setup
19 *
20 * @since 4.0.0
21 */
22 class PlayerVersion
23 {
24 /**
25 * Version 0: "Legacy"
26 */
27 public const LEGACY_VERSION = '0';
28
29 /**
30 * Version 1: "Latest"
31 */
32 public const LATEST_VERSION = '1';
33
34 /**
35 * API Client.
36 *
37 * @since 3.0.0
38 */
39 private $apiClient;
40
41 /**
42 * Constructor.
43 *
44 * @since 3.0.0
45 */
46 public function __construct($apiClient)
47 {
48 $this->apiClient = $apiClient;
49 }
50
51 /**
52 * Init.
53 *
54 * @since 4.0.0
55 */
56 public function init()
57 {
58 add_action('admin_init', array($this, 'addSettingsField'));
59 add_action('add_option_beyondwords_player_version', array($this, 'onAddPlayerVersionOption'), 10, 2);
60 add_action('update_option_beyondwords_player_version', array($this, 'onUpdatePlayerVersionOption'), 10, 2);
61 }
62
63 /**
64 * Init setting.
65 *
66 * @since 4.0.0
67 *
68 * @return void
69 */
70 public function addSettingsField()
71 {
72 if (! SettingsUtils::hasApiSettings()) {
73 return;
74 }
75
76 register_setting(
77 'beyondwords',
78 'beyondwords_player_version',
79 [
80 'default' => PlayerVersion::LATEST_VERSION,
81 'sanitize_callback' => array($this, 'sanitize'),
82 ]
83 );
84
85 add_settings_field(
86 'beyondwords-player-version',
87 __('Player version', 'speechkit'),
88 array($this, 'render'),
89 'beyondwords',
90 'player'
91 );
92 }
93
94 /**
95 * Render setting field.
96 *
97 * @since 4.0.0
98 *
99 * @return void
100 **/
101 public function render()
102 {
103 $playerVersion = get_option('beyondwords_player_version', PlayerVersion::LATEST_VERSION);
104
105 // Remove any "reverted" notifications if player is "Latest"
106 if ($playerVersion === PlayerVersion::LATEST_VERSION) {
107 delete_transient('beyondwords_player_reverted');
108 }
109
110 // Sync Player Version with API
111 $playerSettings = $this->apiClient->getPlayerSettings();
112
113 $syncedPlayerMessage = '';
114
115 // TODO refactor - it's a bit messy :/
116 // Does the API response have a player_version?
117 if ($playerSettings && array_key_exists('player_version', $playerSettings)) {
118 $apiPlayerVersion = $playerSettings['player_version'];
119
120 if ($apiPlayerVersion !== $playerVersion) {
121 $allPlayerVersions = PlayerVersion::getAllPlayerVersions();
122
123 if (array_key_exists($apiPlayerVersion, $allPlayerVersions)) {
124 $versionLabel = $allPlayerVersions[$apiPlayerVersion];
125 $syncedPlayerMessage = sprintf(
126 /* translators: %s is replaced with the player version from the BeyondWords API */
127 __('The player version on this WordPress site doesn’t match the “%s” player in your BeyondWords account. Saving the WordPress plugin settings will update your BeyondWords account to keep the player version in sync.', 'speechkit'), // phpcs:ignore Generic.Files.LineLength.TooLong
128 $versionLabel
129 );
130 } else {
131 $syncedPlayerMessage = __('Your BeyondWords account is currently using another player.', 'speechkit'); // phpcs:ignore Generic.Files.LineLength.TooLong
132 }
133 }
134 }
135 ?>
136 <div class="beyondwords-setting--player--player-version">
137 <select name="beyondwords_player_version">
138 <?php
139 $playerVersions = PlayerVersion::getAllPlayerVersions();
140
141 foreach ($playerVersions as $version => $label) {
142 printf(
143 '<option value="%s" %s>%s</option>',
144 esc_attr($version),
145 selected($version, $playerVersion),
146 esc_html($label)
147 );
148 }
149 ?>
150 </select>
151 </div>
152 <?php if ($syncedPlayerMessage) : ?>
153 <p class="description" style="color: #b32d2e;">
154 <?php echo esc_html($syncedPlayerMessage); ?>
155 </p>
156 <?php endif; ?>
157 <p class="description">
158 <?php
159 _e(
160 'We sync changes to the player version with your Beyondwords account when these settings are saved.', // phpcs:ignore Generic.Files.LineLength.TooLong
161 'speechkit'
162 );
163 ?>
164 </p>
165 <p class="description">
166 <?php
167 _e(
168 'If caching is enabled you may need to clear the cache to change the player version for existing posts.', // phpcs:ignore Generic.Files.LineLength.TooLong
169 'speechkit'
170 );
171 ?>
172 </p>
173 <?php
174 }
175
176 /**
177 * Sanitise the setting value.
178 *
179 * No sanitization is performed in this method. Instead we use it to set a
180 * transient notification if the setting changes from "Latest" to "Legacy"
181 * to help us provide technical support for player downgrades.
182 *
183 * @since 4.0.0
184 *
185 * @param array $value The submitted value.
186 *
187 * @return void
188 **/
189 public function sanitize($value)
190 {
191 $currentPlayerVersion = get_option('beyondwords_player_version');
192
193 // Hide "reverted" notification if player is "Latest"
194 if ($value === PlayerVersion::LATEST_VERSION) {
195 delete_transient('beyondwords_player_reverted');
196 }
197
198 // Are we downgrading from "Latest" to "Legacy"?
199 if ($currentPlayerVersion === PlayerVersion::LATEST_VERSION && $value === PlayerVersion::LEGACY_VERSION) {
200 // Display "reverted" notification for 7 days
201 set_transient('beyondwords_player_reverted', gmdate(DATE_ISO8601), 604800);
202 }
203
204 return $value;
205 }
206
207 /**
208 * Fires after the beyondwords_player_version option has been added.
209 *
210 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
211 *
212 * @since 4.0.0
213 *
214 * @param string $option Name of the option to add.
215 * @param mixed $value Value of the option.
216 *
217 * @return void
218 */
219 public function onAddPlayerVersionOption($option, $value)
220 {
221 $this->syncPlayerVersionSettingWithApi($value);
222 }
223
224 /**
225 * Fires after the value of the beyondwords_player_version option has been
226 * successfully updated.
227 *
228 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
229 *
230 * @since 4.0.0
231 *
232 * @param mixed $oldValue The old option value.
233 * @param mixed $value The new option value.
234 *
235 * @return void
236 */
237 public function onUpdatePlayerVersionOption($oldValue, $value)
238 {
239 $this->syncPlayerVersionSettingWithApi($value);
240 }
241
242 /**
243 * Sync "Player version" setting with BeyondWords API.
244 *
245 * @since 4.0.0
246 *
247 * @param mixed $value The new option value.
248 *
249 * @return void
250 */
251 public function syncPlayerVersionSettingWithApi($value)
252 {
253 // Exit if there are no API settings (required to make the API call)
254 if (! SettingsUtils::hasApiSettings()) {
255 return;
256 }
257
258 // Send updated "Player version" value to our API
259 $response = $this->apiClient->updatePlayerSettings([
260 'player_version' => $value,
261 ]);
262
263 if (
264 ! is_array($response)
265 || ! array_key_exists('player_version', $response)
266 || $response['player_version'] !== $value
267 ) {
268 $errors = get_transient('beyondwords_settings_errors', []);
269
270 if (empty($value)) {
271 $errors['Settings/PlayerVersion'] = __(
272 'There was an error syncing the player version with your BeyondWords account. The versions may be different. Sign in to your BeyondWords dashboard and change the player version there if you need them to be in sync.', // phpcs:ignore Generic.Files.LineLength.TooLong
273 'speechkit'
274 );
275 set_transient('beyondwords_settings_errors', $errors);
276 }
277 }
278 }
279
280 /**
281 * Get all player versions.
282 *
283 * @since 4.0.0
284 *
285 * @static
286 *
287 * @return string[] Associative array of player versions and labels.
288 **/
289 public static function getAllPlayerVersions()
290 {
291 return [
292 PlayerVersion::LATEST_VERSION => __('Latest', 'speechkit'),
293 PlayerVersion::LEGACY_VERSION => __('Legacy', 'speechkit'),
294 ];
295 }
296 }
297