', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = 150;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '[...]');
$text = implode(' ', $words);
}
return $text;
}
function lookup_cat($cat){
global $wpdb;
$cat = apply_filters('pre_category_nicename', trim($cat));
$results = $wpdb->get_results("SELECT cat_ID, cat_name FROM $wpdb->categories WHERE cat_name = '$cat'");
if(!$results || count($results) < 1){
$wpdb->query("INSERT INTO $wpdb->categories SET cat_name='$cat', category_nicename='" . sanitize_title($cat) . "'");
return $wpdb->insert_id;
} else {
return $results[0]->cat_ID;
}
}
function wp_autoblog(){
global $wpdb;
$feeds = preg_split("|[\r\n]+|i", get_option('wp_autoblog_feeds'), -1, PREG_SPLIT_NO_EMPTY);
if(count($feeds) < 1)
return;
foreach($feeds as $feed){
$rss = auto_blog_fetch_rss($feed);
if (!$rss)
continue;
if(count($rss->items) < 1)
continue;
foreach($rss->items as $item){
$post = array();
// Attribution
if($item['author'] != "") $author = $item['author'];
else if($item['dc']['creator'] != "") $author = $item['dc']['creator'];
else if($item['author_name'] != "") $author = $item['author_name'];
else if($item['source'] != "") $author = $item['source'];
else if($item['dc']['contributor'] != "") $author = $item['dc']['contributor'];
// if there is no author, try two things:
//if($author == "") $author = $rss->channel['title'];
if($author == "") $author = 'Unknown';
$author = strip_tags(trim($author));
// Content
if($item['content']['encoded'] != "") $content = $item['content']['encoded'];
else if($item['xhtml']['div'] != "") $content = $item['xhtml']['div'];
else if($item['xhtml']['body'] != "") $content = $item['xhtml']['body'];
else $content = $item['description'];
$post['post_content'] = '' . htmlentities($author) . ' wrote an interesting post today on' . htmlentities($post['title']) . '
Here\'s a quick excerpt
';
$post['post_content'] .= wp_autoblog_trim_excerpt($content);
// Title
$post['post_title'] = strip_tags(trim($item['title']));
// Categories
$post['post_category'] = array();
if(isset($item['category#'])){
for ($i = 1; $i <= $item['category#']; $i++){
$cat_idx = (($i > 1) ? "#{$i}" : "");
$cat = $item["category{$cat_idx}"];
$post['post_category'][] = $cat;
}
} else if($item['dc']['subject'] != ""){
$post['post_category'][] = $item['dc']['subject'];
}
for($i = 0; $i < count($post['post_category']); $i++){
$post['post_category'][$i] = $wpdb->escape($post['post_category'][$i]);
$post['post_category'][$i] = lookup_cat(strip_tags(trim($post['post_category'][$i])));
}
// Post date
$date = "";
if($item['published'] != "") $date = auto_blog_parse_w3cdtf($item['published']); // Atom 1.0
else if($item['issued'] != "") $date = auto_blog_parse_w3cdtf($item['issued']); // Atom 0.3
else if($item['dcterms']['issued'] != "") $date = auto_blog_parse_w3cdtf($item['dcterms']['issued']);
else if($item['dc']['date'] != "") $date = auto_blog_parse_w3cdtf($item['dc']['date']);
else if($item['pubdate'] != "") $date = strtotime($item['pubdate']); // RSS 2.0
else $date = time();
$post['post_date'] = date('Y-m-d H:i:s', $date);
// Post status
$post['post_status'] = 'publish';
$post['post_content'] .= '
'; $post['post_content'] .= 'Read the rest of this great post here'; $attributions = array( ' ', ); $post['post_content'] .= $attributions[rand(0, count($attributions) - 1)]; $post['post_content'] .= '
'; // Escaping $post['post_content'] = $wpdb->escape($post['post_content']); $post['post_title'] = $wpdb->escape($post['post_title']); // Check existence $sql = "SELECT ID FROM $wpdb->posts WHERE post_title = '" . apply_filters('title_save_pre', $post['post_title']) . "'"; if($wpdb->query($sql) === 0) $pid = wp_insert_post($post); } } } /**** * 3) ADMIN PANEL */ // Update admin options function wp_autoblog_update() { if(isset($_POST['runnow'])) wp_autoblog(); if (isset ($_POST['update'])) { update_option('wp_autoblog_feeds', $_POST['feeds']); } } // Admin options function wp_autoblog_editor() { wp_autoblog_update(); echo ''; echo 'Click this button to parse all the feeds into your blog now:'; echo '
'; echo ''; echo ''; echo ''; echo 'WP Autoblog'; echo ''; echo ' by Elliott Back'; echo '
'; echo '