Hi all,
I'm not a PHP programmer, I can tinker here and there, but am lost on anything that requires more than that.
My band's website is running Wordpress, and we also share samples of our music on ccmixter.org, a site that allows others to remix music. The site also has an API which allows you to request XML of all the remixes made with your samples.
I wanted to have my website query the API and make a new post for each new remix that appeared. None of the plugins I found were able to do exactly what I wanted. In part because I wanted to categorize the remix by original song sample, and that data wasn't in the original XML query - though I could get it by calling another query from information pulled from the first one.
So I set out to make my own plugin to do this, and finally got something that is working. A lot of the code was taken from the RSS_Import class in the core, the rest from scouring the web and trial and error.
No admin options or anything, I just plunk it into the plugins folder, activate and go.
I was wondering if someone would be willing to critique it. Like I said, I'm not a programmer. I got it to work, but obviously don't know much about programming best practices and the like. I'd imagine there is much I could do to streamline the code, or make it more robust. Any input would be greatly appreciated!
<?php
/*
* Plugin Name: ccMixter Poster
* Description: Creates posts automatically anytime someone remixes you on ccMixter.
* Author: Terry Hart
* Plugin URI: http://www.freemarconi.com
* Version: 1.0
* =======================================================================
*/
require_once(ABSPATH . '/wp-admin/includes/post.php');
require_once(ABSPATH . '/wp-admin/includes/taxonomy.php');
require_once(ABSPATH . '/wp-admin/includes/import.php');
if (!class_exists("ccMixter")) {
class ccMixter {
var $posts = array ();
var $sxml; //Do I need this?
function ccMixter() { //constructor
}
function cc_init() {
$this->import();
}
function get_posts() {
global $wpdb;
$remixuri = 'http://ccmixter.org/api/query?&remixesof=freemarconi&f=rss'; //Add this as option at some point
$sxml = simplexml_load_file($remixuri);
$index = 0;
foreach ($sxml->channel->item as $item) {
//Get all the info I want from the feed
$dc = $item->children('http://purl.org/dc/elements/1.1/');
$post_title = $item->title;
$guid = $item->link;
$pubDate = $item->pubDate;
$post_content = $item->description;
$creator = $dc->creator;
$mp3file = $item->enclosure->attributes()->url;
//Get the date
$post_date_gmt = strtotime($pubDate);
$post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
$post_date = get_date_from_gmt( $post_date_gmt );
/*Now, there's no easy way to see what song the remixer used from the first feed
so we have to do a little backtracking and run another api query */
$remixid = substr($guid,-5);
$remix_used_uri = 'http://ccmixter.org/api/query?&sources=' . $remixid . '&f=rss';
$remix_sxml = simplexml_load_file($remix_used_uri);
$source_song = $remix_sxml->channel->item->title;
$categories = array('remix', $source_song);
//Format the post content and add mp3 to Audio Player
$post_content_title = '<p><a href="' . $guid . '" target="_blank">' . $post_title . '</a></p>';
$post_content_creator = '<p>By ' . $creator . '</p>';
$post_content_listen = '</p><p> [audio:'. $mp3file . '] </p>';
// Clean up content
$post_content = preg_replace_callback('|<(/?[A-Z]+)|', create_function('$match', 'return "<" . strtolower($match[1]);'), $post_content);
$post_content = str_replace('<br />', '<br />', $post_content);
$post_content = str_replace('<hr>', '<hr />', $post_content);
$post_content = $post_content_title . $post_content_creator . $post_content . $post_content_listen;
$post_author = 1;
$post_status = 'publish';
$this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
$index++;
}
}
function import_posts() {
foreach ($this->posts as $post) {
extract($post);
if ($post_id = post_exists($post_title, $post_content, $post_date)) {
return;
} else {
$post_id = wp_insert_post($post);
if ( is_wp_error( $post_id ) )
return $post_id;
if (!$post_id) {
return;
}
if (0 != count($categories))
wp_create_categories($categories, $post_id);
}
}
}
function import() {
$file = $sxml;
if ( isset($file['error']) ) {
echo $file['error'];
return;
}
$this->file = $file['file'];
$this->get_posts();
$result = $this->import_posts();
if ( is_wp_error( $result ) )
return $result;
wp_import_cleanup($file['id']);
do_action('import_done', 'rss');
}
}
}
if (class_exists("ccMixter")) {
$ccmixter_plugin = new ccMixter();
}
//Actions and Filters
if (isset($ccmixter_plugin)) {
//Actions
register_activation_hook( __FILE__, array(&$ccmixter_plugin, 'cc_init' ));
if (!wp_next_scheduled('ccschedule_hook')) {
wp_schedule_event( time(), 'hourly', 'ccschedule_hook' );
}
add_action( 'ccschedule_hook', array(&$ccmixter_plugin, 'cc_init' ));
}
?>