<?php

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

/**
 * Convert old data keys to new ones.
 */
function domain_for_sale_convert_old_to_new_data_3_3_2()
{
    $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');

    foreach ($post_ids as $domain_id) {
        $template_options = get_post_meta($domain_id, 'dfs_template_options', true);

        if (!is_array($template_options)) {
            $template_options = [];
        }

        if (isset($template_options['dfs-domainname'])) {
            $template_options['domain_name'] = $template_options['dfs-domainname'] ?? '';
            $template_options['starting_price'] = $template_options['dfs_starting_price'] ?? '';
            $template_options['sell_price'] = $template_options['dfs_sell_price'] ?? '';
            $template_options['sale_title'] = $template_options['dfs-saletitle'] ?? '';
            $template_options['sale_content'] = $template_options['dfs-content'] ?? '';
            $template_options['contact_title'] = $template_options['dfs-contacttitle'] ?? '';
            $template_options['contact_infos'] = $template_options['dfs-contactinfos'] ?? '';
            $template_options['more_domain_title'] = $template_options['dfs-moredomainTitle'] ?? '';
            $template_options['more_domains'] = $template_options['dfs-moredomains'] ?? '';
            $template_options['color_scheme_type'] = $template_options['dfs_color_scheme_type'] ?? '';
            $template_options['custom_scheme'] = $template_options['dfs_custom_scheme'] ?? '';
            $template_options['container_width'] = $template_options['dfs_container_width'] ?? '';
            $template_options['body_typo'] = $template_options['dfs_body_typography'] ?? '';
            $template_options['domain_name_typo'] = $template_options['dfs_domain_name'] ?? '';
            $template_options['sale_title_typo'] = $template_options['dfs_sale_title'] ?? '';
            $template_options['recommendation_title_typo'] = $template_options['dfs_recommendation_title'] ?? '';
            $template_options['recommendation_domain_name_typo'] = $template_options['dfs_recommendation_domain_name'] ?? '';
            $template_options['color_scheme'] = $template_options['dfs-scheme'] ?? '';
            $template_options['single_page'] = $template_options['dfs_single_page'] ?? '';
            $template_options['multi_page'] = $template_options['dfs_multi_page'] ?? '';
            
            $old_keys = [
                'dfs-domainname',
                'dfs_starting_price',
                'dfs_sell_price',
                'dfs-saletitle',
                'dfs-content',
                'dfs-contacttitle',
                'dfs-contactinfos',
                'dfs-moredomainTitle',
                'dfs-moredomains',
                'dfs_color_scheme_type',
                'dfs_custom_scheme',
                'dfs_container_width',
                'dfs_body_typography',
                'dfs_domain_name',
                'dfs_sale_title',
                'dfs_recommendation_title',
                'dfs_recommendation_domain_name',
                'dfs-scheme',
                'dfs_single_page',
                'dfs_multi_page',
            ];

            foreach ($old_keys as $key) {
                unset($template_options[$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_2();
}

function domain_for_sale_recover_domain_name_3_3_2() {
    $query = new \WP_Query(array(
        'post_type'      => 'dfs_template',
        'posts_per_page' => 900,
        'post_status'    => 'any',
    ));
    foreach ($query->posts as $post) {
        $template_options = get_post_meta($post->ID, 'dfs_template_options', true);
        if (!is_array($template_options)) $template_options = [];
        if (empty($template_options['domain_name']) && !empty($post->post_title)) {
            $template_options['domain_name'] = $post->post_title;
            update_post_meta($post->ID, 'dfs_template_options', $template_options);
        }
    }
}
domain_for_sale_recover_domain_name_3_3_2();