forked from dhawalhshah/Export-Wordpress-posts-to-Tumblr
-
Notifications
You must be signed in to change notification settings - Fork 21
/
wordpressToTumblr.php
175 lines (140 loc) · 4.93 KB
/
wordpressToTumblr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
// Set the following parameters
$tumblrEmail = '';
$tumblrPassword = '';
$file = ''; // location of exported wordpress xml
$categories = array(); // ex blog, news, videos
$oldDomain = ''; // ex mywordpress.com (set this if you want relative links in posts converted to absolute before importing… note this should only be done if your old site will be set up to forward links to your new one.)
$resolver = array();
$tumblr = new Tumblr();
$tumblr->setCredentials($tumblrEmail, $tumblrPassword);
$wpt = new WordpressToTumblr();
$wpt->setWordpressXmlFile($file);
$wpt->loadXML();
$wpt->setTumblrObject($tumblr);
$wpt->postToTumblr();
class WordpressToTumblr {
private $wpExportFile;
private $xml;
private $tumblr;
public function setWordpressXmlFile($file) {
$this->wpExportFile = $file;
}
public function loadXML() {
$this->xml = simplexml_load_file($this->wpExportFile);
}
public function setTumblrObject($obj) {
$this->tumblr = $obj;
}
public function postToTumblr() {
global $resolver, $categories;
$ns = $this->xml->getNamespaces(true);
foreach ($this->xml->channel[0]->item as $item)
{
// skip drafts and other unpublished content
if ((string)$item->children($ns['wp'])->status != 'publish')
continue;
// only take posts from specific categories
if ($categories)
{
$title = $item->xpath('category');
if (!in_array((string)$title[1]['nicename'], $categories))
continue;
}
// get the title and body
$title = (string)$item->title;
$body = (string)$item->children($ns['content'])->encoded;
// get the extra parameters
$params = array(
'date' => (string)$item->children($ns['wp'])->post_date,
'slug' => (string)$item->children($ns['wp'])->post_name,
);
// get the tags
$tags = array();
$tagNodes = $item->children($ns['category']);
foreach($tagNodes as $child)
{
if ($child['domain'] == 'tag' && $child['nicename'])
$tags[(string)$child['nicename']] = (string)$child['nicename'];
}
if ($tags)
$params['tags'] = join(',', $tags);
// change relative urls to absolute (pointing to old wordpress site)
if ($oldDomain)
$body = str_replace('href="/', 'href="http://'.$oldDomain.'/', $body);
// post it
$postId = $this->tumblr->postRegular( $title , $body, $params);
// get link
$node = $item->xpath('link');
$resolver[(string)$node[0]] = '/post/'.$postId.'/'.$params['slug'];
}
foreach($resolver as $key => $item)
{
print "'$key' => '$item',\n";
}
}
}
class Tumblr {
private $pass;
private $email;
private $baseUrl = 'http://www.tumblr.com/api/';
private $postParameters = array(
'type' => null,
'generator' => null,
'date' => null,
'private' => '0',
'tags' => null,
'format' => 'html',
'group' => null,
'slug' => null,
'state' => 'published',
'send-to-twitter' => 'no'
);
public function setCredentials($user, $pass) {
$this->email = $user;
$this->pass = $pass;
}
private function write($postType, $params = array()) {
if (empty($params)) {
return false;
}
// add tumblr credentials
$params = array_merge(
$params, array('email' => $this->email, 'password' => $this->pass)
);
$tumblrURL = $this->baseUrl . 'write';
return $this->executeRequest($tumblrURL, $params);
}
public function postRegular($title, $body, $additionalParams = array()) {
if (empty($title) || empty($body)) {
return false;
}
$params = array_intersect_key($additionalParams, $this->postParameters);
$params = array_merge(
$params, array('title' => $title, 'body' => $body, 'type' => 'regular')
);
return $this->write('regular',$params);
}
private function executeRequest($url, $params) {
$data = http_build_query($params);
$c = curl_init($url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
// Check for success
if ($status == 201) {
echo "Success! The request executed succesfully.\n";
return $result;
} else if ($status == 403) {
echo 'Bad email or password';
return false;
} else {
echo "Error: $result\n";
return false;
}
}
}
?>