src/Import_Command.php
private function import_wxr( $file, $args ) {

		$wp_import                  = new WP_Import();
		$wp_import->processed_posts = $this->processed_posts;
		$import_data                = $wp_import->parse( $file );
		if ( is_wp_error( $import_data ) ) {
			return $import_data;
		}

		// Prepare the data to be used in process_author_mapping();
		$wp_import->get_authors_from_import( $import_data );

		// We no longer need the original data, so unset to avoid using excess
		// memory.
		unset( $import_data );

		$author_data = array();
		foreach ( $wp_import->authors as $wxr_author ) {
			$author = new \stdClass();
			// Always in the WXR
			$author->user_login = $wxr_author['author_login'];

			// Should be in the WXR; no guarantees
			if ( isset( $wxr_author['author_email'] ) ) {
				$author->user_email = $wxr_author['author_email'];
			}
			if ( isset( $wxr_author['author_display_name'] ) ) {
				$author->display_name = $wxr_author['author_display_name'];
			}
			if ( isset( $wxr_author['author_first_name'] ) ) {
				$author->first_name = $wxr_author['author_first_name'];
			}
			if ( isset( $wxr_author['author_last_name'] ) ) {
				$author->last_name = $wxr_author['author_last_name'];
			}

			$author_data[] = $author;
		}

		// Build the author mapping
		$author_mapping = $this->process_author_mapping( $args['authors'], $author_data );
		if ( is_wp_error( $author_mapping ) ) {
			return $author_mapping;
		}

		$author_in  = wp_list_pluck( $author_mapping, 'old_user_login' );
		$author_out = wp_list_pluck( $author_mapping, 'new_user_login' );
		unset( $author_mapping, $author_data );

		// $user_select needs to be an array of user IDs
		$user_select         = array();
		$invalid_user_select = array();
		foreach ( $author_out as $author_login ) {
			$user = get_user_by( 'login', $author_login );
			if ( $user ) {
				$user_select[] = $user->ID;
			} else {
				$invalid_user_select[] = $author_login;
			}
		}
		if ( ! empty( $invalid_user_select ) ) {
			return new WP_Error( 'invalid-author-mapping', sprintf( 'These user_logins are invalid: %s', implode( ',', $invalid_user_select ) ) );
		}

		unset( $author_out );

		// Drive the import
		$wp_import->fetch_attachments = ! in_array( 'attachment', $args['skip'], true );

		$_GET  = array(
			'import' => 'wordpress',
			'step'   => 2,
		);
		$_POST = array(
			'imported_authors'  => $author_in,
			'user_map'          => $user_select,
			'fetch_attachments' => $wp_import->fetch_attachments,
		);

		if ( in_array( 'image_resize', $args['skip'], true ) ) {
			add_filter( 'intermediate_image_sizes_advanced', array( $this, 'filter_set_image_sizes' ) );
		}

		$GLOBALS['wpcli_import_current_file'] = basename( $file );
		$wp_import->import( $file );
		$this->processed_posts += $wp_import->processed_posts;

		return true;
	}
private function import_wxr( $file, $args ) {

		$wp_import                  = new WP_Import();
		$wp_import->processed_posts = $this->processed_posts;
		$import_data                = $wp_import->parse( $file );
		if ( is_wp_error( $import_data ) ) {
			return $import_data;
		}

		// Prepare the data to be used in process_author_mapping();
		$wp_import->get_authors_from_import( $import_data );

		// We no longer need the original data, so unset to avoid using excess
		// memory.
		unset( $import_data );

		$author_data = array();
		foreach ( $wp_import->authors as $wxr_author ) {
			$author = new \stdClass();
			// Always in the WXR
			$author->user_login = $wxr_author['author_login'];

			// Should be in the WXR; no guarantees
			if ( isset( $wxr_author['author_email'] ) ) {
				$author->user_email = $wxr_author['author_email'];
			}
			if ( isset( $wxr_author['author_display_name'] ) ) {
				$author->display_name = $wxr_author['author_display_name'];
			}
			if ( isset( $wxr_author['author_first_name'] ) ) {
				$author->first_name = $wxr_author['author_first_name'];
			}
			if ( isset( $wxr_author['author_last_name'] ) ) {
				$author->last_name = $wxr_author['author_last_name'];
			}

			$author_data[] = $author;
		}

		// Build the author mapping
		$author_mapping = $this->process_author_mapping( $args['authors'], $author_data );
		if ( is_wp_error( $author_mapping ) ) {
			return $author_mapping;
		}

		$author_in  = wp_list_pluck( $author_mapping, 'old_user_login' );
		$author_out = wp_list_pluck( $author_mapping, 'new_user_login' );
		unset( $author_mapping, $author_data );

		// $user_select needs to be an array of user IDs
		$user_select         = array();
		$invalid_user_select = array();
		foreach ( $author_out as $author_login ) {
			$user = get_user_by( 'login', $author_login );
			if ( $user ) {
				$user_select[] = $user->ID;
			} else {
				$invalid_user_select[] = $author_login;
			}
		}
		if ( ! empty( $invalid_user_select ) ) {
			return new WP_Error( 'invalid-author-mapping', sprintf( 'These user_logins are invalid: %s', implode( ',', $invalid_user_select ) ) );
		}

		unset( $author_out );

		// Drive the import
		$wp_import->fetch_attachments = ! in_array( 'attachment', $args['skip'], true );

		$_GET  = array(
			'import' => 'wordpress',
			'step'   => 2,
		);
		$_POST = array(
			'imported_authors'  => $author_in,
			'user_map'          => $user_select,
			'fetch_attachments' => $wp_import->fetch_attachments,
		);

		if ( in_array( 'image_resize', $args['skip'], true ) ) {
			add_filter( 'intermediate_image_sizes_advanced', array( $this, 'filter_set_image_sizes' ) );
		}

		$GLOBALS['wpcli_import_current_file'] = basename( $file );
		$wp_import->import( $file );
		$this->processed_posts += $wp_import->processed_posts;

		return true;
	}
private function import_wxr ( $ file , $ args ) { $ wp_import = new WP_Import ( ) ; $ wp_import -> processed_posts = $ this -> processed_posts ; $ import_data = $ wp_import -> parse ( $ file ) ; if ( is_wp_error ( $ import_data ) ) { return $ import_data ; } // Prepare the data to be used in process_author_mapping(); $ wp_import -> get_authors_from_import ( $ import_data ) ; // We no longer need the original data, so unset to avoid using excess // memory. unset ( $ import_data ) ; $ author_data = array ( ) ; foreach ( $ wp_import -> authors as $ wxr_author ) { $ author = new \ stdClass ( ) ; // Always in the WXR $ author -> user_login = $ wxr_author [ 'author_login' ] ; // Should be in the WXR; no guarantees if ( isset ( $ wxr_author [ 'author_email' ] ) ) { $ author -> user_email = $ wxr_author [ 'author_email' ] ; } if ( isset ( $ wxr_author [ 'author_display_name' ] ) ) { $ author -> display_name = $ wxr_author [ 'author_display_name' ] ; } if ( isset ( $ wxr_author [ 'author_first_name' ] ) ) { $ author -> first_name = $ wxr_author [ 'author_first_name' ] ; } if ( isset ( $ wxr_author [ 'author_last_name' ] ) ) { $ author -> last_name = $ wxr_author [ 'author_last_name' ] ; } $ author_data [ ] = $ author ; } // Build the author mapping $ author_mapping = $ this -> process_author_mapping ( $ args [ 'authors' ] , $ author_data ) ; if ( is_wp_error ( $ author_mapping ) ) { return $ author_mapping ; } $ author_in = wp_list_pluck ( $ author_mapping , 'old_user_login' ) ; $ author_out = wp_list_pluck ( $ author_mapping , 'new_user_login' ) ; unset ( $ author_mapping , $ author_data ) ; // $user_select needs to be an array of user IDs $ user_select = array ( ) ; $ invalid_user_select = array ( ) ; foreach ( $ author_out as $ author_login ) { $ user = get_user_by ( 'login' , $ author_login ) ; if ( $ user ) { $ user_select [ ] = $ user -> ID ; } else { $ invalid_user_select [ ] = $ author_login ; } } if ( ! empty ( $ invalid_user_select ) ) { return new WP_Error ( 'invalid-author-mapping' , sprintf ( 'These user_logins are invalid: %s' , implode ( ',' , $ invalid_user_select ) ) ) ; } unset ( $ author_out ) ; // Drive the import $ wp_import -> fetch_attachments = ! in_array ( 'attachment' , $ args [ 'skip' ] , true ) ; $ _GET = array ( 'import' => 'wordpress' , 'step' => 2 , ) ; $ _POST = array ( 'imported_authors' => $ author_in , 'user_map' => $ user_select , 'fetch_attachments' => $ wp_import -> fetch_attachments , ) ; if ( in_array ( 'image_resize' , $ args [ 'skip' ] , true ) ) { add_filter ( 'intermediate_image_sizes_advanced' , array ( $ this , 'filter_set_image_sizes' ) ) ; } $ GLOBALS [ 'wpcli_import_current_file' ] = basename ( $ file ) ; $ wp_import -> import ( $ file ) ; $ this -> processed_posts += $ wp_import -> processed_posts ; return true ; }
Imports a WXR file.
Imports a WXR file .