function tweetimport_import_twitter_feed($twitter_account)
{
require_once (ABSPATH . WPINC . '/class-feed.php');
$feed = new SimplePie();
$account_parts = explode ('/', $twitter_account['twitter_name'], 2);
if ($twitter_account['account_type'] == 1): //Account is Favorites
$feed->set_feed_url(str_replace('#=#USER#=#', $account_parts[0], TWEETIMPORT_API_FAVORITES_URL));
elseif ($twitter_account['account_type'] == 0 && count($account_parts) == 1): //User timeline
$feed->set_feed_url(str_replace('#=#USER#=#', $account_parts[0], TWEETIMPORT_API_USER_TIMELINE_URL));
elseif ($twitter_account['account_type'] == 2 && count($account_parts) == 2): //Account is list
$feed_url = str_replace('#=#USER#=#', $account_parts[0], TWEETIMPORT_API_LIST_URL);
$feed_url = str_replace('#=#LIST#=#', $account_parts[1], $feed_url);
$feed->set_feed_url($feed_url);
else :
return '<strong>ERROR: Account information not correct. Account type wrong?</strong>';
endif;
$feed->set_useragent('Tweet Import http://skinju.com/wordpress/tweetimport');
$feed->set_cache_class('WP_Feed_Cache');
$feed->set_file_class('WP_SimplePie_File');
$feed->enable_cache(true);
$feed->set_cache_duration (apply_filters('tweetimport_cache_duration', 880));
$feed->enable_order_by_date(false);
$feed->init();
$feed->handle_content_type();
if ($feed->error()):
return '<strong>ERROR: Feed Reading Error.</strong>';
endif;
$rss_items = $feed->get_items();
$imported_count = 0;
foreach ($rss_items as $item)
{
$item = apply_filters ('tweetimport_tweet_before_new_post', $item); //return false to stop processing an item.
if (!$item) continue;
$processed_description = $item->get_description();
//Get the twitter author from the beginning of the tweet text
$twitter_author = trim(preg_replace("~^(\w+):(.*?)$~", "\\1", $processed_description));
if ($twitter_account['strip_name'] == 1):
$processed_description = preg_replace("~^(\w+):(.*?)~i", "\\2", $processed_description);
endif;
if ($twitter_account['names_clickable'] == 1):
$processed_description = preg_replace("~@(\w+)~", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $processed_description);
$processed_description = preg_replace("~^(\w+):~", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>:", $processed_description);
endif;
if ($twitter_account['hashtags_clickable'] == 1):
if ($twitter_account['hashtags_clickable_twitter'] == 1):
$processed_description = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $processed_description);
else:
$processed_description = preg_replace("/#(\w+)/", "<a href=\"" . skinju_get_tag_link("\\1") . "\">#\\1</a>", $processed_description);
endif;
endif;
$processed_description = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $processed_description);
$processed_description = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $processed_description);
$new_post = array('post_title' => trim (substr (preg_replace("~{$account_parts[0]}: ~i", "", $item->get_title()), 0, 25) . '...'),
'post_content' => trim ($processed_description),
'post_date' => $item->get_date('Y-m-d H:i:s'),
'post_author' => $twitter_account['author'],
'post_category' => array($twitter_account['category']),
'post_status' => 'publish');
$new_post = apply_filters('tweetimport_new_post_before_create', $new_post); // Offer the chance to manipulate new post data. return false to skip
if (!$new_post) continue;
$new_post_id = wp_insert_post($new_post);
$imported_count++;
add_post_meta ($new_post_id, 'tweetimport_twitter_author', $twitter_author, true);
add_post_meta ($new_post_id, 'tweetimport_date_imported', date ('Y-m-d H:i:s'), true);
add_post_meta ($new_post_id, 'tweetimport_twitter_id', $item->get_id(), true);
add_post_meta ($new_post_id, 'tweetimport_twitter_id', $item->get_id(), true);
add_post_meta ($new_post_id, '_tweetimport_twitter_id_hash', $item->get_id(true), true);
add_post_meta ($new_post_id, 'tweetimport_twitter_post_uri', $item->get_link(0));
add_post_meta ($new_post_id, 'tweetimport_author_avatar', $item->get_link(0, 'image'));
preg_match_all ('~#([A-Za-z0-9_]+)(?=\s|\Z)~', $item->get_description(), $out);
if ($twitter_account['add_tag']) $out[0][] = $twitter_account['add_tag'];
wp_set_post_tags($new_post_id, implode (',', $out[0]));
}
return $imported_count;
}