function multi_twitter($widget) { if (!class_exists('Codebird')) { require 'lib/codebird.php'; } // Initialize Codebird with our keys. We'll wait and // pass the token when we make an actual request Codebird::setConsumerKey($widget['consumer_key'], $widget['consumer_secret']); $cb = Codebird::getInstance(); $output = ''; // Get our root upload directory and create cache if necessary $upload = wp_upload_dir(); $upload_dir = $upload['basedir'] . "/cache"; if (!file_exists($upload_dir)) { if (!mkdir($upload_dir)) { $output .= '<span style="color: red;">' . sprintf(__('Could not create dir "%s"; please create this directory.', 'multi-twitter-widget'), $upload_dir) . '</span>'; return $output; } } // split the accounts and search terms specified in the widget $accounts = explode(" ", $widget['users']); $terms = explode(", ", $widget['terms']); $output .= '<ul class="multi-twitter">'; // Parse the accounts and CRUD cache $feeds = null; foreach ($accounts as $account) { if ($account != "") { $cache = false; // Assume the cache is empty $cFile = "{$upload_dir}/users_{$account}.txt"; if (file_exists($cFile)) { $modtime = filemtime($cFile); $timeago = time() - 1800; // 30 minutes ago // Check if length is less than new limit $str = file_get_contents($cFile); $content = unserialize($str); $length = count($content); if ($modtime < $timeago or $length != $widget['user_limit']) { // Set to false just in case as the cache needs to be renewed $cache = false; } else { // The cache is not too old so the cache can be used. $cache = true; } } // begin if ($cache === false) { $cb->setToken($widget['access_token'], $widget['access_token_secret']); $params = array('screen_name' => $account, 'count' => $widget['user_limit']); // let Codebird make an authenticated request Result is json $reply = $cb->statuses_userTimeline($params); // turn the json into an array $json = json_decode($reply, true); $length = count($json); for ($i = 0; $i < $length; $i++) { // add it to the feeds array $feeds[] = $json[$i]; // prepare it for caching $content[] = $json[$i]; } // Let's save our data into uploads/cache/ $fp = fopen($cFile, 'w'); if (!$fp) { $output .= '<li style="color: red;">' . sprintf(__('Permission to write cache dir to <em>%s</em> not granted.', 'multi-twitter-widget'), $cFile) . '</li>'; } else { $str = serialize($content); fwrite($fp, $str); } fclose($fp); $content = null; } else { //cache is true let's load the data from the cached file $str = file_get_contents($cFile); $content = unserialize($str); $length = count($content); // echo $length; for ($i = 0; $i < $length; $i++) { // add it to the feeds array $feeds[] = $content[$i]; } } } // end account empty check } // end accounts foreach // Parse the terms and CRUD cache foreach ($terms as $term) { if ($term != "") { $cache = false; // Assume the cache is empty $cFile = "{$upload_dir}/term_{$term}.txt"; if (file_exists($cFile)) { $modtime = filemtime($cFile); $timeago = time() - 1800; // 30 minutes ago // Check if length is less than new limit $str = file_get_contents($cFile); $content = unserialize($str); $length = count($content); if ($modtime < $timeago or $length != $widget['term_limit']) { // Set to false just in case as the cache needs to be renewed $cache = false; } else { // The cache is not too old so the cache can be used. $cache = true; } } if ($cache === false) { $cb->setToken($widget['access_token'], $widget['access_token_secret']); $search_params = array('q' => $term, 'count' => $widget['term_limit']); $reply = $cb->search_tweets($search_params); $json = json_decode($reply, true); $length = count($json['statuses']); for ($i = 0; $i < $length; $i++) { // add it to the feeds array $feeds[] = $json['statuses'][$i]; // prepare it for caching $content[] = $json['statuses'][$i]; } if ($content === false) { // Content couldn't be retrieved... Do something.. $output .= '<li>' . __('Content could not be retrieved; Twitter API failed.', 'multi-twitter-widget') . '</li>'; } else { // Let's save our data into uploads/cache/ $fp = fopen($cFile, 'w'); if (!$fp) { $output .= '<li style="color: red;">' . sprintf(__('Permission to write cache dir to <em>%s</em> not granted.', 'multi-twitter-widget'), $cFile) . '</li>'; } else { fwrite($fp, serialize($content)); } fclose($fp); $content = null; } } else { //cache is true let's load the data from the cached file $str = file_get_contents($cFile); $content = unserialize($str); $length = count($content); for ($i = 0; $i < $length; $i++) { // add it to the feeds array $feeds[] = $content[$i]; } } } // end terms empty check } // end terms foreach // Sort our $feeds array if ($widget['sort_by_date']) { usort($feeds, "feed_sort"); } // Split array and output results $i = 1; // format the tweet for display foreach ($feeds as $feed) { if (!empty($feed)) { $output .= '<li class="tweet clearfix" style="margin-bottom:8px;">' . '<a href="http://twitter.com/' . $feed['user']['screen_name'] . '">' . '<img class="twitter-avatar" src="' . $feed['user']['profile_image_url'] . '" width="40" height="40" alt="' . $feed['user']['screen_name'] . '" />' . '</a>'; $output .= '<span class="tweet-userName">' . $feed['user']['name'] . '</span>'; if ($widget['date']) { $output .= '<span class="tweet-time">' . human_time(strtotime($feed['created_at'])) . '</span>'; } $output .= '<span class="tweet-message">' . format_tweet($feed['text'], $widget) . '</span>'; $output .= '</li>'; } } $output .= '</ul>'; if ($widget['credits'] === true) { $output .= '<hr /><strong>' . __('Development by', 'multi-twitter-widget') . '</strong> ' . '<a href="http://twitter.com/thinkclay" target="_blank">Clay McIlrath</a>, ' . '<a href="http://twitter.com/roger_hamilton" target="_blank">Roger Hamilton</a>, ' . '<a href="http://twitter.com/wrought/" target="_blank">Matt Senate</a>, and ' . '<a href="http://twitter.com/cviebrock/" target="_blank">Colin Viebrock</a>.'; } if ($widget['styles'] === true) { $output .= '<style type="text/css">' . '.twitter-avatar { clear: both; float: left; padding: 6px 12px 2px 0; }' . '.twitter{background:none;}' . '.tweet{min-height:48px;margin:0!important;}' . '.tweet a{text-decoration:underline;}' . '.tweet-userName{padding-top:7px;font-size:12px;line-height:0;color:#454545;font-family:Arial,sans-serif;font-weight:700;margin-bottom:10px;margin-left:8px;float:left;min-width:50px;}' . '.twitter-avatar{width:48px;height:48px;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px;padding:0!important;}' . '.tweet-time{color:#8A8A8A;float:left;margin-top:-3px;font-size:11px!important;}' . '.tweet-message{font-size:11px;line-height:14px;color:#333;font-family:Arial,sans-serif;word-wrap:break-word;margin-top:-30px!important;width:200px;margin-left:58px;}' . '</style>'; } echo $output; }
function get_latest_tweets_html($attributes) { $data = extract(shortcode_atts(array('username' => null, 'count' => 1), $attributes)); $count = intval($count); if ($count < 1 or $count > 100) { return "Numbers of tweets must be between 1 and 100."; } if (!$username) { return "Please specify a twitter username"; } $json = get_json($username, $count); $tweetData = json_decode($json, true); $content = ""; foreach ($tweetData as $index => $tweet) { if ($index == $count) { break; } $content .= ' <div class="clearfix">'; $content .= ' <div class="tw-feed">'; $content .= ' <p>' . format_tweet($tweet["text"]) . '</p>'; $content .= ' </div>'; $content .= ' <div class="tw-feed-author">'; $content .= ' <a target="_blank" href="https://twitter.com/' . $username . '" title="@' . $username . '">@' . $username . '</a><span>, ' . time_ago($tweet["created_at"]) . '</span>'; $content .= ' </div>'; $content .= ' </div>'; } return $content; }