WP_Import::get_author_mapping PHP Method

get_author_mapping() public method

Map old author logins to local user IDs based on decisions made in import options form. Can map to an existing user, create a new user or falls back to the current user in case of error with either of the previous
public get_author_mapping ( )
        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) {
                // Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts.
                $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 />';
                        }
                    }
                }
                // failsafe: if the user_id was invalid, default to the current user
                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();
                }
            }
        }

Usage Example

 public function wpImportAttachments($file, $import_limit = 10)
 {
     $wp_import = new WP_Import();
     $wp_import->fetch_attachments = true;
     // load data from saved option
     $wp_import->post_orphans = get_option('_cri_post_orphans', array());
     $wp_import->processed_posts = get_option('_cri_processed_posts', array());
     $wp_import->url_remap = get_option('_cri_url_remap', array());
     add_filter('import_post_meta_key', array($wp_import, 'is_valid_meta_key'));
     add_filter('http_request_timeout', array(&$wp_import, 'bump_request_timeout'));
     // start buffer
     ob_start();
     // parse file and gather data
     $wp_import->import_start($file);
     // map author
     $wp_import->get_author_mapping();
     // attachment to be imported
     $attachments = array();
     foreach ($wp_import->posts as $post) {
         // only import attachment
         if ($post['post_type'] == 'attachment') {
             // if attachment has been imported already
             if (isset($wp_import->processed_posts[$post['post_id']]) && !empty($post['post_id'])) {
                 continue;
             }
             // if limit exceed, kill the loop
             if ($import_limit < 1) {
                 break;
             } else {
                 $import_limit--;
             }
             $attachments[] = $post;
         }
     }
     // if attachments reach to zero, we are done
     if (empty($attachments)) {
         return true;
     }
     // set importable posts to attachments
     $wp_import->posts = $attachments;
     // this process the attachments, turn off/on cache
     wp_suspend_cache_invalidation(true);
     $wp_import->process_posts();
     wp_suspend_cache_invalidation(false);
     // end has output, so buffer it out
     $wp_import->import_end();
     ob_end_clean();
     // save all post_orphans, processed_posts & url_remap to be used on the next run. also this will run on post import
     update_option('_cri_post_orphans', $wp_import->post_orphans);
     update_option('_cri_processed_posts', $wp_import->processed_posts);
     update_option('_cri_url_remap', $wp_import->url_remap);
     // false means we are going to continue
     return false;
 }