You are here

protected function SocialFacebookFormatter::getComments in Open Social 7

Get facebook comments by post ID.

Parameters

string $id: Post ID.

Return value

array Array with comments.

1 call to SocialFacebookFormatter::getComments()
SocialFacebookFormatter::getData in includes/social_comments.facebook.inc

File

includes/social_comments.facebook.inc, line 130
Facebook class

Class

SocialFacebookFormatter
@file Facebook class

Code

protected function getComments($id) {
  $comments = array();

  // Set cache key for each post id.
  $cache_key = 'social_comments:facebook:' . $id;

  // Try to get comments fom cache.
  if ($cache = cache_get($cache_key)) {
    $comments = $cache->data;
  }
  else {
    $query = array(
      'access_token' => $this->access_token,
      'limit' => !empty($this->max_items) ? $this->max_items : NULL,
    );
    $query = array_filter($query);
    $response_url = url('https://graph.facebook.com/' . $id . '/comments', array(
      'query' => $query,
    ));
    $data = drupal_http_request($response_url);
    if ($data->code != 200) {
      drupal_set_message(t('Facebook comments error'), 'warning');
      watchdog('social_comments', $data->data, array(), WATCHDOG_WARNING);
      return FALSE;
    }
    $result = drupal_json_decode($data->data);
    if (!empty($result['data'])) {
      $comments = $result['data'];

      // Set data to cache.
      cache_set($cache_key, $comments, 'cache', $this->expire + REQUEST_TIME);
    }
  }
  return $comments;
}