parse() public method

Parse a WXR file
public parse ( string $file ) : array
$file string Path to WXR file for parsing
return array Information gathered from the WXR file
Ejemplo n.º 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;
 }
Ejemplo n.º 2
0
 protected function do_import_posts($cfg, $import_options = array('current_user' => true))
 {
     $file = $cfg['file'];
     $import_adventure_data = false;
     // $GLOBALS['wp_import'] is required for vafpress
     if (!empty($GLOBALS['wp_import'])) {
         $import = $GLOBALS['wp_import'];
     } else {
         $GLOBALS['wp_import'] = $import = new WP_Import();
     }
     // authors processing [start]
     $mapping_uid = get_current_user_id();
     if (empty($import_options['current_user'])) {
         $mapping_uid = 0;
         if (empty($import_options['another_user'])) {
             throw new Exception('Please enter username for an author.');
         } else {
             $another_username = $import_options['another_user'];
             $existing_user = get_user_by('login', $another_username);
             if ($existing_user) {
                 $mapping_uid = $existing_user->ID;
             } else {
                 $mapping_uid = wp_create_user($another_username, wp_generate_password());
                 if (is_wp_error($mapping_uid)) {
                     throw new Exception(sprintf('Can not create new user "%s": %s.', $another_username, $mapping_uid->get_error_message()));
                 }
             }
         }
     }
     $file_authors = array();
     if (isset($this->file_authors_cache[$file])) {
         $file_authors = $this->file_authors_cache[$file];
     } else {
         // retrivind author list from data file
         if (isset($_POST['imported_authors'])) {
             unset($_POST['imported_authors']);
         }
         $import_data = $import->parse($file);
         if (is_wp_error($import_data)) {
             throw new Exception(sprintf('Unexpected import error: %s.', esc_html($import_data->get_error_message())));
         }
         $import->get_authors_from_import($import_data);
         $this->file_authors_cache[$file] = $file_authors = $import->authors;
     }
     $authors_mapping = array();
     if ($file_authors) {
         foreach ($file_authors as $_login => $_details) {
             $authors_mapping[sanitize_user($_login, true)] = $mapping_uid;
         }
     }
     $import->author_mapping = $authors_mapping;
     // authors processing [end]
     $this->filter_types_only = empty($import_options['allowed_types']) ? array() : (array) $import_options['allowed_types'];
     $filter_posts_hook = array($this, 'filter_wp_import_posts');
     add_filter('wp_import_posts', $filter_posts_hook);
     if (!$this->filter_types_only || in_array('post', $this->filter_types_only)) {
         $this->allow_categories_import = true;
     }
     $filter_categories_hook = array($this, 'filter_wp_import_categories');
     add_filter('wp_import_categories', $filter_categories_hook);
     if (!$this->filter_types_only || in_array('product', $this->filter_types_only)) {
         $this->allow_product_terms = true;
         $import_adventure_data = !empty($cfg['file_adventure_addons']);
     }
     $filter_terms_hook = array($this, 'filter_wp_import_terms');
     add_filter('wp_import_terms', $filter_terms_hook);
     $filter_postmeta_key = array($this, 'filter_wp_postmeta_key');
     add_filter('import_post_meta_key', $filter_postmeta_key, 20, 3);
     ob_start();
     $import->fetch_attachments = false;
     $import->import($file);
     if ($import_adventure_data) {
         $this->do_import_adventure_data($cfg['file_adventure_addons']);
     }
     $output = ob_get_clean();
     remove_filter('wp_import_posts', $filter_posts_hook);
     remove_filter('wp_import_categories', $filter_categories_hook);
     remove_filter('wp_import_terms', $filter_terms_hook);
     remove_filter('import_post_meta_key', $filter_postmeta_key, 20);
     return $output;
 }