<?php defined( 'ABSPATH' ) or die( 'No script kiddies please!!' );
if ( !class_exists( 'EGPR_API_Settings' ) ) {

    class EGPR_API_Settings extends EGPR_Library{
      
    /*
    * Get all business details and google reviews using WP's HTTP API
    * Parameter: API KEY & Place ID
    */    
     public static function get_google_review_api($check_cache, $cache_period, $api_key, $place_id,$language_code){
        $place_id = trim(esc_attr($place_id),' ');
        $api_key  = trim(esc_attr($api_key),' ');
         if(!isset($check_cache) || $check_cache == 'true'){

                $google_transient = 'google_' . md5($place_id); // set transient id
                $google_transient_details = get_transient($google_transient); // get transient data
                if($google_transient_details === FALSE){
                   // $get_api_url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=$place_id&key=$api_key&language=$language_code";
                    $get_api_url = "https://maps.googleapis.com/maps/api/place/details/json?key=$api_key&placeid=$place_id&language=$language_code";
                     $url = esc_url_raw( $get_api_url );
                     // Send API Call using WordPress HTTP API
                     $result = wp_remote_get($url, array( 'timeout' => 60 )); 

                    if(isset($result['body']) && !empty($result['body'])){
                        $response =  json_decode($result['body'],true);
                    }
                    set_transient($google_transient, $response, $cache_period * HOUR_IN_SECONDS );
                }else{
                     $response = $google_transient_details;
                }
            }else{
                // $get_api_url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=$place_id&key=$api_key&language=$language_code";
               $get_api_url = "https://maps.googleapis.com/maps/api/place/details/json?key=$api_key&placeid=$place_id&language=$language_code";
                 $url = esc_url_raw( $get_api_url );
                 // Send API Call using WordPress HTTP API
                 $result = wp_remote_get($url, array( 'timeout' => 60 )); 
                if(isset($result['body']) && !empty($result['body'])){
                        $response =  json_decode($result['body'],true);
                 }

            }
          return $response;

     } 
}

   $GLOBALS[ 'egpr_api' ] =  new EGPR_API_Settings();
}

