PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.3.0
WP 2FA – Two-factor authentication for WordPress v2.3.0
4.1.0 4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / Utils / class-migration.php
wp-2fa / includes / classes / Utils Last commit date
class-abstract-migration.php 3 years ago class-date-time-utils.php 4 years ago class-debugging.php 4 years ago class-generate-modal.php 3 years ago class-migration.php 3 years ago class-request-utils.php 4 years ago class-settings-utils.php 4 years ago class-user-utils.php 3 years ago index.php 5 years ago
class-migration.php
327 lines
1 <?php
2 /**
3 * Responsible for plugin updates.
4 *
5 * @package wp2fa
6 * @subpackage utils
7 * @copyright 2021 WP White Security
8 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
9 * @link https://wordpress.org/plugins/wp-2fa/
10 */
11
12 namespace WP2FA\Utils;
13
14 use \WP2FA\Utils\User_Utils as User_Utils;
15 use WP2FA\Utils\Settings_Utils as Settings_Utils;
16
17 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
18
19 /**
20 * Migration class
21 */
22 if ( ! class_exists( '\WP2FA\Utils\Migration' ) ) {
23
24 /**
25 * Put all you migration methods here
26 *
27 * @package WP2FA\Utils
28 * @since 1.6
29 */
30 class Migration extends Abstract_Migration {
31
32 /**
33 * The name of the option from which we should extract version
34 * Note: version is expected in version format - 1.0.0; 1; 1.0; 1.0.0.0
35 * Note: only numbers will be processed
36 *
37 * @var string
38 *
39 * @since 1.6.0
40 */
41 protected static $version_option_name = WP_2FA_PREFIX . 'plugin_version';
42
43 /**
44 * The constant name where the plugin version is stored
45 * Note: version is expected in version format - 1.0.0; 1; 1.0; 1.0.0.0
46 * Note: only numbers will be processed
47 *
48 * @var string
49 *
50 * @since 1.6.0
51 */
52 protected static $const_name_of_plugin_version = 'WP_2FA_VERSION';
53
54 /**
55 * The name of the plugin settings
56 *
57 * @var string
58 */
59 private static $plugin_settings_name = WP_2FA_SETTINGS_NAME;
60
61 /**
62 * The name of the plugin policy settings
63 *
64 * @var string
65 */
66 private static $plugin_policy_name = WP_2FA_POLICY_SETTINGS_NAME;
67
68 /**
69 * The name of the plugin white label settings
70 *
71 * @var string
72 */
73 private static $plugin_white_label_name = WP_2FA_WHITE_LABEL_SETTINGS_NAME;
74
75 /**
76 * The name of the plugin email settings
77 *
78 * @var string
79 */
80 private static $plugin_email_settings_name = WP_2FA_EMAIL_SETTINGS_NAME;
81
82 /**
83 * Migration for version upto 1.6.0
84 *
85 * @return void
86 * @since 1.6.0
87 */
88 protected static function migrate_up_to_160() {
89 $settings = self::get_settings( self::$plugin_settings_name );
90 if ( ! is_array( $settings ) ) {
91 return;
92 }
93
94 $needs_update = false;
95
96 $settings_to_convert = array( 'enforced_roles', 'enforced_users', 'excluded_users', 'excluded_roles' );
97 foreach ( $settings_to_convert as $setting_name ) {
98 if ( array_key_exists( $setting_name, $settings ) && ! is_array( $settings[ $setting_name ] ) ) {
99 $settings[ $setting_name ] = array_filter(
100 explode( ',', $settings[ $setting_name ] )
101 );
102 $needs_update = true;
103 }
104 }
105
106 if ( ! isset( $settings['backup_codes_enabled'] ) ) {
107 $settings['backup_codes_enabled'] = 'yes';
108 $needs_update = true;
109 }
110
111 if ( $needs_update ) {
112 // Update settings.
113 self::set_settings( self::$plugin_settings_name, $settings );
114 }
115 }
116
117 /**
118 * Migration for version upto 1.6.2
119 *
120 * @return void
121 * @since 1.6.2
122 */
123 protected static function migrate_up_to_162() {
124 $settings = self::get_settings( self::$plugin_settings_name );
125 if ( ! is_array( $settings ) ) {
126 return;
127 }
128
129 $needs_update = false;
130
131 $settings_to_convert = array( 'excluded_sites' );
132 foreach ( $settings_to_convert as $setting_name ) {
133 if ( array_key_exists( $setting_name, $settings ) && ! is_array( $settings[ $setting_name ] ) ) {
134 $original_settings_split = array_filter(
135 explode( ',', $settings[ $setting_name ] )
136 );
137 $settings[ $setting_name ] = array();
138 foreach ( $original_settings_split as $value ) {
139 $settings[ $setting_name ][] = mb_substr( $value, mb_strrpos( $value, ':' ) + 1 );
140 }
141 $needs_update = true;
142 }
143 }
144
145 self::migrate_up_to_160();
146
147 if ( $needs_update ) {
148 // Update settings.
149 self::set_settings( self::$plugin_settings_name, $settings );
150 }
151 }
152
153 /**
154 * Migration for version upto 1.5.0
155 *
156 * @return void
157 */
158 protected static function migrate_up_to_150() {
159 $settings = self::get_settings( self::$plugin_settings_name );
160
161 if ( is_array( $settings ) && array_key_exists( 'enforcment-policy', $settings ) ) {
162 // Correct setting name.
163 $settings['enforcement-policy'] = $settings['enforcment-policy'];
164 // Remove old setting.
165 unset( $settings['enforcment-policy'] );
166 // Update settings.
167 self::set_settings( self::$plugin_settings_name, $settings );
168 }
169 }
170
171 /**
172 * Migration for version upto 1.7.0
173 *
174 * @return void
175 */
176 protected static function migrate_up_to_170() {
177 $settings = self::get_settings( self::$plugin_settings_name );
178
179 if ( is_array( $settings ) && array_key_exists( 'notify_users', $settings ) ) {
180 // Remove old setting.
181 unset( $settings['notify_users'] );
182 // Update settings.
183 self::set_settings( self::$plugin_settings_name, $settings );
184 }
185
186 $email_settings = self::get_settings( self::$plugin_email_settings_name );
187 $items_to_remove = array( 'send_enforced_email', 'enforced_email_subject', 'enforced_email_body' );
188
189 if ( is_array( $email_settings ) && User_Utils::in_array_all( $items_to_remove, $email_settings ) ) {
190 foreach ( $items_to_remove as $item ) {
191 if ( isset( $email_settings[ $item ] ) ) {
192 unset( $email_settings[ $item ] );
193 }
194 }
195 // Update settings.
196 self::set_settings( self::$plugin_email_settings_name, $email_settings );
197 }
198 }
199
200 /**
201 * Migration for version upto 2.0.0
202 * Separates the current settings into 3 different types of settings:
203 * - Policy
204 * - General
205 * - White label
206 *
207 * @return void
208 */
209 protected static function migrate_up_to_200() {
210 $settings = self::get_settings( self::$plugin_settings_name );
211
212 if ( is_array( $settings ) ) {
213
214 $new_settings_array = array_flip(
215 array(
216 'enable_grace_cron',
217 'limit_access',
218 'delete_data_upon_uninstall',
219 'enable_destroy_session',
220 )
221 );
222
223 $new_white_label_array = array_flip(
224 array(
225 'default-text-code-page',
226 )
227 );
228
229 $settings_array = array_intersect_key(
230 $settings,
231 $new_settings_array
232 );
233
234 $settings = array_diff_key( $settings, $new_settings_array );
235
236 self::set_settings( self::$plugin_settings_name, $settings_array );
237
238 $white_label_settings = array_intersect_key(
239 $settings,
240 $new_white_label_array
241 );
242
243 $settings = array_diff_key( $settings, $new_white_label_array );
244
245 self::set_settings( self::$plugin_white_label_name, $white_label_settings );
246
247 self::set_settings( self::$plugin_policy_name, $settings );
248 }
249 }
250
251 /**
252 * Migration for version upto 2.2.0
253 *
254 * @return void
255 */
256 protected static function migrate_up_to_220() {
257 global $wpdb;
258
259 $new_prefix = 'wp_2fa_trusted_device_';
260 $old_prefix = 'wp2fa_trusted_device_';
261
262 delete_transient( 'wp_2fa_config_file_hash' );
263
264 $wpdb->query(
265 $wpdb->prepare(
266 "
267 UPDATE $wpdb->usermeta
268 SET meta_key = REPLACE( meta_key, %s, %s )
269 WHERE meta_key LIKE %s
270 ",
271 array(
272 $old_prefix,
273 $new_prefix,
274 $old_prefix . '%',
275 )
276 )
277 );
278 }
279
280 /**
281 * Migration for version upto 2.3.0
282 *
283 * @return void
284 */
285 protected static function migrate_up_to_230() {
286
287 $version = self::get_settings( self::$version_option_name );
288
289 if ( $version && version_compare( $version, '2.2.1', '<=' ) ) {
290 $settings = self::get_settings( self::$plugin_white_label_name );
291
292 if ( isset( $settings['enable_wizard_styling'] ) ) {
293 $settings['enable_wizard_styling'] = false;
294 } else {
295 $settings = array();
296 $settings['enable_wizard_styling'] = false;
297 }
298
299 self::set_settings( self::$plugin_white_label_name, $settings );
300 }
301 }
302
303 /**
304 * Returns the plugin settings by a given setting type
305 *
306 * @param mixed $setting_name - The setting which needs to be extracted.
307 *
308 * @return mixed
309 */
310 private static function get_settings( $setting_name ) {
311 return Settings_Utils::get_option( $setting_name );
312 }
313
314 /**
315 * Updates the plugin settings
316 *
317 * @param mixed $setting_name - The setting which needs to be updated.
318 * @param mixed $settings - The settings values.
319 *
320 * @return void
321 */
322 private static function set_settings( $setting_name, $settings ) {
323 Settings_Utils::update_option( $setting_name, $settings );
324 }
325 }
326 }
327