Last active
March 29, 2021 17:30
-
-
Save whyisjake/41a46d51d084e37e47cc to your computer and use it in GitHub Desktop.
WP-CLI script to import posts from an RSS feed into WordPress. Meant to map to existing posts, and if they don't exist, create new ones.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
WP_CLI::add_command( 'podcast', 'Podcast_Importer' ); | |
/** | |
* Podcast Importer | |
*/ | |
class Podcast_Importer extends WP_CLI_Command { | |
/** | |
* Add posts to feed | |
* | |
* @subcommand add-feed | |
* @synopsis <feed> <slug> <author> <category-slug> | |
* | |
*/ | |
public function add_feed( $args, $assoc_args ) { | |
// Setup the args. | |
list( $feed, $slug, $author, $category_slug ) = $args; | |
$category = get_term_by( 'slug', $category_slug, 'category' ); | |
// Load the RSS file. | |
$import = simplexml_load_file( $feed ); | |
// How many posts are we looking for? | |
$count = count( $import->channel->item ); | |
// Some vars. | |
$sucess = 0; | |
$failure = 0; | |
$post_added = 0; | |
WP_CLI::line( 'We are looking for ' . $count . ' posts' ); | |
// Start the loop. | |
// Because the items aren't held in an array directly, we need to use a for loop. | |
for ( $i=0; $i < $count; $i++) { | |
$itunes = $import->channel->item[ $i ]->children('http://www.itunes.com/dtds/podcast-1.0.dtd'); | |
$title = trim( (string) $import->channel->item[ $i ]->title ); | |
// First condition, do we have a link? This makes it a lot easier. | |
if ( isset( $import->channel->item[ $i ]->link ) ) { | |
WP_CLI::line( '/////////////////////////' ); | |
WP_CLI::line( 'Link: ' . $import->channel->item[ $i ]->link ); | |
$url = parse_url( $import->channel->item[ $i ]->link ); | |
$explode = explode( '/', $url['path'] ); | |
WP_CLI::line( 'Slug: ' . $explode[3] ); | |
$args = array( | |
'name' => $explode[3], | |
); | |
$posts = new WP_Query( $args ); | |
WP_CLI::line( 'ID: ' . $posts->post->ID ); | |
$added = wp_add_object_terms( $posts->post->ID, $slug, 'series' ); | |
if ( ! is_wp_error( $added ) ) { | |
$sucess++; | |
WP_CLI::success( $title ); | |
} else { | |
$failure++; | |
} | |
WP_CLI::line( '/////////////////////////' ); | |
} | |
// Do we have an enclosure URL? | |
elseif ( isset( $import->channel->item[ $i ]->enclosure['url'] ) ) { # Looks like we don't have a link... #gadgetlab #WTF | |
$url = (string) $import->channel->item[ $i ]->enclosure['url']; | |
$args = array( | |
'meta_query' => array( | |
'relation' => 'OR', | |
array( | |
'key' => 'audio_file', | |
'value' => $url, | |
'compare' => 'LIKE', | |
), | |
array( | |
'key' => 'enclosure', | |
'value' => $url, | |
'compare' => 'LIKE', | |
), | |
) | |
); | |
$meta_query = new WP_Query( $args ); | |
// Did we find anything? If we do, add the series. | |
if ( $meta_query->found_posts > 0 ) { | |
WP_CLI::line( '/////////////////////////' ); | |
WP_CLI::line( 'File: ' . $url ); | |
WP_CLI::line( 'ID: ' . $meta_query->post->ID ); | |
$added = wp_add_object_terms( $meta_query->post->ID, $slug, 'series' ); | |
if ( ! is_wp_error( $added ) ) { | |
$sucess++; | |
WP_CLI::success( $title ); | |
} else { | |
$failure++; | |
WP_CLI::warning( $title ); | |
} | |
WP_CLI::line( '/////////////////////////' ); | |
} else { | |
$post_arr = array( | |
'post_content' => apply_filters( 'the_content', trim( (string) $itunes->summary ) ), | |
'post_excerpt' => apply_filters( 'the_excerpt', trim( (string) $itunes->subtitle ) ), | |
'post_title' => apply_filters( 'the_title', $title ), | |
'post_status' => 'publish', | |
'post_author' => $author, | |
'post_date' => date("Y-m-d H:i:s", strtotime( (string) $import->channel->item[ $i ]->pubDate ) ), | |
'post_category' => array( $category->term_id ), | |
); | |
WP_CLI::line( '/////////////////////////' ); | |
WP_CLI::line( 'Inserting: ' . $title ); | |
// Add the post | |
$post_id = wp_insert_post( $post_arr ); | |
WP_CLI::line( 'New ID: ' . $post_id ); | |
if ( ! is_wp_error( $post_id ) ) { | |
// Update the added count | |
$post_added++; | |
// Set the post meta | |
$audio_file = add_post_meta( $post_id, 'audio_file', $url ); | |
// Set the series term | |
$added = wp_add_object_terms( $post_id, $slug, 'series' ); | |
// Log whether or not this failed. | |
if ( ! is_wp_error( $added ) ) { | |
$sucess++; | |
WP_CLI::success( get_the_title( $post_id ) ); | |
} else { | |
$failure++; | |
WP_CLI::warning( $title ); | |
} | |
} | |
WP_CLI::line( '/////////////////////////' ); | |
} | |
} | |
// Can't find the post, let's add it. | |
else { | |
WP_CLI::line( '/////////////////////////' ); | |
WP_CLI::warning( (string) $title ); | |
$failure++; | |
WP_CLI::line( '/////////////////////////' ); | |
} | |
} | |
WP_CLI::success( 'Success: ' . $sucess ); | |
WP_CLI::success( 'Added: ' . $post_added ); | |
WP_CLI::warning( 'Failed: ' . $failure ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment