<?php
function serpentsoft_twitter_ticker($consumerkey='', $consumersecret='', $accesstoken='', $accesstokensecret='', $screenname='', $tweets_count=10, $avatar_size=24){

$twitter = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

//$tweets = $twitter->get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=screenName&count=10');

/* for PHP 5.3.13 */
/* $tweets = $connection->get(“https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=”.$twitteruser.”&count=”.$notweets);
I changed https to http and the “null” was replaced with my tweets. */
        
//try{
    $tweets = $twitter->get('statuses/user_timeline', array( 'screen_name' => $screenname, 'count' => $tweets_count ));
    //echo print_r($tweets);

$output = '';

if( is_array($tweets) && !isset($tweets->errors) ){
    $i = 0;
    $lnk_msg = NULL;
    
    foreach($tweets as $tweet){
        $i++;
        
        $lnk_page = 'http://twitter.com/#!/'. $screenname;
        $page_name = $tweet->user->name;

        $msg = $tweet->text;
        
        if( is_array($tweet->entities->urls) ){
            try{
                if( array_key_exists('0', $tweet->entities->urls) ){
                    $lnk_msg = $tweet->entities->urls[0]->url;
                }
                else { $lnk_msg = NULL; }
            }
            catch (Exception $e) { $lnk_msg = NULL; }
        }
        
        $lnk_tweet = 'http://twitter.com/#!/'. $screenname .'/status/'. $tweet->id_str;

        if($i == 1) { $output .= '<li class="tweet_first tweet_odd">'; } elseif($i % 2 == 0) { $output .= '<li class="tweet_even">'; } elseif($i % 2 != 0) { $output .= '<li class="tweet_odd">'; }

        /* Avatar */
        $avatar = is_ssl() ? $tweet->user->profile_image_url_https : $tweet->user->profile_image_url;
        $output .= '<a class="tweet_avatar" href="'. $lnk_page .'"><img src="' . $avatar . '" height="'. $avatar_size .'" width="'. $avatar_size .'"
            alt="'. $screenname .'" title="'. $page_name .'" border="0" /></a>';

        /* Tweet Text */
        $output .= '<span class="tweet_text">';
        if( !is_null($lnk_msg) ) {
            $output .= sprintf('<a href="%1$s">%2$s</a>', $lnk_msg, $msg);
            //$output .= '<a href="' . $lnk_msg . '">' . $msg . '</a>';
        } else { $output .= $msg; }
        
        $output .= '</span>';

        /* Tweet Time */
        $time = strtotime($tweet->created_at);
        $delta = abs( time() - $time); /* in seconds */
        $result = '';
        if ($delta < 1) {
                $result = ' just now';
        } elseif ($delta < 60) {
                $result = $delta . ' seconds ago';
        } elseif($delta < 120) {
                $result = ' about a minute ago';
        } elseif($delta < (45*60)) {
                $result = ' about ' . round( ($delta/60), 0) . ' minutes ago';
        } elseif($delta < (2*60*60)) {
                $result = ' about an hour ago';
        } elseif($delta < (24*60*60)) {
                $result = ' about ' . round( ($delta/3600), 0) . ' hours ago';
        } elseif($delta < (48*60*60)) {
                $result = ' about a day ago';
        } else{
                $result = ' about ' . round( ($delta / 86400), 0) . ' days ago';
        }

        $output .= '<span class="tweet_time" title="' . date(__('d/m/Y H:i:s', 'serpentsoft'), $time) . '">' . $result . '</span>';

        $output .= '</li>';

        if ( $i >= $tweets_count ) break;
    } /* foreach */

    if( !empty($output) ) { echo $output; }
    
}else{
    if( isset($tweets->errors) ):
        $output .= '<span class="tweet_error">Message: '. $tweets->errors[0]->message .', Please check your Twitter Authentication Data or internet connection.</span>';
    else:
        $output .= '<span class="tweet_error">Please check your internet connection.</span>';
    endif;
    
    if( !empty($output) ) { echo $output; }
}

} ?>