<?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
    
    add_action( 'wp_footer', 'meteosales_ms_product_page_counter' );
    
    function meteosales_ms_product_page_counter() {
        if ( is_product() ) { 
            
            if(isset($_POST['hid-id']) && $_POST['hid-id'] != "" ){
                $type = 2;
                $id_m = sanitize_text_field($_POST['hid-id']);
                $server = $_SERVER["SERVER_NAME"];
                
                $sort = get_option("MeteoCommerceProductsSetting");
                $arr_sort = json_decode($sort, true);
                $token = $arr_sort["token"];
                
                if($token != ""){
                    $api_key = 'https://my.meteosales.com/api/revelations';
                    $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'];}
                    
                    if($ipaddress != ''){
                        
                        global $product;
                        $id = $product->get_id();
                        $product = wc_get_product( $id );
                        $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'=>$id,'p_name'=>$p_name,'p_image'=>$image,'m_id'=>$id_m,'t'=>$type)
                        ));
                    }
                } 
            }
        }
    }
    
    
    
   
}