예제 #1
0
    public function widget($args, $instance)
    {
        extract($args);
        if (!empty($instance['title'])) {
            $title = apply_filters('widget_title', $instance['title']);
        }
        echo $before_widget;
        if (!empty($title)) {
            echo $before_title . $title . $after_title;
        }
        //check settings and die if not set
        if (empty($instance['consumerkey']) || empty($instance['consumersecret']) || empty($instance['accesstoken']) || empty($instance['accesstokensecret']) || empty($instance['cachetime']) || empty($instance['username'])) {
            echo '<strong>Please fill all widget settings!</strong>' . $after_widget;
            return;
        }
        //check if cache needs update
        $tp_twitter_plugin_last_cache_time = get_option('tp_twitter_plugin_last_cache_time');
        $diff = time() - $tp_twitter_plugin_last_cache_time;
        $crt = $instance['cachetime'] * 3600;
        //	yes, it needs update
        if ($diff >= $crt || empty($tp_twitter_plugin_last_cache_time)) {
            if (!(require_once 'twitteroauth.php')) {
                echo '<strong>Couldn\'t find twitteroauth.php!</strong>' . $after_widget;
                return;
            }
            function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret)
            {
                $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
                return $connection;
            }
            $connection = getConnectionWithAccessToken($instance['consumerkey'], $instance['consumersecret'], $instance['accesstoken'], $instance['accesstokensecret']);
            $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $instance['username'] . "&count=10&exclude_replies=" . $instance['excludereplies']) or die('Couldn\'t retrieve tweets! Wrong username?');
            if (!empty($tweets->errors)) {
                if ($tweets->errors[0]->message == 'Invalid or expired token') {
                    echo '<strong>' . $tweets->errors[0]->message . '!</strong><br />You\'ll need to regenerate it <a href="https://dev.twitter.com/apps" target="_blank">here</a>!' . $after_widget;
                } else {
                    echo '<strong>' . $tweets->errors[0]->message . '</strong>' . $after_widget;
                }
                return;
            }
            $tweets_array = array();
            for ($i = 0; $i <= count($tweets); $i++) {
                if (!empty($tweets[$i])) {
                    $tweets_array[$i]['created_at'] = $tweets[$i]->created_at;
                    //clean tweet text
                    $tweets_array[$i]['text'] = preg_replace('/[\\x{10000}-\\x{10FFFF}]/u', '', $tweets[$i]->text);
                    if (!empty($tweets[$i]->id_str)) {
                        $tweets_array[$i]['status_id'] = $tweets[$i]->id_str;
                    }
                }
            }
            //save tweets to wp option
            update_option('tp_twitter_plugin_tweets', serialize($tweets_array));
            update_option('tp_twitter_plugin_last_cache_time', time());
            echo '<!-- twitter cache has been updated! -->';
        }
        $tp_twitter_plugin_tweets = maybe_unserialize(get_option('tp_twitter_plugin_tweets'));
        if (!empty($tp_twitter_plugin_tweets)) {
            print '
						<div class="tp_recent_tweets">
							<ul>';
            $fctr = '1';
            foreach ($tp_twitter_plugin_tweets as $tweet) {
                if (!empty($tweet['text'])) {
                    if (empty($tweet['status_id'])) {
                        $tweet['status_id'] = '';
                    }
                    if (empty($tweet['created_at'])) {
                        $tweet['created_at'] = '';
                    }
                    print '<li><span>' . tp_convert_links($tweet['text']) . '</span><br /><a class="twitter_time" target="_blank" href="http://twitter.com/' . $instance['username'] . '/statuses/' . $tweet['status_id'] . '">' . tp_relative_time($tweet['created_at']) . '</a></li>';
                    if ($fctr == $instance['tweetstoshow']) {
                        break;
                    }
                    $fctr++;
                }
            }
            print '
							</ul>
						</div>';
        }
        echo $after_widget;
    }
예제 #2
0
function cs_latest_twitter_render($instance)
{
    extract(shortcode_atts(array('twittertitle' => '', 'heading_size' => 'h4', 'consumerkey' => '2Jd4h7mTLRi7XHlWMpX4w', 'consumersecret' => 'M3n1cMi3HPSmpKUJNgdPFmzjlDkXIDRTf1oHZIkM', 'accesstoken' => '1406608410-6TbCsgWzjqWD2aagTslnPd4ShxbWP9ZoFyXbiEN', 'accesstokensecret' => 'bnd86DE8Rm8A93MlwnylOGlWc8dvmQHrjzQT8BaI', 'tweetstoshow' => '3', 'showavatar' => 'yes', 'username' => 'realjoomlaman'), $instance));
    $consumer_key = $consumerkey;
    $consumer_secret = $consumersecret;
    $access_token = $accesstoken;
    $access_token_secret = $accesstokensecret;
    $twitter_id = $username;
    $show_date = $showavatar;
    $count = (int) $tweetstoshow;
    echo "<{$heading_size}>{$twittertitle}</{$heading_size}>";
    if ($twitter_id && $consumer_key && $consumer_secret && $access_token && $access_token_secret && $count) {
        $transName = 'list_tweets_' . $twitter_id;
        $cacheTime = 10;
        if (false === ($twitterData = get_transient($transName))) {
            $token = get_option('cfTwitterToken_' . $twitter_id);
            // get a new token anyways
            delete_option('cfTwitterToken_' . $twitter_id);
            // getting new auth bearer only if we don't have one
            if (!$token) {
                // preparing credentials
                $credentials = $consumer_key . ':' . $consumer_secret;
                $toSend = base64_encode($credentials);
                // http post arguments
                $args = array('method' => 'POST', 'httpversion' => '1.1', 'blocking' => true, 'headers' => array('Authorization' => 'Basic ' . $toSend, 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'), 'body' => array('grant_type' => 'client_credentials'));
                add_filter('https_ssl_verify', '__return_false');
                $response = wp_remote_post('https://api.twitter.com/oauth2/token', $args);
                $keys = json_decode(wp_remote_retrieve_body($response));
                if ($keys) {
                    // saving token to wp_options table
                    $token = $keys->access_token;
                }
            }
            // we have bearer token wether we obtained it from API or from options
            $args = array('httpversion' => '1.1', 'blocking' => true, 'headers' => array('Authorization' => "Bearer {$token}"));
            add_filter('https_ssl_verify', '__return_false');
            $api_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $twitter_id . '&count=' . $count;
            $response = wp_remote_get($api_url, $args);
            set_transient($transName, wp_remote_retrieve_body($response), 60 * $cacheTime);
        }
        @($twitter = json_decode(get_transient($transName), true));
        if ($twitter && is_array($twitter)) {
            //var_dump($twitter);
            ?>
    		<div class="twitter-box">
    			<div class="twitter-holder">
    				<div class="b">
    					<div class="tweets-container">
    						<ul id="jtwt">
    							<?php 
            foreach ($twitter as $tweet) {
                ?>
    							<li class="jtwt_tweet">
    								<p class="jtwt_tweet_text">
    								<?php 
                $latestTweet = $tweet['text'];
                $latestTweet = preg_replace('/http:\\/\\/([a-z0-9_\\.\\-\\+\\&\\!\\#\\~\\/\\,]+)/i', '&nbsp;<a href="http://$1" target="_blank">http://$1</a>&nbsp;', $latestTweet);
                $latestTweet = preg_replace('/@([a-z0-9_]+)/i', '&nbsp;<a href="http://twitter.com/$1" target="_blank">@$1</a>&nbsp;', $latestTweet);
                echo $latestTweet;
                ?>
    								</p>
    								<?php 
                $twitterTime = strtotime($tweet['created_at']);
                $timeAgo = tp_relative_time($twitterTime);
                ?>
    								<?php 
                if ($show_date == 'yes') {
                    ?>
    								<a href="http://twitter.com/<?php 
                    echo $tweet['user']['screen_name'];
                    ?>
/statuses/<?php 
                    echo $tweet['id_str'];
                    ?>
" class="jtwt_date"><?php 
                    echo $timeAgo;
                    ?>
</a>
    								<?php 
                }
                ?>
    							</li>
    							<?php 
            }
            ?>
    						</ul>
    					</div>
    				</div>
    			</div>
    			<span class="arrow"></span>
    		</div>
    		<?php 
        }
    }
}