Make WordPress Core

Ticket #7917: tumblr.php

File tumblr.php, 7.2 KB (added by martinjansen, 17 years ago)
Line 
1<?php
2/**
3 * Import class for Tumblr blogs
4 *
5 * @author Martin Jansen (http://martinjansen.com/)
6 */
7class 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&#8217;ll import the posts into this blog.") . "</p>";
21
22        echo "<form action=\"admin.php?import=tumblr&amp;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://&lt;blogname&gt;.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_title = "A Quote";
81                    $post_content = "<blockquote>" . (string)$post->{"quote-text"} . "</blockquote>";
82                    $post_content .= "<p>" . (string)$post->{"quote-source"} . "</p>";
83                   
84                    break;
85                   
86                case "photo" :
87                    $post_title = "A Photo";
88
89                    $upload_dir = wp_upload_dir();
90                    if (get_option("uploads_use_yearmonth_folders")) {
91                        $dir = sprintf("%s/%02d/%02d",
92                            $upload_dir['basedir'],
93                            date("Y", strtotime($post['date'])),
94                            date("m", strtotime($post['date'])));
95                        $subdir = sprintf("/%s/%02d/%02d/",
96                            get_option("upload_path"),
97                            date("Y", strtotime($post['date'])),
98                            date("m", strtotime($post['date'])));
99                    } else {
100                        $dir = $upload_dir['basedir'];
101                        $subdir = get_option("upload_path") . "/" . $upload_dir['subdir'];
102                    }
103                   
104                    list($url) = $post->xpath("photo-url[@max-width=500]");
105                    list($width, $height) = getimagesize($url);
106                   
107                    if (!is_dir($dir)) {
108                        mkdir($dir, 0777, true);
109                    }
110                    file_put_contents($dir . "/" . basename($url), fopen($url, "r"));
111                   
112                    $post_content = sprintf("<img src=\"%s\" alt=\"\" width=\"%d\" height=\"%d\" />",
113                        $subdir . basename($url),
114                        $width,
115                        $height);
116                    $post_content .= "<p>" . (string)$post->{"photo-caption"} . "</p>";
117
118                    break;
119            }
120
121            $postdata = compact("post_author", "post_date", "post_date_gmt", "post_content", "post_title", "post_status");
122
123            $post_id = $this->insert_artifact($postdata);
124            if (!$post_id) {
125                _e("Unable to save post");
126            } else {
127                add_post_meta($post_id, "imported_from", "tumblr", true);
128                add_post_meta($post_id, "tumblr_type", (string)$post['type'], true);
129                add_post_meta($post_id, "tumblr_id", (string)$post['id'], true);
130                add_post_meta($post_id, "tumblr_url", (string)$post['url'], true);
131            }
132
133            echo "</li>";
134        }
135       
136        if ((int)$xml->posts['total'] > ($start + 1) * 50) {
137            $this->import_posts(($start + 1) * 50);
138        }
139    }
140
141    function insert_artifact(&$postdata) {
142        global $wpdb;
143       
144        $postdata['post_title']    = $wpdb->escape($postdata['post_title']);
145        $postdata['post_content']  = $wpdb->escape($postdata['post_content']);
146
147        if ($post_id = post_exists($postdata['post_title'], $postdata['post_content'], $postdata['post_date'])) {
148            printf(__("Post <em>%s</em> already exists."), stripslashes($postdata['post_title']));
149
150            return -1;
151        } else {
152            printf(__("Importing post <em>%s</em>..."), stripslashes($postdata['post_title']));
153
154            $post_id = wp_insert_post($postdata);
155            if (is_wp_error($post_id)) {
156                return false;
157            }
158
159            return $post_id;
160        }
161    }
162   
163    function import() {
164        echo "<ol>";
165        $result = $this->import_posts(0);
166        echo "</ol>";
167        if (is_wp_error($result)) {
168            return $result;
169        }
170        do_action("import_done", "tumblr");
171
172        echo "<h3>";
173        printf(__("All done. <a href=\"%s\">Have fun!</a>"), get_option("home"));
174        echo "</h3>";
175    }
176
177    function dispatch() {
178        $step = (!empty($_GET['step'])? (int)$_GET['step'] : 0);
179
180        $this->header();
181
182        switch ($step) {
183            case 0 :
184                $this->greet();
185                break;
186
187            case 1 :
188                check_admin_referer("import-tumblr");
189                $result = $this->import();
190                if (is_wp_error($result)) {
191                    echo $result->get_error_message();
192                }
193                break;
194        }
195
196        $this->footer();
197    }
198}
199
200$tumblr_import = new Tumblr_Import();
201
202register_importer("tumblr", __("Tumblr"), __("Import posts from a Tumblr blog."), array($tumblr_import, "dispatch"));