Skip to content

Instantly share code, notes, and snippets.

@PostCucumber
Last active September 3, 2018 13:58
Show Gist options
  • Save PostCucumber/436a4bb7e03e574849d462866ddd0f3c to your computer and use it in GitHub Desktop.
Save PostCucumber/436a4bb7e03e574849d462866ddd0f3c to your computer and use it in GitHub Desktop.
Facebook module for Wordpress
<?php
use GuzzleHttp\Client;
class FacebookFeed
{
/**
* @param int $limit
* @return array
*/
public function fetch($limit = 5)
{
$client = new Client([
'base_uri' => 'https://graph.facebook.com/v2.9'
]);
$page_id = FACEBOOK_PAGE_ID;
$access_token = FACEBOOK_ACCESS_TOKEN;
$fields = 'id,message,link,name,caption,description,created_time,updated_time,picture,object_id,type';
$response = $client->request('GET', '/' . $page_id . '/posts/?fields=' . $fields . '&limit=' . $limit . '&access_token=' . $access_token);
$feed = json_decode($response->getBody());
return $feed;
}
public function photo($fbpost)
{
$client = new Client([
'base_uri' => 'https://graph.facebook.com/v2.9'
]);
$access_token = FACEBOOK_ACCESS_TOKEN;
if ($fbpost->type == 'link') {
$response = $client->request('GET', '/?id=' . $fbpost->link . '&access_token=' . $access_token);
$returned = json_decode($response->getBody());
if(! isset($returned->og_object->id)){
$fbpost->type = 'foo'; //no og_object, so change type to skip this conditional
return $this->photo($fbpost);
}
$og_id = $returned->og_object->id;
$response = $client->request('GET', '/' . $og_id . '/?fields=image&access_token=' . $access_token);
$returned = json_decode($response->getBody());
$photo_url = $returned->image[0]->url;
}
else {
$response = $client->request('GET', '/' . $fbpost->id . '/?fields=object_id&access_token=' . $access_token);
$returned = json_decode($response->getBody());
if ($fbpost->type == 'video') {
$photo_url = $fbpost->picture;
}
else {
$object_id = $returned->object_id;
$photo_url = 'https://graph.facebook.com/v2.9/' . $object_id . '/picture?access_token=' . $access_token;
}
}
return $photo_url;
}
}
<?php
/**
* @package GCRP
*/
require('facebook/FacebookFeed.php');
get_header(); ?>
<?php get_template_part('parts/banner'); ?>
<div id="content" class="section page-content">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php if (have_posts()) :
if (is_home() && !is_front_page()) : ?>
<?php endif; ?>
<div class="blog-container">
<div class="container wide">
<div class="row">
<?php
$feed = new FacebookFeed();
$results = $feed->fetch(9);
$now = time();
foreach ($results->data as $result) {
if (strlen($result->description) > 0)
{
$trimmed = wp_trim_words($result->description, $num_words = 26, '...');
} else {
$trimmed = 'From the walk...';
}
$photo_url = $feed->photo($result);
?>
<div class="col-sm-6 col-lg-4 text-center">
<div class="blog-article">
<div class="blog-image">
<div class="embed-responsive embed-responsive-16by9">
<?php if($result->type != 'video') { ?>
<a href="<?php echo $result->link; ?>" target="_blank" ><img
src="<?php echo $photo_url; ?>"
alt="<?php echo $result->caption; ?>"
class="embed-responsive-item img-fluid border-bottom"></a>
<?php } else { ?>
<a href="<?php echo $result->link; ?>" target="_blank" >
<iframe
src="<?php echo 'https://www.facebook.com/plugins/video.php?href='.$result->link ?>"
style="border:none;overflow:hidden"
scrolling="no" frameborder="0" allowTransparency="true" allowFullScreen="true" class="embed-responsive-item img-fluid border-bottom">
</iframe>
</a>
<?php } ?>
</div>
</div>
<header class="blog-header">
<div class="entry-meta">
<p class="time-posted">
posted <?php echo human_time_diff($now, strtotime($result->created_time)); ?>
ago</p>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<p class="fbblog-msg" style="margin:0;padding:10px"><?php echo $trimmed; ?></p>
</div>
<div class="blog-link">
<a class="btn btn-primary-custom btn-readmore" href="<?php echo $result->link; ?>" target="_blank" >Read more</a>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
<?php else :
get_template_part('template-parts/content', 'none');
endif; ?>
</main><!-- #main -->
</div>
<?php
get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment