From 9651a1d3fcb8c0c524df9b0091b5a3f25915102a Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 13 Dec 2016 13:38:20 +0000 Subject: [PATCH] Update CLI callback --- includes/class-wc-cli.php | 4 +- includes/class-wc-install.php | 13 +++++- includes/cli/class-wc-cli-tool-command.php | 3 +- includes/cli/class-wc-cli-update-command.php | 44 ++++++++++++++++++++ includes/wc-update-functions.php | 7 ++++ 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 includes/cli/class-wc-cli-update-command.php diff --git a/includes/class-wc-cli.php b/includes/class-wc-cli.php index 818da2db06b..d5ff5fceddb 100644 --- a/includes/class-wc-cli.php +++ b/includes/class-wc-cli.php @@ -22,6 +22,7 @@ class WC_CLI { require_once __DIR__ . '/cli/class-wc-cli-runner.php'; require_once __DIR__ . '/cli/class-wc-cli-rest-command.php'; require_once __DIR__ . '/cli/class-wc-cli-tool-command.php'; + require_once __DIR__ . '/cli/class-wc-cli-update-command.php'; } /** @@ -29,6 +30,7 @@ class WC_CLI { */ private function hooks() { WP_CLI::add_hook( 'after_wp_load', 'WC_CLI_Runner::after_wp_load' ); - WP_CLI::add_hook( 'after_wp_load', 'WC_CLI_Tool_Command::register_tool_commands' ); + WP_CLI::add_hook( 'after_wp_load', 'WC_CLI_Tool_Command::register_commands' ); + WP_CLI::add_hook( 'after_wp_load', 'WC_CLI_Update_Command::register_commands' ); } } diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index cd07bd64bf9..cca223c9c20 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -77,6 +77,7 @@ class WC_Install { 'wc_update_270_grouped_products', 'wc_update_270_settings', 'wc_update_270_product_visibility', + 'wc_update_270_db_version', ), ); @@ -223,6 +224,16 @@ class WC_Install { add_option( 'woocommerce_version', WC()->version ); } + /** + * Get list of DB update callbacks. + * + * @since 2.7.0 + * @return array + */ + public static function get_db_update_callbacks() { + return self::$db_updates; + } + /** * Push all needed DB updates to the queue for processing. */ @@ -231,7 +242,7 @@ class WC_Install { $logger = wc_get_logger(); $update_queued = false; - foreach ( self::$db_updates as $version => $update_callbacks ) { + foreach ( self::get_db_update_callbacks() as $version => $update_callbacks ) { if ( version_compare( $current_db_version, $version, '<' ) ) { foreach ( $update_callbacks as $update_callback ) { $logger->add( 'wc_db_updates', sprintf( 'Queuing %s - %s', $version, $update_callback ) ); diff --git a/includes/cli/class-wc-cli-tool-command.php b/includes/cli/class-wc-cli-tool-command.php index 954569a8b40..30b2f7c77b6 100644 --- a/includes/cli/class-wc-cli-tool-command.php +++ b/includes/cli/class-wc-cli-tool-command.php @@ -15,7 +15,7 @@ class WC_CLI_Tool_Command { * since we only want to enable certain actions on the system status * tools endpoints. */ - public static function register_tool_commands() { + public static function register_commands() { global $wp_rest_server; $request = new WP_REST_Request( 'OPTIONS', '/wc/v1/system_status/tools' ); @@ -86,7 +86,6 @@ class WC_CLI_Tool_Command { 'when' => ! empty( $command_args['when'] ) ? $command_args['when'] : '', 'before_invoke' => $before_invoke, ) ); - } } diff --git a/includes/cli/class-wc-cli-update-command.php b/includes/cli/class-wc-cli-update-command.php new file mode 100644 index 00000000000..925a0cf0fcb --- /dev/null +++ b/includes/cli/class-wc-cli-update-command.php @@ -0,0 +1,44 @@ +hide_errors(); + + include_once( WC_ABSPATH . 'includes/class-wc-install.php' ); + include_once( WC_ABSPATH . 'includes/wc-update-functions.php' ); + + $current_db_version = get_option( 'woocommerce_db_version' ); + $update_count = 0; + + foreach ( WC_Install::get_db_update_callbacks() as $version => $update_callbacks ) { + if ( version_compare( $current_db_version, $version, '<' ) ) { + foreach ( $update_callbacks as $update_callback ) { + WP_CLI::log( sprintf( __( 'Calling update function: %s', 'woocommerce' ), $update_callback ) ); + call_user_func( $update_callback ); + $update_count ++; + } + } + } + + WC_Admin_Notices::remove_notice( 'update' ); + WP_CLI::success( sprintf( __( '%1$d updates complete. Database version is %2$s', 'woocommerce' ), absint( $update_count ), get_option( 'woocommerce_db_version' ) ) ); + } +} diff --git a/includes/wc-update-functions.php b/includes/wc-update-functions.php index 981d5290fe4..6b2171eaff9 100644 --- a/includes/wc-update-functions.php +++ b/includes/wc-update-functions.php @@ -1070,3 +1070,10 @@ function wc_update_270_product_visibility() { $wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 5;", $rating_term->term_id ) ); } } + +/** + * Update DB Version. + */ +function wc_update_270_db_version() { + WC_Install::update_db_version( '2.7.0' ); +}