All pastes #941847 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-03-14 00:06 UTC
rendered paste body
<?php
/*
Plugin Name: WP-Autoblog
Plugin URI: http://www.elliottback.com/wp/
Description: Given RSS Feeds, automatically makes posts to your blog
Version: 1.0
Author: Elliott Back
Author URI: http://www.elliottback.com/

NOTE:  Uses WP-Cron and Magpie RSS
*/

require_once(dirname(__FILE__) . '/wp-cron.php');
require_once(dirname(__FILE__) . '/rss_fetch.inc');
define('MAGPIE_FETCH_TIME_OUT', 60);
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
define('MAGPIE_CACHE_DIR', dirname(__FILE__) . '/cache');

/****
 * 1) INSTALLATION
 */

// installation
function wp_autoblog_install() {
	add_option('wp_autoblog_feeds', '');
}

if (isset ($_GET['activate']) && $_GET['activate'] == 'true') {
	add_action('init', 'wp_autoblog_install');
}

/****
 * 2) DAILY USE
 */

add_action ('wp_cron_15', 'wp_autoblog');
add_filter ('the_content', 'wp_autoblog_comment', 100);

function wp_autoblog_comment($text){
	return $text . '';
}

function wp_autoblog_trim_excerpt($text) {
	$text = apply_filters('the_content', $text);
	$text = str_replace(']]>', ']]&gt;', $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();

			// 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'] = '<em><a href="' . htmlentities(strip_tags(trim($item['link']))) . '" title="' . htmlentities($post['title']) . '">' . htmlentities($author) . '</a></em>  wrote an interesting post today on' . htmlentities($post['title']) . '<br>Here\'s a quick excerpt<br>';
			$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';

			// 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));

			$post['post_content'] .= '<p>';
			$post['post_content'] .= 'Read the rest of this great post <em><a href="' . htmlentities(strip_tags(trim($item['link']))) . '" title="' . htmlentities($post['title']) . '">here</a></em>';

			$attributions = array(
				' ',

			);

			$post['post_content'] .= $attributions[rand(0, count($attributions) - 1)];
			$post['post_content'] .= '</p>';

			// 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 '<div class="wrap">';
	echo '<h2>WP Autoblog Settings</h2>';

	echo '<form action="plugins.php?page=' . $_GET['page'] . '" method="post">';
	echo '<h3>The Basic Settings</h3>';
	echo '<p>';
	echo 'A list of RSS or ATOM feeds, one per line, to use as sources:';
	echo '</p>';
	echo '<textarea name="feeds" rows="10" style="width:100%">' . get_option('wp_autoblog_feeds') . '</textarea><br/>';
	echo '<input type="hidden" name="update" value="yes" />';
	echo '<input type="submit" value="Save Settings" />';
	echo '</form>';

	echo '<h3>Run Autoblog Now</h3>';
	echo '<p>';
	echo 'Click this button to parse all the feeds into your blog now:';
	echo '</p>';
	echo '<form action="plugins.php?page=' . $_GET['page'] . '" method="post">';
	echo '<input type="hidden" name="runnow" value="yes" />';
	echo '<input type="submit" value="Run script now" />';
	echo '</form>';

	echo '<p>';
	echo '<strong>';
	echo 'WP Autoblog';
	echo '</strong>';
	echo ' by <a href="http://elliottback.com/wp/">Elliott Back</a>';
	echo '</p>';

	echo '</div>';
}

function wp_autoblog_add_options_to_admin() {
	if ( function_exists('add_submenu_page') )
		add_submenu_page('plugins.php', __('WP Autoblog Configuration'), __('WP Autoblog Configuration'), 8, __FILE__, 'wp_autoblog_editor');
}

if (function_exists('add_action')) {
	add_action('admin_menu', 'wp_autoblog_add_options_to_admin');
}
?>