<?php
/*
    woocommerce core theme extension
*/
if( ! defined( 'ABSPATH' ) ) exit;

// check if woocommerce is installed and active
if (!function_exists('is_plugin_active')) return;
if (!is_plugin_active('woocommerce/woocommerce.php')) return;

$woo_directory = get_template_directory().'/extensions/woocommerce/';

if ( !is_greyd_blocks() ) {
    require_once $woo_directory.'vc/composer.php';
}

require_once $woo_directory.'vc/shortcode.php'; // this can soon be deprecated
require_once $woo_directory.'hooks.php';
require_once $woo_directory.'customizer.php';

new woo_manage();
class woo_manage {

    function __construct() {
        // add_filter( 'woocommerce_enqueue_styles', array($this, 'wc_dequeue_styles') );
        add_action( 'wp_enqueue_scripts', array($this, 'load_scripts') );

        add_action( 'admin_notices', array($this, 'display_wizzard_notice') );
    }
    
    // De-Register Styles
    public function wc_dequeue_styles( $enqueue_styles ) {
        unset( $enqueue_styles['woocommerce-general'] ); // Remove the gloss
        unset( $enqueue_styles['woocommerce-layout'] ); // Remove the layout
        unset( $enqueue_styles['woocommerce-smallscreen'] ); // Remove the smallscreen optimisation
        return $enqueue_styles;
    }
    
    // Frontend
    public function load_scripts() {
        global $config;
        
        // styles
        wp_process_style('default_style', get_template_directory().'/extensions/woocommerce/assets/frontend.css', null, $config->theme_version);
        
        // scripts
        wp_register_script('greyd_woo_js', get_template_directory_uri().'/extensions/woocommerce/assets/frontend.js', array('jquery'), $config->theme_version, false);
        wp_localize_script('greyd_woo_js', 'ajax_details', array(
            'ajax_url'  => admin_url( 'admin-ajax.php' ),
            'nonce'     => wp_create_nonce('greyd_woo'),
            'shop'      => esc_js( wc_get_page_permalink('shop') ),
            'cart'      => esc_js( wc_get_page_permalink('cart') ),
            'checkout'  => esc_js( wc_get_page_permalink('checkout') ),
            'default_redirect' => get_option( 'woocommerce_cart_redirect_after_add' ),
        ) );
        wp_enqueue_script('greyd_woo_js');
    }

    /**
     * Display admin notice to install via wizzard.
     */
    public function display_wizzard_notice() {

        $result = get_posts(array(
            'numberposts'   => 1,
            'post_type'     => 'product'
        ));

        if ( empty($result) ) {
            echo "<div class='updated woocommerce-message'>
                <p>".sprintf(
                    __("Uns ist aufgefallen, dass du noch kein Produkt in deinem Webshop hast. Du kannst dir das Aufsetzen deines Shops erleichtern, indem du mit dem Wizard ganz einfach verschiedene Beispiel-Inhalte installierst. Starte dafür den Wizard im Dashboard und gehe dort zu den Grundeinstellungen: %s", 'greyd_hub'),
                    "<a href='".admin_url('admin.php?page=greyd_dashboard')."'>".__("zum Dashboard", "core")."</a>"
                )."</p>
            </div>";
        }
    }
}