Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.16% |
47 / 51 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
Updater | |
92.16% |
47 / 51 |
|
50.00% |
2 / 4 |
26.33 | |
0.00% |
0 / 1 |
run | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
5.93 | |||
migrateSettings | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
7 | |||
constructPreselectSetting | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
9.02 | |||
renamePluginSettings | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 |
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 | /** |
16 | * BeyondWords updater. |
17 | * |
18 | * @since 3.7.0 |
19 | */ |
20 | class Updater |
21 | { |
22 | /** |
23 | * Run |
24 | * |
25 | * @since 4.0.0 |
26 | * @since 5.4.0 Add beyondwords_date_activated option. |
27 | */ |
28 | public function run() |
29 | { |
30 | $version = get_option('beyondwords_version', '1.0.0'); |
31 | |
32 | if (version_compare($version, '5.0.0', '<') && get_option('beyondwords_api_key')) { |
33 | wp_cache_set('beyondwords_sync_to_wordpress', ['all'], 'beyondwords', 60); |
34 | } |
35 | |
36 | if (version_compare($version, '3.0.0', '<')) { |
37 | $this->migrateSettings(); |
38 | } |
39 | |
40 | if (version_compare($version, '3.7.0', '<')) { |
41 | $this->renamePluginSettings(); |
42 | } |
43 | |
44 | // Record the date activated so we can track how long users have been using the plugin. |
45 | add_option('beyondwords_date_activated', gmdate(\DateTime::ATOM), '', false); |
46 | |
47 | // Always update the plugin version, to handle e.g. FTP plugin updates |
48 | update_option('beyondwords_version', BEYONDWORDS__PLUGIN_VERSION); |
49 | } |
50 | |
51 | /** |
52 | * Migrate settings. |
53 | * |
54 | * In v3.0.0 the locations for plugin settings changed. This method migrates settings from the |
55 | * old location to the new. e.g. from speechkit_settings.speechkit_api_key to beyondwords_api_key. |
56 | * |
57 | * @since 3.0.0 |
58 | * @since 3.5.0 Refactored, adding $this->constructPreselectSetting(). |
59 | * |
60 | * @return void |
61 | */ |
62 | public function migrateSettings() |
63 | { |
64 | $oldSettings = get_option('speechkit_settings', []); |
65 | |
66 | if (! is_array($oldSettings) || empty($oldSettings)) { |
67 | return; |
68 | } |
69 | |
70 | $settingsMap = [ |
71 | 'speechkit_api_key' => 'speechkit_api_key', |
72 | 'speechkit_id' => 'speechkit_project_id', |
73 | 'speechkit_merge_excerpt' => 'speechkit_prepend_excerpt', |
74 | ]; |
75 | |
76 | // Simple mapping of 'old key' -> 'new key' |
77 | foreach ($settingsMap as $oldKey => $newKey) { |
78 | if (array_key_exists($oldKey, $oldSettings) && ! get_option($newKey)) { |
79 | add_option($newKey, $oldSettings[$oldKey]); |
80 | } |
81 | } |
82 | |
83 | if (get_option('speechkit_preselect') === false) { |
84 | $preselectSetting = $this->constructPreselectSetting(); |
85 | |
86 | add_option('speechkit_preselect', $preselectSetting); |
87 | } |
88 | } |
89 | |
90 | /** |
91 | * Construct 'beyondwords_preselect' setting. |
92 | * |
93 | * The v3 `beyondwords_preselect` setting is constructed from the data contained in the v2 |
94 | * `speechkit_select_post_types` and `speechkit_selected_categories` fields. |
95 | * |
96 | * @since 3.5.0 |
97 | * |
98 | * @return array `beyondwords_preselect` setting. |
99 | */ |
100 | public function constructPreselectSetting() |
101 | { |
102 | $oldSettings = get_option('speechkit_settings', []); |
103 | |
104 | if (! is_array($oldSettings) || empty($oldSettings)) { |
105 | return false; |
106 | } |
107 | |
108 | $preselect = []; |
109 | |
110 | // Build the top level of beyondwords_preselect, for post types |
111 | if ( |
112 | array_key_exists('speechkit_select_post_types', $oldSettings) && |
113 | ! empty($oldSettings['speechkit_select_post_types']) |
114 | ) { |
115 | $preselect = array_fill_keys($oldSettings['speechkit_select_post_types'], '1'); |
116 | } |
117 | |
118 | // Build the taxonomy level of beyondwords_preselect |
119 | if ( |
120 | array_key_exists('speechkit_selected_categories', $oldSettings) && |
121 | ! empty($oldSettings['speechkit_selected_categories']) |
122 | ) { |
123 | // Categories can be assigned to multiple post types |
124 | $taxonomy = get_taxonomy('category'); |
125 | |
126 | if (is_array($taxonomy->object_type)) { |
127 | foreach ($taxonomy->object_type as $postType) { |
128 | // Post type: e.g. "post" |
129 | $preselect[$postType] = [ |
130 | // Taxonomy: "category" |
131 | 'category' => $oldSettings['speechkit_selected_categories'], |
132 | ]; |
133 | } |
134 | } |
135 | } |
136 | |
137 | return $preselect; |
138 | } |
139 | |
140 | /** |
141 | * Rename plugin settings. |
142 | * |
143 | * In v3.7.0 the plugin settings change from `speechkit_*` to `beyondwords_*`. |
144 | * |
145 | * For now, we will leave `speechkit_*` settings in the db, to support plugin downgrades. |
146 | * |
147 | * @since 3.7.0 |
148 | * |
149 | * @return void |
150 | */ |
151 | public function renamePluginSettings() |
152 | { |
153 | $apiKey = get_option('speechkit_api_key'); |
154 | $projectId = get_option('speechkit_project_id'); |
155 | $prependExcerpt = get_option('speechkit_prepend_excerpt'); |
156 | $preselect = get_option('speechkit_preselect'); |
157 | |
158 | if ($apiKey) { |
159 | update_option('beyondwords_api_key', $apiKey, false); |
160 | } |
161 | |
162 | if ($projectId) { |
163 | update_option('beyondwords_project_id', $projectId, false); |
164 | } |
165 | |
166 | if ($prependExcerpt) { |
167 | update_option('beyondwords_prepend_excerpt', $prependExcerpt, false); |
168 | } |
169 | |
170 | if ($preselect) { |
171 | update_option('beyondwords_preselect', $preselect, false); |
172 | } |
173 | } |
174 | } |