wp/wp-content/plugins/wordpress-importer/wordpress-importer.php
author ymh <ymh.work@gmail.com>
Wed, 06 Nov 2013 03:21:17 +0000
changeset 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
first import
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Plugin Name: WordPress Importer
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
Plugin URI: http://wordpress.org/extend/plugins/wordpress-importer/
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
Description: Import posts, pages, comments, custom fields, categories, tags and more from a WordPress export file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
Author: wordpressdotorg
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
Author URI: http://wordpress.org/
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
Version: 0.6.1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
Text Domain: wordpress-importer
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
*/
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
	return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
/** Display verbose errors */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
define( 'IMPORT_DEBUG', false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
// Load Importer API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
require_once ABSPATH . 'wp-admin/includes/import.php';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
if ( ! class_exists( 'WP_Importer' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
	$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
	if ( file_exists( $class_wp_importer ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
		require $class_wp_importer;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
// include WXR file parsers
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
require dirname( __FILE__ ) . '/parsers.php';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
 * WordPress Importer class for managing the import process of a WXR file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
 * @subpackage Importer
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
if ( class_exists( 'WP_Importer' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
class WP_Import extends WP_Importer {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
	var $max_wxr_version = 1.2; // max. supported WXR version
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
	var $id; // WXR attachment ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	// information to import from WXR file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
	var $version;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
	var $authors = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
	var $posts = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	var $terms = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
	var $categories = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	var $tags = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
	var $base_url = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
	// mappings from old information to new
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
	var $processed_authors = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
	var $author_mapping = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	var $processed_terms = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
	var $processed_posts = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
	var $post_orphans = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
	var $processed_menu_items = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	var $menu_item_orphans = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	var $missing_menu_items = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
	var $fetch_attachments = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
	var $url_remap = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
	var $featured_images = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
	function WP_Import() { /* nothing */ }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	 * Registered callback function for the WordPress Importer
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	 * Manages the three separate stages of the WXR import process
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
	function dispatch() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
		$this->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
		$step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
		switch ( $step ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
			case 0:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
				$this->greet();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
			case 1:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
				check_admin_referer( 'import-upload' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
				if ( $this->handle_upload() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
					$this->import_options();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
			case 2:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
				check_admin_referer( 'import-wordpress' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
				$this->fetch_attachments = ( ! empty( $_POST['fetch_attachments'] ) && $this->allow_fetch_attachments() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
				$this->id = (int) $_POST['import_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
				$file = get_attached_file( $this->id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
				set_time_limit(0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
				$this->import( $file );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
		$this->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
	 * The main controller for the actual import stage.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	 * @param string $file Path to the WXR file for importing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
	function import( $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
		add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		$this->import_start( $file );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
		$this->get_author_mapping();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		wp_suspend_cache_invalidation( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		$this->process_categories();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
		$this->process_tags();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		$this->process_terms();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
		$this->process_posts();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
		wp_suspend_cache_invalidation( false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
		// update incorrect/missing information in the DB
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		$this->backfill_parents();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		$this->backfill_attachment_urls();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		$this->remap_featured_images();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
		$this->import_end();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	 * Parses the WXR file and prepares us for the task of processing parsed data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
	 * @param string $file Path to the WXR file for importing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
	function import_start( $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
		if ( ! is_file($file) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
			echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
			echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
			$this->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
			die();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
		$import_data = $this->parse( $file );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
		if ( is_wp_error( $import_data ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
			echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
			echo esc_html( $import_data->get_error_message() ) . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
			$this->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
			die();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		$this->version = $import_data['version'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		$this->get_authors_from_import( $import_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		$this->posts = $import_data['posts'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
		$this->terms = $import_data['terms'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
		$this->categories = $import_data['categories'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
		$this->tags = $import_data['tags'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
		$this->base_url = esc_url( $import_data['base_url'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
		wp_defer_term_counting( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
		wp_defer_comment_counting( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		do_action( 'import_start' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
	 * Performs post-import cleanup of files and the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	function import_end() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
		wp_import_cleanup( $this->id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
		wp_cache_flush();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
		foreach ( get_taxonomies() as $tax ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
			delete_option( "{$tax}_children" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
			_get_term_hierarchy( $tax );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
		wp_defer_term_counting( false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
		wp_defer_comment_counting( false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
		echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		echo '<p>' . __( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
		do_action( 'import_end' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
	 * Handles the WXR upload and initial parsing of the file to prepare for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
	 * displaying author import options
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
	 * @return bool False if error uploading or invalid file, true otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
	function handle_upload() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
		$file = wp_import_handle_upload();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
		if ( isset( $file['error'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
			echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
			echo esc_html( $file['error'] ) . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
		} else if ( ! file_exists( $file['file'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
			echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
			printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'wordpress-importer' ), esc_html( $file['file'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
			echo '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
		$this->id = (int) $file['id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
		$import_data = $this->parse( $file['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
		if ( is_wp_error( $import_data ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
			echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
			echo esc_html( $import_data->get_error_message() ) . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
		$this->version = $import_data['version'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
		if ( $this->version > $this->max_wxr_version ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
			echo '<div class="error"><p><strong>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
			printf( __( 'This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'wordpress-importer' ), esc_html($import_data['version']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
			echo '</strong></p></div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
		$this->get_authors_from_import( $import_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
	 * Retrieve authors from parsed WXR data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
	 * Uses the provided author information from WXR 1.1 files
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
	 * or extracts info from each post for WXR 1.0 files
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
	 * @param array $import_data Data returned by a WXR parser
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
	function get_authors_from_import( $import_data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
		if ( ! empty( $import_data['authors'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
			$this->authors = $import_data['authors'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
		// no author information, grab it from the posts
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
			foreach ( $import_data['posts'] as $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
				$login = sanitize_user( $post['post_author'], true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
				if ( empty( $login ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
					printf( __( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
					echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
				if ( ! isset($this->authors[$login]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
					$this->authors[$login] = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
						'author_login' => $login,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
						'author_display_name' => $post['post_author']
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
					);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
	 * Display pre-import options, author importing/mapping and option to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
	 * fetch attachments
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
	function import_options() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
		$j = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
<form action="<?php echo admin_url( 'admin.php?import=wordpress&amp;step=2' ); ?>" method="post">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
	<?php wp_nonce_field( 'import-wordpress' ); ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
	<input type="hidden" name="import_id" value="<?php echo $this->id; ?>" />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
<?php if ( ! empty( $this->authors ) ) : ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
	<h3><?php _e( 'Assign Authors', 'wordpress-importer' ); ?></h3>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
	<p><?php _e( 'To make it easier for you to edit and save the imported content, you may want to reassign the author of the imported item to an existing user of this site. For example, you may want to import all the entries as <code>admin</code>s entries.', 'wordpress-importer' ); ?></p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
<?php if ( $this->allow_create_users() ) : ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
	<p><?php printf( __( 'If a new user is created by WordPress, a new password will be randomly generated and the new user&#8217;s role will be set as %s. Manually changing the new user&#8217;s details will be necessary.', 'wordpress-importer' ), esc_html( get_option('default_role') ) ); ?></p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
<?php endif; ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
	<ol id="authors">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
<?php foreach ( $this->authors as $author ) : ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
		<li><?php $this->author_select( $j++, $author ); ?></li>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
<?php endforeach; ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
	</ol>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
<?php endif; ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
<?php if ( $this->allow_fetch_attachments() ) : ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
	<h3><?php _e( 'Import Attachments', 'wordpress-importer' ); ?></h3>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
	<p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
		<input type="checkbox" value="1" name="fetch_attachments" id="import-attachments" />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
		<label for="import-attachments"><?php _e( 'Download and import file attachments', 'wordpress-importer' ); ?></label>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
	</p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
<?php endif; ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
	<p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Submit', 'wordpress-importer' ); ?>" /></p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
</form>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
	 * Display import options for an individual author. That is, either create
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
	 * a new user based on import info or map to an existing user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
	 * @param int $n Index for each author in the form
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
	 * @param array $author Author information, e.g. login, display name, email
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
	function author_select( $n, $author ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
		_e( 'Import author:', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
		echo ' <strong>' . esc_html( $author['author_display_name'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
		if ( $this->version != '1.0' ) echo ' (' . esc_html( $author['author_login'] ) . ')';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
		echo '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
		if ( $this->version != '1.0' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
			echo '<div style="margin-left:18px">';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
		$create_users = $this->allow_create_users();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
		if ( $create_users ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
			if ( $this->version != '1.0' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
				_e( 'or create new user with login name:', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
				$value = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
				_e( 'as a new user:', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
				$value = esc_attr( sanitize_user( $author['author_login'], true ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
			echo ' <input type="text" name="user_new['.$n.']" value="'. $value .'" /><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
		if ( ! $create_users && $this->version == '1.0' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
			_e( 'assign posts to an existing user:', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
			_e( 'or assign posts to an existing user:', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
		wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
		echo '<input type="hidden" name="imported_authors['.$n.']" value="' . esc_attr( $author['author_login'] ) . '" />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
		if ( $this->version != '1.0' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
			echo '</div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
	 * Map old author logins to local user IDs based on decisions made
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
	 * in import options form. Can map to an existing user, create a new user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
	 * or falls back to the current user in case of error with either of the previous
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
	function get_author_mapping() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
		if ( ! isset( $_POST['imported_authors'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
		$create_users = $this->allow_create_users();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
		foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
			// Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
			$santized_old_login = sanitize_user( $old_login, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
			$old_id = isset( $this->authors[$old_login]['author_id'] ) ? intval($this->authors[$old_login]['author_id']) : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
			if ( ! empty( $_POST['user_map'][$i] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
				$user = get_userdata( intval($_POST['user_map'][$i]) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
				if ( isset( $user->ID ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
					if ( $old_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
						$this->processed_authors[$old_id] = $user->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
					$this->author_mapping[$santized_old_login] = $user->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
			} else if ( $create_users ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
				if ( ! empty($_POST['user_new'][$i]) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
					$user_id = wp_create_user( $_POST['user_new'][$i], wp_generate_password() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
				} else if ( $this->version != '1.0' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
					$user_data = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
						'user_login' => $old_login,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
						'user_pass' => wp_generate_password(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
						'user_email' => isset( $this->authors[$old_login]['author_email'] ) ? $this->authors[$old_login]['author_email'] : '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
						'display_name' => $this->authors[$old_login]['author_display_name'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
						'first_name' => isset( $this->authors[$old_login]['author_first_name'] ) ? $this->authors[$old_login]['author_first_name'] : '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
						'last_name' => isset( $this->authors[$old_login]['author_last_name'] ) ? $this->authors[$old_login]['author_last_name'] : '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
					);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
					$user_id = wp_insert_user( $user_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
				if ( ! is_wp_error( $user_id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
					if ( $old_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
						$this->processed_authors[$old_id] = $user_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
					$this->author_mapping[$santized_old_login] = $user_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
					printf( __( 'Failed to create new user for %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html($this->authors[$old_login]['author_display_name']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
					if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
						echo ' ' . $user_id->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
					echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
			// failsafe: if the user_id was invalid, default to the current user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
			if ( ! isset( $this->author_mapping[$santized_old_login] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
				if ( $old_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
					$this->processed_authors[$old_id] = (int) get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
				$this->author_mapping[$santized_old_login] = (int) get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
	 * Create new categories based on import information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
	 * Doesn't create a new category if its slug already exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
	function process_categories() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
		$this->categories = apply_filters( 'wp_import_categories', $this->categories );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
		if ( empty( $this->categories ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
		foreach ( $this->categories as $cat ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
			// if the category already exists leave it alone
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
			$term_id = term_exists( $cat['category_nicename'], 'category' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
			if ( $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
				if ( is_array($term_id) ) $term_id = $term_id['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
				if ( isset($cat['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
					$this->processed_terms[intval($cat['term_id'])] = (int) $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
			$category_parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
			$category_description = isset( $cat['category_description'] ) ? $cat['category_description'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
			$catarr = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
				'category_nicename' => $cat['category_nicename'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
				'category_parent' => $category_parent,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
				'cat_name' => $cat['cat_name'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
				'category_description' => $category_description
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
			$id = wp_insert_category( $catarr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
			if ( ! is_wp_error( $id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
				if ( isset($cat['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
					$this->processed_terms[intval($cat['term_id'])] = $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
				printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
					echo ': ' . $id->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
				echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
		unset( $this->categories );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
	 * Create new post tags based on import information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
	 * Doesn't create a tag if its slug already exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
	function process_tags() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
		$this->tags = apply_filters( 'wp_import_tags', $this->tags );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
		if ( empty( $this->tags ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
		foreach ( $this->tags as $tag ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
			// if the tag already exists leave it alone
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
			$term_id = term_exists( $tag['tag_slug'], 'post_tag' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
			if ( $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
				if ( is_array($term_id) ) $term_id = $term_id['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
				if ( isset($tag['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
					$this->processed_terms[intval($tag['term_id'])] = (int) $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
			$tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
			$tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
			$id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
			if ( ! is_wp_error( $id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
				if ( isset($tag['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
					$this->processed_terms[intval($tag['term_id'])] = $id['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
				printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
					echo ': ' . $id->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
				echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
		unset( $this->tags );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
	 * Create new terms based on import information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
	 * Doesn't create a term its slug already exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
	function process_terms() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
		$this->terms = apply_filters( 'wp_import_terms', $this->terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
		if ( empty( $this->terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
		foreach ( $this->terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
			// if the term already exists in the correct taxonomy leave it alone
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
			$term_id = term_exists( $term['slug'], $term['term_taxonomy'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
			if ( $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
				if ( is_array($term_id) ) $term_id = $term_id['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
				if ( isset($term['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
					$this->processed_terms[intval($term['term_id'])] = (int) $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
			if ( empty( $term['term_parent'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
				$parent = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
				$parent = term_exists( $term['term_parent'], $term['term_taxonomy'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
				if ( is_array( $parent ) ) $parent = $parent['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
			$description = isset( $term['term_description'] ) ? $term['term_description'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
			$termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
			$id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
			if ( ! is_wp_error( $id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
				if ( isset($term['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
					$this->processed_terms[intval($term['term_id'])] = $id['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
				printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
					echo ': ' . $id->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
				echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
		unset( $this->terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
	 * Create new posts based on import information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
	 * Posts marked as having a parent which doesn't exist will become top level items.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
	 * Doesn't create a new post if: the post type doesn't exist, the given post ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
	 * is already noted as imported or a post with the same title and date already exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
	 * Note that new/updated terms, comments and meta are imported for the last of the above.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
	function process_posts() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
		$this->posts = apply_filters( 'wp_import_posts', $this->posts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
		foreach ( $this->posts as $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
			$post = apply_filters( 'wp_import_post_data_raw', $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
			if ( ! post_type_exists( $post['post_type'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
				printf( __( 'Failed to import &#8220;%s&#8221;: Invalid post type %s', 'wordpress-importer' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
					esc_html($post['post_title']), esc_html($post['post_type']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
				echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
				do_action( 'wp_import_post_exists', $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
			if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
			if ( $post['status'] == 'auto-draft' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
			if ( 'nav_menu_item' == $post['post_type'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
				$this->process_menu_item( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
			$post_type_object = get_post_type_object( $post['post_type'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
			$post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
			if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
				printf( __('%s &#8220;%s&#8221; already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
				echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
				$comment_post_ID = $post_id = $post_exists;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
				$post_parent = (int) $post['post_parent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
				if ( $post_parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
					// if we already know the parent, map it to the new local ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
					if ( isset( $this->processed_posts[$post_parent] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
						$post_parent = $this->processed_posts[$post_parent];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
					// otherwise record the parent for later
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
					} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
						$this->post_orphans[intval($post['post_id'])] = $post_parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
						$post_parent = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
				// map the post author
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
				$author = sanitize_user( $post['post_author'], true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
				if ( isset( $this->author_mapping[$author] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
					$author = $this->author_mapping[$author];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
					$author = (int) get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
				$postdata = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
					'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
					'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
					'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
					'post_status' => $post['status'], 'post_name' => $post['post_name'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
					'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
					'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
					'post_type' => $post['post_type'], 'post_password' => $post['post_password']
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
				);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
				$original_post_ID = $post['post_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
				$postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
				if ( 'attachment' == $postdata['post_type'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
					$remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
					// try to use _wp_attached file for upload folder placement to ensure the same location as the export site
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
					// e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
					$postdata['upload_date'] = $post['post_date'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
					if ( isset( $post['postmeta'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
						foreach( $post['postmeta'] as $meta ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
							if ( $meta['key'] == '_wp_attached_file' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
								if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
									$postdata['upload_date'] = $matches[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
								break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
							}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
						}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
					$comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
					$comment_post_ID = $post_id = wp_insert_post( $postdata, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
					do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
				if ( is_wp_error( $post_id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
					printf( __( 'Failed to import %s &#8220;%s&#8221;', 'wordpress-importer' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
						$post_type_object->labels->singular_name, esc_html($post['post_title']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
					if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
						echo ': ' . $post_id->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
					echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
				if ( $post['is_sticky'] == 1 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
					stick_post( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
			// map pre-import ID to local ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
			$this->processed_posts[intval($post['post_id'])] = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
			if ( ! isset( $post['terms'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
				$post['terms'] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
			$post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
			// add categories, tags and other terms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
			if ( ! empty( $post['terms'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
				$terms_to_set = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
				foreach ( $post['terms'] as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
					// back compat with WXR 1.0 map 'tag' to 'post_tag'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
					$taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
					$term_exists = term_exists( $term['slug'], $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
					$term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
					if ( ! $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
						$t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
						if ( ! is_wp_error( $t ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
							$term_id = $t['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
							do_action( 'wp_import_insert_term', $t, $term, $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
						} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
							printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($taxonomy), esc_html($term['name']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
							if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
								echo ': ' . $t->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
							echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
							do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
							continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
						}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
					$terms_to_set[$taxonomy][] = intval( $term_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
				foreach ( $terms_to_set as $tax => $ids ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
					$tt_ids = wp_set_post_terms( $post_id, $ids, $tax );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
					do_action( 'wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
				unset( $post['terms'], $terms_to_set );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
			if ( ! isset( $post['comments'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
				$post['comments'] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
			$post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
			// add/update comments
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
			if ( ! empty( $post['comments'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
				$num_comments = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
				$inserted_comments = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
				foreach ( $post['comments'] as $comment ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
					$comment_id	= $comment['comment_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
					$newcomments[$comment_id]['comment_post_ID']      = $comment_post_ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
					$newcomments[$comment_id]['comment_author']       = $comment['comment_author'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
					$newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
					$newcomments[$comment_id]['comment_author_IP']    = $comment['comment_author_IP'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
					$newcomments[$comment_id]['comment_author_url']   = $comment['comment_author_url'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
					$newcomments[$comment_id]['comment_date']         = $comment['comment_date'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
					$newcomments[$comment_id]['comment_date_gmt']     = $comment['comment_date_gmt'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
					$newcomments[$comment_id]['comment_content']      = $comment['comment_content'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
					$newcomments[$comment_id]['comment_approved']     = $comment['comment_approved'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
					$newcomments[$comment_id]['comment_type']         = $comment['comment_type'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
					$newcomments[$comment_id]['comment_parent'] 	  = $comment['comment_parent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
					$newcomments[$comment_id]['commentmeta']          = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
					if ( isset( $this->processed_authors[$comment['comment_user_id']] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
						$newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
				ksort( $newcomments );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
				foreach ( $newcomments as $key => $comment ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
					// if this is a new post we can skip the comment_exists() check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
					if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
						if ( isset( $inserted_comments[$comment['comment_parent']] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
							$comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
						$comment = wp_filter_comment( $comment );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
						$inserted_comments[$key] = wp_insert_comment( $comment );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
						do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
						foreach( $comment['commentmeta'] as $meta ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
							$value = maybe_unserialize( $meta['value'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
							add_comment_meta( $inserted_comments[$key], $meta['key'], $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
						}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
						$num_comments++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
				unset( $newcomments, $inserted_comments, $post['comments'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
			if ( ! isset( $post['postmeta'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
				$post['postmeta'] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
			$post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
			// add/update post meta
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
			if ( ! empty( $post['postmeta'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
				foreach ( $post['postmeta'] as $meta ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
					$key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
					$value = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
					if ( '_edit_last' == $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
						if ( isset( $this->processed_authors[intval($meta['value'])] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
							$value = $this->processed_authors[intval($meta['value'])];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
						else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
							$key = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
					if ( $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
						// export gets meta straight from the DB so could have a serialized string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
						if ( ! $value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
							$value = maybe_unserialize( $meta['value'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
						add_post_meta( $post_id, $key, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
						do_action( 'import_post_meta', $post_id, $key, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
						// if the post has a featured image, take note of this in case of remap
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
						if ( '_thumbnail_id' == $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
							$this->featured_images[$post_id] = (int) $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
		unset( $this->posts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
	 * Attempt to create a new menu item from import data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
	 * Fails for draft, orphaned menu items and those without an associated nav_menu
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
	 * or an invalid nav_menu term. If the post type or term object which the menu item
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
	 * represents doesn't exist then the menu item will not be imported (waits until the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
	 * end of the import to retry again before discarding).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
	 * @param array $item Menu item details from WXR file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
	function process_menu_item( $item ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
		// skip draft, orphaned menu items
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
		if ( 'draft' == $item['status'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
		$menu_slug = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
		if ( isset($item['terms']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
			// loop through terms, assume first nav_menu term is correct menu
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
			foreach ( $item['terms'] as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
				if ( 'nav_menu' == $term['domain'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
					$menu_slug = $term['slug'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
		// no nav_menu term associated with this menu item
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
		if ( ! $menu_slug ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
			_e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
			echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
		$menu_id = term_exists( $menu_slug, 'nav_menu' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
		if ( ! $menu_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
			printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
			echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
			$menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
		foreach ( $item['postmeta'] as $meta )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
			$$meta['key'] = $meta['value'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
		if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
			$_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
		} else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
			$_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
		} else if ( 'custom' != $_menu_item_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
			// associated object is missing or not imported yet, we'll retry later
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
			$this->missing_menu_items[] = $item;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
		if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
			$_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
		} else if ( $_menu_item_menu_item_parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
			$this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
			$_menu_item_menu_item_parent = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
		// wp_update_nav_menu_item expects CSS classes as a space separated string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
		$_menu_item_classes = maybe_unserialize( $_menu_item_classes );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
		if ( is_array( $_menu_item_classes ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
			$_menu_item_classes = implode( ' ', $_menu_item_classes );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
		$args = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
			'menu-item-object-id' => $_menu_item_object_id,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
			'menu-item-object' => $_menu_item_object,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
			'menu-item-parent-id' => $_menu_item_menu_item_parent,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
			'menu-item-position' => intval( $item['menu_order'] ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
			'menu-item-type' => $_menu_item_type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
			'menu-item-title' => $item['post_title'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
			'menu-item-url' => $_menu_item_url,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
			'menu-item-description' => $item['post_content'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
			'menu-item-attr-title' => $item['post_excerpt'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
			'menu-item-target' => $_menu_item_target,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
			'menu-item-classes' => $_menu_item_classes,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
			'menu-item-xfn' => $_menu_item_xfn,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
			'menu-item-status' => $item['status']
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
		$id = wp_update_nav_menu_item( $menu_id, 0, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
		if ( $id && ! is_wp_error( $id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
			$this->processed_menu_items[intval($item['post_id'])] = (int) $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
	 * If fetching attachments is enabled then attempt to create a new attachment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
	 * @param array $post Attachment post details from WXR
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
	 * @param string $url URL to fetch attachment from
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
	 * @return int|WP_Error Post ID on success, WP_Error otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
	function process_attachment( $post, $url ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
		if ( ! $this->fetch_attachments )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
			return new WP_Error( 'attachment_processing_error',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
				__( 'Fetching attachments is not enabled', 'wordpress-importer' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
		// if the URL is absolute, but does not contain address, then upload it assuming base_site_url
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
		if ( preg_match( '|^/[\w\W]+$|', $url ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
			$url = rtrim( $this->base_url, '/' ) . $url;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
		$upload = $this->fetch_remote_file( $url, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
		if ( is_wp_error( $upload ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
			return $upload;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
		if ( $info = wp_check_filetype( $upload['file'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
			$post['post_mime_type'] = $info['type'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
			return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
		$post['guid'] = $upload['url'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
		// as per wp-admin/includes/upload.php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
		$post_id = wp_insert_attachment( $post, $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
		wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
		// remap resized image URLs, works by stripping the extension and remapping the URL stub.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
		if ( preg_match( '!^image/!', $info['type'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
			$parts = pathinfo( $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
			$name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
			$parts_new = pathinfo( $upload['url'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
			$name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
			$this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
		return $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
	 * Attempt to download a remote file attachment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
	 * @param string $url URL of item to fetch
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
	 * @param array $post Attachment details
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
	 * @return array|WP_Error Local file location details on success, WP_Error otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
	function fetch_remote_file( $url, $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
		// extract the file name and extension from the url
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
		$file_name = basename( $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
		// get placeholder file in the upload dir with a unique, sanitized filename
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
		$upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
		if ( $upload['error'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
			return new WP_Error( 'upload_dir_error', $upload['error'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
		// fetch the remote url and write it to the placeholder file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
		$headers = wp_get_http( $url, $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
		// request failed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
		if ( ! $headers ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
			@unlink( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
			return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
		// make sure the fetch was successful
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
		if ( $headers['response'] != '200' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
			@unlink( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
			return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
		$filesize = filesize( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
		if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
			@unlink( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
			return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
		if ( 0 == $filesize ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
			@unlink( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
			return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
		$max_size = (int) $this->max_attachment_size();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
		if ( ! empty( $max_size ) && $filesize > $max_size ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
			@unlink( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
			return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
		// keep track of the old and new urls so we can substitute them later
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
		$this->url_remap[$url] = $upload['url'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
		$this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
		// keep track of the destination if the remote url is redirected somewhere else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
		if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
			$this->url_remap[$headers['x-final-location']] = $upload['url'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
		return $upload;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
	 * Attempt to associate posts and menu items with previously missing parents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
	 * An imported post's parent may not have been imported when it was first created
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
	 * so try again. Similarly for child menu items and menu items which were missing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
	 * the object (e.g. post) they represent in the menu
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
	function backfill_parents() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   957
		// find parents for post orphans
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
		foreach ( $this->post_orphans as $child_id => $parent_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
			$local_child_id = $local_parent_id = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   960
			if ( isset( $this->processed_posts[$child_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   961
				$local_child_id = $this->processed_posts[$child_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
			if ( isset( $this->processed_posts[$parent_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
				$local_parent_id = $this->processed_posts[$parent_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
			if ( $local_child_id && $local_parent_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
				$wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
		// all other posts/terms are imported, retry menu items with missing associated object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
		$missing_menu_items = $this->missing_menu_items;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
		foreach ( $missing_menu_items as $item )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
			$this->process_menu_item( $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
		// find parents for menu item orphans
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
		foreach ( $this->menu_item_orphans as $child_id => $parent_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
			$local_child_id = $local_parent_id = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
			if ( isset( $this->processed_menu_items[$child_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
				$local_child_id = $this->processed_menu_items[$child_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
			if ( isset( $this->processed_menu_items[$parent_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
				$local_parent_id = $this->processed_menu_items[$parent_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
			if ( $local_child_id && $local_parent_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
				update_post_meta( $local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
	 * Use stored mapping information to update old attachment URLs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
	function backfill_attachment_urls() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
		// make sure we do the longest urls first, in case one is a substring of another
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
		uksort( $this->url_remap, array(&$this, 'cmpr_strlen') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
		foreach ( $this->url_remap as $from_url => $to_url ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
			// remap urls in post_content
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
			$wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
			// remap enclosure urls
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
			$result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
	 * Update _thumbnail_id meta to new, imported attachment IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
	function remap_featured_images() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
		// cycle through posts that have a featured image
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
		foreach ( $this->featured_images as $post_id => $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
			if ( isset( $this->processed_posts[$value] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
				$new_id = $this->processed_posts[$value];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
				// only update if there's a difference
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
				if ( $new_id != $value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
					update_post_meta( $post_id, '_thumbnail_id', $new_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
	 * Parse a WXR file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
	 * @param string $file Path to WXR file for parsing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
	 * @return array Information gathered from the WXR file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
	function parse( $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
		$parser = new WXR_Parser();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
		return $parser->parse( $file );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
	// Display import page title
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
	function header() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
		echo '<div class="wrap">';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
		screen_icon();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
		echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
		$updates = get_plugin_updates();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
		$basename = plugin_basename(__FILE__);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
		if ( isset( $updates[$basename] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
			$update = $updates[$basename];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
			echo '<div class="error"><p><strong>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
			printf( __( 'A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'wordpress-importer' ), $update->update->new_version );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
			echo '</strong></p></div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
	// Close div.wrap
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
	function footer() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
		echo '</div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
	 * Display introductory text and file upload form
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
	function greet() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
		echo '<div class="narrow">';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
		echo '<p>'.__( 'Howdy! Upload your WordPress eXtended RSS (WXR) file and we&#8217;ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer' ).'</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
		echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
		wp_import_upload_form( 'admin.php?import=wordpress&amp;step=1' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
		echo '</div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
	 * Decide if the given meta key maps to information we will want to import
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
	 * @param string $key The meta key to check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
	 * @return string|bool The key if we do want to import, false if not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
	function is_valid_meta_key( $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
		// skip attachment metadata since we'll regenerate it from scratch
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
		// skip _edit_lock as not relevant for import
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
		if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
		return $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
	 * Decide whether or not the importer is allowed to create users.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
	 * Default is true, can be filtered via import_allow_create_users
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
	 * @return bool True if creating users is allowed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
	function allow_create_users() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
		return apply_filters( 'import_allow_create_users', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
	 * Decide whether or not the importer should attempt to download attachment files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
	 * Default is true, can be filtered via import_allow_fetch_attachments. The choice
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
	 * made at the import options screen must also be true, false here hides that checkbox.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
	 * @return bool True if downloading attachments is allowed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
	function allow_fetch_attachments() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
		return apply_filters( 'import_allow_fetch_attachments', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
	 * Decide what the maximum file size for downloaded attachments is.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
	 * Default is 0 (unlimited), can be filtered via import_attachment_size_limit
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
	 * @return int Maximum attachment file size to import
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
	function max_attachment_size() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
		return apply_filters( 'import_attachment_size_limit', 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1107
	 * Added to http_request_timeout filter to force timeout at 60 seconds during import
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
	 * @return int 60
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
	function bump_request_timeout() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
		return 60;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
	// return the difference in length between two strings
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
	function cmpr_strlen( $a, $b ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
		return strlen($b) - strlen($a);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
} // class_exists( 'WP_Importer' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
function wordpress_importer_init() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
	load_plugin_textdomain( 'wordpress-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
	 * WordPress Importer object for registering the import callback
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
	 * @global WP_Import $wp_import
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
	$GLOBALS['wp_import'] = new WP_Import();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
	register_importer( 'wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'wordpress-importer'), array( $GLOBALS['wp_import'], 'dispatch' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
add_action( 'admin_init', 'wordpress_importer_init' );