9 echo '<div class="wrap">';
10 echo '<h2>'.__('Import RSS').'</h2>';
17 function unhtmlentities($string) { // From php.net for < 4.3 compat
18 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
19 $trans_tbl = array_flip($trans_tbl);
20 return strtr($string, $trans_tbl);
24 echo '<div class="narrow">';
25 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>';
26 wp_import_upload_form("admin.php?import=rss&step=1");
30 function get_posts() {
33 set_magic_quotes_runtime(0);
34 $datalines = file($this->file); // Read the file into an array
35 $importdata = implode('', $datalines); // squish it
36 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
38 preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
39 $this->posts = $this->posts[1];
41 foreach ($this->posts as $post) {
42 preg_match('|<title>(.*?)</title>|is', $post, $post_title);
43 $post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) ));
45 preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt);
48 $post_date_gmt = strtotime($post_date_gmt[1]);
50 // if we don't already have something from pubDate
51 preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt);
52 $post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
53 $post_date_gmt = str_replace('T', ' ', $post_date_gmt);
54 $post_date_gmt = strtotime($post_date_gmt);
57 $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
58 $post_date = get_date_from_gmt( $post_date_gmt );
60 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
61 $categories = $categories[1];
64 preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
65 $categories = $categories[1];
69 foreach ($categories as $category) {
70 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
74 preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid);
76 $guid = $wpdb->escape(trim($guid[1]));
80 preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
81 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
84 // This is for feeds that put content in description
85 preg_match('|<description>(.*?)</description>|is', $post, $post_content);
86 $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1])));
90 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
91 $post_content = str_replace('<br>', '<br />', $post_content);
92 $post_content = str_replace('<hr>', '<hr />', $post_content);
95 $post_status = 'publish';
96 $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
101 function import_posts() {
104 foreach ($this->posts as $post) {
105 echo "<li>".__('Importing post...');
109 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
110 _e('Post already imported');
112 $post_id = wp_insert_post($post);
113 if ( is_wp_error( $post_id ) )
116 _e("Couldn't get post ID");
120 if (0 != count($categories))
121 wp_create_categories($categories, $post_id);
132 $file = wp_import_handle_upload();
133 if ( isset($file['error']) ) {
138 $this->file = $file['file'];
140 $result = $this->import_posts();
141 if ( is_wp_error( $result ) )
143 wp_import_cleanup($file['id']);
146 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
150 function dispatch() {
151 if (empty ($_GET['step']))
154 $step = (int) $_GET['step'];
163 check_admin_referer('import-upload');
164 $result = $this->import();
165 if ( is_wp_error( $result ) )
166 echo $result->get_error_message();
173 function RSS_Import() {
178 $rss_import = new RSS_Import();
180 register_importer('rss', __('RSS'), __('Import posts from an RSS feed'), array ($rss_import, 'dispatch'));