1 | <?php |
---|
2 | /** |
---|
3 | * Import class for Tumblr blogs |
---|
4 | * |
---|
5 | * @author Martin Jansen (http://martinjansen.com/) |
---|
6 | */ |
---|
7 | class Tumblr_Import { |
---|
8 | |
---|
9 | function header() { |
---|
10 | echo "<div class=\"wrap\">"; |
---|
11 | echo "<h2>" . __("Import Tumblr") . "</h2>"; |
---|
12 | } |
---|
13 | |
---|
14 | function footer() { |
---|
15 | echo "</div>"; |
---|
16 | } |
---|
17 | |
---|
18 | function greet() { |
---|
19 | echo "<div class=\"narrow\">"; |
---|
20 | echo "<p>" . __("Howdy! Tell us your Tumblr blog name and we’ll import the posts into this blog.") . "</p>"; |
---|
21 | |
---|
22 | echo "<form action=\"admin.php?import=tumblr&step=1\" method=\"post\">"; |
---|
23 | wp_nonce_field("import-tumblr"); |
---|
24 | echo "<table class=\"form-table\">"; |
---|
25 | printf("<tr><th><label for=\"blogname\">%s</label></th><td><input type=\"text\" name=\"blogname\" id=\"blogname\" /> (<code>http://<blogname>.tumblr.com/</code>)</td></tr>", __("Tumblr blog name:")); |
---|
26 | echo "</table>"; |
---|
27 | echo "<p class=\"submit\"><input type=\"submit\" name=\"submit\" value=\"" . attribute_escape(__("Import")) . "\" /></p>"; |
---|
28 | echo "</form>"; |
---|
29 | } |
---|
30 | |
---|
31 | function import_posts($start) { |
---|
32 | global $current_user; |
---|
33 | |
---|
34 | $url = "http://" . $_POST['blogname'] . ".tumblr.com/api/read?start=" . $start . "&num=50"; |
---|
35 | |
---|
36 | $contents = @file_get_contents($url); |
---|
37 | if ($contents === false) { |
---|
38 | _e("ups, something is wrong with the response from Tumblr."); |
---|
39 | return false; |
---|
40 | } |
---|
41 | |
---|
42 | /* At the moment we are using SimpleXML here, which requires PHP 5. |
---|
43 | * This should probably be changed to something that is supported in |
---|
44 | * PHP 4 as well. |
---|
45 | */ |
---|
46 | $xml = @simplexml_load_string($contents); |
---|
47 | |
---|
48 | if ($xml === false) { |
---|
49 | _e("ups, something is wrong with the response from Tumblr."); |
---|
50 | return false; |
---|
51 | } |
---|
52 | |
---|
53 | $post_author = $current_user->ID; |
---|
54 | $post_status = "publish"; |
---|
55 | |
---|
56 | foreach ($xml->posts->post as $post) { |
---|
57 | echo "<li>"; |
---|
58 | $post_title = $post_content = ""; |
---|
59 | |
---|
60 | $post_date = date("Y-m-d H:i:s", strtotime($post['date'])); |
---|
61 | $post_date_gmt = gmdate("Y-m-d H:i:s", strtotime($post['date-gmt'])); |
---|
62 | |
---|
63 | switch ($post['type']) { |
---|
64 | case "regular" : |
---|
65 | $post_title = (string)$post->{"regular-title"}; |
---|
66 | $post_content = (string)$post->{"regular-body"}; |
---|
67 | |
---|
68 | break; |
---|
69 | |
---|
70 | case "link" : |
---|
71 | $post_title = (string)$post->{"link-text"}; |
---|
72 | $post_content = sprintf("<a href=\"%s\">%s</a>", |
---|
73 | (string)$post->{"link-url"}, |
---|
74 | (string)$post->{"link-text"}); |
---|
75 | $post_content .= "<p>" . (string)$post->{"link-description"} . "</p>"; |
---|
76 | |
---|
77 | break; |
---|
78 | |
---|
79 | case "quote" : |
---|
80 | $post_content = "<blockquote>" . (string)$post->{"quote-text"} . "</blockquote>"; |
---|
81 | $post_content .= "<p>" . (string)$post->{"quote-source"} . "</p>"; |
---|
82 | |
---|
83 | break; |
---|
84 | |
---|
85 | case "photo" : |
---|
86 | $upload_dir = wp_upload_dir(); |
---|
87 | if (get_option("uploads_use_yearmonth_folders")) { |
---|
88 | $dir = sprintf("%s/%02d/%02d", |
---|
89 | $upload_dir['basedir'], |
---|
90 | date("Y", strtotime($post['date'])), |
---|
91 | date("m", strtotime($post['date']))); |
---|
92 | $subdir = sprintf("/%s/%02d/%02d/", |
---|
93 | get_option("upload_path"), |
---|
94 | date("Y", strtotime($post['date'])), |
---|
95 | date("m", strtotime($post['date']))); |
---|
96 | } else { |
---|
97 | $dir = $upload_dir['basedir']; |
---|
98 | $subdir = get_option("upload_path") . "/" . $upload_dir['subdir']; |
---|
99 | } |
---|
100 | |
---|
101 | list($url) = $post->xpath("photo-url[@max-width=500]"); |
---|
102 | list($width, $height) = getimagesize($url); |
---|
103 | |
---|
104 | if (!is_dir($dir)) { |
---|
105 | mkdir($dir, 0777, true); |
---|
106 | } |
---|
107 | file_put_contents($dir . "/" . basename($url), fopen($url, "r")); |
---|
108 | |
---|
109 | $post_content = sprintf("<img src=\"%s\" alt=\"\" width=\"%d\" height=\"%d\" />", |
---|
110 | $subdir . basename($url), |
---|
111 | $width, |
---|
112 | $height); |
---|
113 | $post_content .= "<p>" . (string)$post->{"photo-caption"} . "</p>"; |
---|
114 | |
---|
115 | break; |
---|
116 | |
---|
117 | case "conversation" : |
---|
118 | case "video" : |
---|
119 | case "audio" : |
---|
120 | default : |
---|
121 | printf(__("Found a <em>%s</em> post that we cannot process yet."), stripslashes($post['type'])); |
---|
122 | continue 2; |
---|
123 | break; |
---|
124 | } |
---|
125 | |
---|
126 | $postdata = compact("post_author", "post_date", "post_date_gmt", "post_content", "post_title", "post_status"); |
---|
127 | |
---|
128 | $post_id = $this->insert_artifact($postdata); |
---|
129 | if (!$post_id) { |
---|
130 | _e("Unable to save post"); |
---|
131 | } else { |
---|
132 | add_post_meta($post_id, "imported_from", "tumblr", true); |
---|
133 | add_post_meta($post_id, "tumblr_type", (string)$post['type'], true); |
---|
134 | add_post_meta($post_id, "tumblr_id", (string)$post['id'], true); |
---|
135 | add_post_meta($post_id, "tumblr_url", (string)$post['url'], true); |
---|
136 | } |
---|
137 | |
---|
138 | echo "</li>"; |
---|
139 | } |
---|
140 | |
---|
141 | if ((int)$xml->posts['total'] > ($start + 1) * 50) { |
---|
142 | $this->import_posts(($start + 1) * 50); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | function insert_artifact(&$postdata) { |
---|
147 | global $wpdb; |
---|
148 | |
---|
149 | $postdata['post_title'] = $wpdb->escape($postdata['post_title']); |
---|
150 | $postdata['post_content'] = $wpdb->escape($postdata['post_content']); |
---|
151 | |
---|
152 | if ($post_id = post_exists($postdata['post_title'], $postdata['post_content'], $postdata['post_date'])) { |
---|
153 | printf(__("Post <em>%s</em> already exists."), stripslashes($postdata['post_title'])); |
---|
154 | |
---|
155 | return -1; |
---|
156 | } else { |
---|
157 | printf(__("Importing post <em>%s</em>..."), stripslashes($postdata['post_title'])); |
---|
158 | |
---|
159 | $post_id = wp_insert_post($postdata); |
---|
160 | if (is_wp_error($post_id)) { |
---|
161 | return false; |
---|
162 | } |
---|
163 | |
---|
164 | return $post_id; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | function import() { |
---|
169 | echo "<ol>"; |
---|
170 | $result = $this->import_posts(0); |
---|
171 | echo "</ol>"; |
---|
172 | if (is_wp_error($result)) { |
---|
173 | return $result; |
---|
174 | } |
---|
175 | do_action("import_done", "tumblr"); |
---|
176 | |
---|
177 | echo "<h3>"; |
---|
178 | printf(__("All done. <a href=\"%s\">Have fun!</a>"), get_option("home")); |
---|
179 | echo "</h3>"; |
---|
180 | } |
---|
181 | |
---|
182 | function dispatch() { |
---|
183 | $step = (!empty($_GET['step'])? (int)$_GET['step'] : 0); |
---|
184 | |
---|
185 | $this->header(); |
---|
186 | |
---|
187 | switch ($step) { |
---|
188 | case 0 : |
---|
189 | $this->greet(); |
---|
190 | break; |
---|
191 | |
---|
192 | case 1 : |
---|
193 | check_admin_referer("import-tumblr"); |
---|
194 | $result = $this->import(); |
---|
195 | if (is_wp_error($result)) { |
---|
196 | echo $result->get_error_message(); |
---|
197 | } |
---|
198 | break; |
---|
199 | } |
---|
200 | |
---|
201 | $this->footer(); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | $tumblr_import = new Tumblr_Import(); |
---|
206 | |
---|
207 | register_importer("tumblr", __("Tumblr"), __("Import posts from a Tumblr blog."), array($tumblr_import, "dispatch")); |
---|