<?php

defined( 'ABSPATH' ) || exit;

class Wiaas_DB_Update {

	private static $db_updates = array(
		'20180728222206' => 'wiaas_db_update_enable_product_by_user_role',
		'20180801222206' => 'wiaas_db_update_setup_gravity',
		'20180802222206' => 'wiaas_db_update_add_delivery_process_forms',
		'20180807222206' => 'wiaas_db_update_setup_customer_capabilities',
		'20180811134511' => 'wiaas_db_update_enable_orders_access_management',
		'20180813134511' => 'wiaas_db_update_enable_order_numbers',
		'20180826153509' => 'wiaas_create_broker_access_group',
		'20180911101010' => 'wiaas_db_setup_exclusive_taxonomies',
		'20180912101010' => 'wiaas_db_setup_default_cl'
	);

	public static function execute() {
		$pending_db_updates = self::get_pending_db_updates();
		foreach ( $pending_db_updates as $update_callback ) {
			self::execute_update($update_callback);
		}
	}

	public static function get_pending_db_updates() {
		$active_db_version = get_option( 'wiaas_db_version', '0' );
		$pending_db_updates = array();
		foreach (self::$db_updates as $version => $update_callback) {
			if ($active_db_version < $version) {
				$pending_db_updates[] = $update_callback;
			}
		}
		return $pending_db_updates;
	}

	public static function execute_update($update_callback) {
		call_user_func($update_callback);
		$version = array_search($update_callback, self::$db_updates);
		update_option('wiaas_db_version', $version);
	}
}
