<?php

/**
 * Theme Demo Import.
 *
 * @package Arendelle
 */
if ( !defined( 'ABSPATH' ) ) {
    exit( 'Direct script access denied.' );
}
/*
* Demo Import
*/
function arendelle_ocdi_import_files() {
    return array(array(
        'import_file_name'           => 'Demo Import Free',
        'categories'                 => array('Category 1', 'Category 2'),
        'import_file_url'            => 'https://arendelle-free.deothemes.com/import/demo-content.xml',
        'import_widget_file_url'     => 'https://arendelle-free.deothemes.com/import/widgets.wie',
        'import_customizer_file_url' => 'https://arendelle-free.deothemes.com/import/customizer.dat',
    ));
}

add_filter( 'pt-ocdi/import_files', 'arendelle_ocdi_import_files' );
/*
* OCDI plugins
*/
function arendelle_ocdi_register_plugins(  $plugins  ) {
    $plugins = [
        [
            'name'     => esc_html__( 'Kirki', 'arendelle' ),
            'slug'     => 'kirki',
            'required' => true,
        ],
        [
            'name'     => esc_html__( 'Elementor', 'arendelle' ),
            'slug'     => 'elementor',
            'required' => true,
        ],
        [
            'name'     => esc_html__( 'WooCommerce', 'arendelle' ),
            'slug'     => 'woocommerce',
            'required' => true,
        ],
        [
            'name'     => esc_html__( 'Contact Form 7', 'arendelle' ),
            'slug'     => 'contact-form-7',
            'required' => false,
        ],
        [
            'name'     => esc_html__( 'Smash Balloon Social Photo Feed', 'arendelle' ),
            'slug'     => 'instagram-feed',
            'required' => false,
        ],
        [
            'name'     => esc_html__( 'MailChimp for WordPress', 'arendelle' ),
            'slug'     => 'mailchimp-for-wp',
            'required' => false,
        ],
        [
            'name'     => esc_html__( 'YITH WooCommerce Quick View', 'arendelle' ),
            'slug'     => 'yith-woocommerce-quick-view',
            'required' => false,
        ],
        [
            'name'     => esc_html__( 'YITH WooCommerce Wishlist', 'arendelle' ),
            'slug'     => 'yith-woocommerce-wishlist',
            'required' => false,
        ],
        [
            'name'     => esc_html__( 'WooCommerce Variation Swatches', 'arendelle' ),
            'slug'     => 'woo-variation-swatches',
            'required' => false,
        ]
    ];
    return $plugins;
}

add_filter( 'ocdi/register_plugins', 'arendelle_ocdi_register_plugins' );
/**
* Assign menus and front page after demo import
*
* @param array $selected_import array with demo import data
*/
function arendelle_ocdi_after_import(  $selected_import  ) {
    global $wpdb;
    // Assign menus to their locations.
    $primary_menu = get_term_by( 'name', 'Primary Menu', 'nav_menu' );
    $footer_menu = get_term_by( 'name', 'Footer Bottom Menu', 'nav_menu' );
    set_theme_mod( 'nav_menu_locations', array(
        'primary-menu'       => $primary_menu->term_id,
        'footer-bottom-menu' => $footer_menu->term_id,
    ) );
    // Delete WooCommerce duplicates
    $pages2 = array(
        'cart',
        'checkout',
        'my-account',
        'wishlist'
    );
    foreach ( $pages2 as $page2 ) {
        $page = get_page_by_path( $page2 . '-2' );
        if ( $page ) {
            wp_delete_post( $page->ID, true );
        }
    }
    $shop2 = get_page_by_path( 'shop-2' );
    if ( $shop2 ) {
        $shop1 = get_page_by_path( 'shop' );
        wp_delete_post( $shop1->ID, true );
        wp_update_post( [
            'post_name' => 'shop',
            'ID'        => $shop2->ID,
        ] );
    }
    // Assign WooCommerce pages
    $shop = get_page_by_path( 'shop' );
    $cart = get_page_by_path( 'cart' );
    $checkout = get_page_by_path( 'checkout' );
    $wishlist = get_page_by_path( 'wishlist' );
    $myaccount = get_page_by_path( 'my-account' );
    update_option( 'woocommerce_shop_page_id', $shop->ID );
    update_option( 'woocommerce_cart_page_id', $cart->ID );
    update_option( 'woocommerce_checkout_page_id', $checkout->ID );
    update_option( 'woocommerce_myaccount_page_id', $myaccount->ID );
    // Assign front page based on demo import
    switch ( $selected_import['import_file_name'] ) {
        case 'Demo Import Pro':
            $front_page_id = arendelle_get_page_by_title( 'Home Main' );
            $blog_page_id = arendelle_get_page_by_title( 'Blog' );
            update_option( 'page_on_front', $front_page_id->ID );
            set_theme_mod( 'arendelle_settings_header_show', false );
            set_theme_mod( 'arendelle_settings_footer_show', false );
            break;
        case 'Demo Import Free':
            $front_page_id = arendelle_get_page_by_title( 'Home' );
            $blog_page_id = arendelle_get_page_by_title( 'Blog' );
            update_option( 'page_on_front', $front_page_id->ID );
            break;
        default:
            break;
    }
    update_option( 'show_on_front', 'page' );
    update_option( 'page_for_posts', $blog_page_id->ID );
    update_option( 'elementor_active_kit', 1333 );
    flush_rewrite_rules();
    // Change attribute types
    $table_name = $wpdb->prefix . 'woocommerce_attribute_taxonomies';
    $wpdb->query( "UPDATE `{$table_name}` SET `attribute_type` = 'color' WHERE `attribute_name` = 'color'" );
    $wpdb->query( "UPDATE `{$table_name}` SET `attribute_type` = 'button' WHERE `attribute_name` = 'size'" );
    $woo_atts_transient = get_transient( 'wc_attribute_taxonomies' );
    $woo_atts_transient[0]->attribute_type = 'color';
    $woo_atts_transient[1]->attribute_type = 'button';
    set_transient( 'wc_attribute_taxonomies', $woo_atts_transient );
}

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