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 | * @since 6.0.0 Make static. |
| 28 | */ |
| 29 | public static function run() |
| 30 | { |
| 31 | $version = get_option('beyondwords_version', '1.0.0'); |
| 32 | |
| 33 | if (version_compare($version, '5.0.0', '<') && get_option('beyondwords_api_key')) { |
| 34 | wp_cache_set('beyondwords_sync_to_wordpress', ['all'], 'beyondwords', 60); |
| 35 | } |
| 36 | |
| 37 | if (version_compare($version, '3.0.0', '<')) { |
| 38 | self::migrateSettings(); |
| 39 | } |
| 40 | |
| 41 | if (version_compare($version, '3.7.0', '<')) { |
| 42 | self::renamePluginSettings(); |
| 43 | } |
| 44 | |
| 45 | // Record the date activated so we can track how long users have been using the plugin. |
| 46 | add_option('beyondwords_date_activated', gmdate(\DateTime::ATOM), '', false); |
| 47 | |
| 48 | // Always update the plugin version, to handle e.g. FTP plugin updates |
| 49 | update_option('beyondwords_version', BEYONDWORDS__PLUGIN_VERSION); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Migrate settings. |
| 54 | * |
| 55 | * In v3.0.0 the locations for plugin settings changed. This method migrates settings from the |
| 56 | * old location to the new. e.g. from speechkit_settings.speechkit_api_key to beyondwords_api_key. |
| 57 | * |
| 58 | * @since 3.0.0 |
| 59 | * @since 3.5.0 Refactored, adding self::constructPreselectSetting(). |
| 60 | * @since 6.0.0 Make static. |
| 61 | * |
| 62 | * @return void |
| 63 | */ |
| 64 | public static function migrateSettings() |
| 65 | { |
| 66 | $oldSettings = get_option('speechkit_settings', []); |
| 67 | |
| 68 | if (! is_array($oldSettings) || empty($oldSettings)) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | $settingsMap = [ |
| 73 | 'speechkit_api_key' => 'speechkit_api_key', |
| 74 | 'speechkit_id' => 'speechkit_project_id', |
| 75 | 'speechkit_merge_excerpt' => 'speechkit_prepend_excerpt', |
| 76 | ]; |
| 77 | |
| 78 | // Simple mapping of 'old key' :: 'new key' |
| 79 | foreach ($settingsMap as $oldKey => $newKey) { |
| 80 | if (array_key_exists($oldKey, $oldSettings) && ! get_option($newKey)) { |
| 81 | add_option($newKey, $oldSettings[$oldKey]); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (get_option('speechkit_preselect') === false) { |
| 86 | $preselectSetting = self::constructPreselectSetting(); |
| 87 | |
| 88 | add_option('speechkit_preselect', $preselectSetting); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Construct 'beyondwords_preselect' setting. |
| 94 | * |
| 95 | * The v3 `beyondwords_preselect` setting is constructed from the data contained in the v2 |
| 96 | * `speechkit_select_post_types` and `speechkit_selected_categories` fields. |
| 97 | * |
| 98 | * @since 3.5.0 |
| 99 | * @since 6.0.0 Make static. |
| 100 | * |
| 101 | * @return array `beyondwords_preselect` setting. |
| 102 | */ |
| 103 | public static 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 | * @since 6.0.0 Make static. |
| 152 | * |
| 153 | * @return void |
| 154 | */ |
| 155 | public static function renamePluginSettings() |
| 156 | { |
| 157 | $apiKey = get_option('speechkit_api_key'); |
| 158 | $projectId = get_option('speechkit_project_id'); |
| 159 | $prependExcerpt = get_option('speechkit_prepend_excerpt'); |
| 160 | $preselect = get_option('speechkit_preselect'); |
| 161 | |
| 162 | if ($apiKey) { |
| 163 | update_option('beyondwords_api_key', $apiKey, false); |
| 164 | } |
| 165 | |
| 166 | if ($projectId) { |
| 167 | update_option('beyondwords_project_id', $projectId, false); |
| 168 | } |
| 169 | |
| 170 | if ($prependExcerpt) { |
| 171 | update_option('beyondwords_prepend_excerpt', $prependExcerpt, false); |
| 172 | } |
| 173 | |
| 174 | if ($preselect) { |
| 175 | update_option('beyondwords_preselect', $preselect, false); |
| 176 | } |
| 177 | } |
| 178 | } |