<?php
/**
 * WC_CLI_Update_Command class file.
 *
 * @package Wiaas\CLI
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Allows updates via CLI.
 *
 * @version 3.0.0
 * @package WooCommerce
 */
class Wiaas_CLI_Update_DB_Command {

	/**
	 * Registers the setup command.
	 */
	public static function register_commands() {
		WP_CLI::add_command( 'wiaas update-db', array( 'Wiaas_CLI_Update_DB_Command', 'update' ) );
	}

	/**
	 * Runs all pending Wiaas db updates
	 */
	public static function update() {
		global $wpdb;

		$wpdb->hide_errors();

		include_once WIAAS_DIR . '/includes/class-wiaas-db-update.php';
		include_once WIAAS_DIR . '/includes/db-updates/wiaas-db-update-general.php';
        include_once WIAAS_DIR . '/includes/db-updates/wiaas-db-update-roles.php';
        include_once WIAAS_DIR . '/includes/db-updates/wiaas-db-update-ui-fields.php';

		$pending_db_updates = Wiaas_DB_Update::get_pending_db_updates();

		foreach ( $pending_db_updates as $update_callback ) {
			/* translators: %s: DB update callback key */
			WP_CLI::log( sprintf( __( 'Executing update: %s', 'wiaas' ), $update_callback ) );
			Wiaas_DB_Update::execute_update($update_callback);
		}
		WP_CLI::success('DB update complete.');
	}
}
