<?php
/**
 * Register all actions and filters for the plugin
 *
 * @link       info@meteosales.com
 * @since      1.0.0
 *
 * @package    Meteosales
 * @subpackage Meteosales/includes
 */
if ( ! defined( 'ABSPATH' ) ) exit;
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {//if woocommerce is activated
   
    /**
    * Register all actions and filters for the plugin with shortcode
    *
    * @link       info@meteosales.com
    * @since      1.0.6
    *
    * @package    Meteosales
    * @subpackage Meteosales/includes
    */
 
    function meteosales_ms_products_display_shortcode($atts = array(), $content = null ) {
      	$sort = get_option("MeteoCommerceProductsSetting");
      	
        if(!$sort){ return $banner; }
        else{
            $ipaddress = '';
                 if (isset($_SERVER['HTTP_CLIENT_IP']))       {$ipaddress = $_SERVER['HTTP_CLIENT_IP'];}
            else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];}
            else if (isset($_SERVER['HTTP_X_FORWARDED']))     {$ipaddress = $_SERVER['HTTP_X_FORWARDED'];}
            else if (isset($_SERVER['HTTP_FORWARDED_FOR']))   {$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];}
            else if (isset($_SERVER['HTTP_FORWARDED']))       {$ipaddress = $_SERVER['HTTP_FORWARDED'];}
            else if (isset($_SERVER['REMOTE_ADDR']))          {$ipaddress = $_SERVER['REMOTE_ADDR'];}
            
            $server = $_SERVER["SERVER_NAME"];
            $arr_sort = json_decode($sort, true);
            $token = $arr_sort["token"];
            if($token != ""){
                
                extract(shortcode_atts(array('id' => 1, 'limit' => 'no_limitation'), $atts));
                if($limit == "no_limitation" || ($limit == "only_logged" && is_user_logged_in()) || ($limit == "only_no_logged" && !is_user_logged_in())){
                    $api_key_url = 'https://my.meteosales.com/api/weather';
                    //Call API
                    $response = wp_remote_post($api_key_url, array('body' => array( 'token' => $token, 'server_name' => $server , 'ip'=>$ipaddress,'slider'=>$id)));
                    
                    $output = wp_remote_retrieve_body( $response );
                    $data = json_decode($output, true);
                    $pr = $data["data"];
                    
                    $products = "";
                    $banner = "";
                
                    if(!empty($pr["p"])){
                        
                        $ids = str_replace("[","",$pr["p"]["n"]);
                        $ids = str_replace("]","",$ids);
                        $redirect = get_option("woocommerce_cart_redirect_after_add");
                        $ajax = get_option("woocommerce_enable_ajax_add_to_cart");
                        $products ="<div class='meteosales_products_$id'style='margin-top:30px'><input type='hidden' id='hid_id' value='".esc_html($pr["p"]["id"])."' />". do_shortcode("[products ids='$ids' columns='4' orderby='post__in']"). "</div>";
                        $products .= "<script type='text/javascript'>
                                        jQuery('.meteosales_products_$id a').click(function(event) {
                                            var value = jQuery(this).attr('data-product_id');
                                            var n = 0;
                                            var reload = false;
                                            if (typeof value === 'undefined') {
                                                value = jQuery(this).attr('href');
                                                n=2;
                                                event.preventDefault();
                                                reload = true;
                                            }
                                            else{
                                                n=1;
                                                var redirect = '$redirect';
                                                if(redirect == 'yes'){
                                                    event.preventDefault();
                                                    reload = true;
                                                }
                                                else{
                                                    var ajax_add = '$ajax';
                                                    if(ajax_add == 'no'){
                                                        event.preventDefault();
                                                        reload = true;
                                                    }
                                                }
                                                
                                            }
                                            var hid_id = jQuery('.meteosales_products_$id #hid_id').val();
                                            var s = '$id';
                                            console.log('type: '+n+'hid_id :'+hid_id+'slider: '+s+'value :'+value);
                                            jQuery.post(ajaxurl,{'action': 'product_clicks','type': n, 'value':value,'hid_id':hid_id,'slider':s}, 
                                                function () { 
                                                    if(reload){
                                                        event.returnValue = true;
                                                        window.location.href = value;
                                                    }
                                                }   
                                            );
                                        });
                                    </script>";
                    }
                    if(!empty($pr["b"])){
                     
                        $b_sort = get_option("MeteoCommerceBannerSetting");
                        if(!$b_sort){ /*if Get_Option does not exist*/ }
                        else{
                            $arr_b_sort = json_decode($b_sort, true);
                            $banner_id  = $arr_b_sort["id"];
                            $banner_class  = $arr_b_sort["class"];
                            
                            $my_search = $banner_id != "" ? "#$banner_id" : ($banner_class != "" ? ".$banner_class" : "" );
                           
                            if($my_search != ""){
                                
                                $banner .= "<script>
                                                var banner = '".esc_html($pr["b"]["u"])."';
                                                var banner_text = '". esc_js($pr["b"]["c"])."';";
                                $theme = wp_get_theme();
                                if($theme->Template == "flatsome"){
                                    $banner .= "jQuery( document ).ready(function() {
                                                    var n_banner = jQuery('$my_search').length;
                                                    if(n_banner > 0){
                                                        jQuery( '$my_search div:first' ).css( 'background-image', 'url('+banner+')' );
                                                        if(banner_text != ''){
                                                            var n_text = jQuery('$my_search a').length
                                                            if(n_text > 0){
                                                                jQuery('$my_search a').text(banner_text);
                                                            }   
                                                        }
                                                    }
                                                });";
                                }
                                else if($theme->Template == "astra"){
                                    $banner .= "jQuery( document ).ready(function() {
                                                    var n_banner = jQuery('$my_search').length;
                                                    if(n_banner > 0){
                                                        jQuery( '$my_search' ).css( 'background-image', 'url('+banner+')' );
                                                        if(banner_text != ''){
                                                            const tag = ['h1', 'h2', 'h3', 'h4', 'h5','h6', 'p', 'span'];
                                                            for (const t of tag) { 
                                                                var n_text = jQuery('$my_search ' +t).length
                                                                if(n_text > 0){
                                                                    var url = '".get_permalink( woocommerce_get_page_id( 'shop' ) )."';
                                                                    jQuery('$my_search ' +t).html('<a href=\"'+url+'\" style=\'text-decoration: none;\'>'+banner_text+'</a>');
                                                                    break;
                                                                }
                                                            }   
                                                        }
                                                    }
                                                });";
                                }
                                else if($theme->Template == "hello-elementor"){
                                    $banner .= "jQuery( document ).ready(function() {
                                                    var n_banner = jQuery('$my_search').length;
                                                    if(n_banner > 0){
                                                        jQuery( '$my_search' ).css( 'background-image', 'url('+banner+')' );
                                                        var n_text = jQuery('$my_search a').length;
                                                        if(n_text > 0 && banner_text != ''){
                                                            jQuery('$my_search a').text(banner_text);
                                                        }
                                                    }
                                                });";
                                }
                                else if($theme->Template == "Divi"){
                                    $banner .= "jQuery( document ).ready(function() {
                                                    var n_banner = jQuery('$my_search').length;
                                                    if(n_banner > 0){
                                                        jQuery( '$my_search' ).attr( 'style','background-image: url('+banner+') !important');
                                                        var n_text = jQuery('$my_search a').length
                                                        if(n_text > 0 && banner_text != ''){
                                                            jQuery('$my_search a').text(banner_text);
                                                        }
                                                    }
                                                });";
                                }
                                else if($theme->Template == "Avada"){
                                    $banner .= "jQuery( document ).ready(function() {
                                                    var n_banner = jQuery('$my_search').length;
                                                    if(n_banner > 0){
                                                        jQuery( document ).ready(function() {
                                                            jQuery( '$my_search div:first' ).attr( 'style','background-image: url('+banner+') !important');
                                                       });
                                                        var n_text = jQuery('$my_search a').length
                                                        
                                                        if(n_text > 0 && banner_text != ''){
                                                            jQuery('$my_search a').text(banner_text);
                                                        }
                                                    }
                                                 });";
                                }
                                else if($theme->Template == "storefront"){
                                    $banner .= "var n_banner = jQuery('$my_search').length;
                                                if(n_banner > 0){
                                                    
                                                    jQuery( document ).ready(function() {
                                                        jQuery( '$my_search img' ).attr( 'src', banner);
                                                        jQuery( '$my_search img' ).attr( 'srcset', banner);
                                                    });
                                                    var n_text = jQuery('$my_search a').length
                                                        
                                                    if(n_text > 0 && banner_text != ''){
                                                        jQuery( document ).ready(function() {
                                                            jQuery('$my_search a').text(banner_text);
                                                        });
                                                    }
                                                }";
                                }
                                else if($theme->Template == "oceanwp"){
                                    $banner .= "jQuery( document ).ready(function() {
                                                    var n_banner = jQuery('$my_search').length;
                                                    if(n_banner > 0){
                                                        jQuery( '$my_search' ).attr( 'style','background-image: url('+banner+') !important');
                                                        var n_text = jQuery('$my_search a').length
                                                        
                                                        if(n_text > 0 && banner_text != ''){
                                                            jQuery('$my_search a').text(banner_text);
                                                        }
                                                    }
                                                });";
                                }
                                else if($theme->Template == "porto"){
                                    $banner .= "jQuery( document ).ready(function() {
                                                    var n_banner = jQuery('$my_search').length;
                                                    if(n_banner > 0){
                                                        jQuery( '$my_search img' ).attr( 'src', banner);
                                                        jQuery( '$my_search img' ).attr( 'srcset', banner);
                                                        
                                                        var n_text = jQuery('$my_search button').length
                                                        
                                                        if(n_text > 0 && banner_text != ''){
                                                            
                                                            jQuery('$my_search button').text(banner_text);
                                                        }
                                                    }
                                                });";
                                }
                                else if($theme->Template == "enfold"){
                                    $banner .= "jQuery( document ).ready(function() {
                                                    setTimeout(function() {
                                                        var n_banner = jQuery('$my_search').length;
                                                        if(n_banner > 0){
                                                           
                                                            jQuery( '$my_search li.active-slide' ).attr( 'style','background-image: url('+banner+') !important');
                                                            jQuery('$my_search li.active-slide').css('opacity', '1');
                                                            jQuery('$my_search li.active-slide').css('visibility', 'visible');
                                                            var n_text = jQuery('$my_search a').length
                                                            
                                                            if(n_text > 0 && banner_text != ''){
                                                                
                                                                jQuery('$my_search a').text(banner_text);
                                                            }
                                                        }
                                                    }, 1000);
                                                });";
                                }
                                $banner .="</script>";
                            }
                        }
                    }
                }
            return $products.$banner;
            }
        }
        die();
    }
    add_shortcode('meteosales_products_display', 'meteosales_ms_products_display_shortcode');
}