<?php
/**
 * Migration depuis WP Downgrade (wp-downgrade).
 *
 * @package CVS
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class CVS_Migration {

	public static function maybe_migrate_from_wp_downgrade() {
		if ( get_option( 'cvs_migrated_from_wp_downgrade' ) ) {
			return;
		}

		$legacy_version = get_option( 'wpdg_specific_version_name', '' );
		$legacy_url     = get_option( 'wpdg_download_url', '' );
		$legacy_edit    = get_option( 'wpdg_edit_download_url', false );

		if ( ! $legacy_version && ! $legacy_url ) {
			return;
		}

		$settings = CVS_Settings::defaults();
		if ( $legacy_version ) {
			$settings['target_version'] = CVS_Settings::sanitize_version( $legacy_version );
		}
		if ( $legacy_edit && $legacy_url ) {
			$settings['use_custom_url']      = true;
			$settings['custom_download_url'] = esc_url_raw( $legacy_url );
		}

		update_option( CVS_Settings::OPTION_KEY, $settings );
		update_option( 'cvs_migrated_from_wp_downgrade', 1, false );
	}
}
