View source
<?php
namespace Drupal\twitter_profile_widget;
use Drupal\twitter_profile_widget\Resources\j7mbo\twitter_api_php\TwitterAPIExchange;
use Drupal\twitter_profile_widget\Entity\TwitterWidget;
class TwitterProfile implements TwitterProfileInterface {
public function pull(TwitterWidget $entity) {
$account = $entity
->get('account')->value;
$type = $entity
->get('type')->value;
$search = $entity
->get('search')->value;
$timeline = $entity
->get('timeline')->value;
$count = $entity
->get('count')->value;
$replies = $entity
->get('replies')->value;
$retweets = $entity
->get('retweets')->value;
if (!($connection = $this
->getConnection())) {
\Drupal::logger('twitter_profile_widget')
->error('No credentials were found for the Twitter API. Check /admin/config/media/twitter_profile_widget.');
return FALSE;
}
$twitter = new TwitterAPIExchange($connection);
$query = $this
->buildQuery($account, $type, $timeline, $search, $replies, $retweets);
$getfield = urldecode($query['getfield']);
$data = $twitter
->setGetfield($getfield)
->buildOauth($query['url'], 'GET')
->performRequest();
$tweets = json_decode($data);
if (empty($tweets)) {
return FALSE;
}
elseif (isset($tweets->errors)) {
\Drupal::logger('twitter_profile_widget')
->error($tweets->errors[0]->message);
return FALSE;
}
elseif (isset($tweets->statuses)) {
return $tweets->statuses;
}
return $tweets;
}
protected function getConnection($key = '', $secret = '') {
if (empty($key) || empty($secret)) {
$config = \Drupal::config('twitter_profile_widget.settings');
$key = $config
->get('twitter_widget_key');
$secret = $config
->get('twitter_widget_secret');
if (empty($key) || empty($secret)) {
return FALSE;
}
}
return [
'oauth_access_token' => '',
'oauth_access_token_secret' => '',
'consumer_key' => $key,
'consumer_secret' => $secret,
];
}
protected function buildQuery($account, $type = '', $timeline = '', $search = '', $replies = 1, $retweets = 1) {
switch ($type) {
case 'timeline':
$url = 'https://api.twitter.com/1.1/lists/statuses.json';
$params = [
'count' => 10,
'slug' => $timeline,
'owner_screen_name' => $account,
'include_rts' => $retweets,
];
if ($replies == 0) {
$params['exclude_replies'] = 1;
}
break;
case 'favorites':
$url = 'https://api.twitter.com/1.1/favorites/list.json';
$params = [
'count' => 10,
'screen_name' => $account,
];
break;
case 'search':
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$params = [
'q' => $search,
'count' => 10,
];
break;
default:
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$params = [
'count' => 10,
'screen_name' => $account,
'include_rts' => $retweets,
];
if ($replies == 0) {
$params['exclude_replies'] = 1;
}
break;
}
$getfield = '?' . http_build_query($params);
return [
'url' => $url,
'getfield' => $getfield,
];
}
public function checkConnection($key = '', $secret = '') {
if ($key == '' || $secret == '') {
return FALSE;
}
$url = 'https://api.twitter.com/1.1/help/tos.json';
$getfield = '';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($this
->getConnection($key, $secret));
$data = $twitter
->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$result = json_decode($data);
if (empty($result)) {
return FALSE;
}
elseif (isset($result->errors)) {
return $result->errors[0]->message;
}
return TRUE;
}
}