1 | <?php |
---|
2 | |
---|
3 | class RSS_Import { |
---|
4 | |
---|
5 | var $posts = array (); |
---|
6 | var $file; |
---|
7 | |
---|
8 | function header() { |
---|
9 | echo '<div class="wrap">'; |
---|
10 | echo '<h2>'.__('Import RSS').'</h2>'; |
---|
11 | } |
---|
12 | |
---|
13 | function footer() { |
---|
14 | echo '</div>'; |
---|
15 | } |
---|
16 | |
---|
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); |
---|
21 | } |
---|
22 | |
---|
23 | function greet() { |
---|
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"); |
---|
27 | echo '</div>'; |
---|
28 | } |
---|
29 | |
---|
30 | function get_posts() { |
---|
31 | global $wpdb; |
---|
32 | |
---|
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); |
---|
37 | |
---|
38 | preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts); |
---|
39 | $this->posts = $this->posts[1]; |
---|
40 | $index = 0; |
---|
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]) )); |
---|
44 | |
---|
45 | preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt); |
---|
46 | |
---|
47 | if ($post_date_gmt) { |
---|
48 | $post_date_gmt = strtotime($post_date_gmt[1]); |
---|
49 | } else { |
---|
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); |
---|
55 | } |
---|
56 | |
---|
57 | $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt); |
---|
58 | $post_date = get_date_from_gmt( $post_date_gmt ); |
---|
59 | |
---|
60 | preg_match_all('|<category>(.*?)</category>|is', $post, $categories); |
---|
61 | $categories = $categories[1]; |
---|
62 | |
---|
63 | if (!$categories) { |
---|
64 | preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories); |
---|
65 | $categories = $categories[1]; |
---|
66 | } |
---|
67 | |
---|
68 | $cat_index = 0; |
---|
69 | foreach ($categories as $category) { |
---|
70 | $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category)); |
---|
71 | $cat_index++; |
---|
72 | } |
---|
73 | |
---|
74 | preg_match('|<guid.*?>(.*?)</guid>|is', $post, $guid); |
---|
75 | if ($guid) |
---|
76 | $guid = $wpdb->escape(trim($guid[1])); |
---|
77 | else |
---|
78 | $guid = ''; |
---|
79 | |
---|
80 | preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content); |
---|
81 | $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1]))); |
---|
82 | |
---|
83 | if (!$post_content) { |
---|
84 | // This is for feeds that put content in description |
---|
85 | preg_match('|<description>(.*?)</description>|is', $post, $post_content); |
---|
86 | $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape($this->unhtmlentities(trim($post_content[1])))); |
---|
87 | } |
---|
88 | |
---|
89 | // Clean up content |
---|
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); |
---|
93 | |
---|
94 | $post_author = 1; |
---|
95 | $post_status = 'publish'; |
---|
96 | $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories'); |
---|
97 | $index++; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | function import_posts() { |
---|
102 | echo '<ol>'; |
---|
103 | |
---|
104 | foreach ($this->posts as $post) { |
---|
105 | echo "<li>".__('Importing post...'); |
---|
106 | |
---|
107 | extract($post); |
---|
108 | |
---|
109 | if ($post_id = post_exists($post_title, $post_content, $post_date)) { |
---|
110 | _e('Post already imported'); |
---|
111 | } else { |
---|
112 | $post_id = wp_insert_post($post); |
---|
113 | if ( is_wp_error( $post_id ) ) |
---|
114 | return $post_id; |
---|
115 | if (!$post_id) { |
---|
116 | _e("Couldn't get post ID"); |
---|
117 | return; |
---|
118 | } |
---|
119 | |
---|
120 | if (0 != count($categories)) |
---|
121 | wp_create_categories($categories, $post_id); |
---|
122 | _e('Done !'); |
---|
123 | } |
---|
124 | echo '</li>'; |
---|
125 | } |
---|
126 | |
---|
127 | echo '</ol>'; |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | function import() { |
---|
132 | $file = wp_import_handle_upload(); |
---|
133 | if ( isset($file['error']) ) { |
---|
134 | echo $file['error']; |
---|
135 | return; |
---|
136 | } |
---|
137 | |
---|
138 | $this->file = $file['file']; |
---|
139 | $this->get_posts(); |
---|
140 | $result = $this->import_posts(); |
---|
141 | if ( is_wp_error( $result ) ) |
---|
142 | return $result; |
---|
143 | wp_import_cleanup($file['id']); |
---|
144 | do_action('import_done', 'rss'); |
---|
145 | |
---|
146 | echo '<h3>'; |
---|
147 | printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')); |
---|
148 | echo '</h3>'; |
---|
149 | } |
---|
150 | |
---|
151 | function dispatch() { |
---|
152 | if (empty ($_GET['step'])) |
---|
153 | $step = 0; |
---|
154 | else |
---|
155 | $step = (int) $_GET['step']; |
---|
156 | |
---|
157 | $this->header(); |
---|
158 | |
---|
159 | switch ($step) { |
---|
160 | case 0 : |
---|
161 | $this->greet(); |
---|
162 | break; |
---|
163 | case 1 : |
---|
164 | check_admin_referer('import-upload'); |
---|
165 | $result = $this->import(); |
---|
166 | if ( is_wp_error( $result ) ) |
---|
167 | echo $result->get_error_message(); |
---|
168 | break; |
---|
169 | } |
---|
170 | |
---|
171 | $this->footer(); |
---|
172 | } |
---|
173 | |
---|
174 | function RSS_Import() { |
---|
175 | // Nothing. |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | $rss_import = new RSS_Import(); |
---|
180 | |
---|
181 | register_importer('rss', __('RSS'), __('Import posts from an RSS feed.'), array ($rss_import, 'dispatch')); |
---|
182 | ?> |
---|