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 / Core / Updater.php

Updater.php in BeyondWords – AI audio for publishers 4.6.0, at src/Core/Updater.php

202 lines 5.9 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 * BeyondWords updater.
7 *
8 * @package Beyondwords\Wordpress
9 * @author Stuart McAlpine <[email protected]>
10 * @since 3.0.0
11 */
12
13 namespace Beyondwords\Wordpress\Core;
14
15 use Beyondwords\Wordpress\Component\Settings\PlayerVersion\PlayerVersion;
16
17 /**
18 * BeyondWords updater.
19 *
20 * @since 3.7.0
21 */
22 class Updater
23 {
24 /**
25 * Run
26 *
27 * @since 4.0.0
28 */
29 public function run()
30 {
31 $version = get_option('beyondwords_version');
32
33 if ($version) {
34 // For existing installs, set default player version to "Legacy"
35 $this->setDefaultPlayerVersion(PlayerVersion::LEGACY_VERSION);
36 } else {
37 // For new installs, set default player version to "Latest"
38 $this->setDefaultPlayerVersion(PlayerVersion::LATEST_VERSION);
39
40 // Check legacy "speechkit" version for version number
41 $version = get_option('speechkit_version', '1.0.0');
42 }
43
44 if (version_compare($version, '3.0.0', '<')) {
45 $this->migrateSettings();
46 }
47
48 if (version_compare($version, '3.7.0', '<')) {
49 $this->renamePluginSettings();
50 }
51
52 // Always update the plugin version, to handle e.g. FTP plugin updates
53 update_option('beyondwords_version', BEYONDWORDS__PLUGIN_VERSION);
54 }
55
56 /**
57 * Migrate settings.
58 *
59 * In v3.0.0 the locations for plugin settings changed. This method migrates settings from the
60 * old location to the new. e.g. from speechkit_settings.speechkit_api_key to beyondwords_api_key.
61 *
62 * @since 3.0.0
63 * @since 3.5.0 Refactored, adding $this->constructPreselectSetting().
64 *
65 * @return void
66 */
67 public function migrateSettings()
68 {
69 $oldSettings = get_option('speechkit_settings', []);
70
71 if (! is_array($oldSettings) || empty($oldSettings)) {
72 return;
73 }
74
75 $settingsMap = [
76 'speechkit_api_key' => 'speechkit_api_key',
77 'speechkit_id' => 'speechkit_project_id',
78 'speechkit_merge_excerpt' => 'speechkit_prepend_excerpt',
79 ];
80
81 // Simple mapping of 'old key' -> 'new key'
82 foreach ($settingsMap as $oldKey => $newKey) {
83 if (array_key_exists($oldKey, $oldSettings) && ! get_option($newKey)) {
84 add_option($newKey, $oldSettings[$oldKey]);
85 }
86 }
87
88 if (get_option('speechkit_preselect') === false) {
89 $preselectSetting = $this->constructPreselectSetting();
90
91 add_option('speechkit_preselect', $preselectSetting);
92 }
93 }
94
95 /**
96 * Construct 'beyondwords_preselect' setting.
97 *
98 * The v3 `beyondwords_preselect` setting is constructed from the data contained in the v2
99 * `speechkit_select_post_types` and `speechkit_selected_categories` fields.
100 *
101 * @since 3.5.0
102 *
103 * @return array `beyondwords_preselect` setting.
104 */
105 public function constructPreselectSetting()
106 {
107 $oldSettings = get_option('speechkit_settings', []);
108
109 if (! is_array($oldSettings) || empty($oldSettings)) {
110 return false;
111 }
112
113 $preselect = [];
114
115 // Build the top level of beyondwords_preselect, for post types
116 if (
117 array_key_exists('speechkit_select_post_types', $oldSettings) &&
118 ! empty($oldSettings['speechkit_select_post_types'])
119 ) {
120 $preselect = array_fill_keys($oldSettings['speechkit_select_post_types'], '1');
121 }
122
123 // Build the taxonomy level of beyondwords_preselect
124 if (
125 array_key_exists('speechkit_selected_categories', $oldSettings) &&
126 ! empty($oldSettings['speechkit_selected_categories'])
127 ) {
128 // Categories can be assigned to multiple post types
129 $taxonomy = get_taxonomy('category');
130
131 if (is_array($taxonomy->object_type)) {
132 foreach ($taxonomy->object_type as $postType) {
133 // Post type: e.g. "post"
134 $preselect[$postType] = [
135 // Taxonomy: "category"
136 'category' => $oldSettings['speechkit_selected_categories'],
137 ];
138 }
139 }
140 }
141
142 return $preselect;
143 }
144
145 /**
146 * Rename plugin settings.
147 *
148 * In v3.7.0 the plugin settings change from `speechkit_*` to `beyondwords_*`.
149 *
150 * For now, we will leave `speechkit_*` settings in the db, to support plugin downgrades.
151 *
152 * @since 3.7.0
153 *
154 * @return void
155 */
156 public function renamePluginSettings()
157 {
158 $apiKey = get_option('speechkit_api_key');
159 $projectId = get_option('speechkit_project_id');
160 $prependExcerpt = get_option('speechkit_prepend_excerpt');
161 $preselect = get_option('speechkit_preselect');
162
163 if ($apiKey) {
164 update_option('beyondwords_api_key', $apiKey);
165 }
166
167 if ($projectId) {
168 update_option('beyondwords_project_id', $projectId);
169 }
170
171 if ($prependExcerpt) {
172 update_option('beyondwords_prepend_excerpt', $prependExcerpt);
173 }
174
175 if ($preselect) {
176 update_option('beyondwords_preselect', $preselect);
177 }
178 }
179
180 /**
181 * Set default player version during install/upgrade.
182 *
183 * From v4.0.0 we support two players: "Legacy" and "Latest".
184 *
185 * New sites should use the Latest player by default.
186 * Sites upgrading from < v4.0.0 should use the Legacy player by default.
187 *
188 * @since 4.0.0
189 *
190 * @return void
191 */
192 public function setDefaultPlayerVersion($newPlayerVersion)
193 {
194 $playerVersion = get_option('beyondwords_player_version');
195
196 // Handle "0" version string, which is falsey in PHP
197 if (! strlen(strval($playerVersion))) {
198 update_option('beyondwords_player_version', $newPlayerVersion);
199 }
200 }
201 }
202