-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wix-import.php
461 lines (375 loc) · 11.9 KB
/
class-wix-import.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
use Willtj\PhpDraftjsHtml\Converter;
use Prezly\DraftPhp\Converter as DraftConverter;
class WixBlogImport
{
/**
* Wix Base API URL
*
* @var string
*/
public $api_url = 'https://www.wixapis.com/blog/v3';
/**
* Wix Static Media URL
*
* @var string
*/
public $static_media_url = 'https://static.wixstatic.com/media';
/**
* Wix API Key
*
* @var string
*/
public $api_key;
/**
* Constructor
*
* @param string $api_key Wix API Key
*
*/
public function __construct($api_key)
{
$this->api_key = $api_key;
}
/**
* Get Wix Blog Posts
*
* @return array
*/
public function get_posts($params = [])
{
// Set default params
$params = wp_parse_args($params, [
'fieldsToInclude' => 'CONTENT',
'paging' => [
'limit' => 100,
'offset' => 0,
]
]);
// Build Request URL
$request_url = add_query_arg($params, $this->api_url . '/posts');
// Create Request
$response = wp_remote_get($request_url, [
'headers' => [
'Authorization' => $this->api_key,
],
]);
// Check for error
if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
return new WP_Error('wix_blog_import_api_error', esc_html__('Error fetching Wix Blog', 'WixBlogImport'));
}
// Get response and decode
$body = json_decode(wp_remote_retrieve_body($response), true);
return $body;
}
/**
* Get Categories
*
* @return array
*/
public function get_categories()
{
// Build Request URL
$request_url = $this->api_url . '/categories';
// Create Request
$response = wp_remote_get($request_url, [
'headers' => [
'Authorization' => $this->api_key,
],
]);
// Check for error
if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
return new WP_Error('wix_blog_import_api_error', esc_html__('Error fetching Wix Blog', 'WixBlogImport'));
}
// Get response and decode
$body = json_decode(wp_remote_retrieve_body($response));
return $body;
}
/**
* Get Tag by ID
*
* @param int $id Tag ID
*
* @return array
*/
public function get_tag($tag_id)
{
// Build Request URL
$request_url = $this->api_url . '/tags/' . $tag_id;
// Create Request
$response = wp_remote_get($request_url, [
'headers' => [
'Authorization' => $this->api_key,
],
]);
// Check for error
if (is_wp_error($response)) {
return $response;
}
// Get response and decode
$body = json_decode(wp_remote_retrieve_body($response));
return $body;
}
/**
* Import Method
*
* @return void
*/
public function import($params = [])
{
// Get categories
$categories = $this->get_categories();
// check for error
if (!is_wp_error($categories)) {
foreach ($categories->categories as $category) {
// Create category
$category_id = wp_insert_term($category->label, 'category', ['slug' => $category->slug]);
// Check for error
if (!is_wp_error($category_id)) {
add_term_meta($category_id['term_id'], 'wix_id', $category->id);
}
}
}
// Get posts
$get_posts = $this->get_posts($params);
// Check for error
if (is_wp_error($get_posts)) {
return $get_posts;
}
// Loop through posts
$posts = $get_posts['posts'];
foreach ($posts as $item) {
// Create post
$post_id = $this->insert_post($item);
// If post is created successfully
if ($post_id) {
// Insert post metas
$this->insert_metas($post_id, $item);
// Insert post categories
$this->insert_categories($post_id, $item);
// Insert post tags
$this->insert_tags($post_id, $item);
// Import post images
$this->import_post_images($post_id);
// Import post featured image
if ($item['coverMedia']['image']['url']) {
$attachment_id = $this->import_image_by_url($item['coverMedia']['image']['url'], $post_id);
// If attachment is created successfully set as featured image
if ($attachment_id) {
set_post_thumbnail($post_id, $attachment_id);
}
}
// Fires after a post has been successfully inserted.
do_action('WixBlogImport/after_insert_post', $post_id, $item);
}
}
}
/**
* Insert Post
*
* @param array $item Post Item
*
* @return int|WP_Error
*/
public function insert_post($item)
{
// Get item content
$content = $item['content'];
// Decode content
$content_decode = json_decode($content, true);
// Get entity map
$entity_map = $content_decode['entityMap'];
// Modify entity item for DraftConverter
if ($entity_map) {
foreach ($entity_map as $key => $value) {
if ($value['type'] == 'wix-draft-plugin-image') {
$entity_map[$key]['type'] = 'IMAGE';
$entity_map[$key]['data']['url'] = $this->static_media_url . '/' . $value['data']['src']['id'];
unset($entity_map[$key]['data']['src']);
}
}
$content_decode['entityMap'] = $entity_map;
}
// Encode content
$content = json_encode($content_decode);
// Convert content to HTML
$content = DraftConverter::convertFromJson($content);
$converter = new Converter;
$content = $converter
->setState($content)
->toHtml();
// Create post
$post_data = [
'post_title' => $item['title'],
'post_content' => $content,
'post_status' => 'publish',
'post_type' => 'post',
'post_date' => $item['firstPublishedDate'],
];
/**
* Filters post data before inserting post
*/
$post_data = apply_filters('WixBlogImport/post_data', $post_data, $item);
$post_id = wp_insert_post($post_data);
return $post_id;
}
/**
* Insert Post Categories
*
* @param int $post_id Post ID
* @param array $item Post Item
*
* @return void
*/
public function insert_categories($post_id, $item)
{
// Get item categories
$categories = $item['categoryIds'];
if (!$categories) {
return;
}
// Loop through categories
// $category_id = wix id
foreach ($categories as $category_id) {
// Find category by wix_id
$term_args = array(
'hide_empty' => false,
'meta_query' => array(
array(
'key' => 'wix_id',
'value' => $category_id,
'compare' => '='
)
),
'taxonomy' => 'category',
);
$terms = get_terms($term_args);
if (!empty($terms)) {
$terms = array_column($terms, 'term_id');
wp_set_post_categories($post_id, $terms);
}
}
}
/**
* Insert Post Tags
*
* @param int $post_id Post ID
* @param array $item Post Item
*
* @return void
*/
public function insert_tags($post_id, $item)
{
// Get item tags
$tags = $item['tagIds'];
if (!$tags) {
return;
}
// Keep added tags in array
$added_tags = [];
// Loop through tags
foreach ($tags as $tag_id) {
// Get tag data
$get_tag = $this->get_tag($tag_id);
// Check for error
if (is_wp_error($get_tag)) {
continue;
}
$tag = $get_tag->tag;
// push tag label to added tags array
$added_tags[] = $tag->label;
}
// Add tags to post
wp_set_post_tags($post_id, $added_tags);
}
/**
* Insert Post Metas
*
* @param int $post_id Post ID
* @param array $item Post Item
*
* @return void
*/
public function insert_metas($post_id, $item)
{
do_action('WixBlogImport/insert_metas', $post_id, $item);
add_post_meta($post_id, 'wix_id', $item['id']);
}
/**
* Save to server images from post content and replace images url with local images url
*
* @param int $post_id Post ID
*
* @return void
*/
public function import_post_images($post_id)
{
// Get post content
$post_content = get_post_field('post_content', $post_id);
// Find all <img tags in content
$post_content = preg_replace_callback('/<img[^>]+>/i', function ($matches) use ($post_id) {
// Get <img tag
$img = $matches[0];
// Get image url
$src = preg_match('/src="([^"]+)"/i', $img, $match);
$src = $match[1];
// Import image to server
$attachment_id = $this->import_image_by_url($src, $post_id);
// If attachment is created successfully change image url to local url
if ($attachment_id) {
$img = str_replace($src, wp_get_attachment_url($attachment_id), $img);
}
return $img;
}, $post_content);
// Update post content
wp_update_post([
'ID' => $post_id,
'post_content' => $post_content,
]);
}
/**
* Download image from url and save to server
*
* @param string $url Image url
* @param int $post_id Post ID
*
* @return int|WP_Error
*/
public function import_image_by_url($url, $post_id)
{
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($url);
$file_ext = pathinfo($url, PATHINFO_EXTENSION);
// Extract file name from image url
$filename = preg_match('/media\/(.*?)~/', $url, $match);
$filename = $match[1];
$filename = $filename . '.' . $file_ext;
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// check if the file already exists
if (file_exists($file)) {
// attachment id
$file_url = $upload_dir['url'] . '/' . $filename;
$attachment_id = attachment_url_to_postid($file_url);
return $attachment_id;
}
// Save image to server
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert attachment to database
$attach_id = wp_insert_attachment($attachment, $file, $post_id);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
return $attach_id;
}
}