From d08a788756e016890c5e6c65e7a914b339bd7607 Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Sat, 9 May 2020 01:26:46 +0300 Subject: [PATCH] Only migrate options on version change (https://github.com/woocommerce/woocommerce-admin/pull/4324) --- plugins/woocommerce-admin/src/Install.php | 35 +++++++---------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/plugins/woocommerce-admin/src/Install.php b/plugins/woocommerce-admin/src/Install.php index c5f8ae1332d..becbebeb03b 100644 --- a/plugins/woocommerce-admin/src/Install.php +++ b/plugins/woocommerce-admin/src/Install.php @@ -71,41 +71,25 @@ class Install { // Add wc-admin report tables to list of WooCommerce tables. add_filter( 'woocommerce_install_get_tables', array( __CLASS__, 'add_tables' ) ); - - // Migrate option names by filtering their default values. - // This attaches a targeted filter for each migrated option name that will retreive - // the old value and use it as the default for the new option. This default - // will be used in the first retreival of the new option. - foreach ( self::$migrated_options as $new_option => $old_option ) { - add_filter( "default_option_{$new_option}", array( __CLASS__, 'handle_option_migration' ), 10, 2 ); - } } /** * Migrate option values to their new keys/names. - * - * @param mixed $default Default value for the option. - * @param string $new_option Option name. - * @return mixed Migrated option value. */ - public static function handle_option_migration( $default, $new_option ) { - if ( isset( self::$migrated_options[ $new_option ] ) ) { - wc_maybe_define_constant( 'WC_ADMIN_MIGRATING_OPTIONS', true ); + public static function migrate_options() { + wc_maybe_define_constant( 'WC_ADMIN_MIGRATING_OPTIONS', true ); - // Avoid infinite loops - this filter is applied in add_option(), update_option(), and get_option(). - remove_filter( "default_option_{$new_option}", array( __CLASS__, 'handle_option_migration' ), 10, 2 ); + foreach ( self::$migrated_options as $new_option => $old_option ) { + $old_option_value = get_option( $old_option, false ); - // Migrate the old option value. - $old_option_name = self::$migrated_options[ $new_option ]; - $old_option_value = get_option( $old_option_name, $default ); + // Continue if no option value was previously set. + if ( false === $old_option_value ) { + continue; + } update_option( $new_option, $old_option_value ); - delete_option( $old_option_name ); - - return $old_option_value; + delete_option( $old_option ); } - - return $default; } /** @@ -157,6 +141,7 @@ class Install { set_transient( 'wc_admin_installing', 'yes', MINUTE_IN_SECONDS * 10 ); wc_maybe_define_constant( 'WC_ADMIN_INSTALLING', true ); + self::migrate_options(); self::create_tables(); self::create_events(); self::delete_obsolete_notes();