MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: obrechts (544)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: /home2/obrechts/bad.public_html.bad/wordpress/wp-content/themes/response/elements/twitter-bar.php
<?php
/**
 * Title: Twitter-bar Element
 *
 * Description: Displays latest tweets of supplied twitter handle.
 *
 * Please do not edit this file. This file is part of the Cyber Chimps Framework and all modifications
 * should be made in a child theme.
 *
 * @category Cyber Chimps Framework
 * @package  Framework
 * @since    1.0
 * @author   CyberChimps
 * @license  http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
 * @link     http://www.cyberchimps.com/
 */

// Don't load directly
if ( !defined('ABSPATH') ) { die('-1'); }

if ( !class_exists( 'CyberChimpsTwitterBar' ) ) {
	class CyberChimpsTwitterBar {
		
		protected static $instance;
		public $options;
		
		/* Static Singleton Factory Method */
		public static function instance() {
			if (!isset(self::$instance)) {
				$className = __CLASS__;
				self::$instance = new $className;
			}
			return self::$instance;
		}	
		
		/**
		 * Initializes plugin variables and sets up WordPress hooks/actions.
		 *
		 * @return void
		 */
		protected function __construct( ) {
			add_action( 'twitterbar_section', array( $this, 'render_display' ) );
			$this->options = get_option( 'cyberchimps_options' );
		}
		
		// Defines mark up for the twitter bar
		public function render_display() {
			global $post;
			
			// Set directory uri
			$directory_uri = get_template_directory_uri();
	
			if( is_page() ) {
				$user_details = array();
				$user_details['screen_name'] = (get_post_meta($post->ID, 'cyberchimps_twitter_handle', true)) ? get_post_meta($post->ID, 'cyberchimps_twitter_handle', true) : apply_filters( 'cyberchimps_twitter_handle_filter', 'CyberChimps' );
				$user_details['count'] = '1';
				$user_details['published_when'] = '1';
				$user_details['exclude_replies'] =  '1';
			}
			else {
				$user_details = array();
				$user_details['screen_name'] = ( $this->options['twitter_handle'] != '' ) ? $this->options['twitter_handle'] : apply_filters( 'cyberchimps_twitter_handle_filter', 'CyberChimps' );
				$user_details['count'] = '1';
				$user_details['published_when'] = '1';
				$user_details['exclude_replies'] = '1';				
			}
			$latest_tweet = self::cyberchimps_get_tweets( $user_details );
			
			if ( is_wp_error( $latest_tweet ) ) {
				echo $latest_tweet->get_error_code() . ' - ' . $latest_tweet->get_error_message();
			} else {
			?>
			<div id="twitter-container" class="row-fluid">
      	<div id="twitter-bar" class="span12">
					<div id="twitter-text">
						<?php
            if ( $latest_tweet ) {
							// get the tweet text
							$tweet_text = $latest_tweet[0]->text;
							// look for a twitter shortened url and turn it into a link
							$tweet_text = preg_replace("/[^^](http:\/\/+[\S]*)/", '<a href="$0">$0</a>', $tweet_text);
              $screen_name = $latest_tweet[0]->user->screen_name;
              $user_permalink = 'http://twitter.com/#!/'.$screen_name;
              $tweet_permalink = 'http://twitter.com/#!/'.$screen_name.'/status/'.$latest_tweet[0]->id_str;
              echo '<img src="' . $directory_uri . '/elements/lib/images/twitter/twitterbird.png" /> ';
              echo '<p><a href="'.esc_url( $user_permalink ).'"> ';
              echo esc_html( $screen_name ) .'</a> - '.wp_kses( $tweet_text, array( 'a' => array( 'href' => array() ) ) ).' <small><a href="'.esc_url( $tweet_permalink ).'">' .human_time_diff(strtotime( esc_html( $latest_tweet[0]->created_at ) ), current_time( 'timestamp' ) ).' ago</a></small></p>';
            } else {
              echo '<img src="' . $directory_uri . '/elements/lib/images/twitter/twitterbird.png" /> ';
              echo wp_kses( apply_filters( 'cyberchimps_tweets_empty_message', '<p>'.__('No tweets to display', 'cyberchimps' ).'</p>' ), array( 'p' => array() ) );
            }
            ?>
					</div><!-- #twitter-text .span12 -->
        </div><!-- #twitter-bar -->
			</div><!-- .row-fluid -->
			<?php
			}
		}
		
		// Get the latest tweets
		function cyberchimps_get_tweets( $args ) {
			$args['screen_name'] = '@'.$args['screen_name'];
			$request_url = 'https://api.twitter.com/1/statuses/user_timeline.json';
			$request_url = add_query_arg($args,$request_url);
		
			// Generate key
			$key = 'cyberchimps_twitter_'.md5($request_url);
		
			// expires every hour
			$expiration = 60*60;
		
			$transient = get_transient( $key );
			if ( false === $transient ) {
				// Hard expiration
				$data = self::retrieve_remote_tweets( $request_url );
		
				if ( !is_wp_error($data) ) {
					// Update transient
					self::set_twitter_transient($key, $data, $expiration);
				}
				return $data;
		
			} else {
				// Soft expiration. $transient = array( expiration time, data)
				if ( $transient[0] !== 0 && $transient[0] <= time() ) {
		
					// Expiration time passed, attempt to get new data
					$new_data = self::retrieve_remote_tweets( $request_url  );
		
					if ( !is_wp_error($new_data) ) {
						// If successful return update transient and new data
						self::set_twitter_transient($key, $new_data,  $expiration);
						$transient[1] = $new_data;
					}
				}
				return $transient[1];
			}
		}
		
		// Get new tweets from remote
		protected function retrieve_remote_tweets( $request_url ) {
			$raw_response = wp_remote_get( $request_url, array( 'sslverify' => false, 'timeout' => 1 ) );
		
			if ( is_wp_error( $raw_response ) )
				return $raw_response;
		
			$code = (int) wp_remote_retrieve_response_code($raw_response);
			$response = json_decode( wp_remote_retrieve_body($raw_response) );
			
			switch( $code ):
				case 200:
					return $response;
				case 304:
				case 400:
				case 401:
				case 403:
				case 404:
				case 406:
				case 420:
				case 500:
				case 502:
				case 503:
				case 504:
					return new WP_Error($code, $response->error);
		
				default:
					return new WP_Error($code, __('Invalid Response','cyberchimps') );
			endswitch;
		}
		
		// Set the transient for twitter with new retrived tweets
		protected function set_twitter_transient( $key, $data, $expiration ) {
			// Time when transient expires
			$expire = time() + $expiration;
			set_transient( $key, array( $expire, $data ) );
		}
	}
}
CyberChimpsTwitterBar::instance();