<?php

/**
 * Update version.
 */
update_option('domain_for_sale_version', '3.3.0');
update_option('domain_for_sale_db_version', '3.3.0');

/**
 * Convert old data keys to new ones.
 */
function domain_for_sale_convert_old_to_new_data_3_3_0()
{
    $domain_for_sale_posts = new \WP_Query(
        array(
            'post_type'      => 'dfs_template',
            'posts_per_page' => 900,
        )
    );

    $post_ids = wp_list_pluck($domain_for_sale_posts->posts, 'ID');

    // Loop through each post ID to update the 'dfs_template_options' post meta
    foreach ($post_ids as $domain_id) {
        $template_options = get_post_meta($domain_id, 'dfs_template_options', true);

        $key_map = [
            'domain_name'        => 'domain_name',
            'dfs_sell_price'        => 'sell_price',
            'dfs-saletitle'         => 'sale_title',
            'dfs-content'           => 'sale_content',
            'dfs-contacttitle'      => 'contact_title',
            'dfs-contactinfos'      => 'contact_infos',
            'dfs-moredomainTitle'   => 'more_domain_title',
            'dfs-moredomains'       => 'more_domains',
            'dfs_color_scheme_type' => 'color_scheme_type',
            'dfs_custom_scheme'     => 'custom_scheme',
            'dfs_container_width'   => 'container_width',
            'dfs_body_typography'   => 'body_typo',
            'dfs_domain_name'       => 'domain_name_typo',
            'dfs_sale_title'        => 'sale_title_typo',
            'dfs_recommendation_title'   => 'recommendation_title_typo',
            'dfs_recommendation_domain_name'   => 'recommendation_domain_name_typo',
            'dfs-scheme'            => 'color_scheme',
        ];

        foreach ($key_map as $old_key => $new_key) {
            if (array_key_exists($old_key, $template_options)) {
                $template_options[$new_key] = $template_options[$old_key];
                unset($template_options[$old_key]);
            }
        }

        update_post_meta($domain_id, 'dfs_template_options', $template_options);
    }
}

$options = get_option('dfs-opt');
if ($options) {
    domain_for_sale_convert_old_to_new_data_3_3_0();
}