Нема описа

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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. $callbacks = WC_Install::get_db_update_callbacks();
  34. $callbacks_to_run = array();
  35. foreach ( $callbacks as $version => $update_callbacks ) {
  36. if ( version_compare( $current_db_version, $version, '<' ) ) {
  37. foreach ( $update_callbacks as $update_callback ) {
  38. $callbacks_to_run[] = $update_callback;
  39. }
  40. }
  41. }
  42. if ( empty( $callbacks_to_run ) ) {
  43. // Ensure DB version is set to the current WC version to match WP-Admin update routine.
  44. WC_Install::update_db_version();
  45. /* translators: %s Database version number */
  46. WP_CLI::success( sprintf( __( 'No updates required. Database version is %s', 'woocommerce' ), get_option( 'woocommerce_db_version' ) ) );
  47. return;
  48. }
  49. /* translators: 1: Number of database updates 2: List of update callbacks */
  50. WP_CLI::log( sprintf( __( 'Found %1$d updates (%2$s)', 'woocommerce' ), count( $callbacks_to_run ), implode( ', ', $callbacks_to_run ) ) );
  51. $progress = \WP_CLI\Utils\make_progress_bar( __( 'Updating database', 'woocommerce' ), count( $callbacks_to_run ) ); // phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound
  52. foreach ( $callbacks_to_run as $update_callback ) {
  53. call_user_func( $update_callback );
  54. $result = false;
  55. while ( $result ) {
  56. $result = (bool) call_user_func( $update_callback );
  57. }
  58. $update_count ++;
  59. $progress->tick();
  60. }
  61. $progress->finish();
  62. WC_Admin_Notices::remove_notice( 'update', true );
  63. /* translators: 1: Number of database updates performed 2: Database version number */
  64. WP_CLI::success( sprintf( __( '%1$d update functions completed. Database version is %2$s', 'woocommerce' ), absint( $update_count ), get_option( 'woocommerce_db_version' ) ) );
  65. }
  66. }