Advertisement
Guest User

image-src-replacement

a guest
Mar 19th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.67 KB | None | 0 0
  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.             $feed = fetch_feed($feed_links);
  59.             if ( is_wp_error( $feed ) ) {
  60.                $error_string = $feed->get_error_message();
  61.                $err_msg='<div id="message" class="error">';
  62.                $err_msg.='<strong>Unable to fetch the feed</strong><br><span>Error :</span><ul>';
  63.                     foreach( $error_string as $err ):
  64.                     if($err=="file_get_contents could not read the file")
  65.                         $error_detail = "SimplePie could not read the feed";
  66.                     else
  67.                         $error_detail = $err;
  68.                     $err_msg.='<li>'.$error_detail.'</li>';
  69.                     endforeach;
  70.                $err_msg.='</ul>
  71.                     </div>';
  72.                echo $err_msg; // Prints the error message
  73.             }
  74.             if (!is_wp_error($feed)) : $feed->init();
  75.                 $feed->set_output_encoding('UTF-8');    // set encoding
  76.                 $feed->handle_content_type();       // ensure encoding
  77.                 $feed->enable_cache(false); // no cache
  78.                 $limit = $feed->get_item_quantity($options['num']); // get  feed items
  79.                 $items = $feed->get_items(0, $limit);   // set array
  80.             endif;
  81.     }
  82.     if ($limit == 0) {
  83.         echo '<p><strong>RSS Feed currently unavailable.</strong></p>';
  84.     } else {
  85.  
  86.     $returnMarkup = '';
  87.     $returnMarkup .= '<div class="fbf_facebook_page_widget_container"> 
  88. <ul class="fbf_facebook_page_widget">';
  89.    
  90.     $blocks = array_slice($items, 0, $options['num']);
  91.     foreach ($blocks as $block) {  
  92.         $returnMarkup .='<li>';
  93.         if ($options['feed_title'] == "true" ) {
  94.             $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
  95.         }elseif ($options['feed_title'] == "" ) {
  96.             $feedtitle = null;
  97.         }
  98.    
  99.     $returnMarkup .= html_entity_decode($feedtitle, ENT_COMPAT, 'UTF-8');  
  100.     if ($options['like_button'] != '' && $options['like_button_position']=="top") {  
  101.         $returnMarkup .='<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>';
  102.         }
  103.         # Shows avatar of Facebook page
  104.             if ($options['show_avatar'] != '') {
  105.             $returnMarkup .="<div class=\"facebook_page-avatar\"><img src=\"http://graph.facebook.com/".$options['pageID']."/picture?type=".$avatar_size."\"  alt=".$block->author." /></div>";
  106.             }
  107.        
  108.             if ($options['show_description'] != '') {  
  109.                 $desc_feed = str_replace('href="http://www.facebook.com', 'href="', $block->get_description()); // Emptying all links
  110.                 $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
  111.  
  112.                 // Replace the Image-References
  113.                 if($options['replace_img_urls']) {
  114.                     $desc = preg_replace("/img\s+class=\"img\"\s+src\=\"http(?:s|)\:\/\/.*\/hphotos\-ak\-(.*)\/(.*)\_s\.jpg\"/i",
  115.                         'img class="img replaced" src="https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-$1/s320x320/$2_n.jpg"',
  116.                         $desc);
  117.                 }
  118.  
  119.                 $returnMarkup .= "<div class=\"fbf_desc\">".$desc."</div>"; // Full content
  120.             }
  121.        
  122.              if ($options['update'] != '') {
  123.             $time = strtotime($block->get_date("j F Y, H:i:s"));
  124.             $tval = timesince($time);
  125.                     $h_time = ( ( abs( time() - $time) ) < 86400 ) ? sprintf( __('%s ago', 'rstw'), human_time_diff( $time )) : date(__('Y/m/d'), $time);
  126.                     $returnMarkup .= ''.sprintf( __('%s', 'rstw'),' <span class="facebook_page-timestamp"><abbr title="' .$block->get_date("j F Y, H:i:s") . '">'.timesince($time).'</abbr></span>' );
  127.              } //  Show Timestamp - if option enabled
  128.         if ($options['like_button'] != '' && $options['like_button_position']=="bottom") {  
  129.         $returnMarkup .='<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=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100%; height:80px;" allowTransparency="true"></iframe>';
  130.         }
  131.          $returnMarkup .='</li>';
  132.     } // For Loop Ends here
  133.    
  134.     $returnMarkup .='</ul>
  135.         </div>';
  136.     }
  137.  
  138.     return $returnMarkup;
  139. }
  140. function timesince ($time)
  141. {
  142.     $time = time() - $time; // to get the time since that moment
  143.     $tokens = array (
  144.         31536000 => 'year',
  145.         2592000 => 'month',
  146.         604800 => 'week',
  147.         86400 => 'day',
  148.         3600 => 'hour',
  149.         60 => 'minute',
  150.         1 => 'second'
  151.     );
  152.  
  153.     foreach ($tokens as $unit => $text) {
  154.         if ($time < $unit) continue;
  155.         $numberOfUnits = floor($time / $unit);
  156.         $t = $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
  157.         return $t. " ago ";
  158.     }
  159.  
  160. }
  161.  
  162. /**
  163.  * FacebookPageFeedWidget Class
  164.  */
  165. class FacebookPageFeedWidget extends WP_Widget {
  166.     private /** @type {string} */ $languagePath;
  167.  
  168.     /** constructor */
  169.     function FacebookPageFeedWidget() {
  170.        
  171.         $this->options = array(
  172.             array(
  173.                 'name'  => 'title',
  174.                 'label' => __( 'Title', 'fbf' ),
  175.                 'type'  => 'text'
  176.             ),
  177.             array(
  178.                 'name'  => 'pageID',
  179.                 'label' => __( 'Facebook Page ID<br> (Replace your facebook page ID here)', 'fbf' ),
  180.                 'type'  => 'text'
  181.             ),
  182.             array(
  183.                 'name'  => 'num',
  184.                 'label' => __( 'Show # of Posts', 'fbf' ),
  185.                 'type'  => 'text'
  186.             ),
  187.             array(
  188.                 'name'  => 'avatar_size',
  189.                 'label' => __( 'Avatar size', 'fbf' ),
  190.                 'type'  => 'radio',
  191.                 'values' => array('square'=>'Square','small'=>'Small','normal'=>'Normal','large'=>'Large')
  192.             ),         
  193.             array(
  194.                 'name'  => 'update',
  195.                 'label' => __( 'Show timestamps', 'fbf' ),
  196.                 'type'  => 'checkbox'
  197.             ),
  198.             array(
  199.                 'name'  => 'feed_title',
  200.                 'label' => __( 'Show feed title', 'fbf' ),
  201.                 'type'  => 'checkbox'
  202.             ),
  203.             array(
  204.                 'name'  => 'show_description',
  205.                 'label' => __( 'Show Description', 'fbf' ),
  206.                 'type'  => 'checkbox'
  207.             ),
  208.             array(
  209.                 'name'  => 'show_avatar',
  210.                 'label' => __( 'Show Avatar', 'fbf' ),
  211.                 'type'  => 'checkbox'
  212.             ),
  213.            
  214.             array(
  215.                 'name'  => 'link_target_blank',
  216.                 'label' => __( 'Create links on new window / tab', 'fbf' ),
  217.                 'type'  => 'checkbox'
  218.             ),
  219.             array(
  220.                 'name'  => 'replace_img_urls',
  221.                 'label' => __( 'Try to replace img-URLs (beta)', 'fbf' ),
  222.                 'type'  => 'checkbox'
  223.             ),
  224.             array(
  225.                 'name'  => 'like_button',
  226.                 'label' => __( 'Show facebook like button', 'fbf' ),
  227.                 'type'  => 'checkbox'
  228.             ),
  229.             array(
  230.                 'name'  => 'like_button_position',
  231.                 'label' => __( 'Like button position', 'fbf' ),
  232.                 'type'  => 'radio',
  233.                 'values' => array('top'=>'Top','bottom'=>'Bottom')
  234.             ),
  235.            
  236.         );
  237.  
  238.         parent::WP_Widget(false, $name = 'FBF Facebook page Feed Widget'); 
  239.     }
  240.  
  241.     /** @see WP_Widget::widget */
  242.     function widget($args, $instance) {    
  243.         extract( $args );
  244.         $title = $instance['title'];
  245.         echo $before_widget;  
  246.         if ( $title ) {
  247.             echo $before_title . $instance['title'] . $after_title;
  248.         }
  249.         echo fbf_facebook_messages($instance);
  250.         echo $after_widget;
  251.     }
  252.  
  253.     /** @see WP_Widget::update */
  254.     function update($new_instance, $old_instance) {            
  255.         $instance = $old_instance;
  256.        
  257.         foreach ($this->options as $val) {
  258.             if ($val['type']=='text') {
  259.                 $instance[$val['name']] = strip_tags($new_instance[$val['name']]);
  260.             } else if ($val['type']=='checkbox') {
  261.                 $instance[$val['name']] = ($new_instance[$val['name']]=='on') ? true : false;
  262.             } else if ($val['type']=='radio') {
  263.                 $instance[$val['name']] = $new_instance[$val['name']];
  264.             }
  265.            
  266.         }
  267.         return $instance;
  268.     }
  269.  
  270.     /** @see WP_Widget::form */
  271.     function form($instance) {
  272.         $default['title']       = __( 'FBF Facebook page Feed Widget', 'fbf' );
  273.         $default['pageID']      = '133662330114199';
  274.         $default['num']         = '5';
  275.         $default['feed_title']  = true;
  276.         $default['update']      = true;
  277.         $default['show_description']    = true;
  278.         $default['show_avatar'] = true;
  279.         $default['avatar_size'] = 'small'; // Available sizes square, small, normal, or large
  280.         $default['link_target_blank']   = true;
  281.         $default['like_button'] = true;
  282.         $default['like_button_position'] = 'top';
  283.    
  284.         $instance = wp_parse_args($instance,$default);
  285.    
  286.         foreach ($this->options as $val) {
  287.             $label = '<label for="'.$this->get_field_id($val['name']).'">'.$val['label'].'</label>';
  288.             if ($val['type']=='text') {
  289.                 echo '<p>'.$label.'<br />';
  290.                 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>';
  291.             } else if ($val['type']=='checkbox') {
  292.                 $checked = ($instance[$val['name']]) ? 'checked="checked"' : '';
  293.                 echo '<input id="'.$this->get_field_id($val['name']).'" name="'.$this->get_field_name($val['name']).'" type="checkbox" '.$checked.' /> '.$label.'<br />';
  294.             } else if ($val['type']=='radio') {
  295.                 echo '<p>'.$label.'<br />';
  296.                 foreach($val['values'] as $key=>$name){
  297.                     $label = '<label for="'.$this->get_field_id($val['name'].'_'.$key).'">'.$name.'</label>';
  298.                     $checked = ($instance[$val['name']] == $key) ? 'checked="checked"' : '';
  299.                     echo '<input id="'.$this->get_field_id($val['name'].'_'.$key).'" name="'.$this->get_field_name($val['name']).'" type="radio" '.$checked.' value="'.$key.'" />'.$label.'&nbsp;';
  300.                 }
  301.                 echo '<br/><br/>';
  302.             }
  303.         }
  304.     }
  305.  
  306. } // class FacebookPageFeedWidget
  307.  
  308. // register FacebookPageFeedWidget widget
  309. add_action('widgets_init', create_function('', 'return register_widget("FacebookPageFeedWidget");'));
  310.  
  311. // register stylesheet 25-aug-2012
  312. add_action('wp_head', 'fbf_add_header_css', 100);
  313. function fbf_add_header_css() {
  314.     echo '<link type="text/css" media="screen" rel="stylesheet" href="' . plugins_url('fbf-facebook-page-feed-widget/fbf_facebook_page_feed.css') . '" />' . "\n";
  315. }
  316. // Short code FacebookPageFeed 25-aug-2012 - edited 2 feb 2013
  317. function fbf_short_code($atts) {
  318.      $atts['pageID'] = $atts['pageid'];
  319.      $atts= shortcode_atts(array(
  320.             'pageID' => '133662330114199',
  321.             'num' => '5',
  322.             'update' => false,
  323.             'show_description' => false,
  324.             'show_avatar' => false,
  325.             'avatar_size' => 'small',
  326.             'link_target_blank' => false,
  327.             'feed_title' => true,
  328.             'like_button' => true,
  329.             'like_button_position' => true,
  330.      ), $atts);
  331.    
  332.      return fbf_facebook_messages($atts);
  333. }
  334. // sample short code
  335. // [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"]
  336.  
  337. add_shortcode('fbf_page_feed', 'fbf_short_code');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement