mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * RSS Importer
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Importer
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * RSS Importer
mp-wp_genesis 11 *
mp-wp_genesis 12 * Will process a RSS feed for importing posts into WordPress. This is a very
mp-wp_genesis 13 * limited importer and should only be used as the last resort, when no other
mp-wp_genesis 14 * importer is available.
mp-wp_genesis 15 *
mp-wp_genesis 16 * @since unknown
mp-wp_genesis 17 */
mp-wp_genesis 18 class RSS_Import {
mp-wp_genesis 19
mp-wp_genesis 20 var $posts = array ();
mp-wp_genesis 21 var $file;
mp-wp_genesis 22
mp-wp_genesis 23 function header() {
mp-wp_genesis 24 echo '<div class="wrap">';
mp-wp_genesis 25 screen_icon();
mp-wp_genesis 26 echo '<h2>'.__('Import RSS').'</h2>';
mp-wp_genesis 27 }
mp-wp_genesis 28
mp-wp_genesis 29 function footer() {
mp-wp_genesis 30 echo '</div>';
mp-wp_genesis 31 }
mp-wp_genesis 32
mp-wp_genesis 33 function unhtmlentities($string) { // From php.net for < 4.3 compat
mp-wp_genesis 34 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
mp-wp_genesis 35 $trans_tbl = array_flip($trans_tbl);
mp-wp_genesis 36 return strtr($string, $trans_tbl);
mp-wp_genesis 37 }
mp-wp_genesis 38
mp-wp_genesis 39 function greet() {
mp-wp_genesis 40 echo '<div class="narrow">';
mp-wp_genesis 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>';
mp-wp_genesis 42 wp_import_upload_form("admin.php?import=rss&step=1");
mp-wp_genesis 43 echo '</div>';
mp-wp_genesis 44 }
mp-wp_genesis 45
mp-wp_genesis 46 function get_posts() {
mp-wp_genesis 47 global $wpdb;
mp-wp_genesis 48
mp-wp_genesis 49 set_magic_quotes_runtime(0);
mp-wp_genesis 50 $datalines = file($this->file); // Read the file into an array
mp-wp_genesis 51 $importdata = implode('', $datalines); // squish it
mp-wp_genesis 52 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
mp-wp_genesis 53
mp-wp_genesis 54 preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
mp-wp_genesis 55 $this->posts = $this->posts[1];
mp-wp_genesis 56 $index = 0;
mp-wp_genesis 57 foreach ($this->posts as $post) {
mp-wp_genesis 58 preg_match('|<title>(.*?)</title>|is', $post, $post_title);
mp-wp_genesis 59 $post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) ));
mp-wp_genesis 60
mp-wp_genesis 61 preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt);
mp-wp_genesis 62
mp-wp_genesis 63 if ($post_date_gmt) {
mp-wp_genesis 64 $post_date_gmt = strtotime($post_date_gmt[1]);
mp-wp_genesis 65 } else {
mp-wp_genesis 66 // if we don't already have something from pubDate
mp-wp_genesis 67 preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt);
mp-wp_genesis 68 $post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
mp-wp_genesis 69 $post_date_gmt = str_replace('T', ' ', $post_date_gmt);
mp-wp_genesis 70 $post_date_gmt = strtotime($post_date_gmt);
mp-wp_genesis 71 }
mp-wp_genesis 72
mp-wp_genesis 73 $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
mp-wp_genesis 74 $post_date = get_date_from_gmt( $post_date_gmt );
mp-wp_genesis 75
mp-wp_genesis 76 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
mp-wp_genesis 77 $categories = $categories[1];
mp-wp_genesis 78
mp-wp_genesis 79 if (!$categories) {
mp-wp_genesis 80 preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
mp-wp_genesis 81 $categories = $categories[1];
mp-wp_genesis 82 }
mp-wp_genesis 83
mp-wp_genesis 84 $cat_index = 0;
mp-wp_genesis 85 foreach ($categories as $category) {
mp-wp_genesis 86 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
mp-wp_genesis 87 $cat_index++;
mp-wp_genesis 88 }
mp-wp_genesis 89
mp-wp_genesis 90 preg_match('|<guid.*?>(.*?)</guid>|is', $post, $guid);
mp-wp_genesis 91 if ($guid)
mp-wp_genesis 92 $guid = $wpdb->escape(trim($guid[1]));
mp-wp_genesis 93 else
mp-wp_genesis 94 $guid = '';
mp-wp_genesis 95
mp-wp_genesis 96 preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
mp-wp_genesis 97 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
mp-wp_genesis 98
mp-wp_genesis 99 if (!$post_content) {
mp-wp_genesis 100 // This is for feeds that put content in description
mp-wp_genesis 101 preg_match('|<description>(.*?)</description>|is', $post, $post_content);
mp-wp_genesis 102 $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1])));
mp-wp_genesis 103 }
mp-wp_genesis 104
mp-wp_genesis 105 // Clean up content
mp-wp_genesis 106 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
mp-wp_genesis 107 $post_content = str_replace('<br>', '<br />', $post_content);
mp-wp_genesis 108 $post_content = str_replace('<hr>', '<hr />', $post_content);
mp-wp_genesis 109
mp-wp_genesis 110 $post_author = 1;
mp-wp_genesis 111 $post_status = 'publish';
mp-wp_genesis 112 $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
mp-wp_genesis 113 $index++;
mp-wp_genesis 114 }
mp-wp_genesis 115 }
mp-wp_genesis 116
mp-wp_genesis 117 function import_posts() {
mp-wp_genesis 118 echo '<ol>';
mp-wp_genesis 119
mp-wp_genesis 120 foreach ($this->posts as $post) {
mp-wp_genesis 121 echo "<li>".__('Importing post...');
mp-wp_genesis 122
mp-wp_genesis 123 extract($post);
mp-wp_genesis 124
mp-wp_genesis 125 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
mp-wp_genesis 126 _e('Post already imported');
mp-wp_genesis 127 } else {
mp-wp_genesis 128 $post_id = wp_insert_post($post);
mp-wp_genesis 129 if ( is_wp_error( $post_id ) )
mp-wp_genesis 130 return $post_id;
mp-wp_genesis 131 if (!$post_id) {
mp-wp_genesis 132 _e("Couldn't get post ID");
mp-wp_genesis 133 return;
mp-wp_genesis 134 }
mp-wp_genesis 135
mp-wp_genesis 136 if (0 != count($categories))
mp-wp_genesis 137 wp_create_categories($categories, $post_id);
mp-wp_genesis 138 _e('Done !');
mp-wp_genesis 139 }
mp-wp_genesis 140 echo '</li>';
mp-wp_genesis 141 }
mp-wp_genesis 142
mp-wp_genesis 143 echo '</ol>';
mp-wp_genesis 144
mp-wp_genesis 145 }
mp-wp_genesis 146
mp-wp_genesis 147 function import() {
mp-wp_genesis 148 $file = wp_import_handle_upload();
mp-wp_genesis 149 if ( isset($file['error']) ) {
mp-wp_genesis 150 echo $file['error'];
mp-wp_genesis 151 return;
mp-wp_genesis 152 }
mp-wp_genesis 153
mp-wp_genesis 154 $this->file = $file['file'];
mp-wp_genesis 155 $this->get_posts();
mp-wp_genesis 156 $result = $this->import_posts();
mp-wp_genesis 157 if ( is_wp_error( $result ) )
mp-wp_genesis 158 return $result;
mp-wp_genesis 159 wp_import_cleanup($file['id']);
mp-wp_genesis 160 do_action('import_done', 'rss');
mp-wp_genesis 161
mp-wp_genesis 162 echo '<h3>';
mp-wp_genesis 163 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
mp-wp_genesis 164 echo '</h3>';
mp-wp_genesis 165 }
mp-wp_genesis 166
mp-wp_genesis 167 function dispatch() {
mp-wp_genesis 168 if (empty ($_GET['step']))
mp-wp_genesis 169 $step = 0;
mp-wp_genesis 170 else
mp-wp_genesis 171 $step = (int) $_GET['step'];
mp-wp_genesis 172
mp-wp_genesis 173 $this->header();
mp-wp_genesis 174
mp-wp_genesis 175 switch ($step) {
mp-wp_genesis 176 case 0 :
mp-wp_genesis 177 $this->greet();
mp-wp_genesis 178 break;
mp-wp_genesis 179 case 1 :
mp-wp_genesis 180 check_admin_referer('import-upload');
mp-wp_genesis 181 $result = $this->import();
mp-wp_genesis 182 if ( is_wp_error( $result ) )
mp-wp_genesis 183 echo $result->get_error_message();
mp-wp_genesis 184 break;
mp-wp_genesis 185 }
mp-wp_genesis 186
mp-wp_genesis 187 $this->footer();
mp-wp_genesis 188 }
mp-wp_genesis 189
mp-wp_genesis 190 function RSS_Import() {
mp-wp_genesis 191 // Nothing.
mp-wp_genesis 192 }
mp-wp_genesis 193 }
mp-wp_genesis 194
mp-wp_genesis 195 $rss_import = new RSS_Import();
mp-wp_genesis 196
mp-wp_genesis 197 register_importer('rss', __('RSS'), __('Import posts from an RSS feed.'), array ($rss_import, 'dispatch'));
mp-wp_genesis 198 ?>