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; }
function widget($args, $instance) { extract($args); /* Our variables from the widget settings. */ $title = apply_filters('widget_title', $instance['title']); $dt_twitter_username = $instance['username']; $dt_twitter_postcount = $instance['postcount']; $tweettext = $instance['tweettext']; $twitter_access_token = $instance['twitter_access_token']; $twitter_access_token_secret = $instance['twitter_access_token_secret']; $twitter_consumer_key = $instance['twitter_consumer_key']; $twitter_consumer_key_secret = $instance['twitter_consumer_key_secret']; /* Before widget (defined by themes). */ echo $before_widget; /* Display the widget title if one was input (before and after defined by themes). */ if ($title) { echo $before_title . $title . $after_title; } /* Display Latest Tweets */ ?> <?php //ORIONK STARTS require_once 'TwitterAPIExchange.php'; $settings = array('oauth_access_token' => $twitter_access_token, 'oauth_access_token_secret' => $twitter_access_token_secret, 'consumer_key' => $twitter_consumer_key, 'consumer_secret' => $twitter_consumer_key_secret); $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; $getfield = '?screen_name=' . $dt_twitter_username . '&count=' . $dt_twitter_postcount; $requestMethod = 'GET'; $twitter = new TwitterAPIExchange($settings); $new_twits = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(); //convert links to clickable format if (!function_exists('tp_convert_links')) { function tp_convert_links($status, $targetBlank = true, $linkMaxLen = 250) { // the target $target = $targetBlank ? " target=\"_blank\" " : ""; // convert link to url $status = preg_replace("/((http:\\/\\/|https:\\/\\/)[^ )\n ]+)/e", "'<a href=\"\$1\" style=\"font-weight:bold;\" title=\"\$1\" {$target} >'. ((strlen('\$1')>={$linkMaxLen} ? substr('\$1',0,{$linkMaxLen}).'...':'\$1')).'</a>'", $status); // convert @ to follow $status = preg_replace("/(@([_a-z0-9\\-]+))/i", "<a href=\"http://twitter.com/\$2\" style=\"font-weight:bold;\" title=\"Follow \$2\" {$target} >\$1</a>", $status); // convert # to search $status = preg_replace("/(#([_a-z0-9\\-]+))/i", "<a href=\"https://twitter.com/search?q=\$2\" style=\"font-weight:bold;\" title=\"Search \$1\" {$target} >\$1</a>", $status); // return the status return $status; } } $new_twits_json = json_decode($new_twits); /* Show error on front end when user hasn't entered twitter keys */ echo '<ul class="clearfix">'; // Check if any of the four keys are not set or empty if ($settings['oauth_access_token'] == "" || $settings['oauth_access_token_secret'] == "" || $settings['consumer_key'] == "" || $settings['consumer_secret'] == "") { // Display a message echo "You must enter your Twitter OAuth keys in the widget settings."; } else { // Display the tweets. foreach ($new_twits_json as $new_twits_json_single) { echo '<li>' . tp_convert_links($new_twits_json_single->text) . '</li>'; } } echo '</ul>'; //ORIONK ENDS ?> <?php if ($tweettext != '') { ?> <div class="visit-wrap"> <a target="_blank" href="http://twitter.com/<?php echo $dt_twitter_username; ?> " class="follow-me"><?php echo $tweettext; ?> <span class="right-arrow"></span></a> </div> <?php } ?> <?php /* After widget (defined by themes). */ echo $after_widget; }