header(); $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step']; switch ( $step ) { case 0: $this->greet(); break; case 1: check_admin_referer( 'import-attachment-files' ); set_time_limit(0); $this->import(); break; } $this->footer(); } /** * The main controller for the actual import stage. * * @param string $file Path to the WXR file for importing */ function import() { add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) ); add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) ); $this->import_start(); wp_suspend_cache_invalidation( true ); $this->process_posts(); wp_suspend_cache_invalidation( false ); $this->import_end(); } /** * Parses the WXR file and prepares us for the task of processing parsed data * * @param string $file Path to the WXR file for importing */ function import_start() { if (empty($_POST['server_url'])) { echo '
' . __( 'Sorry, there has been an error.', 'attachment-files-importer' ) . '
';
echo __( 'The server URL is not valid, please try again.', 'attachment-files-importer' ) . '
' . __( 'All done.', 'attachment-files-importer' ) . ' ' . __( 'Have fun!', 'attachment-files-importer' ) . '' . '
'; do_action( 'import_end' ); } /** * Create new posts based on import information * * Posts marked as having a parent which doesn't exist will become top level items. * Doesn't create a new post if: the post type doesn't exist, the given post ID * is already noted as imported or a post with the same title and date already exists. * Note that new/updated terms, comments and meta are imported for the last of the above. */ function process_posts() { $this->posts = apply_filters( 'attachment_files_importer_posts', $this->posts ); $stats = array( 'imported' => 0, 'already_exists' => 0, 'other_error' => 0, ); foreach ( $this->posts as $post ) { $post = apply_filters( 'attachment_files_importer_post_data_raw', $post ); if ( $post['status'] == 'auto-draft' || 'attachment' != $post['post_type'] ) continue; $local_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; $remote_url = $this->base_url . parse_url($local_url, PHP_URL_PATH); // try to use _wp_attached file for upload folder placement to ensure the same location as the export site // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() $postdata = array('upload_date' => $post['post_date']); if ( isset( $post['postmeta'] ) ) { foreach( $post['postmeta'] as $meta_key => $arr_value ) { if ( $meta_key == '_wp_attached_file' ) { if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $arr_value[0], $matches ) ) $postdata['upload_date'] = $matches[0]; break; } } } $process_result = $this->process_attachment( $postdata, $remote_url ); if (is_wp_error($process_result)) { if ( array_key_exists('upload_dir_error', $process_result->errors) && in_array('File already exists', $process_result->errors['upload_dir_error']) ) { $stats['already_exists']++; if (ATTACHMENT_FILES_IMPORT_DEBUG) { echo '';
switch($stats['imported']) {
case 0:
$msg[] = sprintf(__('No file has been imported from %s.'), $file_html_link);
break;
case 1:
$msg[] = sprintf(__('One file has been imported from %s.'), $file_html_link);
break;
default:
$msg[] = sprintf(__('%s files has been imported from %s.'), $stats['imported'], $file_html_link);
}
switch($stats['already_exists']) {
case 0:
break;
case 1:
$msg[] = __('One file was already there.');
break;
default:
$msg[] = sprintf(__('%s files were already there.'), $stats['already_exists']);
}
$msg[] = __('You\'re synced!');
echo implode('
', $msg);
echo '
'; printf( __( 'A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'attachment-files-importer' ), $update->update->new_version ); echo '
'.__( 'Indicate the URL of the server that contain the attachment files corresponding to the attachments of this site and we"ll download them into this site.', 'attachment-files-importer' ).'
'; echo ''.__( 'Enter the URL of the server, then click Import.', 'attachment-files-importer' ).'
'; $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() ); $size = size_format( $bytes ); $upload_dir = wp_upload_dir(); if ( ! empty( $upload_dir['error'] ) ) : ?>