<?php

/**
 *
 * Demo data
 *
 **/

function vite_nera_ocdi_import_files()
{
    $import_notice = '<h4>' . __('Important note before importing sample data.', 'vite-nera') . '</h4>';
    $import_notice .= __('Data import is generally not immediate and can take up to 10 minutes.', 'vite-nera') . '<br/>';
    $import_notice .= __('After you import this demo, you will have to configure the Instagram API key and Google Maps API key separately.', 'vite-nera');

    $import_notice = wp_kses(
        $import_notice,
        array(
            'a' => array(
                'href' => array(),
            ),
            'ol' => array(),
            'li' => array(),
            'h4' => array(),
            'br' => array(),
        )
    );

    $demos[] = array(
        'import_file_name' => 'Vite Nera Demo Import',
        'local_import_file' => trailingslashit(get_template_directory()) . 'assets/demo-data/vite-nera.xml',
        'local_import_widget_file' => trailingslashit(get_template_directory()) . 'assets/demo-data/vite-nera-widgets.wie',
        'import_notice' => $import_notice,
        'preview_url' => 'https://themes.getmotopress.com/vite-nera',
    );


    return $demos;
}

add_filter('pt-ocdi/import_files', 'vite_nera_ocdi_import_files');

function vite_nera_ocdi_after_import_setup($selected_import)
{
    // Assign menus to their locations.
    $menu1 = get_term_by('slug', 'primary', 'nav_menu');
    $menu2 = get_term_by('slug', 'contacts', 'nav_menu');
    $menu3 = get_term_by('slug', 'socials', 'nav_menu');
    $menu4 = get_term_by('slug', 'footer', 'nav_menu');

    set_theme_mod('nav_menu_locations', array(
            'menu-1' => $menu1->term_id,
            'menu-2' => $menu4->term_id,
            'menu-3' => $menu2->term_id,
            'menu-4' => $menu3->term_id,
        )
    );

    $menu4 = get_term_by('name', 'Footer 1', 'nav_menu');
    $menu5 = get_term_by('name', 'Footer 2', 'nav_menu');
    $menu6 = get_term_by('name', 'Footer 3', 'nav_menu');
    $nav_menu_widget = get_option('widget_nav_menu');

    if ($nav_menu_widget) {
        if ($menu4 && !empty($nav_menu_widget[2])) {
            $nav_menu_widget[2]['nav_menu'] = $menu4->term_id;
        }
        if ($menu5 && !empty($nav_menu_widget[3])) {
            $nav_menu_widget[3]['nav_menu'] = $menu5->term_id;
        }
        if ($menu6 && !empty($nav_menu_widget[4])) {
            $nav_menu_widget[4]['nav_menu'] = $menu6->term_id;
        }
    }

    // Assign front page and posts page (blog page).
    $front_page_id = get_page_by_title('Home');
    $blog_page_id = get_page_by_title('Blog');

    update_option('show_on_front', 'page');
    update_option('page_on_front', $front_page_id->ID);
    update_option('page_for_posts', $blog_page_id->ID);

    $shop_page_id       = get_page_by_title( 'Shop' );
    $cart_page_id       = get_page_by_title( 'Cart' );
    $checkout_page_id   = get_page_by_title( 'Checkout' );
    $account_page_id    = get_page_by_title( 'My account' );
    $privacy_page_id    = get_page_by_title( 'Terms & Conditions' );

    update_option( 'woocommerce_shop_page_id', $shop_page_id->ID );
    update_option( 'woocommerce_cart_page_id', $cart_page_id->ID );
    update_option( 'woocommerce_checkout_page_id', $checkout_page_id->ID );
    update_option( 'woocommerce_myaccount_page_id', $account_page_id->ID );
    update_option( 'woocommerce_terms_page_id', $privacy_page_id->ID );

    //update taxonomies
    $update_taxonomies = array(
        'post_tag',
        'category',
        'product_category'
    );

    foreach ($update_taxonomies as $taxonomy) {
        vite_nera_ocdi_update_taxonomy($taxonomy);
    }

    //set site default logo
    $logo = vite_nera_get_attachment_by_name('logo-icon');
    if ($logo) {
        set_theme_mod('custom_logo', $logo->ID);
    }

    $product_categories = [
        'Red Wine' => 'red-wine',
        'Rose Wine' => 'rose-wine',
        'White Wine' => 'white-wine',
    ];

    vite_nera_add_images_to_product_categories($product_categories);

    // need flush rewrite rules to woo pages be assigned properly
	// flush_rewrite_rules(); not working inside this action so simple delete option
	// option will be regenerated on next page generation
	delete_option('rewrite_rules');

}

add_action('pt-ocdi/after_import', 'vite_nera_ocdi_after_import_setup');

// Disable generation of smaller images (thumbnails) during the content import
//add_filter( 'pt-ocdi/regenerate_thumbnails_in_content_import', '__return_false' );

// Disable the branding notice
add_filter('pt-ocdi/disable_pt_branding', '__return_true');

function vite_nera_ocdi_update_taxonomy($taxonomy)
{
    $get_terms_args = array(
        'taxonomy' => $taxonomy,
        'fields' => 'ids',
        'hide_empty' => false,
    );

    $update_terms = get_terms($get_terms_args);
    if ($taxonomy && $update_terms) {
        wp_update_term_count_now($update_terms, $taxonomy);
    }
}

function vite_nera_make_existed_widgets_inactive()
{
    $widgets = get_option('sidebars_widgets');

    $sidebars = array(
        'footer-1',
        'footer-2',
        'footer-3',
        'footer-4',
        'header',
    );

    foreach ($sidebars as $sidebar) {
        if (is_active_sidebar($sidebar)) {
            $widgets['wp_inactive_widgets'] = array_merge($widgets['wp_inactive_widgets'], $widgets[$sidebar]);
            $widgets[$sidebar] = [];
        }
    }

    update_option('sidebars_widgets', $widgets);
}

add_action('pt-ocdi/widget_importer_before_widgets_import', 'vite_nera_make_existed_widgets_inactive');

function vite_nera_add_images_to_product_categories($room_categories)
{
    foreach ($room_categories as $room_category => $category_image) {
        $category_image = wp_get_attachment_image_url(vite_nera_get_attachment_by_name($category_image)->ID, 'full');
        $category_id = get_term_by('slug', $room_category, 'product_cat')->term_id;
        update_option('z_taxonomy_image' . $category_id, $category_image);
    }
}
