<?php
/*
Plugin Name: Multistore Elements
Plugin URI: http://netbaseteam.com/
Description: Provide all multistore elements
Version: 1.0.0
Author: NBTeam
Author URI: http://themeforest.net/user/netbaseteam
*/

define('NB_ELEMENTS_PATH', plugin_dir_path(__FILE__));
define('NB_ELEMENTS_NAME', plugin_basename(__FILE__));
define('NB_ELEMENTS_URL', plugin_dir_url( __FILE__ ));

require_once(NB_ELEMENTS_PATH . 'widgets/widget.php');
require_once(NB_ELEMENTS_PATH . 'metaboxes/brands_metabox.php');



add_action( 'vc_before_init', 'vc_before_init_actions' );
function vc_before_init_actions() {
     
    // Require new custom Element
    require_once( NB_ELEMENTS_PATH . 'vc_elements/heading.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/list-store-dokan.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/brands-by-category.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/ajax-tab-products.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/banner.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/category-images.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/services.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/our-team.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/timeline-simple.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/brands-list.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/countdown.php' ); 
    require_once( NB_ELEMENTS_PATH . 'vc_elements/category-icon.php' ); 
     
}
NBT_Widget::init();
class NBT_Panel
{

    protected static $initialized = false;

    public static function init()
    {
        if (self::$initialized) {
            return;
        }
        add_filter( 'pt-ocdi/import_files', array(__CLASS__, 'ocdi_import_files') );
		
        add_action( 'pt-ocdi/after_import', array(__CLASS__, 'after_import') );
    }

    public static function ocdi_import_files() {
        return array(
            array(
                'import_file_name'           => 'Estore',
                'local_import_file'            => NB_ELEMENTS_PATH . 'ocdi/content.xml',
                'local_import_widget_file'     => NB_ELEMENTS_PATH . 'ocdi/ebay/widgets.wie',
                'local_import_customizer_file' => NB_ELEMENTS_PATH . 'ocdi/ebay/customizer.dat',
                'import_preview_image_url'     => plugins_url('ocdi/ebay/screenshot.png',__FILE__),
            ),
            array(
                'import_file_name'           => 'Amazone',
                'local_import_file'            => NB_ELEMENTS_PATH . 'ocdi/content.xml',
                'local_import_widget_file'     => NB_ELEMENTS_PATH . 'ocdi/amazone/widgets.wie',
                'local_import_customizer_file' => NB_ELEMENTS_PATH . 'ocdi/amazone/customizer.dat',
                'import_preview_image_url'     => plugins_url('ocdi/amazone/screenshot.png',__FILE__),
            ),
            array(
                'import_file_name'           => 'Lazaa',
                'local_import_file'            => NB_ELEMENTS_PATH . 'ocdi/content.xml',
                'local_import_widget_file'     => NB_ELEMENTS_PATH . 'ocdi/lazaa/widgets.wie',
                'local_import_customizer_file' => NB_ELEMENTS_PATH . 'ocdi/lazaa/customizer.dat',
                'import_preview_image_url'     => plugins_url('ocdi/lazaa/screenshot.png',__FILE__),
            ),
            array(
                'import_file_name'           => 'Bestbuy',
                'local_import_file'            => NB_ELEMENTS_PATH . 'ocdi/content.xml',
                'local_import_widget_file'     => NB_ELEMENTS_PATH . 'ocdi/bestbuy/widgets.wie',
                'local_import_customizer_file' => NB_ELEMENTS_PATH . 'ocdi/bestbuy/customizer.dat',
                'import_preview_image_url'     => plugins_url('ocdi/bestbuy/screenshot.png',__FILE__),
            ),
        );
    }

    public static function after_import( $selected_import ) {

        if ( 'Estore' === $selected_import['import_file_name'] || 'Amazone' === $selected_import['import_file_name'] ||  'Lazaa' === $selected_import['import_file_name'] || 'Bestbuy' === $selected_import['import_file_name']  ) {
            if ( 'Estore' === $selected_import['import_file_name'] ) {
                $page = get_page_by_title( 'Estore');
            } elseif ( 'Amazone' === $selected_import['import_file_name'] ) {
                $page = get_page_by_title( 'Amazone');
            } elseif ( 'Lazaa' === $selected_import['import_file_name'] ) {
                $page = get_page_by_title( 'Lazaa');
            } elseif ( 'Bestbuy' === $selected_import['import_file_name'] ) {
                $page = get_page_by_title( 'Best buy');
            }

            //setup Front Page
            if ( isset( $page->ID ) ) {
                update_option( 'page_on_front', $page->ID );
                update_option( 'show_on_front', 'page' );
            }

            echo 'setup Front Page';

            // Main Menu
            $primary_menu = get_term_by('name', 'Top Menu', 'nav_menu');
            // Header Menu
            $header_menu = get_term_by('name', 'header sub menu', 'nav_menu');
            // Category Menu
            $category_menu = get_term_by('name', 'main', 'nav_menu');

            set_theme_mod( 'nav_menu_locations', array(
                    'primary' => $primary_menu->term_id,
                    'header-sub' => $header_menu->term_id,
                    'menu-cat' => $category_menu->term_id,
                )
            );

            echo 'Setup Menu Done';

            //import Max Megamenu
            if( class_exists( 'Mega_Menu' ) ) {

                $sidebar_widgets    = get_option( 'sidebars_widgets' );

                if( isset( $sidebar_widgets[ 'mega-menu' ] ) ) {

                    $mm_sidebar_widgets = $sidebar_widgets[ 'mega-menu' ];

                    
                    $mm_imported_widgets  = NBT_Panel::get_imported_megamenu_data( $mm_sidebar_widgets );
                    $new_mm_widget_id       = NBT_Panel::rebuild_mmm_widget_id( $mm_sidebar_widgets , $mm_imported_widgets );

                    //update megamenu grid type post meta
                    NBT_Panel::update_megamenu_grid_type_post_meta( $new_mm_widget_id );
                }

                NBT_Panel::setup_theme_location();

                echo 'Max megamenu processed';
            }

            // import Revolution Slider
            
            if ( class_exists( 'RevSlider' ) ) {
                $slider_array = array(
                    NB_ELEMENTS_PATH .'ocdi/slider/ebay-slider.zip',
                    NB_ELEMENTS_PATH .'ocdi/slider/amazon-slider.zip',
                    NB_ELEMENTS_PATH .'ocdi/slider/home4-slider.zip',
                );

                $slider = new RevSlider();

                foreach($slider_array as $filepath){
                    
                    echo 'Importing slider from ' . $filepath;
                    $slider->importSliderFromPost(true,true,$filepath);
                    echo 'Imported slider from ' . $filepath;
                }

                echo 'All Sliders imported';
            }
        }
    }

    /**
     * Get all imported megamenu data
     * @param  array $mm_imported_widgets
     */
    public static function get_imported_megamenu_data( $mm_sidebar_widgets ) {

        global $wpdb;

        $mm_imported_widgets = array();

        $megamenu_meta = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key = '_megamenu'" );

        foreach ( $megamenu_meta as $meta ) {

            $meta_value = unserialize( $meta->meta_value );

            if( isset( $meta_value['type'] ) && $meta_value['type'] == 'grid' ) {

                foreach( $meta_value['grid'] as $i => $grids ) {                
                        
                    foreach( $grids['columns'] as $j => $column ) {

                        foreach ( $column[ 'items' ] as $k => $item ) {                         

                            // $meta_value['grid'][$i]['columns'][$j]['items'][$k]['id'] = $new_widget_id;
                            
                            $mm_imported_widgets[] = $item[ 'id' ];
                        }
                    }
                }
            }
        }

        return $mm_imported_widgets;
    }

    /**
     * Rebuild max megamenu metadata after imported
     * @param  [array] $mm_sidebar_widgets     
     * @param  [array] $mm_imported_widgets 
     * @return [array]                        
     */
    public static function rebuild_mmm_widget_id( $mm_sidebar_widgets, $mm_imported_widgets) {

        $mm_sidebar_widgets_by_key      = array();
        $mm_imported_widgets_by_key    = array();
        $rebuild_array                  = array();

        foreach($mm_sidebar_widgets as $value) {
            $exploded_widgets = explode('-', $value);
            $mm_sidebar_widgets_by_key[$exploded_widgets[0]][] = $exploded_widgets[1];
        }

        foreach($mm_imported_widgets as $value) {
            $exploded_widgets = explode('-', $value);
            $mm_imported_widgets_by_key[$exploded_widgets[0]][] = $exploded_widgets[1];
        }

        foreach($mm_sidebar_widgets_by_key as $key => $a) {
            rsort($a);
            $mm_sidebar_widgets_by_key[$key] = $a;
        }

        foreach($mm_imported_widgets_by_key as $key => $a) {
            rsort($a);
            $mm_imported_widgets_by_key[$key] = $a;
        }

        foreach ($mm_imported_widgets_by_key as $key => $values) {

            foreach($values as $index => $value ) {
                $widget_key = $key . '-' . $value;
                $new_value = $key . '-' . $mm_sidebar_widgets_by_key[$key][$index];
                $rebuild_array[$widget_key] = $new_value;        
            }
        }

        return $rebuild_array;
    }

    /**
     * Update incorrect wiget name in megamenu post metadata
     */
    public static function update_megamenu_grid_type_post_meta( $new_mm_widget_id ) {

        global $wpdb;

        $megamenu_meta = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key = '_megamenu'" );

        foreach ( $megamenu_meta as $meta ) {

            $meta_value = unserialize( $meta->meta_value );

            if( isset( $meta_value['type'] ) && $meta_value['type'] == 'grid' ) {

                foreach( $meta_value['grid'] as $i => $grids ) {                
                        
                    foreach( $grids['columns'] as $j => $column ) {

                        foreach ( $column[ 'items' ] as $k => $item ) {                         

                            $meta_value['grid'][$i]['columns'][$j]['items'][$k]['id'] = isset($new_mm_widget_id[ $item[ 'id' ] ]) ? $new_mm_widget_id[ $item[ 'id' ] ] : $item[ 'id' ];
                        }
                    }
                }

                // update grid post meta data
                update_post_meta( $meta->post_id, '_megamenu', $meta_value);
            }
        }
    }

    /**
     * assign theme location to first menu
     * enable megamenu settings in options table
     * 
     */
    public static function setup_theme_location() { 
        
        // update megamenu settings in options table
        $default_mmm_settings = array(
            'prefix'        => 'disabled',
            'descriptions'  => 'enabled',
            'primary'       => array(
                                'enabled'       => 1,
                                'event'         => 'hover',
                                'effect'        => 'fade_up',
                                'effect_speed'  => 200,
                                'theme'         => 'default',
                                'orientation'   => 'horizontal'
                                ),
            'sticky'        => array(
                                'location'      => 'primary',
                                'opacity'       => '1',
                                'mobile'        => false,
                                'offset'        => 0,
                                ),
            'menu-cat'      => array(
                                'enabled'       => 1,
                                'event'         => 'hover',
                                'effect'        => 'disabled',
                                'effect_speed'  => 200,
                                'theme'         => 'default',
                                'orientation'   => 'vertical'
                                ),

        );

        update_option( 'megamenu_settings', $default_mmm_settings);

        do_action( "megamenu_after_save_settings" );

        do_action( "megamenu_delete_cache" );

    }
}
NBT_Panel::init();
