wp/wp-content/plugins/wordpress-importer/wordpress-importer.php
changeset 7 cf61fcea0001
parent 0 d970ebf37754
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 <?php
     1 <?php
     2 /*
     2 /*
     3 Plugin Name: WordPress Importer
     3 Plugin Name: WordPress Importer
     4 Plugin URI: http://wordpress.org/extend/plugins/wordpress-importer/
     4 Plugin URI: https://wordpress.org/plugins/wordpress-importer/
     5 Description: Import posts, pages, comments, custom fields, categories, tags and more from a WordPress export file.
     5 Description: Import posts, pages, comments, custom fields, categories, tags and more from a WordPress export file.
     6 Author: wordpressdotorg
     6 Author: wordpressdotorg
     7 Author URI: http://wordpress.org/
     7 Author URI: https://wordpress.org/
     8 Version: 0.6.1
     8 Version: 0.6.4
     9 Text Domain: wordpress-importer
     9 Text Domain: wordpress-importer
    10 License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    10 License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    11 */
    11 */
    12 
    12 
    13 if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
    13 if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
    60 	var $missing_menu_items = array();
    60 	var $missing_menu_items = array();
    61 
    61 
    62 	var $fetch_attachments = false;
    62 	var $fetch_attachments = false;
    63 	var $url_remap = array();
    63 	var $url_remap = array();
    64 	var $featured_images = array();
    64 	var $featured_images = array();
    65 
       
    66 	function WP_Import() { /* nothing */ }
       
    67 
    65 
    68 	/**
    66 	/**
    69 	 * Registered callback function for the WordPress Importer
    67 	 * Registered callback function for the WordPress Importer
    70 	 *
    68 	 *
    71 	 * Manages the three separate stages of the WXR import process
    69 	 * Manages the three separate stages of the WXR import process
   414 				'category_nicename' => $cat['category_nicename'],
   412 				'category_nicename' => $cat['category_nicename'],
   415 				'category_parent' => $category_parent,
   413 				'category_parent' => $category_parent,
   416 				'cat_name' => $cat['cat_name'],
   414 				'cat_name' => $cat['cat_name'],
   417 				'category_description' => $category_description
   415 				'category_description' => $category_description
   418 			);
   416 			);
       
   417 			$catarr = wp_slash( $catarr );
   419 
   418 
   420 			$id = wp_insert_category( $catarr );
   419 			$id = wp_insert_category( $catarr );
   421 			if ( ! is_wp_error( $id ) ) {
   420 			if ( ! is_wp_error( $id ) ) {
   422 				if ( isset($cat['term_id']) )
   421 				if ( isset($cat['term_id']) )
   423 					$this->processed_terms[intval($cat['term_id'])] = $id;
   422 					$this->processed_terms[intval($cat['term_id'])] = $id;
   426 				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
   425 				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
   427 					echo ': ' . $id->get_error_message();
   426 					echo ': ' . $id->get_error_message();
   428 				echo '<br />';
   427 				echo '<br />';
   429 				continue;
   428 				continue;
   430 			}
   429 			}
       
   430 
       
   431 			$this->process_termmeta( $cat, $id['term_id'] );
   431 		}
   432 		}
   432 
   433 
   433 		unset( $this->categories );
   434 		unset( $this->categories );
   434 	}
   435 	}
   435 
   436 
   452 				if ( isset($tag['term_id']) )
   453 				if ( isset($tag['term_id']) )
   453 					$this->processed_terms[intval($tag['term_id'])] = (int) $term_id;
   454 					$this->processed_terms[intval($tag['term_id'])] = (int) $term_id;
   454 				continue;
   455 				continue;
   455 			}
   456 			}
   456 
   457 
       
   458 			$tag = wp_slash( $tag );
   457 			$tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
   459 			$tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
   458 			$tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc );
   460 			$tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc );
   459 
   461 
   460 			$id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr );
   462 			$id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr );
   461 			if ( ! is_wp_error( $id ) ) {
   463 			if ( ! is_wp_error( $id ) ) {
   466 				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
   468 				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
   467 					echo ': ' . $id->get_error_message();
   469 					echo ': ' . $id->get_error_message();
   468 				echo '<br />';
   470 				echo '<br />';
   469 				continue;
   471 				continue;
   470 			}
   472 			}
       
   473 
       
   474 			$this->process_termmeta( $tag, $id['term_id'] );
   471 		}
   475 		}
   472 
   476 
   473 		unset( $this->tags );
   477 		unset( $this->tags );
   474 	}
   478 	}
   475 
   479 
   498 				$parent = 0;
   502 				$parent = 0;
   499 			} else {
   503 			} else {
   500 				$parent = term_exists( $term['term_parent'], $term['term_taxonomy'] );
   504 				$parent = term_exists( $term['term_parent'], $term['term_taxonomy'] );
   501 				if ( is_array( $parent ) ) $parent = $parent['term_id'];
   505 				if ( is_array( $parent ) ) $parent = $parent['term_id'];
   502 			}
   506 			}
       
   507 			$term = wp_slash( $term );
   503 			$description = isset( $term['term_description'] ) ? $term['term_description'] : '';
   508 			$description = isset( $term['term_description'] ) ? $term['term_description'] : '';
   504 			$termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) );
   509 			$termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) );
   505 
   510 
   506 			$id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr );
   511 			$id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr );
   507 			if ( ! is_wp_error( $id ) ) {
   512 			if ( ! is_wp_error( $id ) ) {
   512 				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
   517 				if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
   513 					echo ': ' . $id->get_error_message();
   518 					echo ': ' . $id->get_error_message();
   514 				echo '<br />';
   519 				echo '<br />';
   515 				continue;
   520 				continue;
   516 			}
   521 			}
       
   522 
       
   523 			$this->process_termmeta( $term, $id['term_id'] );
   517 		}
   524 		}
   518 
   525 
   519 		unset( $this->terms );
   526 		unset( $this->terms );
       
   527 	}
       
   528 
       
   529 	/**
       
   530 	 * Add metadata to imported term.
       
   531 	 *
       
   532 	 * @since 0.6.2
       
   533 	 *
       
   534 	 * @param array $term    Term data from WXR import.
       
   535 	 * @param int   $term_id ID of the newly created term.
       
   536 	 */
       
   537 	protected function process_termmeta( $term, $term_id ) {
       
   538 		if ( ! isset( $term['termmeta'] ) ) {
       
   539 			$term['termmeta'] = array();
       
   540 		}
       
   541 
       
   542 		/**
       
   543 		 * Filters the metadata attached to an imported term.
       
   544 		 *
       
   545 		 * @since 0.6.2
       
   546 		 *
       
   547 		 * @param array $termmeta Array of term meta.
       
   548 		 * @param int   $term_id  ID of the newly created term.
       
   549 		 * @param array $term     Term data from the WXR import.
       
   550 		 */
       
   551 		$term['termmeta'] = apply_filters( 'wp_import_term_meta', $term['termmeta'], $term_id, $term );
       
   552 
       
   553 		if ( empty( $term['termmeta'] ) ) {
       
   554 			return;
       
   555 		}
       
   556 
       
   557 		foreach ( $term['termmeta'] as $meta ) {
       
   558 			/**
       
   559 			 * Filters the meta key for an imported piece of term meta.
       
   560 			 *
       
   561 			 * @since 0.6.2
       
   562 			 *
       
   563 			 * @param string $meta_key Meta key.
       
   564 			 * @param int    $term_id  ID of the newly created term.
       
   565 			 * @param array  $term     Term data from the WXR import.
       
   566 			 */
       
   567 			$key = apply_filters( 'import_term_meta_key', $meta['key'], $term_id, $term );
       
   568 			if ( ! $key ) {
       
   569 				continue;
       
   570 			}
       
   571 
       
   572 			// Export gets meta straight from the DB so could have a serialized string
       
   573 			$value = maybe_unserialize( $meta['value'] );
       
   574 
       
   575 			add_term_meta( $term_id, $key, $value );
       
   576 
       
   577 			/**
       
   578 			 * Fires after term meta is imported.
       
   579 			 *
       
   580 			 * @since 0.6.2
       
   581 			 *
       
   582 			 * @param int    $term_id ID of the newly created term.
       
   583 			 * @param string $key     Meta key.
       
   584 			 * @param mixed  $value   Meta value.
       
   585 			 */
       
   586 			do_action( 'import_term_meta', $term_id, $key, $value );
       
   587 		}
   520 	}
   588 	}
   521 
   589 
   522 	/**
   590 	/**
   523 	 * Create new posts based on import information
   591 	 * Create new posts based on import information
   524 	 *
   592 	 *
   553 			}
   621 			}
   554 
   622 
   555 			$post_type_object = get_post_type_object( $post['post_type'] );
   623 			$post_type_object = get_post_type_object( $post['post_type'] );
   556 
   624 
   557 			$post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
   625 			$post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
       
   626 
       
   627 			/**
       
   628 			* Filter ID of the existing post corresponding to post currently importing.
       
   629 			*
       
   630 			* Return 0 to force the post to be imported. Filter the ID to be something else
       
   631 			* to override which existing post is mapped to the imported post.
       
   632 			*
       
   633 			* @see post_exists()
       
   634 			* @since 0.6.2
       
   635 			*
       
   636 			* @param int   $post_exists  Post ID, or 0 if post did not exist.
       
   637 			* @param array $post         The post array to be inserted.
       
   638 			*/
       
   639 			$post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post );
       
   640 
   558 			if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
   641 			if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
   559 				printf( __('%s &#8220;%s&#8221; already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) );
   642 				printf( __('%s &#8220;%s&#8221; already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) );
   560 				echo '<br />';
   643 				echo '<br />';
   561 				$comment_post_ID = $post_id = $post_exists;
   644 				$comment_post_ID = $post_id = $post_exists;
       
   645 				$this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists );
   562 			} else {
   646 			} else {
   563 				$post_parent = (int) $post['post_parent'];
   647 				$post_parent = (int) $post['post_parent'];
   564 				if ( $post_parent ) {
   648 				if ( $post_parent ) {
   565 					// if we already know the parent, map it to the new local ID
   649 					// if we already know the parent, map it to the new local ID
   566 					if ( isset( $this->processed_posts[$post_parent] ) ) {
   650 					if ( isset( $this->processed_posts[$post_parent] ) ) {
   589 					'post_type' => $post['post_type'], 'post_password' => $post['post_password']
   673 					'post_type' => $post['post_type'], 'post_password' => $post['post_password']
   590 				);
   674 				);
   591 
   675 
   592 				$original_post_ID = $post['post_id'];
   676 				$original_post_ID = $post['post_id'];
   593 				$postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
   677 				$postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
       
   678 
       
   679 				$postdata = wp_slash( $postdata );
   594 
   680 
   595 				if ( 'attachment' == $postdata['post_type'] ) {
   681 				if ( 'attachment' == $postdata['post_type'] ) {
   596 					$remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
   682 					$remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
   597 
   683 
   598 					// try to use _wp_attached file for upload folder placement to ensure the same location as the export site
   684 					// try to use _wp_attached file for upload folder placement to ensure the same location as the export site
   698 				foreach ( $newcomments as $key => $comment ) {
   784 				foreach ( $newcomments as $key => $comment ) {
   699 					// if this is a new post we can skip the comment_exists() check
   785 					// if this is a new post we can skip the comment_exists() check
   700 					if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {
   786 					if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {
   701 						if ( isset( $inserted_comments[$comment['comment_parent']] ) )
   787 						if ( isset( $inserted_comments[$comment['comment_parent']] ) )
   702 							$comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
   788 							$comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
       
   789 						$comment = wp_slash( $comment );
   703 						$comment = wp_filter_comment( $comment );
   790 						$comment = wp_filter_comment( $comment );
   704 						$inserted_comments[$key] = wp_insert_comment( $comment );
   791 						$inserted_comments[$key] = wp_insert_comment( $comment );
   705 						do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post );
   792 						do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post );
   706 
   793 
   707 						foreach( $comment['commentmeta'] as $meta ) {
   794 						foreach( $comment['commentmeta'] as $meta ) {
   793 		} else {
   880 		} else {
   794 			$menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
   881 			$menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
   795 		}
   882 		}
   796 
   883 
   797 		foreach ( $item['postmeta'] as $meta )
   884 		foreach ( $item['postmeta'] as $meta )
   798 			$$meta['key'] = $meta['value'];
   885 			${$meta['key']} = $meta['value'];
   799 
   886 
   800 		if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) {
   887 		if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) {
   801 			$_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
   888 			$_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
   802 		} else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) {
   889 		} else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) {
   803 			$_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
   890 			$_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
   900 		$upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
   987 		$upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
   901 		if ( $upload['error'] )
   988 		if ( $upload['error'] )
   902 			return new WP_Error( 'upload_dir_error', $upload['error'] );
   989 			return new WP_Error( 'upload_dir_error', $upload['error'] );
   903 
   990 
   904 		// fetch the remote url and write it to the placeholder file
   991 		// fetch the remote url and write it to the placeholder file
   905 		$headers = wp_get_http( $url, $upload['file'] );
   992 		$remote_response = wp_safe_remote_get( $url, array(
       
   993 			'timeout' => 300,
       
   994             		'stream' => true,
       
   995             		'filename' => $upload['file'],
       
   996         	) );
       
   997 
       
   998 		$headers = wp_remote_retrieve_headers( $remote_response );
   906 
   999 
   907 		// request failed
  1000 		// request failed
   908 		if ( ! $headers ) {
  1001 		if ( ! $headers ) {
   909 			@unlink( $upload['file'] );
  1002 			@unlink( $upload['file'] );
   910 			return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
  1003 			return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
   911 		}
  1004 		}
   912 
  1005 
       
  1006 		$remote_response_code = wp_remote_retrieve_response_code( $remote_response );
       
  1007 
   913 		// make sure the fetch was successful
  1008 		// make sure the fetch was successful
   914 		if ( $headers['response'] != '200' ) {
  1009 		if ( $remote_response_code != '200' ) {
   915 			@unlink( $upload['file'] );
  1010 			@unlink( $upload['file'] );
   916 			return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) );
  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) ) );
   917 		}
  1012 		}
   918 
  1013 
   919 		$filesize = filesize( $upload['file'] );
  1014 		$filesize = filesize( $upload['file'] );
   920 
  1015 
   921 		if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
  1016 		if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
   960 			if ( isset( $this->processed_posts[$child_id] ) )
  1055 			if ( isset( $this->processed_posts[$child_id] ) )
   961 				$local_child_id = $this->processed_posts[$child_id];
  1056 				$local_child_id = $this->processed_posts[$child_id];
   962 			if ( isset( $this->processed_posts[$parent_id] ) )
  1057 			if ( isset( $this->processed_posts[$parent_id] ) )
   963 				$local_parent_id = $this->processed_posts[$parent_id];
  1058 				$local_parent_id = $this->processed_posts[$parent_id];
   964 
  1059 
   965 			if ( $local_child_id && $local_parent_id )
  1060 			if ( $local_child_id && $local_parent_id ) {
   966 				$wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' );
  1061 				$wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' );
       
  1062 				clean_post_cache( $local_child_id );
       
  1063 			}
   967 		}
  1064 		}
   968 
  1065 
   969 		// all other posts/terms are imported, retry menu items with missing associated object
  1066 		// all other posts/terms are imported, retry menu items with missing associated object
   970 		$missing_menu_items = $this->missing_menu_items;
  1067 		$missing_menu_items = $this->missing_menu_items;
   971 		foreach ( $missing_menu_items as $item )
  1068 		foreach ( $missing_menu_items as $item )
  1027 	}
  1124 	}
  1028 
  1125 
  1029 	// Display import page title
  1126 	// Display import page title
  1030 	function header() {
  1127 	function header() {
  1031 		echo '<div class="wrap">';
  1128 		echo '<div class="wrap">';
  1032 		screen_icon();
       
  1033 		echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>';
  1129 		echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>';
  1034 
  1130 
  1035 		$updates = get_plugin_updates();
  1131 		$updates = get_plugin_updates();
  1036 		$basename = plugin_basename(__FILE__);
  1132 		$basename = plugin_basename(__FILE__);
  1037 		if ( isset( $updates[$basename] ) ) {
  1133 		if ( isset( $updates[$basename] ) ) {
  1105 
  1201 
  1106 	/**
  1202 	/**
  1107 	 * Added to http_request_timeout filter to force timeout at 60 seconds during import
  1203 	 * Added to http_request_timeout filter to force timeout at 60 seconds during import
  1108 	 * @return int 60
  1204 	 * @return int 60
  1109 	 */
  1205 	 */
  1110 	function bump_request_timeout() {
  1206 	function bump_request_timeout( $val ) {
  1111 		return 60;
  1207 		return 60;
  1112 	}
  1208 	}
  1113 
  1209 
  1114 	// return the difference in length between two strings
  1210 	// return the difference in length between two strings
  1115 	function cmpr_strlen( $a, $b ) {
  1211 	function cmpr_strlen( $a, $b ) {
  1118 }
  1214 }
  1119 
  1215 
  1120 } // class_exists( 'WP_Importer' )
  1216 } // class_exists( 'WP_Importer' )
  1121 
  1217 
  1122 function wordpress_importer_init() {
  1218 function wordpress_importer_init() {
  1123 	load_plugin_textdomain( 'wordpress-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
  1219 	load_plugin_textdomain( 'wordpress-importer' );
  1124 
  1220 
  1125 	/**
  1221 	/**
  1126 	 * WordPress Importer object for registering the import callback
  1222 	 * WordPress Importer object for registering the import callback
  1127 	 * @global WP_Import $wp_import
  1223 	 * @global WP_Import $wp_import
  1128 	 */
  1224 	 */