class-wc-cli-update-command.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * WC_CLI_Update_Command class file.
  4. *
  5. * @package WooCommerce\CLI
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * Allows updates via CLI.
  12. *
  13. * @version 3.0.0
  14. * @package WooCommerce
  15. */
  16. class WC_CLI_Update_Command {
  17. /**
  18. * Registers the update command.
  19. */
  20. public static function register_commands() {
  21. WP_CLI::add_command( 'wc update', array( 'WC_CLI_Update_Command', 'update' ) );
  22. }
  23. /**
  24. * Runs all pending WooCommerce database updates.
  25. */
  26. public static function update() {
  27. global $wpdb;
  28. $wpdb->hide_errors();
  29. include_once WC_ABSPATH . 'includes/class-wc-install.php';
  30. include_once WC_ABSPATH . 'includes/wc-update-functions.php';
  31. $current_db_version = get_option( 'woocommerce_db_version' );
  32. $update_count = 0;
  33. foreach ( WC_Install::get_db_update_callbacks() as $version => $update_callbacks ) {
  34. if ( version_compare( $current_db_version, $version, '<' ) ) {
  35. foreach ( $update_callbacks as $update_callback ) {
  36. /* translators: %s: DB update callback key */
  37. WP_CLI::log( sprintf( __( 'Calling update function: %s', 'woocommerce' ), $update_callback ) );
  38. call_user_func( $update_callback );
  39. $update_count ++;
  40. }
  41. }
  42. }
  43. WC_Admin_Notices::remove_notice( 'update' );
  44. /* translators: 1: Number of database updates performed 2: Database version number */
  45. WP_CLI::success( sprintf( __( '%1$d updates complete. Database version is %2$s', 'woocommerce' ), absint( $update_count ), get_option( 'woocommerce_db_version' ) ) );
  46. }
  47. }