PHP Class WXR_Importer, WordPress-Importer

Inheritance: extends WP_Importer
Show file Open project: humanmade/WordPress-Importer Class Usage Examples

Protected Properties

Property Type Description
$base_url
$categories information to import from WXR file
$exists
$featured_images
$logger WP_Importer_Logger Logger instance.
$mapping NEW STYLE
$menu_item_orphans
$missing_menu_items
$processed_menu_items
$processed_posts
$processed_terms TODO: REMOVE THESE
$requires_remapping
$tags
$url_remap
$user_slug_override
$version string Defaults to 1.0 for compatibility. Typically overridden by a tag at the start of the file.

Public Methods

Method Description
__construct ( array $options = [] ) Constructor
bump_request_timeout ( $val ) : integer Added to http_request_timeout filter to force timeout at 60 seconds during import
cmpr_strlen ( $a, $b ) return the difference in length between two strings
get_preliminary_information ( string $file ) The main controller for the actual import stage.
import ( string $file ) The main controller for the actual import stage.
is_valid_meta_key ( string $key ) : string | boolean Decide if the given meta key maps to information we will want to import
parse_authors ( string $file ) The main controller for the actual import stage.
remap_featured_images ( ) Update _thumbnail_id meta to new, imported attachment IDs
set_logger ( $logger )
set_user_mapping ( array $mapping ) Set the user mapping.
set_user_slug_overrides ( string[] $overrides ) Set the user slug overrides.
sort_comments_by_id ( array $a, array $b ) : integer Callback for usort to sort comments by ID

Protected Methods

Method Description
comment_exists ( array $data ) : integer | boolean Does the comment exist?
fetch_remote_file ( string $url, array $post ) : array | WP_Error Attempt to download a remote file attachment
get_reader ( string $file ) : XMLReader | WP_Error Get a stream reader for the file.
import_end ( ) Performs post-import cleanup of files and the cache
import_start ( string $file ) Parses the WXR file and prepares us for the task of processing parsed data
log_error ( WP_Error $error ) Log an error instance to the logger.
mark_comment_exists ( array $data, integer $comment_id ) Mark the comment as existing.
mark_post_exists ( array $data, integer $post_id ) Mark the post as existing.
mark_term_exists ( array $data, integer $term_id ) Mark the term as existing.
max_attachment_size ( ) : integer Decide what the maximum file size for downloaded attachments is.
parse_author_node ( $node )
parse_category_node ( $node )
parse_comment_node ( DOMElement $node ) : array Parse a comment node into comment data.
parse_meta_node ( DOMElement $node ) : array | null Parse a meta node into meta data.
parse_post_node ( DOMElement $node ) : array | WP_Error Parse a post node into post data.
parse_term_node ( $node, $type = 'term' )
post_exists ( array $data ) : integer | boolean Does the post exist?
post_process ( )
post_process_comments ( $todo )
post_process_menu_item ( $post_id )
post_process_posts ( $todo )
prefill_existing_comments ( ) Prefill existing comment data.
prefill_existing_posts ( ) Prefill existing post data.
prefill_existing_terms ( ) Prefill existing term data.
process_attachment ( array $post, $meta, $remote_url ) : integer | WP_Error If fetching attachments is enabled then attempt to create a new attachment
process_author ( $data, $meta )
process_comments ( array $comments, integer $post_id, array $post, $post_exists = false ) : integer | WP_Error Process and import comment data.
process_menu_item_meta ( $post_id, $data, $meta ) Attempt to create a new menu item from import data
process_post ( $data, $meta, $comments, $terms ) Create new posts based on import information
process_post_meta ( array $meta, integer $post_id, array $post ) : integer | WP_Error Process and import post meta items.
process_term ( $data, $meta )
replace_attachment_urls_in_content ( ) Use stored mapping information to update old attachment URLs
term_exists ( array $data ) : integer | boolean Does the term exist?

Method Details

__construct() public method

Constructor
public __construct ( array $options = [] )
$options array { @var bool $prefill_existing_posts Should we prefill `post_exists` calls? (True prefills and uses more memory, false checks once per imported post and takes longer. Default is true.) @var bool $prefill_existing_comments Should we prefill `comment_exists` calls? (True prefills and uses more memory, false checks once per imported comment and takes longer. Default is true.) @var bool $prefill_existing_terms Should we prefill `term_exists` calls? (True prefills and uses more memory, false checks once per imported term and takes longer. Default is true.) @var bool $update_attachment_guids Should attachment GUIDs be updated to the new URL? (True updates the GUID, which keeps compatibility with v1, false doesn't update, and allows deduplication and reimporting. Default is false.) @var bool $fetch_attachments Fetch attachments from the remote server. (True fetches and creates attachment posts, false skips attachments. Default is false.) @var bool $aggressive_url_search Should we search/replace for URLs aggressively? (True searches all posts' content for old URLs and replaces, false checks for `` only. Default is false.) @var int $default_author User ID to use if author is missing or invalid. (Default is null, which leaves posts unassigned.) }

bump_request_timeout() public method

Added to http_request_timeout filter to force timeout at 60 seconds during import
public bump_request_timeout ( $val ) : integer
return integer 60

cmpr_strlen() public method

return the difference in length between two strings
public cmpr_strlen ( $a, $b )

comment_exists() protected method

Does the comment exist?
protected comment_exists ( array $data ) : integer | boolean
$data array Comment data to check against.
return integer | boolean Existing comment ID if it exists, false otherwise.

fetch_remote_file() protected method

Attempt to download a remote file attachment
protected fetch_remote_file ( string $url, array $post ) : array | WP_Error
$url string URL of item to fetch
$post array Attachment details
return array | WP_Error Local file location details on success, WP_Error otherwise

get_preliminary_information() public method

The main controller for the actual import stage.
public get_preliminary_information ( string $file )
$file string Path to the WXR file for importing

get_reader() protected method

Get a stream reader for the file.
protected get_reader ( string $file ) : XMLReader | WP_Error
$file string Path to the XML file.
return XMLReader | WP_Error Reader instance on success, error otherwise.

import() public method

The main controller for the actual import stage.
public import ( string $file )
$file string Path to the WXR file for importing

import_end() protected method

Performs post-import cleanup of files and the cache
protected import_end ( )

import_start() protected method

Parses the WXR file and prepares us for the task of processing parsed data
protected import_start ( string $file )
$file string Path to the WXR file for importing

is_valid_meta_key() public method

Decide if the given meta key maps to information we will want to import
public is_valid_meta_key ( string $key ) : string | boolean
$key string The meta key to check
return string | boolean The key if we do want to import, false if not

log_error() protected method

Log an error instance to the logger.
protected log_error ( WP_Error $error )
$error WP_Error Error instance to log.

mark_comment_exists() protected method

Mark the comment as existing.
protected mark_comment_exists ( array $data, integer $comment_id )
$data array Comment data to mark as existing.
$comment_id integer Comment ID.

mark_post_exists() protected method

Mark the post as existing.
protected mark_post_exists ( array $data, integer $post_id )
$data array Post data to mark as existing.
$post_id integer Post ID.

mark_term_exists() protected method

Mark the term as existing.
protected mark_term_exists ( array $data, integer $term_id )
$data array Term data to mark as existing.
$term_id integer Term ID.

max_attachment_size() protected method

Default is 0 (unlimited), can be filtered via import_attachment_size_limit
protected max_attachment_size ( ) : integer
return integer Maximum attachment file size to import

parse_author_node() protected method

protected parse_author_node ( $node )

parse_authors() public method

The main controller for the actual import stage.
public parse_authors ( string $file )
$file string Path to the WXR file for importing

parse_category_node() protected method

protected parse_category_node ( $node )

parse_comment_node() protected method

Parse a comment node into comment data.
protected parse_comment_node ( DOMElement $node ) : array
$node DOMElement Parent node of comment data (typically `wp:comment`).
return array Comment data array.

parse_meta_node() protected method

Parse a meta node into meta data.
protected parse_meta_node ( DOMElement $node ) : array | null
$node DOMElement Parent node of meta data (typically `wp:postmeta` or `wp:commentmeta`).
return array | null Meta data array on success, or null on error.

parse_post_node() protected method

Parse a post node into post data.
protected parse_post_node ( DOMElement $node ) : array | WP_Error
$node DOMElement Parent node of post data (typically `item`).
return array | WP_Error Post data array on success, error otherwise.

parse_term_node() protected method

protected parse_term_node ( $node, $type = 'term' )

post_exists() protected method

Does the post exist?
protected post_exists ( array $data ) : integer | boolean
$data array Post data to check against.
return integer | boolean Existing post ID if it exists, false otherwise.

post_process() protected method

protected post_process ( )

post_process_comments() protected method

protected post_process_comments ( $todo )

post_process_menu_item() protected method

protected post_process_menu_item ( $post_id )

post_process_posts() protected method

protected post_process_posts ( $todo )

prefill_existing_comments() protected method

Prefill existing comment data.
See also: self::prefill_existing_posts() for justification of why this exists.

prefill_existing_posts() protected method

This preloads all GUIDs into memory, allowing us to avoid hitting the database when we need to check for existence. With larger imports, this becomes prohibitively slow to perform SELECT queries on each. By preloading all this data into memory, it's a constant-time lookup in PHP instead. However, this does use a lot more memory, so for sites doing small imports onto a large site, it may be a better tradeoff to use on-the-fly checking instead.
protected prefill_existing_posts ( )

prefill_existing_terms() protected method

Prefill existing term data.
See also: self::prefill_existing_posts() for justification of why this exists.
protected prefill_existing_terms ( )

process_attachment() protected method

If fetching attachments is enabled then attempt to create a new attachment
protected process_attachment ( array $post, $meta, $remote_url ) : integer | WP_Error
$post array Attachment post details from WXR
return integer | WP_Error Post ID on success, WP_Error otherwise

process_author() protected method

protected process_author ( $data, $meta )

process_comments() protected method

Process and import comment data.
protected process_comments ( array $comments, integer $post_id, array $post, $post_exists = false ) : integer | WP_Error
$comments array List of comment data arrays.
$post_id integer Post to associate with.
$post array Post data.
return integer | WP_Error Number of comments imported on success, error otherwise.

process_menu_item_meta() protected method

Fails for draft, orphaned menu items and those without an associated nav_menu or an invalid nav_menu term. If the post type or term object which the menu item represents doesn't exist then the menu item will not be imported (waits until the end of the import to retry again before discarding).
protected process_menu_item_meta ( $post_id, $data, $meta )

process_post() protected method

Posts marked as having a parent which doesn't exist will become top level items. Doesn't create a new post if: the post type doesn't exist, the given post ID is already noted as imported or a post with the same title and date already exists. Note that new/updated terms, comments and meta are imported for the last of the above.
protected process_post ( $data, $meta, $comments, $terms )

process_post_meta() protected method

Process and import post meta items.
protected process_post_meta ( array $meta, integer $post_id, array $post ) : integer | WP_Error
$meta array List of meta data arrays
$post_id integer Post to associate with
$post array Post data
return integer | WP_Error Number of meta items imported on success, error otherwise.

process_term() protected method

protected process_term ( $data, $meta )

replace_attachment_urls_in_content() protected method

Use stored mapping information to update old attachment URLs

set_logger() public method

public set_logger ( $logger )

set_user_mapping() public method

Set the user mapping.
public set_user_mapping ( array $mapping )
$mapping array List of map arrays (containing `old_slug`, `old_id`, `new_id`)

set_user_slug_overrides() public method

Allows overriding the slug in the import with a custom/renamed version.
public set_user_slug_overrides ( string[] $overrides )
$overrides string[] Map of old slug to new slug.

sort_comments_by_id() public static method

Callback for usort to sort comments by ID
public static sort_comments_by_id ( array $a, array $b ) : integer
$a array Comment data for the first comment
$b array Comment data for the second comment
return integer

term_exists() protected method

Does the term exist?
protected term_exists ( array $data ) : integer | boolean
$data array Term data to check against.
return integer | boolean Existing term ID if it exists, false otherwise.

Property Details

$base_url protected property

protected $base_url

$categories protected property

information to import from WXR file
protected $categories

$exists protected property

protected $exists

$logger protected property

Logger instance.
protected WP_Importer_Logger $logger
return WP_Importer_Logger

$mapping protected property

NEW STYLE
protected $mapping

$menu_item_orphans protected property

protected $menu_item_orphans

$missing_menu_items protected property

protected $missing_menu_items

$processed_menu_items protected property

protected $processed_menu_items

$processed_posts protected property

protected $processed_posts

$processed_terms protected property

TODO: REMOVE THESE
protected $processed_terms

$requires_remapping protected property

protected $requires_remapping

$tags protected property

protected $tags

$url_remap protected property

protected $url_remap

$user_slug_override protected property

protected $user_slug_override

$version protected property

Defaults to 1.0 for compatibility. Typically overridden by a tag at the start of the file.
protected string $version
return string