Exemplo n.º 1
0
 /**
  * Helper method to parse the list of social services to get share counts
  */
 public static function parse_services($services)
 {
     $result = array();
     if (empty($services) || !is_array($services)) {
         return $result;
     }
     // make the set of services unique
     $services = array_unique($services);
     // only get the services we can get share counts for
     $social_services = array_keys(ShareaholicSeqShareCount::get_services_config());
     foreach ($services as $service) {
         if (in_array($service, $social_services)) {
             array_push($result, $service);
         }
     }
     return $result;
 }
 /**
  * This test may fail if the APIs fail
  */
 public function testMissingServices()
 {
     // test that this function returns response WITHOUT facebook
     $share_count = new ShareaholicSeqShareCount('https://dev.losloslos.com/', $this->services, $this->options);
     $response = $share_count->get_counts();
     $this->assertNotNull($response, 'The response array should not be null');
     $this->assertNull($response['data']['facebook'], 'The facebook count should be null');
 }
Exemplo n.º 3
0
 /**
  * Function to handle the share count API requests
  *
  */
 public static function share_counts_api()
 {
     // sometimes the drupal http request function throws errors so setting handler
     set_error_handler(array('ShareaholicPublic', 'custom_error_handler'));
     $cache_key = 'shr_api_res-' . md5($_SERVER['QUERY_STRING']);
     $result = ShareaholicCache::get($cache_key);
     if (!$result) {
         $url = isset($_GET['url']) ? $_GET['url'] : NULL;
         $services = isset($_GET['services']) ? $_GET['services'] : NULL;
         $result = array();
         if (is_array($services) && count($services) > 0 && !empty($url)) {
             if (self::has_curl()) {
                 $shares = new ShareaholicCurlMultiShareCount($url, $services);
             } else {
                 $shares = new ShareaholicSeqShareCount($url, $services);
             }
             $result = $shares->get_counts();
             if (isset($result['data'])) {
                 ShareaholicCache::set($cache_key, $result, SHARE_COUNTS_CHECK_CACHE_LENGTH);
             }
         }
     }
     header('Content-Type: application/json');
     echo json_encode($result);
     exit;
 }
Exemplo n.º 4
0
 /**
  * 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 = admin_url('admin-ajax.php') . '?action=shareaholic_share_counts_api&url=https%3A%2F%2Fwww.google.com%2F&services[]=' . $param_string;
     $cache_key = 'share_counts_api_connectivity_check';
     $response = get_transient($cache_key);
     if (!$response) {
         $response = ShareaholicCurl::get($share_counts_api_url, array(), '', 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') {
         set_transient($cache_key, $response, SHARE_COUNTS_CHECK_CACHE_LENGTH);
     }
     self::update_options(array('share_counts_connect_check' => $response_status));
     return $response_status;
 }
Exemplo n.º 5
0
 /**
  * This test may fail if the APIs fail
  */
 public function testIsUrlEncoded()
 {
     $url = 'http://eatnabout.com/2014/03/29/bestie/#more-10144';
     $encoded_url = 'http%3A%2F%2Featnabout.com%2F2014%2F03%2F29%2Fbestie%2F%23more-10144';
     $share_count = new ShareaholicSeqShareCount($url, $this->services);
     $this->assertTrue($share_count->is_url_encoded($encoded_url), 'It should return true when the url is encoded');
     $this->assertFalse($share_count->is_url_encoded($url), 'It should return false when the url is not encoded');
 }
Exemplo n.º 6
0
 /**
  * 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;
 }
 /**
  * Function to handle the share count API requests
  *
  */
 public static function share_counts_api()
 {
     // sometimes the drupal http request function throws errors so setting handler
     set_error_handler(array('ShareaholicPublic', 'custom_error_handler'));
     $debug_mode = isset($_GET['debug']) && $_GET['debug'] === '1';
     $cache_key = 'shr_api_res-' . md5($_SERVER['QUERY_STRING']);
     $result = ShareaholicCache::get($cache_key);
     $has_curl_multi = self::has_curl();
     if (!$result) {
         $url = isset($_GET['url']) ? $_GET['url'] : NULL;
         $services = isset($_GET['services']) ? $_GET['services'] : NULL;
         $result = array();
         $options = array();
         if ($debug_mode && isset($_GET['timeout'])) {
             $options['timeout'] = intval($_GET['timeout']);
         }
         if (is_array($services) && count($services) > 0 && !empty($url)) {
             if ($debug_mode && isset($_GET['client'])) {
                 if ($has_curl_multi && $_GET['client'] !== 'seq') {
                     $shares = new ShareaholicCurlMultiShareCount($url, $services, $options);
                 } else {
                     $shares = new ShareaholicSeqShareCount($url, $services, $options);
                 }
             } else {
                 if ($has_curl_multi) {
                     $shares = new ShareaholicCurlMultiShareCount($url, $services, $options);
                 } else {
                     $shares = new ShareaholicSeqShareCount($url, $services, $options);
                 }
             }
             $result = $shares->get_counts();
             if ($debug_mode) {
                 $result['has_curl_multi'] = $has_curl_multi;
                 $result['curl_type'] = get_class($shares);
                 $result['raw'] = $shares->raw_response;
             }
             if (isset($result['data']) && !$debug_mode) {
                 ShareaholicCache::set($cache_key, $result, SHARE_COUNTS_CHECK_CACHE_LENGTH);
             }
         }
     }
     header('Content-Type: application/json');
     echo json_encode($result);
     exit;
 }