<?php
/**
 * Plugin Name: Express Shop
 * Plugin URI: https://wordpress.org/plugins/express-shop
 * Description: Express Shop is a WooCommerce addon to show all products in one page
 * Version: 4.2.3
 * Author: QuantumCloud
 * Author URI: https://www.quantumcloud.com/
 * Text Domain: express-shop
 * Requires at least: 4.0 or higher
 * Tested up to: 6.9
 * License: GPL2
 */

if (!defined('ABSPATH')) exit; // Exit if accessed directly

if ( ! defined( 'QCLD_EXPRESS_PLUGIN_DIR_URL' ) ) {
    define('QCLD_EXPRESS_PLUGIN_DIR_URL', plugin_dir_url(__FILE__));
}
if ( ! defined( 'QCLD_EXPRESS_ASSETS_URL' ) ) {
    define('QCLD_EXPRESS_ASSETS_URL', QCLD_EXPRESS_PLUGIN_DIR_URL . "assets");
}
if ( ! defined( 'QCLD_EXPRESS_IMG_URL' ) ) {
    define('QCLD_EXPRESS_IMG_URL', QCLD_EXPRESS_ASSETS_URL . "/images");
}
if ( ! defined( 'QCLD_EXPRESS_REQUIRED_WOOCOMMERCE_VERSION' ) ) {
    define('QCLD_EXPRESS_REQUIRED_WOOCOMMERCE_VERSION', 2.2);
}
if ( ! defined( 'QCLD_EXPRESS_FILE_DIR' ) ) {
    define('QCLD_EXPRESS_FILE_DIR', dirname(__FILE__));
}
//setting dir

/**
 * Do not forget about translating your plugin
 */
if ( ! function_exists( 'QCLD_EXPRESS_languages' ) ) {
  function QCLD_EXPRESS_languages(){
    load_plugin_textdomain( 'express-shop', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
  }
}
add_action('init', 'QCLD_EXPRESS_languages');

//Load dependencies.
require_once('qcld-express-shortcode.php');
require_once('qcld-express-ajax.php');
require_once('inc/option-tree/ot-loader.php');
require_once('qcld-express-framework.php');
require_once('qcld-express-assets.php');
require_once('qcld-express-min-max-qty.php');
require_once('qcld-express-dynamic-discount-price.php');

require_once("class-plugin-deactivate-feedback.php");


require_once( 'qc-support-promo-page/class-qc-support-promo-page.php' );

require_once('qc-free-ai-chatbot/class-qc-free-ai-page.php');

//checking the woocommerce Plugin is activated or not then express shop will work.
Class QCLD_Express_Shop
{

    private static $instance;

    public static function qcld_express_get_instance()
    {
        if (!self::$instance) {
            self::$instance = new self();
            self::$instance->qcld_express_init();
        }

    }


    public function qcld_express_init(){

        // Check if WooCommerce is active, and is required WooCommerce version.
        if (!class_exists('WooCommerce') || version_compare(get_option('woocommerce_db_version'), QCLD_EXPRESS_REQUIRED_WOOCOMMERCE_VERSION, '<')) {
            add_action('admin_notices', array($this, 'qcld_express_woocommerce_inactive_notice'));
            return;
        }
    }


    /**
     * Display Notifications on specific criteria.
     *
     * @since    2.14
     */
    public static function qcld_express_woocommerce_inactive_notice()
    {
        if (current_user_can('activate_plugins')) :
            if (!class_exists('WooCommerce')) :
                deactivate_plugins(plugin_basename(__FILE__));
                //wp_die('You need to activate WooCommerce first.');
                ?>
                <div id="message" class="error">
                    <p>
                        <?php
                        printf(
                            __('%sQCLD Express Shop for WooCommerce REQUIRES WooCommerce%s %sWooCommerce%s must be active for QCLD Express Shop to work. Please install & activate WooCommerce.', 'qcld_express_shop'),
                            '<strong>',
                            '</strong><br>',
                            '<a href="http://wordpress.org/extend/plugins/woocommerce/" target="_blank" >',
                            '</a>'
                        );
                        ?>
                    </p>
                </div>
                <?php
            elseif (version_compare(get_option('woocommerce_db_version'), QC_JARVIS_REQUIRED_WOOCOMMERCE_VERSION, '<')) :
                ?>
                <div id="message" class="error">
                    <p>
                        <?php
                        printf(
                            __('%sQCLD Express Shop for WooCommerce is inactive%s This version of QCLD Express Shop requires WooCommerce %s or newer. For more information about our WooCommerce version support %sclick here%s.', 'qcld_express_shop'),
                            '<strong>',
                            '</strong><br>',
                            QC_JARVIS_REQUIRED_WOOCOMMERCE_VERSION
                        );
                        ?>
                    </p>
                    <div style="clear:both;"></div>
                </div>
                <?php
            endif;
        endif;
    }
}


/**
 * Instantiate plugin.
 *
 */

if (!function_exists('qcld_express_shop_load')) {
    function qcld_express_shop_load(){

        global $qcld_express_shop;

        $qcld_express_shop = QCLD_Express_Shop::qcld_express_get_instance();
    }
}
add_action('plugins_loaded', 'qcld_express_shop_load');


function qcld_express_order_index_menu_page( $menu_ord )
{

    global $submenu;

    //echo '<pre>'; print_r( $submenu['express-shop'] ); echo '</pre>'; exit();

    $arr = array();

    if( isset($submenu['express-shop'][0]) )
        $arr[] = $submenu['express-shop'][0];

    if( isset($submenu['express-shop'][2]) )
        $arr[] = $submenu['express-shop'][2];

    if( isset($submenu['express-shop'][302]) )
        $arr[] = $submenu['express-shop'][302];

    if( isset($submenu['express-shop'][1]) )
        $arr[] = $submenu['express-shop'][1];

    if( isset($submenu['express-shop'][301]) )
        $arr[] = $submenu['express-shop'][301];

    if( isset($submenu['express-shop'][303]) )
        $arr[] = $submenu['express-shop'][303];

    $submenu['express-shop'] = $arr;

    return $menu_ord;

}
// add the filter to wordpress
add_filter( 'custom_menu_order', 'qcld_express_order_index_menu_page' );


// Add  menu 
function qcld_express_add_sublavel_menu(){

    include_once 'class-qcld-free-plugin-upgrade-notice.php';

    add_menu_page(
        __('Express Shop', 'express-shop'),
        __('Express Shop', 'express-shop'),
        'manage_options',
        'express-shop',
        'qcld_express_opening_closing_hours',
        'dashicons-cart',
        20
    );

    add_submenu_page('express-shop',
        __('Opening  Hours', 'express-shop'),
        __('Opening  Hours', 'express-shop'),
        'manage_options',
        'express-shop'
    );

    add_submenu_page('express-shop',
        __('Addons', 'express-shop'),
        __('Addons', 'express-shop'),
        'manage_options',
        'qcld-express-wholesale-addon',
        'qcld_express_shop_wholesale_addon_feature'
    );


}
add_action('admin_menu', 'qcld_express_add_sublavel_menu');



function qcld_express_shop_wholesale_addon_feature(){

?>

        <div class="qcld_express_shop_wholessale_woo_wrap">
            <div class="qcld_express_shop_wholessale_woo">
                <a href="<?php echo esc_url('https://www.quantumcloud.com/products/wholesale-plugin-for-woocommerce/'); ?>" target="_blank"><img src="<?php echo QCLD_EXPRESS_IMG_URL; ?>/wholesale-woocommerce-plugin.png" alt=""></a>
                
                <p><?php esc_html_e( 'Increase sales on your Woocommerce shop with the Wholesale addon. Provides useful features like Role Based Pricing, Dynamic Pricing and discounts based on Weight, Quantity, Price, Total Units and Number of items and Minimum Maximum Quantity for checkout.', 'express-shop' ); ?></p> <p style="font-size: 15px;"> <?php esc_html_e( 'Wholesale features plugin works with Express Shop', 'express-shop' ); ?> <b><?php esc_html_e( 'Free', 'express-shop' ); ?></b>, <?php esc_html_e( 'Express Shop', 'express-shop' ); ?> <b><?php esc_html_e( 'Pro', 'express-shop' ); ?></b> <?php esc_html_e( 'and', 'express-shop' ); ?> <b> <?php esc_html_e( 'any', 'express-shop' ); ?></b> <?php esc_html_e( 'Woocommerce site.', 'express-shop' ); ?></p>
                <p><a href="<?php echo esc_url('https://www.quantumcloud.com/products/wholesale-plugin-for-woocommerce/'); ?>" target="_blank"><?php esc_html_e( 'Get it Now!', 'express-shop' ); ?> </a></p>
            </div>
        </div>


<?php 

}

function qcld_express_opening_closing_hours(){

    ?>

    <div class="wrap-content">
        <div class="qcld_express_wrap">
            <h2> <?php esc_html_e( 'Express Shop', 'express-shop' ); ?></h2>
            <p> <?php esc_html_e( 'Opening Hours is a Pro Version Feature. Please ', 'express-shop' ); ?> <a href="<?php echo esc_url( 'https://www.quantumcloud.com/products/express-shop/', 'express-shop' ); ?>" target="_blank"><span style="font-weight: bold; color: #FCB214"> <?php esc_html_e( 'Upgrade to the Pro Version.', 'express-shop' ); ?></span> </a></p>

            <div id="qcld_express_content">                  
              <!-- Promo Block 1 -->
              <div style="margin-top: 20px;"> <div class="qc-promo-plugins" ><img src="<?php echo QCLD_EXPRESS_IMG_URL ?>/qc-logo-full.png" alt="QuantumCloud Logo"><br><br><hr><br><a href="<?php echo esc_url( 'https://www.quantumcloud.com', 'express-shop' ); ?>" target="_blank" style="margin-left: 65px;"> <?php esc_html_e( 'QuantumCloud', 'express-shop' ); ?></a></div> </div>
            </div>
        </div>
    </div>

    <?php


}


//Floating Global cart start
$qcld_express_options           = get_option('qcld_express_options');
$qcld_express_cart_mode         = $qcld_express_options['qcld_express_cart_mode'];


if($qcld_express_cart_mode=='wooinstants'){
    add_action('wp_footer', 'qcld_express_float_cart_global');
    function qcld_express_float_cart_global(){

        if (is_cart() || is_checkout()) {
            return false;
        }


        $qcld_express_options           = get_option('qcld_express_options');
        $qcld_express_lang_cart_summary = $qcld_express_options['qcld_express_lang_cart_summary'];
        $qcld_express_lang_edit_on_cart = $qcld_express_options['qcld_express_lang_edit_on_cart'];
        $qcld_express_lang_checkout     = $qcld_express_options['qcld_express_lang_checkout'];
        $qcld_express_lang_no_products  = $qcld_express_options['qcld_express_lang_no_products'];
               
       
            global $woocommerce;

            if ( class_exists('Woocommerce') ): ?>
            <div class="qcld-express-container">
                <a id="qcld-express-toggler" class="qcld-express-cart-header hascart <?php /*if( WC()->cart->get_cart_contents_count() > 0 ){ echo 'hascart'; }*/ ?>">
                    <i class="fa fa-shopping-cart"></i>
                    <?php echo qcld_express_cart_count(); ?>
                </a>
                <div class="qcld-express-inner">
                    <h3> 
                        <?php
                            if(isset($qcld_express_lang_cart_summary) && $qcld_express_lang_cart_summary != ''){
                                echo esc_html($qcld_express_lang_cart_summary, 'express-shop'); 
                            }else{
                                // echo 'Cart Summary';
                                esc_html_e( 'Cart Summary', 'express-shop' );
                            }
                        ?> 
                        </h3>
                    <div class="wooinstant-content woocommerce">
                        <div id="qcld-express-cart-area">
                            <?php echo qcld_express_cart_inner(); ?>
                        </div>
                        <div id="qcld-express-checkout-area">
                            <?php echo qcld_express_checkout_inner(); ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        endif;



    }//End of function
} //End of if condition.


// redirect when activation plugin ...
register_activation_hook(__FILE__, 'qcld_express_plugin_activate');
function qcld_express_plugin_activate(){
    add_option('qcld_express_plugin_do_activation_redirect', true);
}

add_action('admin_init', 'qcld_express_plugin_redirect');
function qcld_express_plugin_redirect(){
    if (get_option('qcld_express_plugin_do_activation_redirect', false)){
        delete_option('qcld_express_plugin_do_activation_redirect');

        if(!isset($_GET['activate-multi'])){
            wp_redirect("admin.php?page=qcld-express-options#section_help");
        }
    }
}




require_once('qcld-express-cart-function.php');

/**
 *  Enqueue Wooinstant scripts
 *
 */
function qcld_expresses_plugin_enqueue_scriptss(){

    if (is_cart() || is_checkout()) {
        return false;
    }

    wp_enqueue_style('font-awesome', plugin_dir_url( __FILE__ ) . 'assets/css/font-awesome.min.css' );
    wp_enqueue_style('qcld-express-stylesheet', plugin_dir_url( __FILE__ ) . 'assets/css/qcld-express-cart-style.css' );

    wp_enqueue_script('qcld-express-script', plugin_dir_url( __FILE__ ) . 'assets/js/qcld-express-cart-scripts.js', array( 'jquery' ), '', true );
    wp_enqueue_script( 'qcld-express-ajax-script', plugin_dir_url( __FILE__ ) . 'assets/js/qcld-express-cart-ajax-scripts.js', array('jquery'), '', true );
    

    /**
     * Handle WC frontend scripts
     */
    
    //first check that woo exists to prevent fatal errors
    if( function_exists('is_woocommerce') ) {
        $suffix               = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? : '.min';
        $lightbox_en          = 'yes' === get_option( 'woocommerce_enable_lightbox' );
        $ajax_cart_en         = 'yes' === get_option( 'woocommerce_enable_ajax_add_to_cart' );
        $assets_path          = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/';
        $frontend_script_path = $assets_path . 'js/frontend/';

        wp_enqueue_script( 'wc-cart', $frontend_script_path . 'cart' . $suffix . '.js', array( 'jquery', 'wc-country-select', 'wc-address-i18n' ) );

        wp_localize_script('wc-cart', 'wc_cart_params', apply_filters('wc_cart_params', array(
            'ajax_url' => WC()->ajax_url() ,
            'wc_ajax_url' => WC_AJAX::get_endpoint(' %%endpoint%%') ,
            'ajax_loader_url' => apply_filters('woocommerce_ajax_loader_url', $assets_path . 'images / ajax - loader@2x . gif') ,
            'update_shipping_method_nonce' => wp_create_nonce('update-shipping-method') ,
        )));

        wp_enqueue_script( 'wc-checkout', $frontend_script_path . 'checkout' . $suffix . '.js', array( 'jquery', 'wc-address-i18n' ) );

        wp_localize_script('wc-checkout', 'wc_checkout_params', apply_filters('wc_checkout_params', array(
            'ajax_url'                  => WC()->ajax_url(),
            'wc_ajax_url'               => WC_AJAX::get_endpoint( '%%endpoint%%' ),
            'update_order_review_nonce' => wp_create_nonce( 'update-order-review' ),
            'apply_coupon_nonce'        => wp_create_nonce( 'apply-coupon' ),
            'remove_coupon_nonce'       => wp_create_nonce( 'remove-coupon' ),
            'option_guest_checkout'     => get_option( 'woocommerce_enable_guest_checkout' ),
            'checkout_url'              => WC_AJAX::get_endpoint( 'checkout' ),
            'is_checkout'               => is_checkout() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0,
            'debug_mode'                => defined( 'WP_DEBUG' ) && WP_DEBUG,
            'i18n_checkout_error'       => esc_attr__( 'Error processing checkout. Please try again.', 'woocommerce' ),
        )));

        wp_enqueue_script( 'wc-add-to-cart-variation', $frontend_script_path . 'add-to-cart-variation' . $suffix . '.js', array( 'jquery', 'wp-util', 'jquery-blockui' ) );
        
        wp_localize_script('wc-add-to-cart-variation', 'wc_add_to_cart_variation_params', apply_filters('wc_add_to_cart_variation_params', array(
            'wc_ajax_url'                      => WC_AJAX::get_endpoint( '%%endpoint%%' ),
            'i18n_no_matching_variations_text' => esc_attr__( 'Sorry, no products matched your selection. Please choose a different combination.', 'woocommerce' ),
            'i18n_make_a_selection_text'       => esc_attr__( 'Please select some product options before adding this product to your cart.', 'woocommerce' ),
            'i18n_unavailable_text'            => esc_attr__( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ),
        )));


        $qcld_express_options           = get_option('qcld_express_options');
        $qcld_express_cart_mode         = isset($qcld_express_options['qcld_express_cart_mode']) ? $qcld_express_options['qcld_express_cart_mode'] : '';

        wp_add_inline_script( 'wc-checkout', '
            var ajaxurl = "' . admin_url('admin-ajax.php') . '"; 
            var image_loader = "' . QCLD_EXPRESS_IMG_URL . '/product-loader.gif";
            var currency_symbol = "' . get_woocommerce_currency_symbol() . '" ;
            var qcld_get_checkout_url = "' . wc_get_checkout_url(). '";
            var qcld_express_cart_mode = "' . $qcld_express_cart_mode . '";
            var qcld_express_security_nonce = "' . wp_create_nonce('qcld_express_security_nonce') . '";
        ');

        wp_enqueue_style('select2');
        wp_enqueue_script('select2');
        wp_enqueue_script( 'wc-country-select' );

    }


}
add_filter( 'wp_enqueue_scripts', 'qcld_expresses_plugin_enqueue_scriptss', 200 ,2 );

/**
 *
 * deactive Plugin Feedback.
 *
 */
$wpbot_feedback = new Wp_express_shop_Usage_Feedback( __FILE__,'quantumcloud@gmail.com', false,  true );

// multi vendor function 
$qcld_express_options_multi_vendor    = get_option('qcld_express_options');
$qcld_expresses_multi_vendor_enable   = $qcld_express_options_multi_vendor['qcld_express_multi_vendor_dokan_enable'];

if(( function_exists('dokan') || function_exists('dokan_pro') ) && isset($qcld_expresses_multi_vendor_enable) && $qcld_expresses_multi_vendor_enable  == 'on'){

    add_action('wp_enqueue_scripts', 'qcld_express_load_front_scripts');

add_action('dokan_store_profile_frame_after', 'qcld_express_multi_vendor_theme_shortcode_use');
function qcld_express_multi_vendor_theme_shortcode_use($store_user=null,$store_info=null){

    echo do_shortcode( '[qcld_express_shop author='.$store_user->ID.']' );
   
}

add_action('wp_footer', 'qcld_expresses_multi_vendor_css');
function qcld_expresses_multi_vendor_css(){
?>
<style>
    .seller-items{
        display: none !important;
    }
</style>
<?php 
}

}



//add_action( 'admin_notices', 'qcld_expresses_pro_notice',100 );
function qcld_expresses_pro_notice(){
    global $pagenow, $typenow;
    ?>
    <div id="message-express" class="notice notice-info is-dismissible" style="padding:4px 0px 0px 4px;background:#C13825;">
        <?php
            printf(
                __('%s  %s  %s', 'dna88-wp-notice'),
                '<a href="'.esc_url('https://www.quantumcloud.com/products/express-shop/').'" target="_blank">',
                '<img src="'.esc_url(QCLD_EXPRESS_IMG_URL).'/4th-of-july.gif" >',
                '</a>'
            );

        ?>
    </div>
<?php

}