-
+ 932FDF7B823F34069393E54B3E58C67F17BC4B5ED208DF60D56A64E6E08792CDA1CBEBA1F42C2BC140421E05E8412D2E71EF6A70A4375E6E9FC03DB49D1C8591
mp-wp/wp-admin/import/rss.php
(0 . 0)(1 . 198)
19978 <?php
19979 /**
19980 * RSS Importer
19981 *
19982 * @package WordPress
19983 * @subpackage Importer
19984 */
19985
19986 /**
19987 * RSS Importer
19988 *
19989 * Will process a RSS feed for importing posts into WordPress. This is a very
19990 * limited importer and should only be used as the last resort, when no other
19991 * importer is available.
19992 *
19993 * @since unknown
19994 */
19995 class RSS_Import {
19996
19997 var $posts = array ();
19998 var $file;
19999
20000 function header() {
20001 echo '<div class="wrap">';
20002 screen_icon();
20003 echo '<h2>'.__('Import RSS').'</h2>';
20004 }
20005
20006 function footer() {
20007 echo '</div>';
20008 }
20009
20010 function unhtmlentities($string) { // From php.net for < 4.3 compat
20011 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
20012 $trans_tbl = array_flip($trans_tbl);
20013 return strtr($string, $trans_tbl);
20014 }
20015
20016 function greet() {
20017 echo '<div class="narrow">';
20018 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>';
20019 wp_import_upload_form("admin.php?import=rss&step=1");
20020 echo '</div>';
20021 }
20022
20023 function get_posts() {
20024 global $wpdb;
20025
20026 set_magic_quotes_runtime(0);
20027 $datalines = file($this->file); // Read the file into an array
20028 $importdata = implode('', $datalines); // squish it
20029 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
20030
20031 preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
20032 $this->posts = $this->posts[1];
20033 $index = 0;
20034 foreach ($this->posts as $post) {
20035 preg_match('|<title>(.*?)</title>|is', $post, $post_title);
20036 $post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) ));
20037
20038 preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt);
20039
20040 if ($post_date_gmt) {
20041 $post_date_gmt = strtotime($post_date_gmt[1]);
20042 } else {
20043 // if we don't already have something from pubDate
20044 preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt);
20045 $post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
20046 $post_date_gmt = str_replace('T', ' ', $post_date_gmt);
20047 $post_date_gmt = strtotime($post_date_gmt);
20048 }
20049
20050 $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
20051 $post_date = get_date_from_gmt( $post_date_gmt );
20052
20053 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
20054 $categories = $categories[1];
20055
20056 if (!$categories) {
20057 preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
20058 $categories = $categories[1];
20059 }
20060
20061 $cat_index = 0;
20062 foreach ($categories as $category) {
20063 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
20064 $cat_index++;
20065 }
20066
20067 preg_match('|<guid.*?>(.*?)</guid>|is', $post, $guid);
20068 if ($guid)
20069 $guid = $wpdb->escape(trim($guid[1]));
20070 else
20071 $guid = '';
20072
20073 preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
20074 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
20075
20076 if (!$post_content) {
20077 // This is for feeds that put content in description
20078 preg_match('|<description>(.*?)</description>|is', $post, $post_content);
20079 $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1])));
20080 }
20081
20082 // Clean up content
20083 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
20084 $post_content = str_replace('<br>', '<br />', $post_content);
20085 $post_content = str_replace('<hr>', '<hr />', $post_content);
20086
20087 $post_author = 1;
20088 $post_status = 'publish';
20089 $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
20090 $index++;
20091 }
20092 }
20093
20094 function import_posts() {
20095 echo '<ol>';
20096
20097 foreach ($this->posts as $post) {
20098 echo "<li>".__('Importing post...');
20099
20100 extract($post);
20101
20102 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
20103 _e('Post already imported');
20104 } else {
20105 $post_id = wp_insert_post($post);
20106 if ( is_wp_error( $post_id ) )
20107 return $post_id;
20108 if (!$post_id) {
20109 _e("Couldn't get post ID");
20110 return;
20111 }
20112
20113 if (0 != count($categories))
20114 wp_create_categories($categories, $post_id);
20115 _e('Done !');
20116 }
20117 echo '</li>';
20118 }
20119
20120 echo '</ol>';
20121
20122 }
20123
20124 function import() {
20125 $file = wp_import_handle_upload();
20126 if ( isset($file['error']) ) {
20127 echo $file['error'];
20128 return;
20129 }
20130
20131 $this->file = $file['file'];
20132 $this->get_posts();
20133 $result = $this->import_posts();
20134 if ( is_wp_error( $result ) )
20135 return $result;
20136 wp_import_cleanup($file['id']);
20137 do_action('import_done', 'rss');
20138
20139 echo '<h3>';
20140 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
20141 echo '</h3>';
20142 }
20143
20144 function dispatch() {
20145 if (empty ($_GET['step']))
20146 $step = 0;
20147 else
20148 $step = (int) $_GET['step'];
20149
20150 $this->header();
20151
20152 switch ($step) {
20153 case 0 :
20154 $this->greet();
20155 break;
20156 case 1 :
20157 check_admin_referer('import-upload');
20158 $result = $this->import();
20159 if ( is_wp_error( $result ) )
20160 echo $result->get_error_message();
20161 break;
20162 }
20163
20164 $this->footer();
20165 }
20166
20167 function RSS_Import() {
20168 // Nothing.
20169 }
20170 }
20171
20172 $rss_import = new RSS_Import();
20173
20174 register_importer('rss', __('RSS'), __('Import posts from an RSS feed.'), array ($rss_import, 'dispatch'));
20175 ?>