public function testGetCount()
 {
     // test that this function returns the expected API response
     $share_count = new ShareaholicCurlMultiShareCount($this->url, $this->services);
     $response = $share_count->get_counts();
     $this->assertNotNull($response, 'The response array should not be null');
     foreach ($this->services as $service) {
         $this->assertNotNull($response['data'][$service], 'The ' . $service . ' count should not be null');
     }
 }
 public function setUp()
 {
     $this->url = 'https://www.google.com';
     $this->services = array_keys(ShareaholicCurlMultiShareCount::get_services_config());
     $this->options = array();
     $this->share_count = new ShareaholicCurlMultiShareCount($this->url, $this->services, $this->options);
     // all callbacks take a predefined response structure
     $this->response = array('response' => array('code' => 200));
 }
Example #3
0
 public function update_counts($posts, $type = 'permalink', $networks = null)
 {
     // or archive
     if (empty($type) || !in_array(strtolower($type), array('archive', 'permalink'))) {
         return;
     }
     if (empty($networks)) {
         $networks = $this->get_option('networks');
     }
     $archive_url = $this->get_option('archive_url');
     // no archive url set
     if ($type == 'archive' && empty($archive_url)) {
         return;
     }
     foreach ($posts as $post) {
         $permalink = get_permalink($post->ID);
         $_key = 'counts';
         if ($type == 'archive') {
             $permalink = str_replace('%%slug%%', $post->post_name, $archive_url);
             $_key = 'counts_archived';
         }
         $_blmd_social_shares = (array) get_post_meta($post->ID, '_blmd_social_shares', true);
         if (empty($_blmd_social_shares[$_key])) {
             $_blmd_social_shares[$_key] = array();
         }
         $services = $networks;
         if (($_ = array_search('googleplus', $services)) !== false) {
             $services[$_] = 'google_plus';
         }
         $options = array();
         $result = array();
         $shortcircuit = apply_filters('blmd_social_before_update_counts', null, $post);
         if ($shortcircuit !== null) {
             continue;
         }
         if (($result = get_transient(md5($permalink))) === false) {
             $shares = new ShareaholicCurlMultiShareCount($permalink, $services, $options);
             $result = $shares->get_counts();
             if ($result['status'] == 200 && !empty($result['data'])) {
                 set_transient(md5($permalink), $result, 60);
             }
         }
         if (isset($result['data']['google_plus'])) {
             $result['data']['googleplus'] = $result['data']['google_plus'];
             unset($result['data']['google_plus']);
         }
         foreach ($networks as $network) {
             $shares = $result['data'][$network];
             if ($shares === false) {
                 continue;
             }
             $shares = (int) $shares;
             $existing_shares = (int) $_blmd_social_shares[$_key][$network];
             if ($shares > $existing_shares) {
                 $_blmd_social_shares[$_key][$network] = $shares;
             }
             // echo "$permalink / $network / $shares<br>";
         }
         // for facebook returning double counts for 301'd url
         if ($type == 'archive' && count($_blmd_social_shares['counts_archived']) <= 1) {
             $_blmd_social_shares['counts_archived'] = array();
         }
         // $_blmd_social_shares['counts_lastupdate'] = current_time( 'timestamp', true );
         $_blmd_social_shares[$_key . '_lastupdate'] = current_time('timestamp', true);
         update_post_meta($post->ID, '_blmd_social_shares', $_blmd_social_shares);
         // $myvals = get_post_meta($post->ID);
         // echo "<br><hr><br>";
     }
 }