PluginProbe
BeyondWords – AI audio for publishers / 4.5.0
BeyondWords – AI audio for publishers v4.5.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.5.0, at src/Component/Settings/PlayerVersion/PlayerVersion.php

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