/** * This function should get all the counts for the * supported services * * It should return an associative array with the services as * the keys and the counts as the value. * * Example: * array('facebook' => 12, 'google_plus' => 0, 'twitter' => 14, ...); * * @return Array an associative array of service => counts */ public function get_counts() { $services_length = count($this->services); $config = self::get_services_config(); $response = array(); $response['status'] = 200; for ($i = 0; $i < $services_length; $i++) { $service = $this->services[$i]; if (!isset($config[$service])) { continue; } if (isset($config[$service]['prepare'])) { $this->{$config}[$service]['prepare']($this->url, $config); } $options = array('method' => $config[$service]['method'], 'timeout' => 1, 'headers' => isset($config[$service]['headers']) ? $config[$service]['headers'] : array(), 'body' => isset($config[$service]['body']) ? $config[$service]['body'] : NULL); $result = ShareaholicHttp::send(str_replace('%s', $this->url, $config[$service]['url']), $options); if (!$result) { $response['status'] = 500; } $callback = $config[$service]['callback']; $counts = $this->{$callback}($result); if (is_numeric($counts)) { $response['data'][$service] = $counts; } } return $response; }
/** * Share Counts API Connectivity check * */ public static function share_counts_api_connectivity_check() { // if we already checked and it is successful, then do not call the API again $share_counts_connect_check = self::get_option('share_counts_connect_check'); if (isset($share_counts_connect_check) && $share_counts_connect_check == 'SUCCESS') { return $share_counts_connect_check; } $services_config = ShareaholicSeqShareCount::get_services_config(); $services = array_keys($services_config); $param_string = implode('&services[]=', $services); $share_counts_api_url = url('shareaholic/api/share_counts/v1', array('absolute' => TRUE)) . '?action=shareaholic_share_counts_api&url=https%3A%2F%2Fwww.google.com%2F&services[]=' . $param_string; $cache_key = 'share_counts_api_connectivity_check'; // fetch cached response if it exists or has not expired $response = ShareaholicCache::get($cache_key); if (!$response) { $response = ShareaholicHttp::send($share_counts_api_url, array('method' => 'GET'), true); } $response_status = self::get_share_counts_api_status($response); // if this was the first time we are doing this and it failed, disable // the share counts API if (empty($share_counts_connect_check) && $response_status == 'FAIL') { self::update_options(array('disable_internal_share_counts_api' => 'on')); } if ($response_status == 'SUCCESS') { ShareaholicCache::set($cache_key, $response, SHARE_COUNTS_CHECK_CACHE_LENGTH); } self::update_options(array('share_counts_connect_check' => $response_status)); return $response_status; }