From 083c9947328df00c0af65cfdf300e31ea74aa337 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 25 May 2018 14:18:19 +0100 Subject: [PATCH 1/2] Break up update routine and allow it run to multuple times --- includes/class-wc-background-updater.php | 13 +++++++--- includes/class-wc-install.php | 1 + includes/wc-update-functions.php | 33 ++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/includes/class-wc-background-updater.php b/includes/class-wc-background-updater.php index 6208871ac5d..4fdba3fbe01 100644 --- a/includes/class-wc-background-updater.php +++ b/includes/class-wc-background-updater.php @@ -102,15 +102,22 @@ class WC_Background_Updater extends WC_Background_Process { include_once dirname( __FILE__ ) . '/wc-update-functions.php'; + $result = false; + if ( is_callable( $callback ) ) { $logger->info( sprintf( 'Running %s callback', $callback ), array( 'source' => 'wc_db_updates' ) ); - call_user_func( $callback ); - $logger->info( sprintf( 'Finished %s callback', $callback ), array( 'source' => 'wc_db_updates' ) ); + $result = (bool) call_user_func( $callback ); + + if ( $result ) { + $logger->info( sprintf( '%s callback needs to run again', $callback ), array( 'source' => 'wc_db_updates' ) ); + } else { + $logger->info( sprintf( 'Finished running %s callback', $callback ), array( 'source' => 'wc_db_updates' ) ); + } } else { $logger->notice( sprintf( 'Could not find %s callback', $callback ), array( 'source' => 'wc_db_updates' ) ); } - return false; + return $result; } /** diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index d96ac02521e..f0cce6cc094 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -101,6 +101,7 @@ class WC_Install { ), '3.4.0' => array( 'wc_update_340_states', + 'wc_update_340_state', 'wc_update_340_last_active', 'wc_update_340_db_version', ), diff --git a/includes/wc-update-functions.php b/includes/wc-update-functions.php index f0e246ba06b..1e6a73226a2 100644 --- a/includes/wc-update-functions.php +++ b/includes/wc-update-functions.php @@ -1638,8 +1638,6 @@ function wc_update_330_db_version() { * Update state codes for Ireland and BD. */ function wc_update_340_states() { - global $wpdb; - $country_states = array( 'IE' => array( 'CK' => 'CO', @@ -1714,6 +1712,23 @@ function wc_update_340_states() { ), ); + update_option( 'woocommerce_update_340_states', $country_states ); +} + +/** + * Update next state in the queue. + * + * @return void + */ +function wc_update_340_state() { + global $wpdb; + + $country_states = array_filter( (array) get_option( 'woocommerce_update_340_states', array() ) ); + + if ( empty( $country_states ) ) { + return false; + } + foreach ( $country_states as $country => $states ) { foreach ( $states as $old => $new ) { $wpdb->query( @@ -1743,8 +1758,22 @@ function wc_update_340_states() { 'tax_rate_state' => strtoupper( $old ), ) ); + unset( $country_states[ $country ][ $old ] ); + + if ( empty( $country_states[ $country ] ) ) { + unset( $country_states[ $country ] ); + } + break 2; } } + + if ( ! empty( $country_states ) ) { + return update_option( 'woocommerce_update_340_states', $country_states ); + } + + delete_option( 'woocommerce_update_340_states' ); + + return false; } /** From 048b95e1358e7f12bc80d25f7c1635881a88f454 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 25 May 2018 16:36:07 +0100 Subject: [PATCH 2/2] Correct code to run again --- includes/class-wc-background-updater.php | 4 ++-- includes/wc-update-functions.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-background-updater.php b/includes/class-wc-background-updater.php index 4fdba3fbe01..3f80666a767 100644 --- a/includes/class-wc-background-updater.php +++ b/includes/class-wc-background-updater.php @@ -93,7 +93,7 @@ class WC_Background_Updater extends WC_Background_Process { * item from the queue. * * @param string $callback Update callback function. - * @return mixed + * @return string|bool */ protected function task( $callback ) { wc_maybe_define_constant( 'WC_UPDATING', true ); @@ -117,7 +117,7 @@ class WC_Background_Updater extends WC_Background_Process { $logger->notice( sprintf( 'Could not find %s callback', $callback ), array( 'source' => 'wc_db_updates' ) ); } - return $result; + return $result ? $callback : false; } /** diff --git a/includes/wc-update-functions.php b/includes/wc-update-functions.php index 1e6a73226a2..3d32c0ef66d 100644 --- a/includes/wc-update-functions.php +++ b/includes/wc-update-functions.php @@ -1718,7 +1718,7 @@ function wc_update_340_states() { /** * Update next state in the queue. * - * @return void + * @return bool True to run again, false if completed. */ function wc_update_340_state() { global $wpdb;