__('Shows tweets for @users, @user/lists, #hashtags, and keywords blended together', 'tweetblender'))); } // display widget function widget($args, $instance) { global $post; if (sizeof($args) > 0) { extract($args, EXTR_SKIP); } $tb_o = get_option('tweet-blender'); // find out id/url of the archive page $archive_post_id = tb_get_archive_post_id(); $archive_page_url = trim($instance['widget_view_more_url']); if ($archive_page_url == '' && $archive_post_id > 0) { $archive_page_url = get_permalink($archive_post_id); } // don't show widget on the archive page if ($post == null || ($post->ID != $archive_post_id && $archive_page_url != "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])) { if (isset($before_widget)) { echo $before_widget; } $title = empty($instance['title']) ? '' : apply_filters('widget_title', trim($instance['title'])); if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }; $sources = preg_split('/[\n\r]/m',trim($instance['widget_sources'])); $private_sources = array(); if (isset($instance['widget_private_sources']) && $instance['widget_private_sources'] != '') { $private_sources = split(',',$instance['widget_private_sources']); } // remove private from general sources to avoid duplicates $sources = array_diff($sources,$private_sources); // mark private sources as private by prepending ! sign array_walk($private_sources,create_function('&$v,$k','if($v != "") { $v = "!".$v; }')); // set "view more" text if(isset($instance['widget_view_more_text']) && $instance['widget_view_more_text'] != '') { $view_more_text = $instance['widget_view_more_text']; } else { $view_more_text = __("view more", 'tweetblender'); } // add configuraiton options echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; // print out header and list of tweets echo '
'; echo tb_create_markup($mode = 'widget',$instance,$this->id,$tb_o); echo ''; echo '
'; if (isset($after_widget)) { echo $after_widget; } } } // update/save function function update($new_instance, $old_instance) { $instance = $old_instance; $tb_o = get_option('tweet-blender'); // process sources $errors = array(); if(isset($old_instance['widget_sources'])) { $old_sources = preg_split('/[\n\r]/m', $old_instance['widget_sources']); } else { $old_sources = array(); } $new_sources = preg_split('/[\n\r]/m', $new_instance['widget_sources']); $oAuth = null; $have_bad_sources = false; $need_oauth_tokens = false; $status_msg = array(); $log_msg = ''; $private_sources = array(); // capture aliases $aliases = array(); foreach($new_sources as $src) { $src = trim($src); // if there is an alias if(strpos($src,':') > 0 ) { $source_parts = explode(':',$src); $aliases[substr($source_parts[0],1)] = $source_parts[1]; } } if (sizeof($aliases) > 0) { $tb_o['alt_source_names'] = $aliases; update_option('tweet-blender',$tb_o); } if (isset($tb_o['widget_check_sources']) && $tb_o['widget_check_sources']) { foreach($new_sources as $src) { $src = trim($src); // if there is an alias if(strpos($src,':') > 0 ) { $sourceToCheck = substr($src, 0, strpos($src,':')); } else { $sourceToCheck = $src; } if ($src != '') { list($is_ok,$is_private,$need_oauth,$msg,$log) = $this->check_source($sourceToCheck,$tb_o); if (!$is_ok) { $have_bad_sources = true; } if ($need_oauth) { $need_oauth_tokens = true; } if ($is_private) { $private_sources[] = $src; } $status_msg[] = $msg; $log_msg .= $log; } } if ($need_oauth_tokens) { if (class_exists('TwitterOAuth')) { // Create TwitterOAuth object and get request token $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); // Get request token $request_token = $connection->getRequestToken(get_bloginfo('url') . '/' . PLUGINDIR . "/tweet-blender/lib/twitteroauth/callback.php"); if ($connection->http_code == 200) { // Save request token to session $tb_o['oauth_token'] = $token = $request_token['oauth_token']; $tb_o['oauth_token_secret'] = $request_token['oauth_token_secret']; update_option('tweet-blender',$tb_o); $errors[] = __("Sources have protected screen names.", 'tweetblender') . " " . __('Use your Twitter account to access them', 'tweetblender') . "."; } else { $errors[] = __("Sources have protected screen names but Twitter oAuth is not possible at this time. Please remove them from the list.", 'tweetblender') . ""; } } else { $errors[] = __("Sources have protected screen names but Twitter oAuth class is not available. Please remove them from the list.", 'tweetblender'); } } } if (sizeof($errors) == 0 && !$have_bad_sources) { $this->message = __('Settings saved', 'tweetblender') . '

' . join(', ',$status_msg); $instance['title'] = trim(strip_tags($new_instance['title'])); $instance['widget_refresh_rate'] = $new_instance['widget_refresh_rate']; $instance['widget_tweets_num'] = $new_instance['widget_tweets_num']; $instance['widget_sources'] = $new_instance['widget_sources']; $instance['widget_private_sources'] = join(',',$private_sources); $instance['widget_view_more_url'] = $new_instance['widget_view_more_url']; $instance['widget_view_more_text'] = trim($new_instance['widget_view_more_text']); return $instance; } else { $this->error = join(', ',$status_msg) . ""; if (sizeof($errors) > 0) { $this->error .= '

' . join(', ', $errors); } $this->bad_input = $new_instance; return false; } } // admin control form function form($instance) { global $tb_refresh_periods; $default = array( 'title' => __('Tweet Blender', 'tweetblender'), 'widget_refresh_rate' => 0, 'widget_tweets_num' => 4, 'widget_sources' => '' ); $instance = wp_parse_args( (array) $instance, $default ); // report errors if any if (isset($this->error)) { echo tb_wrap_javascript("function tAuth(url) {var tWin = window.open(url,'tWin','width=800,height=410,toolbar=0,location=1,status=0,menubar=0,resizable=1');}"); echo '
' . $this->error . '
'; $instance = $this->bad_input; } // report messages if an if (isset($this->message)) { echo '
' . $this->message . '
'; } // title $field_id = $this->get_field_id('title'); $field_name = $this->get_field_name('title'); echo "\r\n".'

'; // sources $field_id = $this->get_field_id('widget_sources'); $field_name = $this->get_field_name('widget_sources'); echo "\r\n".'

'; // private sources $field_id = $this->get_field_id('widget_private_sources'); $field_name = $this->get_field_name('widget_private_sources'); if (!isset($instance['widget_private_sources'])) { $instance['widget_private_sources'] = ''; } echo "\r\n".''; // specify refresh $field_id = $this->get_field_id('widget_refresh_rate'); $field_name = $this->get_field_name('widget_refresh_rate'); echo "\r\n".''; echo "\r\n".'
'; // specify number of tweets $field_id = $this->get_field_id('widget_tweets_num'); $field_name = $this->get_field_name('widget_tweets_num'); echo "\r\n".'

'; // specify text for "view more" link $field_id = $this->get_field_id('widget_view_more_text'); $field_name = $this->get_field_name('widget_view_more_text'); if (!isset($instance['widget_view_more_text'])) { $instance['widget_view_more_text'] = ''; } echo "\r\n".'
'; echo "\r\n".''; // specify URL for "view more" link $field_id = $this->get_field_id('widget_view_more_url'); $field_name = $this->get_field_name('widget_view_more_url'); if (!isset($instance['widget_view_more_url'])) { $instance['widget_view_more_url'] = ''; } echo "\r\n".'
'; echo "\r\n".'
'; if ($archive_post = tb_get_archive_post_id()) { echo '' . __('Leave blank to use ', 'tweetblender') . '' . __('existing page', 'tweetblender') . ''; } } function check_source($src,$tb_o) { global $wp_json; $need_oauth = false; $is_private = false; $source_check_result = ''; $log_msg = ''; $is_ok = false; // if we don't have json class, get the library if ( !is_a($wp_json, 'Services_JSON') ) { if (file_exists(ABSPATH . WPINC . '/class-json.php')) { require_once( ABSPATH . WPINC . '/class-json.php' ); } else { require(dirname(__FILE__).'/lib/JSON.php'); } $wp_json = new Services_JSON(); } // remove private account markup if (stripos($src,'!') === 0) { $src = substr($src,1); } // remove modifiers if (stripos($src,'|') > 1) { $src = substr($src,0,stripos($src,'|')); } $source_is_screen_name = false; // if it's a list, use list API if (stripos($src,'@') === 0 && stripos($src,'/') > 1) { $apiUrl = 'http://api.twitter.com/1/' . substr($src,1,strpos($src,'/')) . 'lists' . substr($src,strpos($src,'/')) . '/statuses.json'; } // if it's a screen name, use timeline API (search would not give us private/public check) elseif (stripos($src,'@') === 0) { $source_is_screen_name = true; $apiUrl = 'http://twitter.com/statuses/user_timeline.json?screen_name=' . substr($src,1); } // else assume it's a hashtag or keyword else { $apiUrl = 'http://search.twitter.com/search.json?q=' . urlencode($src); } if (!class_exists('WP_Http')) { include_once( ABSPATH . WPINC. '/class-http.php' ); } $http = new WP_Http; $result = $http->request($apiUrl); // try to get data from Twitter if (!is_wp_error($result)) { $jsonData = $wp_json->decode($result['body']); if (!isset($jsonData)) { $source_check_result = ' ' . $src . ' - ' . __('FAIL', 'tweetblender') . ''; $log_msg = "($src) " . __('json error','tweetblender') . ': ' . __('can not get json', 'tweetblender') . "\n" . $apiUrl; } // if Twitter reported error elseif (isset($jsonData->{'error'})) { // if it's a private user if (strpos($jsonData->{'error'},"Not authorized") !== false){ $is_private = true; // if we don't have access tokens - error if (!array_key_exists('oauth_access_token',$tb_o)) { $source_check_result = $src . ' - ' . __('PRIVATE', 'tweetblender') . ''; $log_msg = "($src) " . __('Private: needs oAuth', 'tweetblender') . "\n"; $need_oauth = true; } // if we do have tokens - OK else { $is_ok = true; $source_check_result = ' ' . $src . ' - ' . __('PRIVATE', 'tweetblender') . ''; $log_msg = "($src) " . __('Private: we have oAuth', 'tweetblender') . "\n"; } } // if it's just limit error we are OK elseif (strpos($jsonData->{error},"Rate limit exceeded") === 0) { $is_ok = true; $source_check_result = ' ' . $src . ' - ' . __('OK', 'tweetblender') . ''; $log_msg = "($src) " . __('Error','tweetblender') . ': ' . __('limit error', 'tweetblender') . "\n"; } // any other error is an error else { $source_check_result = ' ' . $src . ' - ' . __('FAIL', 'tweetblender') . ''; $log_msg = "($src) " . __('json error','tweetblender') . ': ' . $jsonData->{error} . "\n"; } } // else we assume OK else { $is_ok = true; $source_check_result = ' ' . $src . ' - ' . __('OK', 'tweetblender') . ''; $log_msg = "($src) " . __('Got json with no errors', 'tweetblender') . "\n"; } } // if HTTP request failed else { // if it's a protected source if ($source_is_screen_name && $result['response']['code'] == 401) { $is_private = true; // if have tokens - try to get it if($tb_o['oauth_user_access_key'] && $tb_o['oauth_user_access_secret']) { if (!$oAuth) { $oAuth = new TwitterOAuth( $tb_o['oauth_consumer_key'], $tb_o['oauth_consumer_secret'], $tb_o['oauth_user_access_key'], $tb_o['oauth_user_access_secret'] ); } $content = $oAuth->OAuthRequest('https://search.twitter.com/search.json', array('q' => '','from' => substr($src,1)), 'GET'); } // else make user authorize else { $need_oauth = true; $source_check_result = ' ' . $src . ' - ' . __('PRIVATE', 'tweetblender') . ''; $log_msg = "($src)" . __('Private: needs oAuth', 'tweetblender') . "\n"; } } // else it's a bad source else { $source_check_result = ' ' . $src . ':' . __('FAIL', 'tweetblender') . ''; $log_msg = "($src)" . __('HTTP error: ', 'tweetblender') . $result->get_error_message() . "\n"; } } return array($is_ok,$is_private,$need_oauth,$source_check_result,$log_msg); } } add_action( 'widgets_init', create_function('', 'return register_widget("TweetBlender");') ); ?>