-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
329 lines (257 loc) · 12.5 KB
/
index.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
<?php
/**
* Plugin Name: Attachment Importer
* Plugin URI: http://github.com/toastedlime/wp-attachment-importer
* Description: Imports images from a WordPress XML export file. This is useful if you have a large number of images to import and your server times out while importing using the WordPress Importer plugin.
* Version: 0.6.0
* Author: Toasted Lime
* Author URI: http://www.toastedlime.com
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: attachment-importer
*/
function attachment_importer_scripts(){
wp_register_script( 'attachment-importer-js', plugins_url( 'main.js', __FILE__ ), array( 'jquery', 'jquery-ui-tooltip', 'jquery-ui-progressbar' ), 20140421, true );
}
function attachment_importer_add_page(){
register_importer( 'attachment-importer', 'Attachment Importer', 'Import attachments from a WordPress export file.', 'attachment_importer_options_page' );
}
function attachment_importer_options_page(){
wp_enqueue_script( 'attachment-importer-js' );
wp_localize_script( 'attachment-importer-js', 'aiL10n', array(
'emptyInput' => __( 'Please select a file.', 'attachment-importer' ),
'noAttachments' => __( 'There were no attachment files found in the import file.', 'attachment-importer' ),
'parsing' => __( 'Parsing the file.', 'attachment-importer' ),
'importing' => __( 'Importing file ', 'attachment-importer' ),
'progress' => __( 'Overall progress: ', 'attachment-importer' ),
'retrying' => __( 'An error occured. In 5 seconds, retrying file ', 'attachment-importer' ),
'done' => __( 'All done!', 'attachment-importer' ),
'ajaxFail' => __( 'There was an error connecting to the server.', 'attachment-importer' ),
'pbAjaxFail' => __( 'The program could not run. Check the error log below or your JavaScript console for more information', 'attachment-importer' ),
'fatalUpload' => __( 'There was a fatal error. Check the last entry in the error log below.', 'attachment-importer' )
) );
wp_localize_script( 'attachment-importer-js', 'aiSecurity', array(
'nonce' => wp_create_nonce( 'import-attachment-plugin' )
) );
wp_enqueue_style( 'jquery-ui', plugins_url( 'inc/jquery-ui.css', __FILE__ ) );
wp_enqueue_style( 'attachment-importer', plugins_url( 'inc/style.css', __FILE__ ) );
?>
<div class="wrap">
<h2><?php _e( 'Attachment Importer', 'attachment-importer' ); ?></h2>
<noscript>
<div class="error">
<p><?php _e( 'Sorry, but your browser doesn\'t have JavaScript enabled, and this plugin requires JavaScript.', 'attachment-importer' ); ?></p>
<p><?php _e( 'Please enable JavaScript for this site to continue.', 'attachment-importer' ); ?></p>
</div>
</noscript>
<div id="attachment-importer-init"></div>
<div id="attachment-importer-progressbar"><div id="attachment-importer-progresslabel"></div></div>
<div id="attachment-importer-output"></div>
</div>
<?php
}
add_action( 'admin_enqueue_scripts', 'attachment_importer_scripts' );
add_action( 'admin_menu', 'attachment_importer_add_page' );
add_action( 'wp_ajax_attachment_importer_init_success', 'attachment_importer_init_success' );
add_action( 'wp_ajax_attachment_importer_init_failure', 'attachment_importer_init_failure' );
add_action( 'wp_ajax_attachment_importer_upload', 'attachment_importer_uploader' );
// AJAX functions are below this line.
function attachment_importer_init_success(){
?>
<p><?php _e( 'Select the WordPress eXtended RSS (WXR) file and we\'ll try to get the images and upload them to your blog.', 'attachment-importer' ); ?></p>
<p><?php _e( 'Choose a WXR (.xml) file from your computer and press upload.', 'attachment-importer' ); ?></p>
<p><input type="file" name="file" id="file"/></p>
<p><?php _e( 'Attribute uploaded images to:', 'attachment-importer' ); ?><br/>
<input type="radio" name="author" value=1 checked /> <?php _e( 'Current User', 'attachment-importer' ); ?><br/>
<input type="radio" name="author" value=2 /> <?php _e( 'User in the import file', 'attachment-importer'); ?><br/>
<input type="radio" name="author" value=3 /> <?php _e( 'Select User:', 'attachment-importer' ); ?> <?php wp_dropdown_users(); ?>
<p><input type="checkbox" name="delay" /> <?php _e( 'Delay file requests by at least five seconds.', 'attachment-importer' ); ?> <a href="#" title="<?php _e( 'This delay can be useful to mitigate hosts that throttle traffic when too many requests are detected from an IP address and mistaken for a DDOS attack.', 'attachment-importer' ); ?>" style="text-decoration:none;"><span class="dashicons dashicons-editor-help"></span></a></p>
<p><?php submit_button( _x( 'Upload', 'A button which will submit the attachment for processing when clicked.', 'attachment-importer'), 'secondary', 'upload', false ); ?></p>
<?php
die();}
function attachment_importer_init_failure(){
?>
<div class="error">
<p><?php _e( 'Sorry, but you\'re using an <strong>outdated</strong> browser that doesn\'t support the features required to use this plugin.', 'attachment-importer' ); ?></p>
<p><?php echo sprintf( __( 'You must <a href="%s">upgrade your browser</a> in order to use this plugin.', 'attachment-importer' ), 'http://browsehappy.com' ); ?></p>
</div>
<?php
die();}
function attachment_importer_uploader(){
// check nonce before doing anything else
if( !check_ajax_referer( 'import-attachment-plugin', false, false ) ){
$nonce_error = new WP_Error( 'nonce_error', __('Are you sure you want to do this?', 'attachment-importer') );
echo json_encode ( array(
'fatal' => true,
'type' => 'error',
'code' => $nonce_error->get_error_code(),
'message' => $nonce_error->get_error_message(),
'text' => sprintf( __( 'The <a href="%1$s">security key</a> provided with this request is invalid. Is someone trying to trick you to upload something you don\'t want to? If you really meant to take this action, reload your browser window and try again. (<strong>%2$s</strong>: %3$s)', 'attachment-importer' ), 'http://codex.wordpress.org/WordPress_Nonces', $nonce_error->get_error_code(), $nonce_error->get_error_message() )
) );
die();
}
$parameters = array(
'url' => $_POST['url'],
'post_title' => $_POST['title'],
'link' => $_POST['link'],
'pubDate' => $_POST['pubDate'],
'post_author' => $_POST['creator'],
'guid' => $_POST['guid'],
'import_id' => $_POST['post_id'],
'post_date' => $_POST['post_date'],
'post_date_gmt' => $_POST['post_date_gmt'],
'comment_status' => $_POST['comment_status'],
'ping_status' => $_POST['ping_status'],
'post_name' => $_POST['post_name'],
'post_status' => $_POST['status'],
'post_parent' => $_POST['post_parent'],
'menu_order' => $_POST['menu_order'],
'post_type' => $_POST['post_type'],
'post_password' => $_POST['post_password'],
'is_sticky' => $_POST['is_sticky'],
'attribute_author1' => $_POST['author1'],
'attribute_author2' => $_POST['author2']
);
function process_attachment( $post, $url ) {
$pre_process = pre_process_attachment( $post, $url );
if( is_wp_error( $pre_process ) )
return array(
'fatal' => false,
'type' => 'error',
'code' => $pre_process->get_error_code(),
'message' => $pre_process->get_error_message(),
'text' => sprintf( __( '%1$s was not uploaded. (<strong>%2$s</strong>: %3$s)', 'attachment-importer' ), $post['post_title'], $pre_process->get_error_code(), $pre_process->get_error_message() )
);
// if the URL is absolute, but does not contain address, then upload it assuming base_site_url
if ( preg_match( '|^/[\w\W]+$|', $url ) )
$url = rtrim( $this->base_url, '/' ) . $url;
$upload = fetch_remote_file( $url, $post );
if ( is_wp_error( $upload ) )
return array(
'fatal' => ( $upload->get_error_code() == 'upload_dir_error' && $upload->get_error_message() != 'Invalid file type' ? true : false ),
'type' => 'error',
'code' => $upload->get_error_code(),
'message' => $upload->get_error_message(),
'text' => sprintf( __( '%1$s could not be uploaded because of an error. (<strong>%2$s</strong>: %3$s)', 'attachment-importer' ), $post['post_title'], $upload->get_error_code(), $upload->get_error_message() )
);
if ( $info = wp_check_filetype( $upload['file'] ) )
$post['post_mime_type'] = $info['type'];
else {
$upload = new WP_Error( 'attachment_processing_error', __('Invalid file type', 'attachment-importer') );
return array(
'fatal' => false,
'type' => 'error',
'code' => $upload->get_error_code(),
'message' => $upload->get_error_message(),
'text' => sprintf( __( '%1$s could not be uploaded because of an error. (<strong>%2$s</strong>: %3$s)', 'attachment-importer' ), $post['post_title'], $upload->get_error_code(), $upload->get_error_message() )
);
}
$post['guid'] = $upload['url'];
// Set author per user options.
switch( $post['attribute_author1'] ){
case 1: // Attribute to current user.
$post['post_author'] = (int) wp_get_current_user()->ID;
break;
case 2: // Attribute to user in import file.
if( !username_exists( $post['post_author'] ) )
wp_create_user( $post['post_author'], wp_generate_password() );
$post['post_author'] = (int) username_exists( $post['post_author'] );
break;
case 3: // Attribute to selected user.
$post['post_author'] = (int) $post['attribute_author2'];
break;
}
// as per wp-admin/includes/upload.php
$post_id = wp_insert_attachment( $post, $upload['file'] );
wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
// remap image URL's
backfill_attachment_urls( $url, $upload['url'] );
return array(
'fatal' => false,
'type' => 'updated',
'text' => sprintf( __( '%s was uploaded successfully', 'attachment-importer' ), $post['post_title'] )
);
}
function pre_process_attachment( $post, $url ){
global $wpdb;
$imported = $wpdb->get_results(
$wpdb->prepare(
"
SELECT ID, post_date_gmt, guid
FROM $wpdb->posts
WHERE post_type = 'attachment'
AND post_title = %s
",
$post['post_title']
)
);
if( $imported ){
foreach( $imported as $attachment ){
if( basename( $url ) == basename( $attachment->guid ) ){
if( $post['post_date_gmt'] == $attachment->post_date_gmt ){
$headers = wp_get_http( $url );
if( filesize( get_attached_file( $attachment->ID ) ) == $headers['content-length'] ){
return new WP_Error( 'duplicate_file_notice', __( 'File already exists', 'attachment-importer' ) );
}
}
}
}
}
return false;
}
function fetch_remote_file( $url, $post ) {
// extract the file name and extension from the url
$file_name = basename( $url );
// get placeholder file in the upload dir with a unique, sanitized filename
$upload = wp_upload_bits( $file_name, 0, '', $post['post_date'] );
if ( $upload['error'] )
return new WP_Error( 'upload_dir_error', $upload['error'] );
// fetch the remote url and write it to the placeholder file
$headers = wp_get_http( $url, $upload['file'] );
// request failed
if ( ! $headers ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', __('Remote server did not respond', 'attachment-importer') );
}
// make sure the fetch was successful
if ( $headers['response'] != '200' ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'attachment-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) );
}
$filesize = filesize( $upload['file'] );
if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'attachment-importer') );
}
if ( 0 == $filesize ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'attachment-importer') );
}
return $upload;
}
function backfill_attachment_urls( $from_url, $to_url ) {
global $wpdb;
// remap urls in post_content
$wpdb->query(
$wpdb->prepare(
"
UPDATE {$wpdb->posts}
SET post_content = REPLACE(post_content, %s, %s)
",
$from_url, $to_url
)
);
// remap enclosure urls
$result = $wpdb->query(
$wpdb->prepare(
"
UPDATE {$wpdb->postmeta}
SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'
",
$from_url, $to_url
)
);
}
$remote_url = ! empty($parameters['attachment_url']) ? $parameters['attachment_url'] : $parameters['guid'];
echo json_encode( process_attachment( $parameters, $remote_url ) );
die();}