function socialfeed_facebook_feeds in Social Feed 7.2
Same name and namespace in other branches
- 7 socialfeed.module \socialfeed_facebook_feeds()
Returns HTML with feeds in required format.
Parameters
string $page_name: Accepts string as value.
Return value
array Returns Facebook Feed.
Throws
\Exception
1 call to socialfeed_facebook_feeds()
- socialfeed_facebook_posts in ./
socialfeed.block.inc - Uses socialfeed_facebook_posts() for fetching Facebook feeds.
File
- ./
socialfeed.block.inc, line 124 - File include for Social Feed module.
Code
function socialfeed_facebook_feeds($page_name = '') {
global $user;
$selected_type_value = $config = $message_feed = [];
$display_time = $display_pic = $display_video = $display_all_posts = $teaser_text = $facebook_hash_tag = $selected_type = $selected_type_key = $fb_type = $selected_type_value = $url = $request = '';
$config['app_id'] = variable_get('socialfeed_facebook_app_id');
$config['secret'] = variable_get('socialfeed_facebook_secret_key');
$config['page_name'] = variable_get('socialfeed_facebook_page_name');
$config['user_token'] = variable_get('socialfeed_facebook_user_token');
if (isset($config['app_id']) && !empty($config['app_id']) && isset($config['secret']) && !empty($config['secret'])) {
$uri = "https://graph.facebook.com/v4.0/oauth/access_token?grant_type=fb_exchange_token&client_id={$config['app_id']}&client_secret={$config['secret']}&fb_exchange_token={$config['user_token']}";
$response = drupal_http_request($uri);
$json_decode = json_decode($response->data);
$long_token = $json_decode->access_token;
$uri = "https://graph.facebook.com/v4.0/me?access_token={$long_token}";
$response = drupal_http_request($uri);
$json_decode = json_decode($response->data);
$account_id = $json_decode->id;
$uri = "https://graph.facebook.com/v4.0/{$config['page_name']}?fields=id&access_token={$long_token}";
$response = drupal_http_request($uri);
$json_decode = json_decode($response->data);
$page_id = $json_decode->id;
$uri = "https://graph.facebook.com/v4.0/{$account_id}/accounts?access_token={$long_token}";
$response = drupal_http_request($uri);
$json_decode = json_decode($response->data);
foreach ($json_decode->data as $response_data) {
if ($response_data->id == $page_id) {
variable_set('page_permanent_token', $response_data->access_token);
}
}
$page_permanent_token = variable_get('page_permanent_token');
$uri = "https://graph.facebook.com/v4.0/{$page_id}/feed?access_token={$page_permanent_token}&fields=permalink_url,message,created_time,picture,status_type";
$response = drupal_http_request($uri);
if ($response->status_message && $response->status_message == "OK") {
$display_time = variable_get('socialfeed_facebook_time_stamp');
$display_pic = variable_get('socialfeed_facebook_display_pic');
$display_video = variable_get('socialfeed_facebook_display_video');
$display_all_posts = variable_get('socialfeed_facebook_all_types');
$teaser_text = variable_get('socialfeed_facebook_teaser_text');
$facebook_hash_tag = variable_get('socialfeed_facebook_hashtag');
$selected_type = variable_get('socialfeed_facebook_post_type');
// @TODO: Check why this is used?
$selected_type_key = $selected_type;
$fb_type = [
'permalink_url',
'photo',
'status',
'video',
];
if (isset($selected_type_key)) {
$selected_type_value = $fb_type[$selected_type_key];
}
$i = 0;
$post_type_filter = $next_post_type_filter = [];
$json_response = drupal_json_decode($response->data);
if (isset($json_response['data']) && !empty($json_response['data'])) {
foreach ($json_response['data'] as $facebook_value) {
// If specific post type is selected.
if ($display_all_posts == 0 && isset($facebook_value['type']) && $facebook_value['type'] == $selected_type_value) {
$post_type_filter[$facebook_value['type']][] = $facebook_value;
}
elseif ($display_all_posts == 1) {
$message_feed[] = socialfeed_facebook_data($i, $facebook_value, $display_all_posts, $display_time, $display_pic, $display_video, $teaser_text, $facebook_hash_tag);
$i++;
if ($i == variable_get('socialfeed_facebook_no_feeds')) {
break;
}
}
}
}
// Showcase of array of specific post type feeds.
if (array_key_exists($selected_type_value, $post_type_filter)) {
foreach ($post_type_filter[$selected_type_value] as $facebook_entry) {
$message_feed[] = socialfeed_facebook_data($i, $facebook_entry, $display_all_posts, $display_time, $display_pic, $display_video, $teaser_text, $facebook_hash_tag);
$i++;
if ($i == variable_get('socialfeed_facebook_no_feeds')) {
break;
}
}
$existing_feeds = count($post_type_filter[$selected_type_value]);
$remaining = variable_get('socialfeed_facebook_no_feeds') - $existing_feeds;
if ($existing_feeds < variable_get('socialfeed_facebook_no_feeds')) {
$i = 0;
$pager_next = $json_response['paging']['next'];
$next_page_json = file_get_contents($pager_next);
$next_page = json_decode($next_page_json);
foreach ($next_page->data as $next_facebook_value) {
if ($display_all_posts == 0 && $next_facebook_value->type == $selected_type_value) {
$next_post_type_filter[$next_facebook_value->type][] = $next_facebook_value;
}
}
if (array_key_exists($selected_type_value, $next_post_type_filter)) {
foreach ($next_post_type_filter[$selected_type_value] as $facebook_entry) {
$message_feed[] = socialfeed_facebook_data($i, $facebook_entry, $display_all_posts, $display_time, $display_pic, $display_video, $teaser_text, $facebook_hash_tag);
$i++;
if ($i == $remaining) {
break;
}
}
}
}
}
else {
if (empty($message_feed)) {
drupal_set_message(t('No related posts'), 'error');
}
}
return $message_feed;
}
else {
drupal_set_message(t('The access_token provided is invalid.'), 'error');
}
}
else {
if (in_array('administrator', array_values($user->roles))) {
drupal_set_message(t('Please provide your Facebook credentials <a href="@facebook">here</a>.', [
'@facebook' => url('admin/config/services/socialfeed'),
]), 'warning');
}
else {
drupal_set_message(t('Please login to provide Facebook App values <a href="@facebook">here</a>.', [
'@facebook' => url('user'),
]), 'error');
}
}
}