<?php
if ( ! defined( 'ABSPATH' ) ) exit;
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {//if woocommerce is activated
    
    /**
     * Register meteosales products clicks
     *
     * @link       info@meteosales.com
     * @since      1.0.6
     *
     * @package    Meteosales
     * @subpackage Meteosales/includes
     */
    add_action('wp_ajax_nopriv_product_clicks', 'meteosales_ms_clicks_product');
    add_action('wp_ajax_product_clicks', 'meteosales_ms_clicks_product');
    function meteosales_ms_clicks_product(){
        
        $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"];
        
        $sort = get_option("MeteoCommerceProductsSetting");
        $arr_sort = json_decode($sort, true);
        $token = $arr_sort["token"];
        
        if($token != ""){
            $type=$_POST['type'];
            $slider = $_POST['slider'];
            $p_id = "";
            if($type == 1){
                $p_id = $_POST['value'];
            }
            else if($type == 2){
                $p_id = url_to_postid($_POST['value']);
            }
            
            if($p_id != ""){
                $api_key = 'https://my.meteosales.com/api/revelations';
                $id_m = $_POST['hid_id'];
                $is_guest = is_user_logged_in() ? 0 : 1 ;
                $product = wc_get_product( $p_id );
                if(!$product){ die(); }
                $p_name= $product->get_name();
                $image = wp_get_attachment_url($product->get_image_id());
                
                //Call API
                $response = wp_remote_post($api_key, array('body' => array('token' => $token, 'server_name' => $server , 'ip'=>$ipaddress, 'p_id'=>$p_id, 'p_name'=>$p_name, 'p_image'=>$image, 'm_id'=>$id_m,'is_guest' => $is_guest, 't'=>$type,'slider'=> $slider)));
                
            }
        }
        exit();
    }
    
    /**
     * Create var ajaxurl
     *
     * @link       info@meteosales.com
     * @since      1.0.6
     *
     * @package    Meteosales
     * @subpackage Meteosales/includes
     */
    add_action('wp_head', 'meteosales_ms_ajaxurl');
    function meteosales_ms_ajaxurl() {
        
        echo '<script type="text/javascript">
                var ajaxurl = "' . admin_url('admin-ajax.php') . '";
            </script>';
    }
    
} ?>