WP_Import::get_authors_from_import PHP Method

get_authors_from_import() public method

Uses the provided author information from WXR 1.1 files or extracts info from each post for WXR 1.0 files
public get_authors_from_import ( array $import_data )
$import_data array Data returned by a WXR parser
        function get_authors_from_import($import_data)
        {
            if (!empty($import_data['authors'])) {
                $this->authors = $import_data['authors'];
                // no author information, grab it from the posts
            } 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']);
                    }
                }
            }
        }

Usage Example

Example #1
0
 /**
  * Import a WXR file
  */
 private function import_wxr($file, $args)
 {
     $wp_import = new WP_Import();
     $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);
     $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');
     // $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)));
     }
     // Drive the import
     $wp_import->fetch_attachments = !in_array('attachment', $args['skip']);
     $_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'])) {
         add_filter('intermediate_image_sizes_advanced', array($this, 'filter_set_image_sizes'));
     }
     $wp_import->import($file);
     return true;
 }
All Usage Examples Of WP_Import::get_authors_from_import