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