checkLogin()){ echo json_encode(array('error'=>__('Please check Google Analytics Settings','geodirectory'))); return false; } $account = $gaApi->getSingleProfile(); if(!isset($account[0]['id'])){ echo json_encode(array('error'=>__('Please check Google Analytics Settings','geodirectory'))); return false; } $account = $account[0]['id']; # Set the account to the one requested $gaApi->setAccount($account); # Get the metrics needed to build the visits graph; try { $stats = $gaApi->getMetrics($metrics, $start_date, $end_date, $dimensions, $sort, $filters, $limit , $realtime); } catch (Exception $e) { print 'GA Summary Widget - there was a service error ' . $e->getCode() . ':' . $e->getMessage(); } //print_r($stats); echo json_encode($stats); exit; }// end GA function function geodir_ga_get_token(){ $at = get_option('gd_ga_access_token'); $use_url = "https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=".$at; $response = wp_remote_get($use_url,array('timeout' => 15)); if(!empty($response['response']['code']) && $response['response']['code']==200) {//access token is valid return $at; }else{//else get new access token $refresh_at = get_option('gd_ga_refresh_token'); if(!$refresh_at){ echo json_encode(array('error'=>__('Not authorized, please click authorized in GD > Google analytic settings.', 'geodirectory')));exit; } $rat_url = "https://www.googleapis.com/oauth2/v3/token?"; $client_id = "client_id=".get_option('geodir_ga_client_id'); $client_secret = "&client_secret=".get_option('geodir_ga_client_secret'); $refresh_token = "&refresh_token=".$refresh_at; $grant_type = "&grant_type=refresh_token"; $rat_url_use = $rat_url.$client_id.$client_secret.$refresh_token.$grant_type; $rat_response = wp_remote_post($rat_url_use,array('timeout' => 15)); if(!empty($rat_response['response']['code']) && $rat_response['response']['code']==200) { $parts = json_decode($rat_response['body']); update_option('gd_ga_access_token', $parts->access_token); return $parts->access_token; }else{ echo json_encode(array('error'=>__('Login failed', 'geodirectory')));exit; } } }