Eklentinin kodları aşağıdadır.
/*
Plugin Name: Feed2Post
Plugin URI: http://www.rasmusbengtsson.se/feed2post
Description: Imports posts from RSS-feed as regular posts. Runs once every hour using WordPress internal cronjob and feed functions.
Author: Rasmus Bengtsson
Author URI: http://www.rasmusbengtsson.se/
Version: 1.2
*/
class Feed2Post {
function Feed2Post() {
// Creates admin menu
add_action( 'admin_menu', array( &$this, 'add_admin_menu' ) );
// Add hook called when cronjob is executed
add_action( 'f2p_cronjob', array( &$this, 'cronjob' ) );
}
function add_admin_menu() {
add_management_page( "Feed2Post", "Feed2Post", "manage_options", "feed2post", array( &$this, 'admin_page' ) );
}
function cronjob() {
global $wpdb;
// Get list of feeds
$feeds=$wpdb->get_results( "SELECT * FROM {$wpdb->prefix}feed2post_feeds WHERE 1;" );
foreach ( $feeds as $feed ) {
// Get feed contents
$items = $this->fetchfeed( base64_decode( $feed->uri ) );
foreach ( $items as $item ) :
// Compare to list of already posted
$postid=$wpdb->get_var( $wpdb->prepare( "SELECT postid FROM {$wpdb->prefix}feed2post_posted WHERE id = %s;", $item->get_id() ) );
$header_footer[0] = @$feed->post_header;
$header_footer[1] = @$feed->post_footer;
$feedtags = array("%title%", "%link%");
$feedtags_replace = array($item->get_title(), $item->get_permalink());
$header_footer = str_ireplace($feedtags, $feedtags_replace, $header_footer);
// If it not exists
if ( $postid === null ) {
// Create post object
$post = array();
$post['post_title'] = $item->get_title();
$post['post_content'] = $header_footer[0] . $item->get_content() . $header_footer[1];
$post['post_status'] = 'publish';
$post['post_author'] = $feed->author;
$post['post_category'] = array($feed->category);
$post['post_date_gmt'] = gmdate("Y-m-d H:i:s", $item->get_date("U"));
$post['post_date'] = get_date_from_gmt( $post['post_date_gmt'] );
// Insert the post into the database
$postid = wp_insert_post( $post );
$wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->prefix}feed2post_posted (`id` ,`postid`) VALUES (%s,%d);", $item->get_id(), $postid ) );
}
// If it exists and should be updated
elseif ( $feed->updateposts == 1 ) {
$post = array();
$post['ID'] = $postid;
$post['post_title'] = $item->get_title();
$post['post_content'] = $header_footer[0] . $item->get_content() . $header_footer[1];
$post['post_date_gmt'] = gmdate("Y-m-d H:i:s", $item->get_date("U"));
$post['post_date'] = get_date_from_gmt( $post['post_date_gmt'] );
// Update the post into the database
wp_update_post($post);
}
endforeach;
}
}
// Uses built-in feed-reader, returns SimplePie-object
function fetchfeed( $address ) {
include_once( ABSPATH . WPINC . '/feed.php' );
$rss = fetch_feed( $address );
if ( !is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity( 20 );
$rss_items = $rss->get_items( 0, $maxitems );
return $rss_items;
endif;
return;
}
// Handles the different admin pages
function admin_page() {
if ( empty( $_GET['subpage'] ) ) $this->admin_list_feeds();
elseif ( $_GET['subpage'] == "add" ) $this->admin_add_feed();
elseif ( $_GET['subpage'] == "edit" ) $this->admin_edit_feed();
elseif ( $_GET['subpage'] == "delete" ) {
$this->admin_delete_feed();
$this->admin_list_feeds();
}
}
// Lists added feeds
function admin_list_feeds(){
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __('You do not have sufficient permissions to access this page.', 'feed2post') );
}
global $wpdb;
// Get list of feeds
$feeds=$wpdb->get_results( "SELECT * FROM {$wpdb->prefix}feed2post_feeds WHERE 1;" );
?>
Feed2Post -
if ( $feeds !== null ) {
?>
$i = 0;
foreach ( $feeds as $feed ) {
$i++;
$author = get_userdata( $feed->author );
$category = get_cat_name( $feed->category );
if ( $feed->updateposts == 1 ) {
$updateposts = __( "Yes" , 'feed2post');
}
else {
$updateposts = __( "No" , 'feed2post');
}
?>
" valign="top">
uri );?>
display_name;?>
}
?>
}
?>
}
// Add feed
function admin_add_feed(){
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' , 'feed2post') );
}
if ( !empty( $_POST ) ) {
$_POST['f2p_address'] = trim( $_POST['f2p_address'] );
if (!empty($_POST['f2p_address'])) {
global $wpdb;
$wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->prefix}feed2post_feeds (`uri`, `author`, `category`, `post_header`, `post_footer`, `updateposts`) VALUES ('%s', %d, %d, '%s', '%s', %d);", base64_encode( $_POST['f2p_address'] ), $_POST['f2p_author'], $_POST['f2p_category'], $_POST['f2p_post_header'], $_POST['f2p_post_footer'], ( isset( $_POST['f2p_updateposts'] ) ? $_POST['f2p_updateposts'] : 0 ) ) );
?>
Feed2Post -
return;
}
else $f2p_error = __( "You have to enter a valid address", 'feed2post' );
}
?>
Feed2Post -
}
// Edit feed
function admin_edit_feed() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' , 'feed2post') );
}
if ( !empty( $_POST ) ) {
$_POST['f2p_address'] = trim( @$_POST['f2p_address'] );
if ( !empty( $_POST['f2p_address'] ) ) {
global $wpdb;
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}feed2post_feeds SET `uri` = '%s', `author` = %d, `category`= %d, `post_header` = '%s', `post_footer` = '%s', `updateposts` = %d WHERE `uri` = '{$_GET['feed']}' LIMIT 1;", base64_encode( $_POST['f2p_address'] ), $_POST['f2p_author'], $_POST['f2p_category'], $_POST['f2p_post_header'], $_POST['f2p_post_footer'], ( isset($_POST['f2p_updateposts'] ) ? $_POST['f2p_updateposts'] : 0) ) );
?>
Feed2Post -
return;
}
else $f2p_error = __( "You have to enter a valid address", 'feed2post' );
}
else {
global $wpdb;
$feed = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}feed2post_feeds WHERE uri = '%s'", $_GET['feed'] ), ARRAY_A );
$_POST['f2p_address'] = base64_decode( $feed['uri'] );
$_POST['f2p_author'] = $feed['author'];
$_POST['f2p_category'] = $feed['category'];
$_POST['f2p_post_header'] = $feed['post_header'];
$_POST['f2p_post_footer'] = $feed['post_footer'];
$_POST['f2p_updateposts'] = $feed['updateposts'];
}
?>
Feed2Post -
}
// Delete feed
function admin_delete_feed() {
if ( !current_user_can('manage_options') ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' , 'feed2post') );
}
global $wpdb;
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}feed2post_feeds WHERE `uri` = '%s' LIMIT 1;", $_GET['feed'] ) );
}
}
add_action( "init", "f2p_init" );
function f2p_init() {
global $f2p;
$f2p = new Feed2Post();
// Adds support for translation
load_plugin_textdomain( 'feed2post', false, 'feed2post/languages' );
}
add_shortcode( 'f2p_test', 'feed2post_activate' );
register_activation_hook( __FILE__,'feed2post_activate' );
function feed2post_activate() {
global $wpdb;
$database_version = '1.2';
$installed_db = get_option( 'feed2post_db_version' );
if( $database_version != $installed_db ) {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
// Create the table structure
$sql = "CREATE TABLE `{$wpdb->prefix}feed2post_feeds` (
uri varchar(256) NOT NULL,
author bigint(20) NOT NULL,
category smallint(5) UNSIGNED NOT NULL,
updateposts tinyint(1) DEFAULT '0' NOT NULL,
post_header text NOT NULL,
post_footer text NOT NULL,
PRIMARY KEY uri (uri)
);";
ob_start(); // Since dbDelta whines about primary key I have to silent the whining
dbDelta( $sql );
ob_end_clean();
$sql = "CREATE TABLE `{$wpdb->prefix}feed2post_posted` (
id varchar(512) NOT NULL ,
postid bigint(20) NOT NULL ,
PRIMARY KEY id (id)
);";
ob_start(); // Since dbDelta whines about primary key I have to silent the whining
dbDelta( $sql );
ob_end_clean();
update_option( "feed2post_db_version", $database_version );
}
wp_schedule_event( time(), 'hourly', 'f2p_cronjob' );
}
register_deactivation_hook( __FILE__, 'feed2post_deactivate' );
function feed2post_deactivate() {
wp_clear_scheduled_hook( 'f2p_cronjob' );
}
?>
Her Yer Karanlık