View source
<?php
define('SOCIALFEED_FACEBOOK_SDK_MIN_PLUGIN_VERSION', '4.0');
define('SOCIALFEED_TWITTER_SDK_MIN_PLUGIN_VERSION', '1.1');
define('SOCIALFEED_INSTAGRAM_SDK_MIN_PLUGIN_VERSION', '1.0');
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use MetzWeb\Instagram\Instagram;
function socialfeed_help($path, $arg) {
switch ($path) {
case 'admin/help#socialfeed':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Social Feed module allows you to fetch feeds directly from Facebook, Twitter & Instagram by simply insatlling it with the dependent <a href="@url">Libraries</a> module.', array(
'@url' => 'http://www.drupal.org/project/libraries',
)) . '</p>';
return $output;
}
}
function socialfeed_libraries_info() {
$libraries = array();
$libraries['facebook'] = array(
'name' => 'PHP SDK for Facebook APIs',
'vendor url' => 'http://developers.facebook.com/',
'download url' => 'https://github.com/facebook/facebook-php-sdk-v4/archive/4.0-dev.zip',
'path' => 'src/Facebook',
'version arguments' => array(
'file' => 'src/Facebook/FacebookRequest.php',
'pattern' => "/const VERSION = '([\\d.]*)/",
'lines' => 42,
),
'xautoload' => function ($adapter) {
$adapter
->add('Facebook', 'src');
},
'files' => array(
'php' => array(
'autoload.php',
),
),
);
$libraries['twitter'] = array(
'name' => 'PHP SDK for Twitter APIs',
'vendor url' => 'https://dev.twitter.com/overview/api/twitter-libraries',
'download url' => 'https://github.com/J7mbo/twitter-api-php/archive/master.zip',
'version arguments' => array(
'file' => 'TwitterAPIExchange.php',
'pattern' => "/v[-+]?([0-9]*\\.[0-9]+|[0-9]+) API/",
'lines' => 4,
),
'files' => array(
'php' => array(
'TwitterAPIExchange.php',
),
),
);
$libraries['instagram'] = array(
'name' => 'PHP SDK for Instagram APIs',
'vendor url' => 'https://github.com/cosenary/Instagram-PHP-API',
'download url' => 'https://github.com/cosenary/Instagram-PHP-API/archive/master.zip',
'version arguments' => array(
'file' => 'src/Instagram.php',
'pattern' => "/@version [-+]?([0-9]*\\.[0-9]+|[0-9]+)/",
'lines' => 14,
),
'xautoload' => function ($adapter) {
$adapter
->add('MetzWeb\\Instagram', 'src');
},
'files' => array(
'php' => array(
'src/Instagram.php',
),
),
);
return $libraries;
}
function socialfeed_menu() {
$items = array();
$items['admin/config/services/socialfeed'] = array(
'title' => 'Social Feed Settings',
'description' => 'Configure to fit clients needs',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'socialfeed_facebook_settings',
),
'access arguments' => array(
'administer modules',
),
'file' => 'socialfeed.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/services/socialfeed/facebook'] = array(
'title' => 'Facebook',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/services/socialfeed/twitter'] = array(
'title' => 'Twitter',
'description' => 'Configure to fit clients needs',
'page arguments' => array(
'socialfeed_twitter_settings',
),
'access arguments' => array(
'administer modules',
),
'file' => 'socialfeed.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/services/socialfeed/instagram'] = array(
'title' => 'Instagram',
'description' => 'Configure to fit clients needs',
'page arguments' => array(
'socialfeed_instagram_settings',
),
'access arguments' => array(
'administer modules',
),
'file' => 'socialfeed.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function socialfeed_theme($existing, $type, $theme, $path) {
return array(
'socialfeed_facebook' => array(
'variables' => array(
'facebook' => NULL,
),
'template' => 'facebook',
'path' => drupal_get_path('module', 'socialfeed') . '/templates',
),
'socialfeed_twitter' => array(
'variables' => array(
'twitter' => NULL,
),
'template' => 'twitter',
'path' => drupal_get_path('module', 'socialfeed') . '/templates',
),
'socialfeed_instagram' => array(
'variables' => array(
'instagram_images' => NULL,
),
'template' => 'instagram',
'path' => drupal_get_path('module', 'socialfeed') . '/templates',
),
);
}
function socialfeed_block_info() {
$blocks = array();
$blocks['facebook_latest_feed'] = array(
'info' => t('Social Feed Facebook'),
);
$blocks['twitter_latest_feed'] = array(
'info' => t('Social Feed Twitter'),
);
$blocks['instagram_latest_feed'] = array(
'info' => t('Social Feed Instagram'),
);
return $blocks;
}
function socialfeed_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'facebook_latest_feed':
$block['subject'] = '';
$block['content'] = socialfeed_facebook_posts();
break;
case 'twitter_latest_feed':
$block['subject'] = '';
$block['content'] = socialfeed_twitter_posts();
break;
case 'instagram_latest_feed':
$block['subject'] = '';
$block['content'] = socialfeed_instagram_posts();
break;
}
return $block;
}
function socialfeed_facebook_feeds($page_name = '') {
$config = $message_feed = array();
$display_time = $display_pic = $selected_type = $selected_type_key = $fb_type = $selected_type_value = '';
$config['app_id'] = variable_get('socialfeed_facebook_app_id');
$config['secret'] = variable_get('socialfeed_facebook_secret_key');
$fb_page = $page_name;
libraries_load('facebook');
FacebookSession::setDefaultApplication($config['app_id'], $config['secret']);
if (isset($config['app_id']) && !empty($config['app_id']) && isset($config['secret']) && !empty($config['secret'])) {
$session = FacebookSession::newAppSession();
$request = new FacebookRequest($session, 'GET', '/' . $fb_page . '/feed');
$response = $request
->execute();
$graph_object = $response
->getGraphObject();
$facebook_values = $graph_object
->asArray('data');
if ($facebook_values) {
$display_time = variable_get('socialfeed_facebook_time_stamp');
$display_pic = variable_get('socialfeed_facebook_display_pic');
$display_all_posts = variable_get('socialfeed_facebook_all_types');
$selected_type = variable_get('socialfeed_facebook_post_type');
$selected_type_key = $selected_type;
$fb_type = array(
'link',
'status',
'photo',
'video',
);
$selected_type_value = $fb_type[$selected_type_key];
$i = 0;
foreach ($facebook_values['data'] as $facebook_value) {
if ($facebook_value->type == $selected_type_value) {
if (array_key_exists('message', $facebook_value)) {
if ($facebook_value->type == 'photo') {
if ($display_pic == 1) {
$message_feed[$i]['picture'] = $facebook_value->picture;
}
}
$message_feed[$i]['message'] = substr($facebook_value->message, 0, variable_get('socialfeed_facebook_trim_length'));
if ($display_time == 1) {
$formatted_date = new DateTime($facebook_value->created_time);
$message_feed[$i]['created_stamp'] = $formatted_date
->format(variable_get('socialfeed_facebook_time_format'));
}
}
$i++;
if ($i == variable_get('socialfeed_facebook_no_feeds')) {
break;
}
}
elseif ($display_all_posts == 1) {
if (array_key_exists('message', $facebook_value)) {
if ($display_pic == 1) {
$message_feed[$i]['picture'] = $facebook_value->picture;
}
$message_feed[$i]['message'] = substr($facebook_value->message, 0, variable_get('socialfeed_facebook_trim_length'));
$message_feed[$i]['type'] = $facebook_value->type;
if ($display_time == 1) {
$formatted_date = new DateTime($facebook_value->created_time);
$message_feed[$i]['created_stamp'] = $formatted_date
->format(variable_get('socialfeed_facebook_time_format'));
}
}
$i++;
if ($i == variable_get('socialfeed_facebook_no_feeds')) {
break;
}
}
}
return theme('socialfeed_facebook', array(
'facebook' => $message_feed,
));
}
}
else {
drupal_set_message(t('Please provide your credentials at the Facebook <a href="@configuration">configuration page</a>.', array(
'@configuration' => 'admin/config/services/socialfeed',
)), 'warning');
}
}
function socialfeed_facebook_posts() {
$facebook_latest = socialfeed_facebook_feeds(variable_get('socialfeed_facebook_page_name'));
return $facebook_latest;
}
function socialfeed_twitter_posts() {
$twitter_tweets = array();
$display_time = '';
$settings = array(
'oauth_access_token' => variable_get('socialfeed_twitter_access_token'),
'oauth_access_token_secret' => variable_get('socialfeed_twitter_access_token_secret'),
'consumer_key' => variable_get('socialfeed_twitter_consumer_key'),
'consumer_secret' => variable_get('socialfeed_twitter_consumer_secret'),
);
$tweets_count = variable_get('socialfeed_twitter_tweets_count');
$twitter_username = variable_get('socialfeed_twitter_username');
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$request_method = "GET";
$getfield = '?screen_name=' . $twitter_username . '&count=' . $tweets_count;
libraries_load('twitter');
$twitter = new TwitterAPIExchange($settings);
$twitter_values = json_decode($twitter
->setGetfield($getfield)
->buildOauth($url, $request_method)
->performRequest(), $assoc = TRUE);
if (isset($twitter_values) && !empty($twitter_values)) {
if (array_key_exists('errors', $twitter_values)) {
if ($twitter_values["errors"][0]["message"] != "") {
echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>" . $twitter_values[errors][0]["message"] . "</em></p>";
exit;
}
}
$display_time = variable_get('socialfeed_twitter_time_stamp');
foreach ($twitter_values as $key => $twitter_value) {
$twitter_tweets[$key]['username'] = $twitter_value['user']['screen_name'];
$twitter_tweets[$key]['full_username'] = 'http://twitter.com/' . $twitter_value['user']['screen_name'];
preg_match_all('#\\bhttps?://[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/))#', $twitter_value['text'], $extra_links);
foreach ($extra_links[0] as $extra_link) {
$twitter_tweets[$key]['extra_links'][] = $extra_link;
}
$twitter_tweets[$key]['tweet'] = rtrim($twitter_value['text'], $extra_link);
if ($display_time == 1) {
$formatted_twitter_date = new DateTime($twitter_value['created_at']);
$twitter_tweets[$key]['twitter_date'] = $formatted_twitter_date
->format(variable_get('socialfeed_twitter_time_format'));
}
}
return theme('socialfeed_twitter', array(
'twitter' => $twitter_tweets,
));
}
}
function socialfeed_instagram_posts() {
$images = array();
$i = 0;
libraries_load('instagram');
$instagram = new Instagram(array(
'apiKey' => variable_get('socialfeed_instagram_api_key'),
'apiSecret' => variable_get('socialfeed_instagram_api_secret'),
'apiCallback' => variable_get('socialfeed_instagram_redirect_uri'),
));
if (isset($instagram
->getUserMedia(variable_get('socialfeed_instagram_user_id'))->data) && !empty($instagram
->getUserMedia(variable_get('socialfeed_instagram_user_id'))->data)) {
$instagram_objects = $instagram
->getUserMedia(variable_get('socialfeed_instagram_user_id'))->data;
foreach ($instagram_objects as $instagram_object) {
$resolution = variable_get('socialfeed_instagram_picture_resolution');
$images[] = $instagram_object->images->{$resolution}->url;
$i++;
if ($i == variable_get('socialfeed_instagram_picture_count')) {
break;
}
}
return theme('socialfeed_instagram', array(
'instagram_images' => $images,
));
}
elseif (empty($instagram
->getUserMedia(variable_get('socialfeed_instagram_user_id'))->data)) {
drupal_set_message(t('Please provide valid User ID at the Instagram <a href="@configuration">configuration page</a>.', array(
'@configuration' => 'admin/config/services/socialfeed/instagram',
)), 'warning');
}
else {
drupal_set_message(t('Please provide your credentials at the Instagram <a href="@configuration">configuration page</a>.', array(
'@configuration' => 'admin/config/services/socialfeed/instagram',
)), 'warning');
}
}