wp/wp-content/plugins/wordpress-importer/wordpress-importer.php
author ymh <ymh.work@gmail.com>
Tue, 15 Oct 2019 15:48:13 +0200
changeset 13 d255fe9cd479
parent 7 cf61fcea0001
permissions -rw-r--r--
Upgrade wordpress again
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
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     4
Plugin URI: https://wordpress.org/plugins/wordpress-importer/
0
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
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     7
Author URI: https://wordpress.org/
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     8
Version: 0.6.4
0
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
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	 * Registered callback function for the WordPress Importer
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	 * Manages the three separate stages of the WXR import process
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	function dispatch() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		$this->header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
		$step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		switch ( $step ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
			case 0:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
				$this->greet();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
			case 1:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
				check_admin_referer( 'import-upload' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
				if ( $this->handle_upload() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
					$this->import_options();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
			case 2:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
				check_admin_referer( 'import-wordpress' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
				$this->fetch_attachments = ( ! empty( $_POST['fetch_attachments'] ) && $this->allow_fetch_attachments() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
				$this->id = (int) $_POST['import_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
				$file = get_attached_file( $this->id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
				set_time_limit(0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
				$this->import( $file );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		$this->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
	 * The main controller for the actual import stage.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
	 * @param string $file Path to the WXR file for importing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	function import( $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
		add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
		add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		$this->import_start( $file );
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->get_author_mapping();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
		wp_suspend_cache_invalidation( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
		$this->process_categories();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		$this->process_tags();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		$this->process_terms();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
		$this->process_posts();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		wp_suspend_cache_invalidation( false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
		// update incorrect/missing information in the DB
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
		$this->backfill_parents();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
		$this->backfill_attachment_urls();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		$this->remap_featured_images();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		$this->import_end();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
	 * 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
   127
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	 * @param string $file Path to the WXR file for importing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
	function import_start( $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
		if ( ! is_file($file) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
			echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
			echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
			$this->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
			die();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		$import_data = $this->parse( $file );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
		if ( is_wp_error( $import_data ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
			echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
			echo esc_html( $import_data->get_error_message() ) . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
			$this->footer();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
			die();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		$this->version = $import_data['version'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
		$this->get_authors_from_import( $import_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		$this->posts = $import_data['posts'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		$this->terms = $import_data['terms'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		$this->categories = $import_data['categories'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
		$this->tags = $import_data['tags'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
		$this->base_url = esc_url( $import_data['base_url'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
		wp_defer_term_counting( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
		wp_defer_comment_counting( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
		do_action( 'import_start' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
	 * Performs post-import cleanup of files and the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
	function import_end() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
		wp_import_cleanup( $this->id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
		wp_cache_flush();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
		foreach ( get_taxonomies() as $tax ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
			delete_option( "{$tax}_children" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
			_get_term_hierarchy( $tax );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
		wp_defer_term_counting( false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		wp_defer_comment_counting( false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
		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
   177
		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
   178
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		do_action( 'import_end' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
	 * Handles the WXR upload and initial parsing of the file to prepare for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
	 * displaying author import options
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
	 * @return bool False if error uploading or invalid file, true otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
	function handle_upload() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
		$file = wp_import_handle_upload();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
		if ( isset( $file['error'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
			echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
			echo esc_html( $file['error'] ) . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
		} else if ( ! file_exists( $file['file'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
			echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
			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
   198
			echo '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
		$this->id = (int) $file['id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
		$import_data = $this->parse( $file['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
		if ( is_wp_error( $import_data ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
			echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
			echo esc_html( $import_data->get_error_message() ) . '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
		$this->version = $import_data['version'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
		if ( $this->version > $this->max_wxr_version ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
			echo '<div class="error"><p><strong>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
			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
   214
			echo '</strong></p></div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
		$this->get_authors_from_import( $import_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
	 * Retrieve authors from parsed WXR data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
	 * Uses the provided author information from WXR 1.1 files
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
	 * or extracts info from each post for WXR 1.0 files
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
	 * @param array $import_data Data returned by a WXR parser
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
	function get_authors_from_import( $import_data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
		if ( ! empty( $import_data['authors'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
			$this->authors = $import_data['authors'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
		// no author information, grab it from the posts
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
			foreach ( $import_data['posts'] as $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
				$login = sanitize_user( $post['post_author'], true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
				if ( empty( $login ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
					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
   239
					echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
				if ( ! isset($this->authors[$login]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
					$this->authors[$login] = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
						'author_login' => $login,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
						'author_display_name' => $post['post_author']
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
					);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
			}
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
	 * Display pre-import options, author importing/mapping and option to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
	 * fetch attachments
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
	function import_options() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
		$j = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
<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
   260
	<?php wp_nonce_field( 'import-wordpress' ); ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
	<input type="hidden" name="import_id" value="<?php echo $this->id; ?>" />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
<?php if ( ! empty( $this->authors ) ) : ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
	<h3><?php _e( 'Assign Authors', 'wordpress-importer' ); ?></h3>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
	<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
   266
<?php if ( $this->allow_create_users() ) : ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
	<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
   268
<?php endif; ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
	<ol id="authors">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
<?php foreach ( $this->authors as $author ) : ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
		<li><?php $this->author_select( $j++, $author ); ?></li>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
<?php endforeach; ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
	</ol>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
<?php endif; ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
<?php if ( $this->allow_fetch_attachments() ) : ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
	<h3><?php _e( 'Import Attachments', 'wordpress-importer' ); ?></h3>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
	<p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
		<input type="checkbox" value="1" name="fetch_attachments" id="import-attachments" />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
		<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
   281
	</p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
<?php endif; ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
	<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
   285
</form>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
	 * Display import options for an individual author. That is, either create
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
	 * a new user based on import info or map to an existing user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
	 * @param int $n Index for each author in the form
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
	 * @param array $author Author information, e.g. login, display name, email
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
	function author_select( $n, $author ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
		_e( 'Import author:', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
		echo ' <strong>' . esc_html( $author['author_display_name'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
		if ( $this->version != '1.0' ) echo ' (' . esc_html( $author['author_login'] ) . ')';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
		echo '</strong><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
		if ( $this->version != '1.0' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
			echo '<div style="margin-left:18px">';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
		$create_users = $this->allow_create_users();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
		if ( $create_users ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
			if ( $this->version != '1.0' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
				_e( 'or create new user with login name:', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
				$value = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
				_e( 'as a new user:', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
				$value = esc_attr( sanitize_user( $author['author_login'], true ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
			echo ' <input type="text" name="user_new['.$n.']" value="'. $value .'" /><br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
		if ( ! $create_users && $this->version == '1.0' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
			_e( 'assign posts to an existing user:', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
			_e( 'or assign posts to an existing user:', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
		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
   323
		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
   324
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
		if ( $this->version != '1.0' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
			echo '</div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
	 * Map old author logins to local user IDs based on decisions made
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
	 * 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
   332
	 * 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
   333
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
	function get_author_mapping() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
		if ( ! isset( $_POST['imported_authors'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
		$create_users = $this->allow_create_users();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
		foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
			// 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
   342
			$santized_old_login = sanitize_user( $old_login, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
			$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
   344
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
			if ( ! empty( $_POST['user_map'][$i] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
				$user = get_userdata( intval($_POST['user_map'][$i]) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
				if ( isset( $user->ID ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
					if ( $old_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
						$this->processed_authors[$old_id] = $user->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
					$this->author_mapping[$santized_old_login] = $user->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
			} else if ( $create_users ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
				if ( ! empty($_POST['user_new'][$i]) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
					$user_id = wp_create_user( $_POST['user_new'][$i], wp_generate_password() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
				} else if ( $this->version != '1.0' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
					$user_data = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
						'user_login' => $old_login,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
						'user_pass' => wp_generate_password(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
						'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
   360
						'display_name' => $this->authors[$old_login]['author_display_name'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
						'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
   362
						'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
   363
					);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
					$user_id = wp_insert_user( $user_data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
				if ( ! is_wp_error( $user_id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
					if ( $old_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
						$this->processed_authors[$old_id] = $user_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
					$this->author_mapping[$santized_old_login] = $user_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
					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
   373
					if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
						echo ' ' . $user_id->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
					echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
			// failsafe: if the user_id was invalid, default to the current user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
			if ( ! isset( $this->author_mapping[$santized_old_login] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
				if ( $old_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
					$this->processed_authors[$old_id] = (int) get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
				$this->author_mapping[$santized_old_login] = (int) get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
		}
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
	 * Create new categories based on import information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
	 * Doesn't create a new category if its slug already exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
	function process_categories() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
		$this->categories = apply_filters( 'wp_import_categories', $this->categories );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
		if ( empty( $this->categories ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
		foreach ( $this->categories as $cat ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
			// if the category already exists leave it alone
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
			$term_id = term_exists( $cat['category_nicename'], 'category' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
			if ( $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
				if ( is_array($term_id) ) $term_id = $term_id['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
				if ( isset($cat['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
					$this->processed_terms[intval($cat['term_id'])] = (int) $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
			$category_parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
			$category_description = isset( $cat['category_description'] ) ? $cat['category_description'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
			$catarr = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
				'category_nicename' => $cat['category_nicename'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
				'category_parent' => $category_parent,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
				'cat_name' => $cat['cat_name'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
				'category_description' => $category_description
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
			);
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   417
			$catarr = wp_slash( $catarr );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
			$id = wp_insert_category( $catarr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
			if ( ! is_wp_error( $id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
				if ( isset($cat['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
					$this->processed_terms[intval($cat['term_id'])] = $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
				printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
					echo ': ' . $id->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
				echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
			}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   430
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   431
			$this->process_termmeta( $cat, $id['term_id'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
		unset( $this->categories );
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
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
	 * Create new post tags based on import information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
	 * Doesn't create a tag if its slug already exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
	function process_tags() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
		$this->tags = apply_filters( 'wp_import_tags', $this->tags );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
		if ( empty( $this->tags ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
		foreach ( $this->tags as $tag ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
			// if the tag already exists leave it alone
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
			$term_id = term_exists( $tag['tag_slug'], 'post_tag' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
			if ( $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
				if ( is_array($term_id) ) $term_id = $term_id['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
				if ( isset($tag['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
					$this->processed_terms[intval($tag['term_id'])] = (int) $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   458
			$tag = wp_slash( $tag );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
			$tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
			$tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
			$id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
			if ( ! is_wp_error( $id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
				if ( isset($tag['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
					$this->processed_terms[intval($tag['term_id'])] = $id['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
				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
   468
				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
					echo ': ' . $id->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
				echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
			}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   473
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   474
			$this->process_termmeta( $tag, $id['term_id'] );
0
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
		unset( $this->tags );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
	 * Create new terms based on import information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
	 * Doesn't create a term its slug already exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
	function process_terms() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
		$this->terms = apply_filters( 'wp_import_terms', $this->terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
		if ( empty( $this->terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
		foreach ( $this->terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
			// if the term already exists in the correct taxonomy leave it alone
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
			$term_id = term_exists( $term['slug'], $term['term_taxonomy'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
			if ( $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
				if ( is_array($term_id) ) $term_id = $term_id['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
				if ( isset($term['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
					$this->processed_terms[intval($term['term_id'])] = (int) $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
			if ( empty( $term['term_parent'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
				$parent = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
				$parent = term_exists( $term['term_parent'], $term['term_taxonomy'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
				if ( is_array( $parent ) ) $parent = $parent['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
			}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   507
			$term = wp_slash( $term );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
			$description = isset( $term['term_description'] ) ? $term['term_description'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
			$termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
			$id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
			if ( ! is_wp_error( $id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
				if ( isset($term['term_id']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
					$this->processed_terms[intval($term['term_id'])] = $id['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
				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
   517
				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
					echo ': ' . $id->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
				echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
			}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   522
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   523
			$this->process_termmeta( $term, $id['term_id'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
		unset( $this->terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
	/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   530
	 * Add metadata to imported term.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   531
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   532
	 * @since 0.6.2
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   533
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   534
	 * @param array $term    Term data from WXR import.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   535
	 * @param int   $term_id ID of the newly created term.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   536
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   537
	protected function process_termmeta( $term, $term_id ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   538
		if ( ! isset( $term['termmeta'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   539
			$term['termmeta'] = array();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   540
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   541
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   542
		/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   543
		 * Filters the metadata attached to an imported term.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   544
		 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   545
		 * @since 0.6.2
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   546
		 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   547
		 * @param array $termmeta Array of term meta.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   548
		 * @param int   $term_id  ID of the newly created term.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   549
		 * @param array $term     Term data from the WXR import.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   550
		 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   551
		$term['termmeta'] = apply_filters( 'wp_import_term_meta', $term['termmeta'], $term_id, $term );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   552
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   553
		if ( empty( $term['termmeta'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   554
			return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   555
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   556
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   557
		foreach ( $term['termmeta'] as $meta ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   558
			/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   559
			 * Filters the meta key for an imported piece of term meta.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   560
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   561
			 * @since 0.6.2
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   562
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   563
			 * @param string $meta_key Meta key.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   564
			 * @param int    $term_id  ID of the newly created term.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   565
			 * @param array  $term     Term data from the WXR import.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   566
			 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   567
			$key = apply_filters( 'import_term_meta_key', $meta['key'], $term_id, $term );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   568
			if ( ! $key ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   569
				continue;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   570
			}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   571
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   572
			// Export gets meta straight from the DB so could have a serialized string
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   573
			$value = maybe_unserialize( $meta['value'] );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   574
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   575
			add_term_meta( $term_id, $key, $value );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   576
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   577
			/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   578
			 * Fires after term meta is imported.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   579
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   580
			 * @since 0.6.2
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   581
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   582
			 * @param int    $term_id ID of the newly created term.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   583
			 * @param string $key     Meta key.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   584
			 * @param mixed  $value   Meta value.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   585
			 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   586
			do_action( 'import_term_meta', $term_id, $key, $value );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   587
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   588
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   589
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   590
	/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
	 * Create new posts based on import information
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
	 * 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
   594
	 * 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
   595
	 * 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
   596
	 * 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
   597
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
	function process_posts() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
		$this->posts = apply_filters( 'wp_import_posts', $this->posts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
		foreach ( $this->posts as $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
			$post = apply_filters( 'wp_import_post_data_raw', $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
			if ( ! post_type_exists( $post['post_type'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
				printf( __( 'Failed to import &#8220;%s&#8221;: Invalid post type %s', 'wordpress-importer' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
					esc_html($post['post_title']), esc_html($post['post_type']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
				echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
				do_action( 'wp_import_post_exists', $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
			if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
			if ( $post['status'] == 'auto-draft' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
			if ( 'nav_menu_item' == $post['post_type'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
				$this->process_menu_item( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
			$post_type_object = get_post_type_object( $post['post_type'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
			$post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   626
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   627
			/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   628
			* Filter ID of the existing post corresponding to post currently importing.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   629
			*
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   630
			* Return 0 to force the post to be imported. Filter the ID to be something else
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   631
			* to override which existing post is mapped to the imported post.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   632
			*
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   633
			* @see post_exists()
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   634
			* @since 0.6.2
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   635
			*
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   636
			* @param int   $post_exists  Post ID, or 0 if post did not exist.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   637
			* @param array $post         The post array to be inserted.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   638
			*/
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   639
			$post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   640
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
			if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
				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
   643
				echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
				$comment_post_ID = $post_id = $post_exists;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   645
				$this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
				$post_parent = (int) $post['post_parent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
				if ( $post_parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
					// if we already know the parent, map it to the new local ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
					if ( isset( $this->processed_posts[$post_parent] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
						$post_parent = $this->processed_posts[$post_parent];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
					// otherwise record the parent for later
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
					} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
						$this->post_orphans[intval($post['post_id'])] = $post_parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
						$post_parent = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
				// map the post author
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
				$author = sanitize_user( $post['post_author'], true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
				if ( isset( $this->author_mapping[$author] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
					$author = $this->author_mapping[$author];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
					$author = (int) get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
				$postdata = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
					'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
					'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
					'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
					'post_status' => $post['status'], 'post_name' => $post['post_name'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
					'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
					'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
					'post_type' => $post['post_type'], 'post_password' => $post['post_password']
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
				);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
				$original_post_ID = $post['post_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
				$postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   679
				$postdata = wp_slash( $postdata );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   680
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
				if ( 'attachment' == $postdata['post_type'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
					$remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
					// 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
   685
					// 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
   686
					$postdata['upload_date'] = $post['post_date'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
					if ( isset( $post['postmeta'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
						foreach( $post['postmeta'] as $meta ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
							if ( $meta['key'] == '_wp_attached_file' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
								if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
									$postdata['upload_date'] = $matches[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
								break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
							}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
						}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
					$comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
					$comment_post_ID = $post_id = wp_insert_post( $postdata, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
					do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
				if ( is_wp_error( $post_id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
					printf( __( 'Failed to import %s &#8220;%s&#8221;', 'wordpress-importer' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
						$post_type_object->labels->singular_name, esc_html($post['post_title']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
					if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
						echo ': ' . $post_id->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
					echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
					continue;
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
				if ( $post['is_sticky'] == 1 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
					stick_post( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
			// map pre-import ID to local ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
			$this->processed_posts[intval($post['post_id'])] = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
			if ( ! isset( $post['terms'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
				$post['terms'] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
			$post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
			// add categories, tags and other terms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
			if ( ! empty( $post['terms'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
				$terms_to_set = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
				foreach ( $post['terms'] as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
					// back compat with WXR 1.0 map 'tag' to 'post_tag'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
					$taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
					$term_exists = term_exists( $term['slug'], $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
					$term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
					if ( ! $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
						$t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
						if ( ! is_wp_error( $t ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
							$term_id = $t['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
							do_action( 'wp_import_insert_term', $t, $term, $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
						} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
							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
   739
							if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
								echo ': ' . $t->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
							echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
							do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
							continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
						}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
					$terms_to_set[$taxonomy][] = intval( $term_id );
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
				foreach ( $terms_to_set as $tax => $ids ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
					$tt_ids = wp_set_post_terms( $post_id, $ids, $tax );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
					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
   752
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
				unset( $post['terms'], $terms_to_set );
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
			if ( ! isset( $post['comments'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
				$post['comments'] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
			$post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
			// add/update comments
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
			if ( ! empty( $post['comments'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
				$num_comments = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
				$inserted_comments = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
				foreach ( $post['comments'] as $comment ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
					$comment_id	= $comment['comment_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
					$newcomments[$comment_id]['comment_post_ID']      = $comment_post_ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
					$newcomments[$comment_id]['comment_author']       = $comment['comment_author'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
					$newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
					$newcomments[$comment_id]['comment_author_IP']    = $comment['comment_author_IP'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
					$newcomments[$comment_id]['comment_author_url']   = $comment['comment_author_url'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
					$newcomments[$comment_id]['comment_date']         = $comment['comment_date'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
					$newcomments[$comment_id]['comment_date_gmt']     = $comment['comment_date_gmt'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
					$newcomments[$comment_id]['comment_content']      = $comment['comment_content'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
					$newcomments[$comment_id]['comment_approved']     = $comment['comment_approved'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
					$newcomments[$comment_id]['comment_type']         = $comment['comment_type'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
					$newcomments[$comment_id]['comment_parent'] 	  = $comment['comment_parent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
					$newcomments[$comment_id]['commentmeta']          = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
					if ( isset( $this->processed_authors[$comment['comment_user_id']] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
						$newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
				ksort( $newcomments );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
				foreach ( $newcomments as $key => $comment ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
					// if this is a new post we can skip the comment_exists() check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
					if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
						if ( isset( $inserted_comments[$comment['comment_parent']] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
							$comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   789
						$comment = wp_slash( $comment );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
						$comment = wp_filter_comment( $comment );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
						$inserted_comments[$key] = wp_insert_comment( $comment );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
						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
   793
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
						foreach( $comment['commentmeta'] as $meta ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
							$value = maybe_unserialize( $meta['value'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
							add_comment_meta( $inserted_comments[$key], $meta['key'], $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
						}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
						$num_comments++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
				unset( $newcomments, $inserted_comments, $post['comments'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
			if ( ! isset( $post['postmeta'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
				$post['postmeta'] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
			$post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
			// add/update post meta
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
			if ( ! empty( $post['postmeta'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
				foreach ( $post['postmeta'] as $meta ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
					$key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
					$value = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
					if ( '_edit_last' == $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
						if ( isset( $this->processed_authors[intval($meta['value'])] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
							$value = $this->processed_authors[intval($meta['value'])];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
						else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
							$key = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
					if ( $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
						// export gets meta straight from the DB so could have a serialized string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
						if ( ! $value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
							$value = maybe_unserialize( $meta['value'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
						add_post_meta( $post_id, $key, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
						do_action( 'import_post_meta', $post_id, $key, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
						// 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
   832
						if ( '_thumbnail_id' == $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
							$this->featured_images[$post_id] = (int) $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
				}
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
		unset( $this->posts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
	}
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
	 * Attempt to create a new menu item from import data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
	 * Fails for draft, orphaned menu items and those without an associated nav_menu
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
	 * 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
   847
	 * 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
   848
	 * end of the import to retry again before discarding).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
	 * @param array $item Menu item details from WXR file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
	function process_menu_item( $item ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
		// skip draft, orphaned menu items
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
		if ( 'draft' == $item['status'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
		$menu_slug = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
		if ( isset($item['terms']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
			// loop through terms, assume first nav_menu term is correct menu
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
			foreach ( $item['terms'] as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
				if ( 'nav_menu' == $term['domain'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
					$menu_slug = $term['slug'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
		// no nav_menu term associated with this menu item
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
		if ( ! $menu_slug ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
			_e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
			echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
		$menu_id = term_exists( $menu_slug, 'nav_menu' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
		if ( ! $menu_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
			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
   878
			echo '<br />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
			$menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
		foreach ( $item['postmeta'] as $meta )
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   885
			${$meta['key']} = $meta['value'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
		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
   888
			$_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
		} 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
   890
			$_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
		} else if ( 'custom' != $_menu_item_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
			// associated object is missing or not imported yet, we'll retry later
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
			$this->missing_menu_items[] = $item;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
		if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
			$_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
   899
		} else if ( $_menu_item_menu_item_parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
			$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
   901
			$_menu_item_menu_item_parent = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
		// wp_update_nav_menu_item expects CSS classes as a space separated string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
		$_menu_item_classes = maybe_unserialize( $_menu_item_classes );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
		if ( is_array( $_menu_item_classes ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
			$_menu_item_classes = implode( ' ', $_menu_item_classes );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
		$args = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
			'menu-item-object-id' => $_menu_item_object_id,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
			'menu-item-object' => $_menu_item_object,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
			'menu-item-parent-id' => $_menu_item_menu_item_parent,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
			'menu-item-position' => intval( $item['menu_order'] ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
			'menu-item-type' => $_menu_item_type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
			'menu-item-title' => $item['post_title'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
			'menu-item-url' => $_menu_item_url,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
			'menu-item-description' => $item['post_content'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
			'menu-item-attr-title' => $item['post_excerpt'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
			'menu-item-target' => $_menu_item_target,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
			'menu-item-classes' => $_menu_item_classes,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
			'menu-item-xfn' => $_menu_item_xfn,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
			'menu-item-status' => $item['status']
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
		$id = wp_update_nav_menu_item( $menu_id, 0, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
		if ( $id && ! is_wp_error( $id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
			$this->processed_menu_items[intval($item['post_id'])] = (int) $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
	}
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
	 * If fetching attachments is enabled then attempt to create a new attachment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
	 * @param array $post Attachment post details from WXR
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
	 * @param string $url URL to fetch attachment from
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
	 * @return int|WP_Error Post ID on success, WP_Error otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
	function process_attachment( $post, $url ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
		if ( ! $this->fetch_attachments )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
			return new WP_Error( 'attachment_processing_error',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
				__( 'Fetching attachments is not enabled', 'wordpress-importer' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
		// 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
   943
		if ( preg_match( '|^/[\w\W]+$|', $url ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
			$url = rtrim( $this->base_url, '/' ) . $url;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
		$upload = $this->fetch_remote_file( $url, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
		if ( is_wp_error( $upload ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
			return $upload;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
		if ( $info = wp_check_filetype( $upload['file'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
			$post['post_mime_type'] = $info['type'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
			return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
		$post['guid'] = $upload['url'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   957
		// as per wp-admin/includes/upload.php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
		$post_id = wp_insert_attachment( $post, $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
		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
   960
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   961
		// 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
   962
		if ( preg_match( '!^image/!', $info['type'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
			$parts = pathinfo( $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
			$name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
			$parts_new = pathinfo( $upload['url'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
			$name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
			$this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
		return $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
	 * Attempt to download a remote file attachment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
	 * @param string $url URL of item to fetch
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
	 * @param array $post Attachment details
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
	 * @return array|WP_Error Local file location details on success, WP_Error otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
	function fetch_remote_file( $url, $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
		// extract the file name and extension from the url
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
		$file_name = basename( $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
		// get placeholder file in the upload dir with a unique, sanitized filename
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
		$upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
		if ( $upload['error'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
			return new WP_Error( 'upload_dir_error', $upload['error'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
		// fetch the remote url and write it to the placeholder file
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   992
		$remote_response = wp_safe_remote_get( $url, array(
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   993
			'timeout' => 300,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   994
            		'stream' => true,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   995
            		'filename' => $upload['file'],
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   996
        	) );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   997
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   998
		$headers = wp_remote_retrieve_headers( $remote_response );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
		// request failed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
		if ( ! $headers ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
			@unlink( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
			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
  1004
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1006
		$remote_response_code = wp_remote_retrieve_response_code( $remote_response );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1007
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
		// make sure the fetch was successful
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1009
		if ( $remote_response_code != '200' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
			@unlink( $upload['file'] );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1011
			return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($remote_response_code), get_status_header_desc($remote_response_code) ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
		$filesize = filesize( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
		if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
			@unlink( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
			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
  1019
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
		if ( 0 == $filesize ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
			@unlink( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
			return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
		$max_size = (int) $this->max_attachment_size();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
		if ( ! empty( $max_size ) && $filesize > $max_size ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
			@unlink( $upload['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
			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
  1030
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
		// 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
  1033
		$this->url_remap[$url] = $upload['url'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
		$this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
		// keep track of the destination if the remote url is redirected somewhere else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
		if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
			$this->url_remap[$headers['x-final-location']] = $upload['url'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
		return $upload;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
	 * Attempt to associate posts and menu items with previously missing parents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
	 * 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
  1046
	 * 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
  1047
	 * the object (e.g. post) they represent in the menu
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
	function backfill_parents() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
		// find parents for post orphans
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
		foreach ( $this->post_orphans as $child_id => $parent_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
			$local_child_id = $local_parent_id = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
			if ( isset( $this->processed_posts[$child_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
				$local_child_id = $this->processed_posts[$child_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
			if ( isset( $this->processed_posts[$parent_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
				$local_parent_id = $this->processed_posts[$parent_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1060
			if ( $local_child_id && $local_parent_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
				$wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1062
				clean_post_cache( $local_child_id );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1063
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
		// all other posts/terms are imported, retry menu items with missing associated object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
		$missing_menu_items = $this->missing_menu_items;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
		foreach ( $missing_menu_items as $item )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
			$this->process_menu_item( $item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
		// find parents for menu item orphans
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
		foreach ( $this->menu_item_orphans as $child_id => $parent_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
			$local_child_id = $local_parent_id = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
			if ( isset( $this->processed_menu_items[$child_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
				$local_child_id = $this->processed_menu_items[$child_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
			if ( isset( $this->processed_menu_items[$parent_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
				$local_parent_id = $this->processed_menu_items[$parent_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
			if ( $local_child_id && $local_parent_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
				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
  1081
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
	}
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
	 * Use stored mapping information to update old attachment URLs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
	function backfill_attachment_urls() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
		// 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
  1090
		uksort( $this->url_remap, array(&$this, 'cmpr_strlen') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
		foreach ( $this->url_remap as $from_url => $to_url ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
			// remap urls in post_content
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
			$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
  1095
			// remap enclosure urls
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
			$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
  1097
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
	 * Update _thumbnail_id meta to new, imported attachment IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
	function remap_featured_images() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
		// cycle through posts that have a featured image
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
		foreach ( $this->featured_images as $post_id => $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
			if ( isset( $this->processed_posts[$value] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1107
				$new_id = $this->processed_posts[$value];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
				// only update if there's a difference
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
				if ( $new_id != $value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
					update_post_meta( $post_id, '_thumbnail_id', $new_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
			}
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
	 * Parse a WXR file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
	 * @param string $file Path to WXR file for parsing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
	 * @return array Information gathered from the WXR file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
	function parse( $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
		$parser = new WXR_Parser();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
		return $parser->parse( $file );
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
	// Display import page title
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
	function header() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
		echo '<div class="wrap">';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
		echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
		$updates = get_plugin_updates();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
		$basename = plugin_basename(__FILE__);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
		if ( isset( $updates[$basename] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
			$update = $updates[$basename];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
			echo '<div class="error"><p><strong>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
			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
  1137
			echo '</strong></p></div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
	// Close div.wrap
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
	function footer() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
		echo '</div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
	 * Display introductory text and file upload form
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
	function greet() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
		echo '<div class="narrow">';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
		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
  1152
		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
  1153
		wp_import_upload_form( 'admin.php?import=wordpress&amp;step=1' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1154
		echo '</div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
	 * 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
  1159
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
	 * @param string $key The meta key to check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
	 * @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
  1162
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
	function is_valid_meta_key( $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
		// skip attachment metadata since we'll regenerate it from scratch
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
		// skip _edit_lock as not relevant for import
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
		if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
		return $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
	 * Decide whether or not the importer is allowed to create users.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
	 * Default is true, can be filtered via import_allow_create_users
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
	 * @return bool True if creating users is allowed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
	function allow_create_users() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
		return apply_filters( 'import_allow_create_users', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
	 * Decide whether or not the importer should attempt to download attachment files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
	 * Default is true, can be filtered via import_allow_fetch_attachments. The choice
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
	 * 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
  1185
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
	 * @return bool True if downloading attachments is allowed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
	function allow_fetch_attachments() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
		return apply_filters( 'import_allow_fetch_attachments', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
	 * Decide what the maximum file size for downloaded attachments is.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
	 * Default is 0 (unlimited), can be filtered via import_attachment_size_limit
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
	 * @return int Maximum attachment file size to import
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
	function max_attachment_size() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
		return apply_filters( 'import_attachment_size_limit', 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
	 * 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
  1204
	 * @return int 60
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
	 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1206
	function bump_request_timeout( $val ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
		return 60;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
	// return the difference in length between two strings
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
	function cmpr_strlen( $a, $b ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
		return strlen($b) - strlen($a);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1213
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1215
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1216
} // class_exists( 'WP_Importer' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1217
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
function wordpress_importer_init() {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1219
	load_plugin_textdomain( 'wordpress-importer' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
	 * WordPress Importer object for registering the import callback
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
	 * @global WP_Import $wp_import
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
	$GLOBALS['wp_import'] = new WP_Import();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
	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
  1227
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
add_action( 'admin_init', 'wordpress_importer_init' );