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 '<p>'.__('Howdy! This importer allows you to extract posts from any 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>';
25 wp_import_upload_form("admin.php?import=rss&step=1");
28 function get_posts() {
31 set_magic_quotes_runtime(0);
32 $datalines = file($this->file); // Read the file into an array
33 $importdata = implode('', $datalines); // squish it
34 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
36 preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
37 $this->posts = $this->posts[1];
39 foreach ($this->posts as $post) {
40 preg_match('|<title>(.*?)</title>|is', $post, $post_title);
41 $post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) ));
43 preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt);
46 $post_date_gmt = strtotime($post_date_gmt[1]);
48 // if we don't already have something from pubDate
49 preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt);
50 $post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
51 $post_date_gmt = str_replace('T', ' ', $post_date_gmt);
52 $post_date_gmt = strtotime($post_date_gmt);
55 $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
56 $post_date = get_date_from_gmt( $post_date_gmt );
58 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
59 $categories = $categories[1];
62 preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
63 $categories = $categories[1];
67 foreach ($categories as $category) {
68 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
72 preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid);
74 $guid = $wpdb->escape(trim($guid[1]));
78 preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
79 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
82 // This is for feeds that put content in description
83 preg_match('|<description>(.*?)</description>|is', $post, $post_content);
84 $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1])));
88 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
89 $post_content = str_replace('<br>', '<br />', $post_content);
90 $post_content = str_replace('<hr>', '<hr />', $post_content);
93 $post_status = 'publish';
94 $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
99 function import_posts() {
102 foreach ($this->posts as $post) {
103 echo "<li>".__('Importing post...');
107 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
108 _e('Post already imported');
110 $post_id = wp_insert_post($post);
112 _e("Couldn't get post ID");
116 if (0 != count($categories))
117 wp_create_categories($categories, $post_id);
128 $file = wp_import_handle_upload();
129 if ( isset($file['error']) ) {
134 $this->file = $file['file'];
136 $this->import_posts();
137 wp_import_cleanup($file['id']);
140 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
144 function dispatch() {
145 if (empty ($_GET['step']))
148 $step = (int) $_GET['step'];
157 check_admin_referer('import-upload');
165 function RSS_Import() {
170 $rss_import = new RSS_Import();
172 register_importer('rss', __('RSS'), __('Import posts from an RSS feed'), array ($rss_import, 'dispatch'));