2016-12-13 13:38:20 +00:00
< ? php
2018-03-06 12:28:29 +00:00
/**
* WC_CLI_Update_Command class file .
*
* @ package WooCommerce\CLI
*/
2017-05-23 14:40:19 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
2016-12-13 13:38:20 +00:00
/**
* Allows updates via CLI .
*
2017-03-15 16:36:53 +00:00
* @ version 3.0 . 0
2016-12-13 13:38:20 +00:00
* @ 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 ();
2018-03-05 19:46:54 +00:00
include_once WC_ABSPATH . 'includes/class-wc-install.php' ;
include_once WC_ABSPATH . 'includes/wc-update-functions.php' ;
2016-12-13 13:38:20 +00:00
$current_db_version = get_option ( 'woocommerce_db_version' );
$update_count = 0 ;
2019-02-27 16:46:53 +00:00
$callbacks = WC_Install :: get_db_update_callbacks ();
$callbacks_to_run = array ();
2016-12-13 13:38:20 +00:00
2019-02-27 16:46:53 +00:00
foreach ( $callbacks as $version => $update_callbacks ) {
2016-12-13 13:38:20 +00:00
if ( version_compare ( $current_db_version , $version , '<' ) ) {
foreach ( $update_callbacks as $update_callback ) {
2019-02-27 16:46:53 +00:00
$callbacks_to_run [] = $update_callback ;
2016-12-13 13:38:20 +00:00
}
}
}
2019-02-27 16:46:53 +00:00
if ( empty ( $callbacks_to_run ) ) {
2019-05-09 09:24:49 +00:00
// Ensure DB version is set to the current WC version to match WP-Admin update routine.
WC_Install :: update_db_version ();
2019-02-27 16:46:53 +00:00
/* translators: %s Database version number */
WP_CLI :: success ( sprintf ( __ ( 'No updates required. Database version is %s' , 'woocommerce' ), get_option ( 'woocommerce_db_version' ) ) );
return ;
}
/* translators: 1: Number of database updates 2: List of update callbacks */
WP_CLI :: log ( sprintf ( __ ( 'Found %1$d updates (%2$s)' , 'woocommerce' ), count ( $callbacks_to_run ), implode ( ', ' , $callbacks_to_run ) ) );
$progress = \WP_CLI\Utils\make_progress_bar ( __ ( 'Updating database' , 'woocommerce' ), count ( $callbacks_to_run ) ); // phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound
foreach ( $callbacks_to_run as $update_callback ) {
call_user_func ( $update_callback );
2019-02-28 11:11:25 +00:00
$result = false ;
while ( $result ) {
$result = ( bool ) call_user_func ( $update_callback );
}
2019-02-27 16:46:53 +00:00
$update_count ++ ;
$progress -> tick ();
}
$progress -> finish ();
2016-12-13 13:38:20 +00:00
WC_Admin_Notices :: remove_notice ( 'update' );
2019-02-27 16:46:53 +00:00
2018-03-06 12:28:29 +00:00
/* translators: 1: Number of database updates performed 2: Database version number */
2019-02-27 16:46:53 +00:00
WP_CLI :: success ( sprintf ( __ ( '%1$d update functions completed. Database version is %2$s' , 'woocommerce' ), absint ( $update_count ), get_option ( 'woocommerce_db_version' ) ) );
2016-12-13 13:38:20 +00:00
}
}