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

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

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