1. <?php
  2. /*
  3. Plugin Name: FBF - Facebook Page Feed Widget
  4. Description: Displays your Facebook Page feed in the sidbar of your blog.Simply add your pageID and all your visitors can see your staus!
  5. Author: Lakshmanan PHP
  6. Version: 1.2.1
  7. */
  8.  
  9. /*
  10.    Version 1.0 - update - Showing avatar, shor code, widget styles 25-aug-2012
  11.    
  12.     This program is free software; you can redistribute it and/or modify
  13.     it under the terms of the GNU General Public License version 2,
  14.     as published by the Free Software Foundation.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20. */
  21.  
  22. // Display Facebook messages
  23. function fbf_facebook_messages($options) {
  24.  
  25.     // CHECK OPTIONS
  26.     add_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', 'return 60;' )); // To reduce cache time
  27.     if ($options['pageID'] == '') {
  28.         return __('Facebook pageID not configured','fbf');
  29.     }
  30.    
  31.     if (!is_numeric($options['num']) or $options['num']<=0) {
  32.         return __('Number of status not valid','fbf');
  33.     }
  34.                  
  35.     $link_target = ($options['link_target_blank']) ? ' target="_blank" ' : ''; // Checking Link target - Open new windo/tab or in same window
  36.    
  37.     $avatar_size = ($options['avatar_size']) ? $options['avatar_size'] : 'small'; // avatar_size   
  38.    
  39.  
  40.  
  41.  // Fetching feed using wordpress // Credits http://digwp.com/2011/09/feed-display-multiple-columns/
  42.  
  43.     include_once(ABSPATH . WPINC . '/feed.php');
  44.    
  45.      $optionID = $options['pageID']; // Getting Facebook Page ID
  46.      $rss_array = explode(',',$optionID);
  47.      $feed_link_array = "";
  48.    
  49.      foreach($rss_array as $feed_link_id){ // Creating string of RSS links seperated by commas
  50.          $feed_link_array .= ',http://www.facebook.com/feeds/page.php?id='.$feed_link_id.'&format=rss20';  
  51.      }
  52.    
  53.      if($feed_link_array{0}==",")
  54.         $feed_link_array=substr($feed_link_array,1,strlen($feed_link_array));    
  55.      $feed_links = explode(",",$feed_link_array); // Array of RSS Feed links
  56.      
  57.      if(function_exists('fetch_feed')) {
  58.        
  59.           $returnMar  = '';
  60.     $returnMar .= '<!--- page ID '.$optionID.' -->';
  61.     $returnMar  .= '<div class="fbf_facebook_page_widget_container">   
  62. <ul class="fbf_facebook_page_widget">';
  63.         for ($f=0;$f< count($rss_array); $f++){
  64.  
  65.        
  66.             $feed[$f] = fetch_feed($feed_links[$f]);
  67.             if ( is_wp_error( $feed[$f] ) ) {
  68.                $error_string[$f] = $feed[$f]->get_error_message();
  69.                $err_msg='<div id="message" class="error">';
  70.                $err_msg.='<strong>Unable to fetch the feed</strong><br><span>Error :</span><ul>';
  71.                     foreach( $error_string as $err ):
  72.                     if($err=="file_get_contents could not read the file")
  73.                         $error_detail = "SimplePie could not read the feed";
  74.                     else
  75.                         $error_detail = $err;
  76.                     $err_msg.='<li>'.$error_detail.'</li>';
  77.                     endforeach;
  78.                $err_msg.='</ul>
  79.                     </div>';
  80.                echo $err_msg; // Prints the error message
  81.             }
  82.             if (!is_wp_error($feed[$f])) : $feed[$f]->init();
  83.                 $feed[$f]->set_output_encoding('UTF-8');    // set encoding
  84.                 $feed[$f]->handle_content_type();       // ensure encoding
  85.                 $feed[$f]->enable_cache(false); // no cache
  86.                 $limit = $feed[$f]->get_item_quantity($options['num']); // get  feed items
  87.                 $items  = $feed[$f]->get_items(0, $limit);  // set array
  88.             endif;
  89.    
  90.     if ($limit == 0) {
  91.         echo '<p><strong>RSS Feed currently unavailable.</strong></p>';
  92.     } else {
  93.  
  94.  
  95.    
  96.     $blocks[$f] = array_slice($items, 0, $options['num']);
  97.     foreach ($blocks[$f] as $block) {  
  98.         $returnMarkup .='<li>';
  99.         if ($options['feed_title'] == "true" ) {
  100.             $feedtitle ="<h4><a href=".$block->get_permalink()." class='facebook_page-link' ".$link_target.">".substr($block->get_title(), 0, 200)."</a></h4>"; // Title of the update
  101.         }elseif ($options['feed_title'] == "" ) {
  102.             $feedtitle = null;
  103.         }
  104.    
  105.     $returnMarkup .= html_entity_decode($feedtitle,ENT_COMPAT, 'UTF-8');   
  106.     # Like button
  107.    
  108.     $like_button = '<iframe src="http://www.facebook.com/plugins/like.php?href='.$block->get_permalink().'&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=30" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100%; height:30px;" allowTransparency="true"></iframe>';
  109.    
  110.     # Shows fb like button on top, below the feed title
  111.         if ($options['like_button'] !='' && $options['like_button'] != false) {  
  112.             if ($options['like_button_position']=="top") {
  113.                 $returnMarkup .= $like_button;
  114.                 }
  115.         }
  116.         # Shows avatar of Facebook page
  117.             if ($options['show_avatar'] != '') {
  118.             $returnMarkup .="<div class=\"facebook_page-avatar\"><img src=\"http://graph.facebook.com/".$rss_array[$f]."/picture?type=".$avatar_size."\"  alt=".$block->author." /></div>";
  119.             }
  120.        
  121.             if ($options['show_description'] != '') {  
  122.                 $desc_feed = str_replace('href="http://www.facebook.com', 'href="', $block->get_description()); // Emptying all links
  123.                 $desc = html_entity_decode(str_replace('href="', 'href="http://www.facebook.com', $desc_feed),ENT_COMPAT, 'UTF-8'); // adding facebook link - to avoid facebook redirector l.php's broken link error
  124.                 $returnMarkup .= "<div class=\"fbf_desc\">".$desc."</div>"; // Full content
  125.             }
  126.        
  127.              if ($options['update'] != '') {
  128.             $time = strtotime($block->get_date("j F Y, H:i:s"));
  129.             $tval = timesince($time);
  130.                     $h_time = ( ( abs( time() - $time) ) < 86400 ) ? sprintf( __('%s ago', 'rstw'), human_time_diff( $time )) : date(__('Y/m/d'), $time);
  131.                     $returnMarkup .= ''.sprintf( __('%s', 'rstw'),' <span class="facebook_page-timestamp"><abbr title="' .$block->get_date("j F Y, H:i:s") . '">'.timesince($time).'</abbr></span>' );
  132.              } //  Show Timestamp - if option enabled
  133.         # Shows fb like button on bottom, below the feed description   
  134.         if ($options['like_button'] !='' && $options['like_button'] != false) {    
  135.             if ($options['like_button_position']=="bottom") {
  136.                 $returnMarkup .= $like_button;
  137.                 }
  138.         }
  139.          $returnMarkup .='</li>';
  140.     } // For Loop Ends here
  141.     }
  142.     unset($feed);
  143.     } // Multiple feeds for loop
  144.     $returnMar_last  .='</ul>
  145.         </div>';
  146.     }
  147. $returnMarkup = $returnMar.$returnMarkup.$returnMar_last;
  148.     return $returnMarkup;
  149. }
  150. function timesince ($time)
  151. {
  152.     $time = time() - $time; // to get the time since that moment
  153.     $tokens = array (
  154.         31536000 => 'year',
  155.         2592000 => 'month',
  156.         604800 => 'week',
  157.         86400 => 'day',
  158.         3600 => 'hour',
  159.         60 => 'minute',
  160.         1 => 'second'
  161.     );
  162.  
  163.     foreach ($tokens as $unit => $text) {
  164.         if ($time < $unit) continue;
  165.         $numberOfUnits = floor($time / $unit);
  166.         $t = $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
  167.         return $t. " ago ";
  168.     }
  169.  
  170. }
  171.  
  172. /**
  173.  * FacebookPageFeedWidget Class
  174.  */
  175. class FacebookPageFeedWidget extends WP_Widget {
  176.     private /** @type {string} */ $languagePath;
  177.  
  178.     /** constructor */
  179.     function FacebookPageFeedWidget() {
  180.        
  181.         $this->options = array(
  182.             array(
  183.                 'name'  => 'title',
  184.                 'label' => __( 'Title', 'fbf' ),
  185.                 'type'  => 'text'
  186.             ),
  187.             array(
  188.                 'name'  => 'pageID',
  189.                 'label' => __( 'Facebook Page ID<br> (Replace your facebook page ID here)', 'fbf' ),
  190.                 'type'  => 'text'
  191.             ),
  192.             array(
  193.                 'name'  => 'num',
  194.                 'label' => __( 'Show # of Posts', 'fbf' ),
  195.                 'type'  => 'text'
  196.             ),
  197.             array(
  198.                 'name'  => 'avatar_size',
  199.                 'label' => __( 'Avatar size', 'fbf' ),
  200.                 'type'  => 'radio',
  201.                 'values' => array('square'=>'Square','small'=>'Small','normal'=>'Normal','large'=>'Large')
  202.             ),         
  203.             array(
  204.                 'name'  => 'update',
  205.                 'label' => __( 'Show timestamps', 'fbf' ),
  206.                 'type'  => 'checkbox'
  207.             ),
  208.             array(
  209.                 'name'  => 'feed_title',
  210.                 'label' => __( 'Show feed title', 'fbf' ),
  211.                 'type'  => 'checkbox'
  212.             ),
  213.             array(
  214.                 'name'  => 'show_description',
  215.                 'label' => __( 'Show Description', 'fbf' ),
  216.                 'type'  => 'checkbox'
  217.             ),
  218.             array(
  219.                 'name'  => 'show_avatar',
  220.                 'label' => __( 'Show Avatar', 'fbf' ),
  221.                 'type'  => 'checkbox'
  222.             ),
  223.            
  224.             array(
  225.                 'name'  => 'link_target_blank',
  226.                 'label' => __( 'Create links on new window / tab', 'fbf' ),
  227.                 'type'  => 'checkbox'
  228.             ),
  229.             array(
  230.                 'name'  => 'like_button',
  231.                 'label' => __( 'Show facebook like button', 'fbf' ),
  232.                 'type'  => 'checkbox'
  233.             ),
  234.             array(
  235.                 'name'  => 'like_button_position',
  236.                 'label' => __( 'Like button position', 'fbf' ),
  237.                 'type'  => 'radio',
  238.                 'values' => array('top'=>'Top','bottom'=>'Bottom')
  239.             ),
  240.            
  241.         );
  242.  
  243.         parent::WP_Widget(false, $name = 'FBF Facebook page Feed Widget'); 
  244.     }
  245.  
  246.     /** @see WP_Widget::widget */
  247.     function widget($args, $instance) {    
  248.         extract( $args );
  249.         $title = $instance['title'];
  250.         echo $before_widget;  
  251.         if ( $title ) {
  252.             echo $before_title . $instance['title'] . $after_title;
  253.         }
  254.         echo fbf_facebook_messages($instance);
  255.         echo $after_widget;
  256.     }
  257.  
  258.     /** @see WP_Widget::update */
  259.     function update($new_instance, $old_instance) {            
  260.         $instance = $old_instance;
  261.        
  262.         foreach ($this->options as $val) {
  263.             if ($val['type']=='text') {
  264.                 $instance[$val['name']] = strip_tags($new_instance[$val['name']]);
  265.             } else if ($val['type']=='checkbox') {
  266.                 $instance[$val['name']] = ($new_instance[$val['name']]=='on') ? true : false;
  267.             } else if ($val['type']=='radio') {
  268.                 $instance[$val['name']] = $new_instance[$val['name']];
  269.             }
  270.            
  271.         }
  272.         return $instance;
  273.     }
  274.  
  275.     /** @see WP_Widget::form */
  276.     function form($instance) {
  277.         $default['title']       = __( 'FBF Facebook page Feed Widget', 'fbf' );
  278.         $default['pageID']      = '133662330114199';
  279.         $default['num']         = '5';
  280.         $default['feed_title']  = true;
  281.         $default['update']      = true;
  282.         $default['show_description']    = true;
  283.         $default['show_avatar'] = true;
  284.         $default['avatar_size'] = 'small'; // Available sizes square, small, normal, or large
  285.         $default['link_target_blank']   = true;
  286.         $default['like_button'] = true;
  287.         $default['like_button_position'] = 'top';
  288.    
  289.         $instance = wp_parse_args($instance,$default);
  290.    
  291.         foreach ($this->options as $val) {
  292.             $label = '<label for="'.$this->get_field_id($val['name']).'">'.$val['label'].'</label>';
  293.             if ($val['type']=='text') {
  294.                 echo '<p>'.$label.'<br />';
  295.                 echo '<input class="widefat" id="'.$this->get_field_id($val['name']).'" name="'.$this->get_field_name($val['name']).'" type="text" value="'.esc_attr($instance[$val['name']]).'" /></p>';
  296.             } else if ($val['type']=='checkbox') {
  297.                 $checked = ($instance[$val['name']]) ? 'checked="checked"' : '';
  298.                 echo '<input id="'.$this->get_field_id($val['name']).'" name="'.$this->get_field_name($val['name']).'" type="checkbox" '.$checked.' /> '.$label.'<br />';
  299.             } else if ($val['type']=='radio') {
  300.                 echo '<p>'.$label.'<br />';
  301.                 foreach($val['values'] as $key=>$name){
  302.                     $label = '<label for="'.$this->get_field_id($val['name'].'_'.$key).'">'.$name.'</label>';
  303.                     $checked = ($instance[$val['name']] == $key) ? 'checked="checked"' : '';
  304.                     echo '<input id="'.$this->get_field_id($val['name'].'_'.$key).'" name="'.$this->get_field_name($val['name']).'" type="radio" '.$checked.' value="'.$key.'" />'.$label.'&nbsp;';
  305.                 }
  306.                 echo '<br/><br/>';
  307.             }
  308.         }
  309.     }
  310.  
  311. } // class FacebookPageFeedWidget
  312.  
  313. // register FacebookPageFeedWidget widget
  314. add_action('widgets_init', create_function('', 'return register_widget("FacebookPageFeedWidget");'));
  315.  
  316. // register stylesheet 25-aug-2012
  317. add_action('wp_head', 'fbf_add_header_css', 100);
  318. function fbf_add_header_css() {
  319.     echo '<link type="text/css" media="screen" rel="stylesheet" href="' . plugins_url('fbf-facebook-page-feed-widget/fbf_facebook_page_feed.css') . '" />' . "\n";
  320. }
  321. // Short code FacebookPageFeed 25-aug-2012 - edited 2 feb 2013
  322. function fbf_short_code($atts) {
  323.      $atts['pageID'] = $atts['pageid'];
  324.      $atts= shortcode_atts(array(
  325.             'pageID' => '133662330114199',
  326.             'num' => '5',
  327.             'update' => false,
  328.             'show_description' => false,
  329.             'show_avatar' => false,
  330.             'avatar_size' => 'small',
  331.             'link_target_blank' => false,
  332.             'feed_title' => true,
  333.             'like_button' => true,
  334.             'like_button_position' => false,
  335.      ), $atts);
  336.    
  337.      return fbf_facebook_messages($atts);
  338. }
  339. // sample short code
  340. // [fbf_page_feed pageID="133662330114199" num="2" show_description="true" update="true" show_avatar="true" avatar_size="square" link_target_blank="true" feed_title = "true" like_button="true" like_button_position="top"]
  341.  
  342. // sample short code without like button
  343. // [fbf_page_feed pageID="133662330114199" num="2" show_description="true" update="true" show_avatar="true" avatar_size="square" link_target_blank="true" feed_title = "true" ]
  344.  
  345.  
  346. add_shortcode('fbf_page_feed', 'fbf_short_code');