class-wc-cli-com-command.php
2 years ago
class-wc-cli-com-extension-command.php
2 years ago
class-wc-cli-rest-command.php
3 years ago
class-wc-cli-runner.php
3 years ago
class-wc-cli-tool-command.php
6 years ago
class-wc-cli-tracker-command.php
4 years ago
class-wc-cli-update-command.php
3 years ago
class-wc-cli-update-command.php
96 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WC_CLI_Update_Command class file. |
| 4 | * |
| 5 | * @package WooCommerce\CLI |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Allows updates via CLI. |
| 14 | * |
| 15 | * @version 3.0.0 |
| 16 | * @package WooCommerce |
| 17 | */ |
| 18 | class WC_CLI_Update_Command { |
| 19 | |
| 20 | /** |
| 21 | * Registers the update command. |
| 22 | */ |
| 23 | public static function register_commands() { |
| 24 | WC()->call_static( WP_CLI::class, 'add_command', 'wc update', array( 'WC_CLI_Update_Command', 'update' ) ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Runs all pending WooCommerce database updates. |
| 29 | */ |
| 30 | public static function update() { |
| 31 | global $wpdb; |
| 32 | |
| 33 | $wpdb->hide_errors(); |
| 34 | |
| 35 | include_once WC_ABSPATH . 'includes/class-wc-install.php'; |
| 36 | include_once WC_ABSPATH . 'includes/wc-update-functions.php'; |
| 37 | |
| 38 | $current_db_version = get_option( 'woocommerce_db_version' ); |
| 39 | $update_count = 0; |
| 40 | $callbacks = WC_Install::get_db_update_callbacks(); |
| 41 | $callbacks_to_run = array(); |
| 42 | |
| 43 | foreach ( $callbacks as $version => $update_callbacks ) { |
| 44 | if ( version_compare( $current_db_version, $version, '<' ) ) { |
| 45 | foreach ( $update_callbacks as $update_callback ) { |
| 46 | $callbacks_to_run[] = $update_callback; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if ( empty( $callbacks_to_run ) ) { |
| 52 | // Ensure DB version is set to the current WC version to match WP-Admin update routine. |
| 53 | WC_Install::update_db_version(); |
| 54 | |
| 55 | WC()->call_static( |
| 56 | WP_CLI::class, |
| 57 | 'success', |
| 58 | /* translators: %s Database version number */ |
| 59 | sprintf( __( 'No updates required. Database version is %s', 'woocommerce' ), get_option( 'woocommerce_db_version' ) ) |
| 60 | ); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | WC()->call_static( |
| 65 | WP_CLI::class, |
| 66 | 'log', |
| 67 | /* translators: 1: Number of database updates 2: List of update callbacks */ |
| 68 | sprintf( __( 'Found %1$d updates (%2$s)', 'woocommerce' ), count( $callbacks_to_run ), implode( ', ', $callbacks_to_run ) ) |
| 69 | ); |
| 70 | |
| 71 | $progress = WC()->call_function( |
| 72 | 'WP_CLI\Utils\make_progress_bar', |
| 73 | __( 'Updating database', 'woocommerce' ), |
| 74 | count( $callbacks_to_run ) // phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound |
| 75 | ); |
| 76 | |
| 77 | foreach ( $callbacks_to_run as $update_callback ) { |
| 78 | call_user_func( $update_callback ); |
| 79 | $update_count ++; |
| 80 | $progress->tick(); |
| 81 | } |
| 82 | |
| 83 | WC_Install::update_db_version(); |
| 84 | $progress->finish(); |
| 85 | |
| 86 | WC_Admin_Notices::remove_notice( 'update', true ); |
| 87 | |
| 88 | WC()->call_static( |
| 89 | WP_CLI::class, |
| 90 | 'success', |
| 91 | /* translators: 1: Number of database updates performed 2: Database version number */ |
| 92 | sprintf( __( '%1$d update functions completed. Database version is %2$s', 'woocommerce' ), absint( $update_count ), get_option( 'woocommerce_db_version' ) ) |
| 93 | ); |
| 94 | } |
| 95 | } |
| 96 |