/
main.js
226 lines (190 loc) · 7.06 KB
/
main.js
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
jQuery(document).ready(function($){
$( document ).tooltip();
var divInit = $( '#attachment-importer-init' );
if ( ! window.FileReader ){
$.get(
ajaxurl,
{action:'attachment_importer_init_failure'},
function( data ){
$( data ).appendTo( divInit );
});
} else{
$.get(
ajaxurl,
{action:'attachment_importer_init_success'},
function( data ){
$( data ).appendTo( divInit );
});
$( document ).on('click', '.button', function(){
var input = $( '#file' ).get(0).files[0],
reader = new FileReader(),
divOutput = $( '#attachment-importer-output' ),
author1 = $( "input[name='author']:checked" ).val(),
author2 = $( "select[name='user']" ).val(),
delay = ( $( "input[name='delay']" ).is( ':checked' ) ? 5000 : 0 ),
progressBar = $( "#attachment-importer-progressbar" ),
progressLabel = $( "#attachment-importer-progresslabel" );
if ( ! input ){
alert( aiL10n.emptyInput );
} else {
divOutput.empty();
$( function(){
progressBar.progressbar({
value: false
});
progressLabel.text( aiL10n.parsing );
});
reader.readAsText(input);
reader.onload = function(e){
var file = e.target.result,
parser = new DOMParser(),
xml = parser.parseFromString( file, "text/xml" ),
url = [],
title = [],
link = [],
pubDate = [],
creator = [],
guid = [],
postID = [],
postDate = [],
postDateGMT = [],
commentStatus = [],
pingStatus = [],
postName = [],
status = [],
postParent = [],
menuOrder = [],
postType = [],
postPassword = [],
isSticky = [];
$( xml ).find( 'item' ).each(function(){
var xml_post_type = $( this ).find( 'wp\\:post_type, post_type' ).text();
if( xml_post_type == 'attachment' ){ // We're only looking for image attachments.
url.push( $( this ).find( 'wp\\:attachment_url, attachment_url' ).text() );
title.push( $( this ).find( 'title' ).text() );
link.push( $( this ).find( 'link' ).text() );
pubDate.push( $( this ).find( 'pubDate' ).text() );
creator.push( $( this ).find( 'dc\\:creator, creator' ).text() );
guid.push( $( this ).find( 'guid' ).text() );
postID.push( $( this ).find( 'wp\\:post_id, post_id' ).text() );
postDate.push( $( this ).find( 'wp\\:post_date, post_date' ).text() );
postDateGMT.push( $( this ).find( 'wp\\:post_date_gmt, post_date_gmt' ).text() );
commentStatus.push( $( this ).find( 'wp\\:comment_status, comment_status' ).text() );
pingStatus.push( $( this ).find( 'wp\\:ping_status, ping_status' ).text() );
postName.push( $( this ).find( 'wp\\:post_name, post_name' ).text() );
status.push( $( this ).find( 'wp\\:status, status' ).text() );
postParent.push( $( this ).find( 'wp\\:post_parent, post_parent' ).text() );
menuOrder.push( $( this ).find( 'wp\\:menu_order, menu_order' ).text() );
postType.push( xml_post_type );
postPassword.push( $( this ).find( 'wp\\:post_password, post_password' ).text() );
isSticky.push( $( this ).find( 'wp\\:is_sticky, is_sticky' ).text() );
}
});
var pbMax = postType.length;
$( function(){
progressBar.progressbar({
value:0,
max: postType.length,
complete: function(){
progressLabel.text( aiL10n.done );
}
});
});
// Define counter variable outside the import attachments function
// to keep track of the failed attachments to re-import them.
var failedAttachments = 0;
function import_attachments( i ){
progressLabel.text( aiL10n.importing + '" ' + title[i] + '". ' + aiL10n.progress + progressBar.progressbar( "value" ) + "/" + pbMax );
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'attachment_importer_upload',
_ajax_nonce: aiSecurity.nonce,
author1:author1,
author2:author2,
url:url[i],
title:title[i],
link:link[i],
pubDate:pubDate[i],
creator:creator[i],
guid:guid[i],
post_id:postID[i],
post_date:postDate[i],
post_date_gmt:postDateGMT[i],
comment_status:commentStatus[i],
ping_status:pingStatus[i],
post_name:postName[i],
status:status[i],
post_parent:postParent[i],
menu_order:menuOrder[i],
post_type:postType[i],
post_password:postPassword[i],
is_sticky:isSticky[i]
}
})
.done(function( data, status, xhr ){
// Parse the response.
var obj = $.parseJSON( data );
// If error shows the server did not respond,
// try the upload again, to a max of 3 tries.
if( obj.message == "Remote server did not respond" && failedAttachments < 5 ){
failedAttachments++;
progressLabel.text( aiL10n.retrying + '"' + title[i] + '". ' + aiL10n.progress + progressBar.progressbar( "value" ) + "/" + pbMax );
setTimeout( function(){
import_attachments( i );
}, 5000 );
}
// If a non-fatal error occurs, note it and move on.
else if( obj.type == "error" && !obj.fatal ){
$( '<p>' + obj.text + '</p>' ).appendTo( divOutput );
next_image(i);
}
// If a fatal error occurs, stop the program and print the error to the browser.
else if( obj.fatal ){
progressBar.progressbar( "value", pbMax );
progressLabel.text( aiL10n.fatalUpload );
$( '<div class="' + obj.type + '">' + obj.text +'</div>' ).appendTo( divOutput );
return false;
}
else { // Moving on.
next_image(i);
}
})
.fail(function( xhr, status, error ){
console.error(status);
console.error(error);
progressBar.progressbar( "value", pbMax );
progressLabel.text( aiL10n.pbAjaxFail );
$( '<div class="error">' + aiL10n.ajaxFail +'</div>' ).appendTo( divOutput );
});
}
function next_image( i ){
// Increment the internal counter and progress bar.
i++;
progressBar.progressbar( "value", progressBar.progressbar( "value" ) + 1 );
failedAttachments = 0;
// If every thing is normal, but we still have posts to process,
// then continue with the program.
if( postType[i] ){
setTimeout( function(){
import_attachments( i )
}, delay );
}
// Getting this far means there are no more attachments, so stop the program.
else {
return false;
}
}
if( postType[0] ){
import_attachments( 0 );
} else{
progressBar.progressbar( "value", pbMax );
progressLabel.text( aiL10n.pbAjaxFail );
$( '<div class="error">' + aiL10n.noAttachments +'</div>' ).appendTo( divOutput );
}
}
}
});
}
});