| 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 | /* We are making the assumption here that people want |
|---|
| 119 | * their conversations to be renderer as an unorderer list. |
|---|
| 120 | */ |
|---|
| 121 | $classes = array("even", "odd"); |
|---|
| 122 | $post_content = "<ul class=\"conversation\">\n"; |
|---|
| 123 | foreach ($post->{"conversation-line"} as $line) { |
|---|
| 124 | $post_content .= "<li class=\"" . $classes[++$i % 2] . "\"><span class=\"label\">" . $line['label'] . "</span> " . (string)$line . "</li>\n"; |
|---|
| 125 | } |
|---|
| 126 | $post_content .= "</ul>\n"; |
|---|
| 127 | break; |
|---|
| 128 | |
|---|
| 129 | case "video" : |
|---|
| 130 | $post_content = (string)$post->{"video-player"} . (string)$post->{"video-caption"}; |
|---|
| 131 | break; |
|---|
| 132 | |
|---|
| 133 | case "audio" : |
|---|
| 134 | $post_content = (string)$post->{"audio-player"} . (string)$post->{"audio-caption"}; |
|---|
| 135 | break; |
|---|
| 136 | |
|---|
| 137 | default : |
|---|
| 138 | printf(__("Found a <em>%s</em> post that we cannot process yet."), stripslashes($post['type'])); |
|---|
| 139 | continue 2; |
|---|
| 140 | break; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | $postdata = compact("post_author", "post_date", "post_date_gmt", "post_content", "post_title", "post_status"); |
|---|
| 144 | |
|---|
| 145 | $post_id = $this->insert_artifact($postdata); |
|---|
| 146 | if (!$post_id) { |
|---|
| 147 | _e("Unable to save post"); |
|---|
| 148 | } else { |
|---|
| 149 | add_post_meta($post_id, "imported_from", "tumblr", true); |
|---|
| 150 | add_post_meta($post_id, "tumblr_type", (string)$post['type'], true); |
|---|
| 151 | add_post_meta($post_id, "tumblr_id", (string)$post['id'], true); |
|---|
| 152 | add_post_meta($post_id, "tumblr_url", (string)$post['url'], true); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | echo "</li>"; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | if ((int)$xml->posts['total'] > ($start + 1) * 50) { |
|---|
| 159 | $this->import_posts(($start + 1) * 50); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | function insert_artifact(&$postdata) { |
|---|
| 164 | global $wpdb; |
|---|
| 165 | |
|---|
| 166 | if ($post_id = post_exists($postdata['post_title'], $postdata['post_content'], $postdata['post_date'])) { |
|---|
| 167 | printf(__("Post <em>%s</em> already exists."), stripslashes($postdata['post_title'])); |
|---|
| 168 | |
|---|
| 169 | return -1; |
|---|
| 170 | } else { |
|---|
| 171 | $postdata['post_title'] = $wpdb->escape($postdata['post_title']); |
|---|
| 172 | $postdata['post_content'] = $wpdb->escape($postdata['post_content']); |
|---|
| 173 | |
|---|
| 174 | printf(__("Importing post <em>%s</em>..."), stripslashes($postdata['post_title'])); |
|---|
| 175 | |
|---|
| 176 | $post_id = wp_insert_post($postdata); |
|---|
| 177 | if (is_wp_error($post_id)) { |
|---|
| 178 | return false; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | return $post_id; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | function import() { |
|---|
| 186 | echo "<ol>"; |
|---|
| 187 | $result = $this->import_posts(0); |
|---|
| 188 | echo "</ol>"; |
|---|
| 189 | if (is_wp_error($result)) { |
|---|
| 190 | return $result; |
|---|
| 191 | } |
|---|
| 192 | do_action("import_done", "tumblr"); |
|---|
| 193 | |
|---|
| 194 | echo "<h3>"; |
|---|
| 195 | printf(__("All done. <a href=\"%s\">Have fun!</a>"), get_option("home")); |
|---|
| 196 | echo "</h3>"; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | function dispatch() { |
|---|
| 200 | $step = (!empty($_GET['step'])? (int)$_GET['step'] : 0); |
|---|
| 201 | |
|---|
| 202 | $this->header(); |
|---|
| 203 | |
|---|
| 204 | switch ($step) { |
|---|
| 205 | case 0 : |
|---|
| 206 | $this->greet(); |
|---|
| 207 | break; |
|---|
| 208 | |
|---|
| 209 | case 1 : |
|---|
| 210 | check_admin_referer("import-tumblr"); |
|---|
| 211 | $result = $this->import(); |
|---|
| 212 | if (is_wp_error($result)) { |
|---|
| 213 | echo $result->get_error_message(); |
|---|
| 214 | } |
|---|
| 215 | break; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | $this->footer(); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | $tumblr_import = new Tumblr_Import(); |
|---|
| 223 | |
|---|
| 224 | register_importer("tumblr", __("Tumblr"), __("Import posts from a Tumblr blog."), array($tumblr_import, "dispatch")); |
|---|