prefix . "tweet_chopper"; if($wpdb->get_var("show tables like '$table_name'") != $table_name) { $sql = "CREATE TABLE " . $table_name . " ( id mediumint(9) NOT NULL AUTO_INCREMENT, user VARCHAR(55) NOT NULL, tweet VARCHAR(255) NOT NULL, userImage VARCHAR(255) NOT NULL, tweetDate datetime NOT NULL, UNIQUE KEY id (id) );"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } add_option("tweet_chopper_ver", '1.0'); add_option("tweet_chopper_updated", ''); add_option("tweet_chopper_tags", ''); add_option("tweet_chopper_limit", '3'); } register_activation_hook(__FILE__,'tweet_chopper_install'); // register settings page add_action('admin_menu', 'tweet_chopper_admin_menu'); function tweet_chopper_admin_menu(){ add_options_page('Tweet Chopper Settings', 'Tweet Chopper', 8, __FILE__, 'tweet_chopper_admin_page'); } // register css add_action('wp_head', 'tweet_chopper_css'); function tweet_chopper_css() { echo ''."\n"; } // register widget add_action("widgets_init", "tweet_chopper_load_widget"); function tweet_chopper_load_widget(){ register_sidebar_widget("Tweet Chopper", "tweet_chopper_widget"); } function tweet_chopper_widget($args = array()) { global $wpdb; $table_name = $wpdb->prefix . "tweet_chopper"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); $last_update = strtotime(get_option('tweet_chopper_updated')); $one_minute_ago = mktime(date("H"), date("i"), date("s")-30, date("m"), date("d"), date("Y")); if($last_update <= $one_minute_ago){ // get latest tweets $tags = str_replace("@", "from:", get_option('tweet_chopper_tags')); $tags = str_replace(" ", "", $tags); $tags = explode(",", $tags); $tags = implode(' OR ', $tags); $qs = urlencode($tags); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://search.twitter.com/search.atom?q=".$qs."&lang=en"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $objDOM = new DOMDocument(); $objDOM->loadXML($output); $entryArr = $objDOM->getElementsByTagName("entry"); $i = 0; $sqlArr = array(); foreach($entryArr AS $entryVal){ $titleArr = $entryArr->item($i)->getElementsByTagName("title"); $tweetStatus = str_replace("\n", "", $titleArr->item(0)->nodeValue); $dateArr = $entryArr->item($i)->getElementsByTagName("published"); $published = strtotime($dateArr->item(0)->nodeValue); $tweetDate = date("Y-m-d H:i:s", $published); $linksArr = $entryArr->item($i)->getElementsByTagName("link"); $j = 0; foreach($linksArr AS $linkVal){ if($linksArr->item($j)->getAttribute('rel') == 'image'){ $tweetImage = $linksArr->item($j)->getAttribute('href'); } $j++; } $userArr = $entryArr->item($i)->getElementsByTagName("name"); $tweetUser = explode(" ", $userArr->item(0)->nodeValue); if($published >= $last_update){ $sqlArr[]= "('".$wpdb->escape($tweetUser[0])."', '".$wpdb->escape(htmlentities($tweetStatus))."', '".$wpdb->escape($tweetImage)."', '".$wpdb->escape($tweetDate)."')"; } $i++; } // insert tweets if(count($sqlArr) > 0){ $insert = "INSERT INTO " . $table_name . " (user, tweet, userImage, tweetDate) " . "VALUES ".implode(",\n",$sqlArr); $results = $wpdb->query( $insert ); update_option('tweet_chopper_updated',date("Y-m-d H:i:s")); } } // get latest tweets $latest_tweets = $wpdb->get_results("SELECT user, tweet, userImage, tweetDate FROM $table_name ORDER BY tweetDate DESC LIMIT 0,".get_option('tweet_chopper_limit')); echo $args['before_widget'].'

Twitter

'; foreach ($latest_tweets as $tweets) { $tweetStatus = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "\\0", $tweets->tweet); $tweetDateUnix = mktime() - strtotime($tweets->tweetDate); $tweetDate = ($tweetDateUnix > 60) ? round($tweetDateUnix / 60) : $tweetDateUnix; $plural = ($tweetDate != '1') ? 's' : '' ; $tweetDate = ($tweetDateUnix > 60) ? $tweetDate.' minute'.$plural.' ago' : $tweetDate.' second'.$plural.' ago'; echo '
'.$tweets->user.'

'.$tweets->user.': '.$tweetStatus.'

'.$tweetDate.'
'; } echo '
'.$args['after_widget']; } function tweet_chopper_admin_page(){ if($_POST['action'] == 'update'){ $_POST['tweet_chopper_tags'] = (substr($_POST['tweet_chopper_tags'], -1) == ',') ? substr($_POST['tweet_chopper_tags'], 0, -1) : $_POST['tweet_chopper_tags'] ; update_option('tweet_chopper_tags',$_POST['tweet_chopper_tags']); update_option('tweet_chopper_limit',$_POST['tweet_chopper_limit']); } $limit = get_option('tweet_chopper_limit'); ?>

Tweet Chopper

Tweet Chopper Homepage Tweet Chopper

Enter your screen names (@screen_name) and hashtags (#trend) seperated with commas (,) below.

eg; @tweetchopper, #wordpress, #twitter