Update CLI callback

This commit is contained in:
Mike Jolley 2016-12-13 13:38:20 +00:00
parent dad7dcbf4b
commit 9651a1d3fc
5 changed files with 67 additions and 4 deletions

View File

@ -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' );
}
}

View File

@ -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 ) );

View File

@ -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,
) );
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* Allows updates via CLI.
*
* @version 2.7.0
* @package WooCommerce
*/
class WC_CLI_Update_Command {
/**
* Registers the update command.
*/
public static function register_commands() {
WP_CLI::add_command( 'wc update', array( 'WC_CLI_Update_Command', 'update' ) );
}
/**
* Runs all pending WooCommerce database updates.
*/
public static function update() {
global $wpdb;
$wpdb->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' ) ) );
}
}

View File

@ -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' );
}