12 * Will process a RSS feed for importing posts into WordPress. This is a very
13 * limited importer and should only be used as the last resort, when no other
14 * importer is available.
20 var $posts = array ();
24 echo '<div class="wrap">';
26 echo '<h2>'.__('Import RSS').'</h2>';
33 function unhtmlentities($string) { // From php.net for < 4.3 compat
34 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
35 $trans_tbl = array_flip($trans_tbl);
36 return strtr($string, $trans_tbl);
40 echo '<div class="narrow">';
41 echo '<p>'.__('Howdy! This importer allows you to extract posts from an RSS 2.0 file into your blog. This is useful if you want to import your posts from a system that is not handled by a custom import tool. Pick an RSS file to upload and click Import.').'</p>';
42 wp_import_upload_form("admin.php?import=rss&step=1");
46 function _normalize_tag( $matches ) {
47 return '<' . strtolower( $matches[1] );
50 function get_posts() {
53 set_magic_quotes_runtime(0);
54 $datalines = file($this->file); // Read the file into an array
55 $importdata = implode('', $datalines); // squish it
56 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
58 preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
59 $this->posts = $this->posts[1];
61 foreach ($this->posts as $post) {
62 preg_match('|<title>(.*?)</title>|is', $post, $post_title);
63 $post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) ));
65 preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt);
68 $post_date_gmt = strtotime($post_date_gmt[1]);
70 // if we don't already have something from pubDate
71 preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt);
72 $post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
73 $post_date_gmt = str_replace('T', ' ', $post_date_gmt);
74 $post_date_gmt = strtotime($post_date_gmt);
77 $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
78 $post_date = get_date_from_gmt( $post_date_gmt );
80 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
81 $categories = $categories[1];
84 preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
85 $categories = $categories[1];
89 foreach ($categories as $category) {
90 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
94 preg_match('|<guid.*?>(.*?)</guid>|is', $post, $guid);
96 $guid = $wpdb->escape(trim($guid[1]));
100 preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
101 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
103 if (!$post_content) {
104 // This is for feeds that put content in description
105 preg_match('|<description>(.*?)</description>|is', $post, $post_content);
106 $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1])));
110 $post_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content);
111 $post_content = str_replace('<br>', '<br />', $post_content);
112 $post_content = str_replace('<hr>', '<hr />', $post_content);
115 $post_status = 'publish';
116 $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
121 function import_posts() {
124 foreach ($this->posts as $post) {
125 echo "<li>".__('Importing post...');
129 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
130 _e('Post already imported');
132 $post_id = wp_insert_post($post);
133 if ( is_wp_error( $post_id ) )
136 _e('Couldn’t get post ID');
140 if (0 != count($categories))
141 wp_create_categories($categories, $post_id);
152 $file = wp_import_handle_upload();
153 if ( isset($file['error']) ) {
158 $this->file = $file['file'];
160 $result = $this->import_posts();
161 if ( is_wp_error( $result ) )
163 wp_import_cleanup($file['id']);
164 do_action('import_done', 'rss');
167 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
171 function dispatch() {
172 if (empty ($_GET['step']))
175 $step = (int) $_GET['step'];
184 check_admin_referer('import-upload');
185 $result = $this->import();
186 if ( is_wp_error( $result ) )
187 echo $result->get_error_message();
194 function RSS_Import() {
199 $rss_import = new RSS_Import();
201 register_importer('rss', __('RSS'), __('Import posts from an RSS feed.'), array ($rss_import, 'dispatch'));