<?php
//checking qcld_express_shop is exist or not in the page.
function qcld_express_shortcode_check($posts) {
    if ( empty($posts) )
        return $posts;

    // false because we have to search through the posts first
    $found = false;

    // search through each post
    foreach ($posts as $post) {
        // check the post content for the short code
        if ( stripos($post->post_content, 'qcld_express_shop') )
            // Found a post with the short code
            $found = true;
        // stop the search
        break;
    }

    if ($found){
        //Load Script and Stylesheets
        add_action('wp_enqueue_scripts', 'qcld_express_load_front_scripts');
    }

    return $posts;
}
//perform the check when the_posts() function is called
add_action('the_posts', 'qcld_express_shortcode_check');

//Loading frontend style & scripts based on the above condition.
function qcld_express_load_front_scripts(){

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

    //Styles
    wp_enqueue_style( 'qcld_express_fontawesome_css', QCLD_EXPRESS_ASSETS_URL . '/css/font-awesome.min.css');
    wp_enqueue_style( 'qcld_express_front_css', QCLD_EXPRESS_ASSETS_URL . '/css/qcld-express-custom-front.css');
    wp_enqueue_style( 'qcld_express_front_rwd_css', QCLD_EXPRESS_ASSETS_URL . '/css/qcld-express-custom-front-rwd.css');
    wp_enqueue_style( 'qcld_express_slick_css', QCLD_EXPRESS_ASSETS_URL . '/css/slick.css');
    wp_enqueue_style( 'qcld_express_magnific_css', QCLD_EXPRESS_ASSETS_URL . '/css/jquery.fancybox.min.css');

    $qcld_express_options               = get_option('qcld_express_options');
    $qcld_express_custom_style          = $qcld_express_options['qcld_express_custom_style'];
    $qcld_express_background            = $qcld_express_options['qcld_express_background'];
    $qcld_express_altern                = $qcld_express_options['qcld_express_altern'];

    wp_add_inline_style( 'qcld_express_front_css', $qcld_express_custom_style );

    $add_inline_styles = ' .express-shop-wrapper{ background-color: '.$qcld_express_background.' ; }
        .qcld-express-row:nth-of-type(odd) { background-color: '.$qcld_express_altern.'; }';
    wp_add_inline_style( 'qcld_express_front_css', $add_inline_styles );

    // Scripts
    wp_enqueue_script( 'jquery', 'jquery');
    wp_enqueue_script( 'wc-add-to-cart-variation' );
    wp_enqueue_script( 'qcld_express_slick_js', QCLD_EXPRESS_ASSETS_URL . '/js/slick.js', array('jquery'));
    wp_enqueue_script( 'qcld_express_magnific_popup_js', QCLD_EXPRESS_ASSETS_URL . '/js/jquery.fancybox.min.js', array('jquery'));
    wp_enqueue_script( 'qcld_express_front_js', QCLD_EXPRESS_ASSETS_URL . '/js/qcld-express-custom-front.js', array('jquery','qcld_express_slick_js','qcld_express_magnific_popup_js','wc-add-to-cart-variation'));


    $qcld_express_options       = get_option('qcld_express_options');
    $qcld_express_show_mode     = $qcld_express_options['qcld_express_show_mode'];
    $qcld_express_cart_mode     = $qcld_express_options['qcld_express_cart_mode'];

    wp_add_inline_script( 'qcld_express_front_js', '
            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') . '";
        ');


}

//Loading admin panel's requried Style & scripts
function qcld_express_load_admin_scripts(){
    
    // Styles
    wp_enqueue_style( 'qcld_express_admin_light_css', QCLD_EXPRESS_ASSETS_URL . "/css/qcld-express-custom-admin.css");
    wp_enqueue_style( 'wp-color-picker' );
    
    // Scripts
    wp_enqueue_script( 'qcld_express_wp_color_picker', QCLD_EXPRESS_ASSETS_URL . '/js/qcld-express-wp-color_picker.js', array( 'wp-color-picker' ), false, true );
    wp_enqueue_script( 'qcld_express_admin_js', QCLD_EXPRESS_ASSETS_URL . "/js/qcld-express-custom-admin.js", array('jquery'));
}
add_action( 'admin_enqueue_scripts', 'qcld_express_load_admin_scripts');