-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_run_import.php
98 lines (75 loc) · 2.57 KB
/
data_run_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
<?php
function handle_local_attachment($local_media_url) {
// Temporarily storing the media in WP.
$tmp = download_url($local_media_url);
$file_info = array(
'name' => basename($local_media_url),
'tmp_name' => $tmp
);
// Check for download errors. If there are error unlink the temp file name.
if (is_wp_error($tmp)) {
@unlink($file_info['tmp_name']);
return $tmp;
}
// $post_id set to '0' to not attach it to any particular post.
$post_id = 0;
$id = media_handle_sideload($file_info, $post_id);
// Check for upload errors. If there are error unlink the temp file name.
if (is_wp_error($id)) {
@unlink($file_info['tmp_name']);
return $id;
}
return $id;
}
function igi_run_import() {
$data = igi_get_data();
if ($data && $data['post_count_valid'] > 0) :
foreach ($data['posts'] as $key => $post) :
ob_start();
?>
<!-- wp:quote -->
<blockquote class="wp-block-quote"><!-- wp:paragraph -->
<p><?php echo $post['caption']; ?></p>
<!-- /wp:paragraph -->
</blockquote>
<!-- /wp:quote -->
<!-- wp:group {"className":"has-#-items"} -->
<div class="wp-block-group has-<?php echo $post['media_count_valid']; ?>-items">
<?php
foreach ($post['media'] as $m) {
$new_media_id = handle_local_attachment($m);
$new_media_url = wp_get_attachment_url($new_media_id);
if (igi_get_file_extension($m) == 'jpg' || igi_get_file_extension($m) == 'webp') {
?>
<!-- wp:image {"id":<?php echo $new_media_id; ?>} -->
<figure class="wp-block-image"><img src="<?php echo $new_media_url; ?>" class="wp-image-<?php echo $new_media_id; ?>" /></figure>
<!-- /wp:image -->
<?php
} // images
if (igi_get_file_extension($m) == 'mp4') {
?>
<!-- wp:video {"id":<?php echo $new_media_id; ?>} -->
<figure class="wp-block-video"><video controls src="<?php echo $new_media_url; ?>"></video></figure>
<!-- /wp:video -->
<?php
} // video
} // foreach media
?>
</div>
<!-- /wp:group -->
<?php
$post_content = ob_get_clean();
$post_atts = array(
'post_type' => 'post',
'post_status' => 'publish',
'post_date' => $post['date_forWP'],
'post_title' => $post['title'],
'post_content' => $post_content,
);
$new_post_id = wp_insert_post($post_atts);
endforeach; // each $data['posts']
echo ('<p class="igi-status-note status success"><strong>Success</strong>: Import comepleted successfully.</p>');
else : // if $data
echo ('<p class="igi-status-note error"><strong>Error</strong>: Import unable to run.</p>');
endif; // if $data
} // igi_run_import()