1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132 |
- <?php
- if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
- return;
- define( 'IMPORT_DEBUG', false );
- require_once ABSPATH . 'wp-admin/includes/import.php';
- if ( ! class_exists( 'WP_Importer' ) ) {
- $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
- if ( file_exists( $class_wp_importer ) )
- require $class_wp_importer;
- }
- require dirname( __FILE__ ) . '/parsers.php';
- if ( class_exists( 'WP_Importer' ) ) {
- class WP_Import extends WP_Importer {
- var $max_wxr_version = 1.2;
- var $id;
-
- var $version;
- var $authors = array();
- var $posts = array();
- var $terms = array();
- var $categories = array();
- var $tags = array();
- var $base_url = '';
-
- var $processed_authors = array();
- var $author_mapping = array();
- var $processed_terms = array();
- var $processed_posts = array();
- var $post_orphans = array();
- var $processed_menu_items = array();
- var $menu_item_orphans = array();
- var $missing_menu_items = array();
- var $fetch_attachments = false;
- var $url_remap = array();
- var $featured_images = array();
- function WP_Import() { }
-
- function dispatch() {
- $this->header();
- $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
- switch ( $step ) {
- case 0:
- $this->greet();
- break;
- case 1:
- check_admin_referer( 'import-upload' );
- if ( $this->handle_upload() )
- $this->import_options();
- break;
- case 2:
- check_admin_referer( 'import-wordpress' );
- $this->fetch_attachments = ( ! empty( $_POST['fetch_attachments'] ) && $this->allow_fetch_attachments() );
- $this->id = (int) $_POST['import_id'];
- $file = get_attached_file( $this->id );
- set_time_limit(0);
- $this->import( $file );
- break;
- }
- $this->footer();
- }
-
- function import( $file ) {
- add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
- add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
- $this->import_start( $file );
- $this->get_author_mapping();
- wp_suspend_cache_invalidation( true );
- $this->process_categories();
- $this->process_tags();
- $this->process_terms();
- $this->process_posts();
- wp_suspend_cache_invalidation( false );
-
- $this->backfill_parents();
- $this->backfill_attachment_urls();
- $this->remap_featured_images();
- $this->import_end();
- }
-
- function import_start( $file ) {
- if ( ! is_file($file) ) {
- echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
- echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
- $this->footer();
- die();
- }
- $import_data = $this->parse( $file );
- if ( is_wp_error( $import_data ) ) {
- echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
- echo esc_html( $import_data->get_error_message() ) . '</p>';
- $this->footer();
- die();
- }
- $this->version = $import_data['version'];
- $this->get_authors_from_import( $import_data );
- $this->posts = $import_data['posts'];
- $this->terms = $import_data['terms'];
- $this->categories = $import_data['categories'];
- $this->tags = $import_data['tags'];
- $this->base_url = esc_url( $import_data['base_url'] );
- wp_defer_term_counting( true );
- wp_defer_comment_counting( true );
- do_action( 'import_start' );
- }
-
- function import_end() {
- wp_import_cleanup( $this->id );
- wp_cache_flush();
- foreach ( get_taxonomies() as $tax ) {
- delete_option( "{$tax}_children" );
- _get_term_hierarchy( $tax );
- }
- wp_defer_term_counting( false );
- wp_defer_comment_counting( false );
- echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>';
- echo '<p>' . __( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>';
- do_action( 'import_end' );
- }
-
- function handle_upload() {
- $file = wp_import_handle_upload();
- if ( isset( $file['error'] ) ) {
- echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
- echo esc_html( $file['error'] ) . '</p>';
- return false;
- } else if ( ! file_exists( $file['file'] ) ) {
- echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
- 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'] ) );
- echo '</p>';
- return false;
- }
- $this->id = (int) $file['id'];
- $import_data = $this->parse( $file['file'] );
- if ( is_wp_error( $import_data ) ) {
- echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
- echo esc_html( $import_data->get_error_message() ) . '</p>';
- return false;
- }
- $this->version = $import_data['version'];
- if ( $this->version > $this->max_wxr_version ) {
- echo '<div class="error"><p><strong>';
- 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']) );
- echo '</strong></p></div>';
- }
- $this->get_authors_from_import( $import_data );
- return true;
- }
-
- function get_authors_from_import( $import_data ) {
- if ( ! empty( $import_data['authors'] ) ) {
- $this->authors = $import_data['authors'];
-
- } else {
- foreach ( $import_data['posts'] as $post ) {
- $login = sanitize_user( $post['post_author'], true );
- if ( empty( $login ) ) {
- printf( __( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) );
- echo '<br />';
- continue;
- }
- if ( ! isset($this->authors[$login]) )
- $this->authors[$login] = array(
- 'author_login' => $login,
- 'author_display_name' => $post['post_author']
- );
- }
- }
- }
-
- function import_options() {
- $j = 0;
- ?>
- <form action="<?php echo admin_url( 'admin.php?import=wordpress&step=2' ); ?>" method="post">
- <?php wp_nonce_field( 'import-wordpress' ); ?>
- <input type="hidden" name="import_id" value="<?php echo $this->id; ?>" />
- <?php if ( ! empty( $this->authors ) ) : ?>
- <h3><?php _e( 'Assign Authors', 'wordpress-importer' ); ?></h3>
- <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>
- <?php if ( $this->allow_create_users() ) : ?>
- <p><?php printf( __( 'If a new user is created by WordPress, a new password will be randomly generated and the new user’s role will be set as %s. Manually changing the new user’s details will be necessary.', 'wordpress-importer' ), esc_html( get_option('default_role') ) ); ?></p>
- <?php endif; ?>
- <ol id="authors">
- <?php foreach ( $this->authors as $author ) : ?>
- <li><?php $this->author_select( $j++, $author ); ?></li>
- <?php endforeach; ?>
- </ol>
- <?php endif; ?>
- <?php if ( $this->allow_fetch_attachments() ) : ?>
- <h3><?php _e( 'Import Attachments', 'wordpress-importer' ); ?></h3>
- <p>
- <input type="checkbox" value="1" name="fetch_attachments" id="import-attachments" />
- <label for="import-attachments"><?php _e( 'Download and import file attachments', 'wordpress-importer' ); ?></label>
- </p>
- <?php endif; ?>
- <p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Submit', 'wordpress-importer' ); ?>" /></p>
- </form>
- <?php
- }
- /**
- * Display import options for an individual author. That is, either create
- * a new user based on import info or map to an existing user
- *
- * @param int $n Index for each author in the form
- * @param array $author Author information, e.g. login, display name, email
- */
- function author_select( $n, $author ) {
- _e( 'Import author:', 'wordpress-importer' );
- echo ' <strong>' . esc_html( $author['author_display_name'] );
- if ( $this->version != '1.0' ) echo ' (' . esc_html( $author['author_login'] ) . ')';
- echo '</strong><br />';
- if ( $this->version != '1.0' )
- echo '<div style="margin-left:18px">';
- $create_users = $this->allow_create_users();
- if ( $create_users ) {
- if ( $this->version != '1.0' ) {
- _e( 'or create new user with login name:', 'wordpress-importer' );
- $value = '';
- } else {
- _e( 'as a new user:', 'wordpress-importer' );
- $value = esc_attr( sanitize_user( $author['author_login'], true ) );
- }
- echo ' <input type="text" name="user_new['.$n.']" value="'. $value .'" /><br />';
- }
- if ( ! $create_users && $this->version == '1.0' )
- _e( 'assign posts to an existing user:', 'wordpress-importer' );
- else
- _e( 'or assign posts to an existing user:', 'wordpress-importer' );
- wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) );
- echo '<input type="hidden" name="imported_authors['.$n.']" value="' . esc_attr( $author['author_login'] ) . '" />';
- if ( $this->version != '1.0' )
- echo '</div>';
- }
-
- function get_author_mapping() {
- if ( ! isset( $_POST['imported_authors'] ) )
- return;
- $create_users = $this->allow_create_users();
- foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) {
-
- $santized_old_login = sanitize_user( $old_login, true );
- $old_id = isset( $this->authors[$old_login]['author_id'] ) ? intval($this->authors[$old_login]['author_id']) : false;
- if ( ! empty( $_POST['user_map'][$i] ) ) {
- $user = get_userdata( intval($_POST['user_map'][$i]) );
- if ( isset( $user->ID ) ) {
- if ( $old_id )
- $this->processed_authors[$old_id] = $user->ID;
- $this->author_mapping[$santized_old_login] = $user->ID;
- }
- } else if ( $create_users ) {
- if ( ! empty($_POST['user_new'][$i]) ) {
- $user_id = wp_create_user( $_POST['user_new'][$i], wp_generate_password() );
- } else if ( $this->version != '1.0' ) {
- $user_data = array(
- 'user_login' => $old_login,
- 'user_pass' => wp_generate_password(),
- 'user_email' => isset( $this->authors[$old_login]['author_email'] ) ? $this->authors[$old_login]['author_email'] : '',
- 'display_name' => $this->authors[$old_login]['author_display_name'],
- 'first_name' => isset( $this->authors[$old_login]['author_first_name'] ) ? $this->authors[$old_login]['author_first_name'] : '',
- 'last_name' => isset( $this->authors[$old_login]['author_last_name'] ) ? $this->authors[$old_login]['author_last_name'] : '',
- );
- $user_id = wp_insert_user( $user_data );
- }
- if ( ! is_wp_error( $user_id ) ) {
- if ( $old_id )
- $this->processed_authors[$old_id] = $user_id;
- $this->author_mapping[$santized_old_login] = $user_id;
- } else {
- 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']) );
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
- echo ' ' . $user_id->get_error_message();
- echo '<br />';
- }
- }
-
- if ( ! isset( $this->author_mapping[$santized_old_login] ) ) {
- if ( $old_id )
- $this->processed_authors[$old_id] = (int) get_current_user_id();
- $this->author_mapping[$santized_old_login] = (int) get_current_user_id();
- }
- }
- }
-
- function process_categories() {
- $this->categories = apply_filters( 'wp_import_categories', $this->categories );
- if ( empty( $this->categories ) )
- return;
- foreach ( $this->categories as $cat ) {
-
- $term_id = term_exists( $cat['category_nicename'], 'category' );
- if ( $term_id ) {
- if ( is_array($term_id) ) $term_id = $term_id['term_id'];
- if ( isset($cat['term_id']) )
- $this->processed_terms[intval($cat['term_id'])] = (int) $term_id;
- continue;
- }
- $category_parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] );
- $category_description = isset( $cat['category_description'] ) ? $cat['category_description'] : '';
- $catarr = array(
- 'category_nicename' => $cat['category_nicename'],
- 'category_parent' => $category_parent,
- 'cat_name' => $cat['cat_name'],
- 'category_description' => $category_description
- );
- $id = wp_insert_category( $catarr );
- if ( ! is_wp_error( $id ) ) {
- if ( isset($cat['term_id']) )
- $this->processed_terms[intval($cat['term_id'])] = $id;
- } else {
- printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) );
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
- echo ': ' . $id->get_error_message();
- echo '<br />';
- continue;
- }
- }
- unset( $this->categories );
- }
-
- function process_tags() {
- $this->tags = apply_filters( 'wp_import_tags', $this->tags );
- if ( empty( $this->tags ) )
- return;
- foreach ( $this->tags as $tag ) {
-
- $term_id = term_exists( $tag['tag_slug'], 'post_tag' );
- if ( $term_id ) {
- if ( is_array($term_id) ) $term_id = $term_id['term_id'];
- if ( isset($tag['term_id']) )
- $this->processed_terms[intval($tag['term_id'])] = (int) $term_id;
- continue;
- }
- $tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
- $tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc );
- $id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr );
- if ( ! is_wp_error( $id ) ) {
- if ( isset($tag['term_id']) )
- $this->processed_terms[intval($tag['term_id'])] = $id['term_id'];
- } else {
- printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) );
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
- echo ': ' . $id->get_error_message();
- echo '<br />';
- continue;
- }
- }
- unset( $this->tags );
- }
-
- function process_terms() {
- $this->terms = apply_filters( 'wp_import_terms', $this->terms );
- if ( empty( $this->terms ) )
- return;
- foreach ( $this->terms as $term ) {
-
- $term_id = term_exists( $term['slug'], $term['term_taxonomy'] );
- if ( $term_id ) {
- if ( is_array($term_id) ) $term_id = $term_id['term_id'];
- if ( isset($term['term_id']) )
- $this->processed_terms[intval($term['term_id'])] = (int) $term_id;
- continue;
- }
- if ( empty( $term['term_parent'] ) ) {
- $parent = 0;
- } else {
- $parent = term_exists( $term['term_parent'], $term['term_taxonomy'] );
- if ( is_array( $parent ) ) $parent = $parent['term_id'];
- }
- $description = isset( $term['term_description'] ) ? $term['term_description'] : '';
- $termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) );
- $id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr );
- if ( ! is_wp_error( $id ) ) {
- if ( isset($term['term_id']) )
- $this->processed_terms[intval($term['term_id'])] = $id['term_id'];
- } else {
- printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) );
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
- echo ': ' . $id->get_error_message();
- echo '<br />';
- continue;
- }
- }
- unset( $this->terms );
- }
-
- function process_posts() {
- $this->posts = apply_filters( 'wp_import_posts', $this->posts );
- foreach ( $this->posts as $post ) {
- $post = apply_filters( 'wp_import_post_data_raw', $post );
- if ( ! post_type_exists( $post['post_type'] ) ) {
- printf( __( 'Failed to import “%s”: Invalid post type %s', 'wordpress-importer' ),
- esc_html($post['post_title']), esc_html($post['post_type']) );
- echo '<br />';
- do_action( 'wp_import_post_exists', $post );
- continue;
- }
- if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) )
- continue;
- if ( $post['status'] == 'auto-draft' )
- continue;
- if ( 'nav_menu_item' == $post['post_type'] ) {
- $this->process_menu_item( $post );
- continue;
- }
- $post_type_object = get_post_type_object( $post['post_type'] );
- $post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
- if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
- printf( __('%s “%s” already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) );
- echo '<br />';
- $comment_post_ID = $post_id = $post_exists;
- } else {
- $post_parent = (int) $post['post_parent'];
- if ( $post_parent ) {
-
- if ( isset( $this->processed_posts[$post_parent] ) ) {
- $post_parent = $this->processed_posts[$post_parent];
-
- } else {
- $this->post_orphans[intval($post['post_id'])] = $post_parent;
- $post_parent = 0;
- }
- }
-
- $author = sanitize_user( $post['post_author'], true );
- if ( isset( $this->author_mapping[$author] ) )
- $author = $this->author_mapping[$author];
- else
- $author = (int) get_current_user_id();
- $postdata = array(
- 'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'],
- 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'],
- 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'],
- 'post_status' => $post['status'], 'post_name' => $post['post_name'],
- 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'],
- 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'],
- 'post_type' => $post['post_type'], 'post_password' => $post['post_password']
- );
- $original_post_ID = $post['post_id'];
- $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
- if ( 'attachment' == $postdata['post_type'] ) {
- $remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
-
-
- $postdata['upload_date'] = $post['post_date'];
- if ( isset( $post['postmeta'] ) ) {
- foreach( $post['postmeta'] as $meta ) {
- if ( $meta['key'] == '_wp_attached_file' ) {
- if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) )
- $postdata['upload_date'] = $matches[0];
- break;
- }
- }
- }
- $comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url );
- } else {
- $comment_post_ID = $post_id = wp_insert_post( $postdata, true );
- do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post );
- }
- if ( is_wp_error( $post_id ) ) {
- printf( __( 'Failed to import %s “%s”', 'wordpress-importer' ),
- $post_type_object->labels->singular_name, esc_html($post['post_title']) );
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
- echo ': ' . $post_id->get_error_message();
- echo '<br />';
- continue;
- }
- if ( $post['is_sticky'] == 1 )
- stick_post( $post_id );
- }
-
- $this->processed_posts[intval($post['post_id'])] = (int) $post_id;
- if ( ! isset( $post['terms'] ) )
- $post['terms'] = array();
- $post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post );
-
- if ( ! empty( $post['terms'] ) ) {
- $terms_to_set = array();
- foreach ( $post['terms'] as $term ) {
-
- $taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain'];
- $term_exists = term_exists( $term['slug'], $taxonomy );
- $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
- if ( ! $term_id ) {
- $t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) );
- if ( ! is_wp_error( $t ) ) {
- $term_id = $t['term_id'];
- do_action( 'wp_import_insert_term', $t, $term, $post_id, $post );
- } else {
- printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($taxonomy), esc_html($term['name']) );
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
- echo ': ' . $t->get_error_message();
- echo '<br />';
- do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post );
- continue;
- }
- }
- $terms_to_set[$taxonomy][] = intval( $term_id );
- }
- foreach ( $terms_to_set as $tax => $ids ) {
- $tt_ids = wp_set_post_terms( $post_id, $ids, $tax );
- do_action( 'wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post );
- }
- unset( $post['terms'], $terms_to_set );
- }
- if ( ! isset( $post['comments'] ) )
- $post['comments'] = array();
- $post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post );
-
- if ( ! empty( $post['comments'] ) ) {
- $num_comments = 0;
- $inserted_comments = array();
- foreach ( $post['comments'] as $comment ) {
- $comment_id = $comment['comment_id'];
- $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID;
- $newcomments[$comment_id]['comment_author'] = $comment['comment_author'];
- $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email'];
- $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP'];
- $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url'];
- $newcomments[$comment_id]['comment_date'] = $comment['comment_date'];
- $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt'];
- $newcomments[$comment_id]['comment_content'] = $comment['comment_content'];
- $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved'];
- $newcomments[$comment_id]['comment_type'] = $comment['comment_type'];
- $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent'];
- $newcomments[$comment_id]['commentmeta'] = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array();
- if ( isset( $this->processed_authors[$comment['comment_user_id']] ) )
- $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']];
- }
- ksort( $newcomments );
- foreach ( $newcomments as $key => $comment ) {
-
- if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {
- if ( isset( $inserted_comments[$comment['comment_parent']] ) )
- $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
- $comment = wp_filter_comment( $comment );
- $inserted_comments[$key] = wp_insert_comment( $comment );
- do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post );
- foreach( $comment['commentmeta'] as $meta ) {
- $value = maybe_unserialize( $meta['value'] );
- add_comment_meta( $inserted_comments[$key], $meta['key'], $value );
- }
- $num_comments++;
- }
- }
- unset( $newcomments, $inserted_comments, $post['comments'] );
- }
- if ( ! isset( $post['postmeta'] ) )
- $post['postmeta'] = array();
- $post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post );
-
- if ( ! empty( $post['postmeta'] ) ) {
- foreach ( $post['postmeta'] as $meta ) {
- $key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post );
- $value = false;
- if ( '_edit_last' == $key ) {
- if ( isset( $this->processed_authors[intval($meta['value'])] ) )
- $value = $this->processed_authors[intval($meta['value'])];
- else
- $key = false;
- }
- if ( $key ) {
-
- if ( ! $value )
- $value = maybe_unserialize( $meta['value'] );
- add_post_meta( $post_id, $key, $value );
- do_action( 'import_post_meta', $post_id, $key, $value );
-
- if ( '_thumbnail_id' == $key )
- $this->featured_images[$post_id] = (int) $value;
- }
- }
- }
- }
- unset( $this->posts );
- }
-
- function process_menu_item( $item ) {
-
- if ( 'draft' == $item['status'] )
- return;
- $menu_slug = false;
- if ( isset($item['terms']) ) {
-
- foreach ( $item['terms'] as $term ) {
- if ( 'nav_menu' == $term['domain'] ) {
- $menu_slug = $term['slug'];
- break;
- }
- }
- }
-
- if ( ! $menu_slug ) {
- _e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' );
- echo '<br />';
- return;
- }
- $menu_id = term_exists( $menu_slug, 'nav_menu' );
- if ( ! $menu_id ) {
- printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) );
- echo '<br />';
- return;
- } else {
- $menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
- }
- foreach ( $item['postmeta'] as $meta )
- $$meta['key'] = $meta['value'];
- if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) {
- $_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
- } else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) {
- $_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
- } else if ( 'custom' != $_menu_item_type ) {
-
- $this->missing_menu_items[] = $item;
- return;
- }
- if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) {
- $_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)];
- } else if ( $_menu_item_menu_item_parent ) {
- $this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent;
- $_menu_item_menu_item_parent = 0;
- }
-
- $_menu_item_classes = maybe_unserialize( $_menu_item_classes );
- if ( is_array( $_menu_item_classes ) )
- $_menu_item_classes = implode( ' ', $_menu_item_classes );
- $args = array(
- 'menu-item-object-id' => $_menu_item_object_id,
- 'menu-item-object' => $_menu_item_object,
- 'menu-item-parent-id' => $_menu_item_menu_item_parent,
- 'menu-item-position' => intval( $item['menu_order'] ),
- 'menu-item-type' => $_menu_item_type,
- 'menu-item-title' => $item['post_title'],
- 'menu-item-url' => $_menu_item_url,
- 'menu-item-description' => $item['post_content'],
- 'menu-item-attr-title' => $item['post_excerpt'],
- 'menu-item-target' => $_menu_item_target,
- 'menu-item-classes' => $_menu_item_classes,
- 'menu-item-xfn' => $_menu_item_xfn,
- 'menu-item-status' => $item['status']
- );
- $id = wp_update_nav_menu_item( $menu_id, 0, $args );
- if ( $id && ! is_wp_error( $id ) )
- $this->processed_menu_items[intval($item['post_id'])] = (int) $id;
- }
-
- function process_attachment( $post, $url ) {
- if ( ! $this->fetch_attachments )
- return new WP_Error( 'attachment_processing_error',
- __( 'Fetching attachments is not enabled', 'wordpress-importer' ) );
-
- if ( preg_match( '|^/[\w\W]+$|', $url ) )
- $url = rtrim( $this->base_url, '/' ) . $url;
- $upload = $this->fetch_remote_file( $url, $post );
- if ( is_wp_error( $upload ) )
- return $upload;
- if ( $info = wp_check_filetype( $upload['file'] ) )
- $post['post_mime_type'] = $info['type'];
- else
- return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') );
- $post['guid'] = $upload['url'];
-
- $post_id = wp_insert_attachment( $post, $upload['file'] );
- wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
-
- if ( preg_match( '!^image/!', $info['type'] ) ) {
- $parts = pathinfo( $url );
- $name = basename( $parts['basename'], ".{$parts['extension']}" );
- $parts_new = pathinfo( $upload['url'] );
- $name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
- $this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
- }
- return $post_id;
- }
-
- function fetch_remote_file( $url, $post ) {
-
- $file_name = basename( $url );
-
- $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
- if ( $upload['error'] )
- return new WP_Error( 'upload_dir_error', $upload['error'] );
-
- $headers = wp_get_http( $url, $upload['file'] );
-
- if ( ! $headers ) {
- @unlink( $upload['file'] );
- return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
- }
-
- if ( $headers['response'] != '200' ) {
- @unlink( $upload['file'] );
- 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']) ) );
- }
- $filesize = filesize( $upload['file'] );
- if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
- @unlink( $upload['file'] );
- return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') );
- }
- if ( 0 == $filesize ) {
- @unlink( $upload['file'] );
- return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
- }
- $max_size = (int) $this->max_attachment_size();
- if ( ! empty( $max_size ) && $filesize > $max_size ) {
- @unlink( $upload['file'] );
- return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) );
- }
-
- $this->url_remap[$url] = $upload['url'];
- $this->url_remap[$post['guid']] = $upload['url'];
-
- if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url )
- $this->url_remap[$headers['x-final-location']] = $upload['url'];
- return $upload;
- }
-
- function backfill_parents() {
- global $wpdb;
-
- foreach ( $this->post_orphans as $child_id => $parent_id ) {
- $local_child_id = $local_parent_id = false;
- if ( isset( $this->processed_posts[$child_id] ) )
- $local_child_id = $this->processed_posts[$child_id];
- if ( isset( $this->processed_posts[$parent_id] ) )
- $local_parent_id = $this->processed_posts[$parent_id];
- if ( $local_child_id && $local_parent_id )
- $wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' );
- }
-
- $missing_menu_items = $this->missing_menu_items;
- foreach ( $missing_menu_items as $item )
- $this->process_menu_item( $item );
-
- foreach ( $this->menu_item_orphans as $child_id => $parent_id ) {
- $local_child_id = $local_parent_id = 0;
- if ( isset( $this->processed_menu_items[$child_id] ) )
- $local_child_id = $this->processed_menu_items[$child_id];
- if ( isset( $this->processed_menu_items[$parent_id] ) )
- $local_parent_id = $this->processed_menu_items[$parent_id];
- if ( $local_child_id && $local_parent_id )
- update_post_meta( $local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id );
- }
- }
-
- function backfill_attachment_urls() {
- global $wpdb;
-
- uksort( $this->url_remap, array(&$this, 'cmpr_strlen') );
- foreach ( $this->url_remap as $from_url => $to_url ) {
-
- $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) );
-
- $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) );
- }
- }
-
- function remap_featured_images() {
-
- foreach ( $this->featured_images as $post_id => $value ) {
- if ( isset( $this->processed_posts[$value] ) ) {
- $new_id = $this->processed_posts[$value];
-
- if ( $new_id != $value )
- update_post_meta( $post_id, '_thumbnail_id', $new_id );
- }
- }
- }
-
- function parse( $file ) {
- $parser = new WXR_Parser();
- return $parser->parse( $file );
- }
-
- function header() {
- echo '<div class="wrap">';
- screen_icon();
- echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>';
- $updates = get_plugin_updates();
- $basename = plugin_basename(__FILE__);
- if ( isset( $updates[$basename] ) ) {
- $update = $updates[$basename];
- echo '<div class="error"><p><strong>';
- 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 );
- echo '</strong></p></div>';
- }
- }
-
- function footer() {
- echo '</div>';
- }
-
- function greet() {
- echo '<div class="narrow">';
- echo '<p>'.__( 'Howdy! Upload your WordPress eXtended RSS (WXR) file and we’ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer' ).'</p>';
- echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>';
- wp_import_upload_form( 'admin.php?import=wordpress&step=1' );
- echo '</div>';
- }
-
- function is_valid_meta_key( $key ) {
-
-
- if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) )
- return false;
- return $key;
- }
-
- function allow_create_users() {
- return apply_filters( 'import_allow_create_users', true );
- }
-
- function allow_fetch_attachments() {
- return apply_filters( 'import_allow_fetch_attachments', true );
- }
-
- function max_attachment_size() {
- return apply_filters( 'import_attachment_size_limit', 0 );
- }
-
- function bump_request_timeout() {
- return 60;
- }
-
- function cmpr_strlen( $a, $b ) {
- return strlen($b) - strlen($a);
- }
- }
- }
- function wordpress_importer_init() {
- load_plugin_textdomain( 'wordpress-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
-
- $GLOBALS['wp_import'] = new WP_Import();
- 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' ) );
- }
- add_action( 'admin_init', 'wordpress_importer_init' );
|