' . esc_html__( 'Sorry, there has been an error.', 'alpus' ) . ' ';
/* translators: %s: file path */
printf( esc_html__( 'The export file could not be found at %s. It is likely that this was caused by a permissions problem.', 'alpus' ), '' . esc_html( $file['file'] ) . '' );
echo '
';
/* translators: %s: import file version */
printf( esc_html__( 'This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'alpus' ), esc_html( $import_data['version'] ) );
echo '
';
}
$this->get_authors_from_import( $import_data );
return true;
}
/**
* Retrieve authors from parsed WXR data
*
* Uses the provided author information from WXR 1.1 files
* or extracts info from each post for WXR 1.0 files
*
* @param array $import_data 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 ) ) {
/* translators: %s: post author */
printf( esc_html__( 'Failed to import author %s. Their posts will be attributed to the current user.', 'alpus' ), esc_html( $post['post_author'] ) );
echo ' ';
continue;
}
if ( ! isset( $this->authors[ $login ] ) ) {
$this->authors[ $login ] = array(
'author_login' => $login,
'author_display_name' => $post['post_author'],
);
}
}
}
}
/**
* Display pre-import options, author importing/mapping and option to
* fetch attachments
*/
function import_options() {
$j = 0;
?>
' . esc_html( $author['author_display_name'] );
if ( '1.0' != $this->version ) {
echo ' (' . esc_html( $author['author_login'] ) . ')';
}
echo ' ';
if ( '1.0' != $this->version ) {
echo '
';
}
$create_users = $this->allow_create_users();
if ( $create_users ) {
if ( '1.0' != $this->version ) {
esc_html_e( 'or create new user with login name:', 'alpus' );
$value = '';
} else {
esc_html_e( 'as a new user:', 'alpus' );
$value = esc_attr( sanitize_user( $author['author_login'], true ) );
}
echo ' ';
}
if ( ! $create_users && '1.0' == $this->version ) {
esc_html_e( 'assign posts to an existing user:', 'alpus' );
} else {
esc_html_e( 'or assign posts to an existing user:', 'alpus' );
}
wp_dropdown_users(
array(
'name' => "user_map[$n]",
'multi' => true,
'show_option_all' => esc_html__(
'- Select -',
'alpus'
),
)
);
echo '';
if ( '1.0' != $this->version ) {
echo '
';
}
}
/**
* 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
*/
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;
}
} elseif ( $create_users ) {
if ( ! empty( $_POST['user_new'][ $i ] ) ) {
$user_id = wp_create_user( $_POST['user_new'][ $i ], wp_generate_password() );
} elseif ( '1.0' != $this->version ) {
$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 {
/* translators: %s: author name */
printf( esc_html__( 'Failed to create new user for %s. Their posts will be attributed to the current user.', 'alpus' ), esc_html( $this->authors[ $old_login ]['author_display_name'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
echo ' ' . $user_id->get_error_message();
}
echo ' ';
}
}
// 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();
}
}
}
/**
* Create new categories based on import information
*
* Doesn't create a new category if its slug already exists
*/
function process_categories() {
$this->categories = apply_filters( 'wp_import_categories', $this->categories );
if ( empty( $this->categories ) ) {
return;
}
foreach ( $this->categories as $cat ) {
// if the category already exists leave it alone
$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;
}
update_option( 'alpha_import_processed_terms', $this->processed_terms );
$tax_meta_array = get_metadata( 'category', $term_id );
if ( $tax_meta_array && is_array( $tax_meta_array ) ) {
foreach ( $tax_meta_array as $old_meta_key => $old_meta_value ) {
delete_metadata( 'category', $term_id, $old_meta_key );
}
}
if ( ! empty( $cat['tax_meta'] ) ) {
foreach ( $cat['tax_meta'] as $meta ) {
$key = $meta['key'];
$value = $meta['value'];
update_metadata( 'category', $term_id, $key, $value );
}
}
continue;
}
$parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] );
$description = isset( $cat['category_description'] ) ? $cat['category_description'] : '';
$data = array(
'category_nicename' => $cat['category_nicename'],
'category_parent' => $parent,
'cat_name' => wp_slash( $cat['cat_name'] ),
'category_description' => wp_slash( $description ),
);
$id = wp_insert_category( $data );
if ( ! is_wp_error( $id ) && $id > 0 ) {
if ( isset( $cat['term_id'] ) ) {
/**
* Fires after add a term.
*
* @since 1.0
*/
do_action( 'alpha_importer_insert_term', $id, (int) $cat['term_id'] );
$this->processed_terms[ intval( $cat['term_id'] ) ] = $id;
}
update_option( 'alpha_import_processed_terms', $this->processed_terms );
if ( ! empty( $cat['tax_meta'] ) ) {
foreach ( $cat['tax_meta'] as $meta ) {
$key = $meta['key'];
$value = $meta['value'];
update_metadata( 'category', $id, $key, $value );
}
}
} else {
/* translators: %s: category name */
printf( esc_html__( 'Failed to import category %s', 'alpus' ), esc_html( $cat['category_nicename'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
echo ': ' . $id->get_error_message();
}
echo ' ';
continue;
}
$this->process_termmeta( $cat, $id );
}
unset( $this->categories );
}
/**
* Create new post tags based on import information
*
* Doesn't create a tag if its slug already exists
*/
function process_tags() {
$this->tags = apply_filters( 'wp_import_tags', $this->tags );
if ( empty( $this->tags ) ) {
return;
}
foreach ( $this->tags as $tag ) {
// if the tag already exists leave it alone
$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;
}
update_option( 'alpha_import_processed_terms', $this->processed_terms );
continue;
}
$description = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
$args = array(
'slug' => $tag['tag_slug'],
'description' => wp_slash( $description ),
);
$id = wp_insert_term( wp_slash( $tag['tag_name'] ), 'post_tag', $args );
if ( ! is_wp_error( $id ) ) {
if ( isset( $tag['term_id'] ) ) {
/**
* Fires after add a term.
*
* @since 1.0
*/
do_action( 'alpha_importer_insert_term', $id['term_id'], (int) $tag['term_id'] );
$this->processed_terms[ intval( $tag['term_id'] ) ] = $id['term_id'];
}
update_option( 'alpha_import_processed_terms', $this->processed_terms );
} else {
/* translators: %s: tag name */
printf( esc_html__( 'Failed to import post tag %s', 'alpus' ), esc_html( $tag['tag_name'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
echo ': ' . $id->get_error_message();
}
echo ' ';
continue;
}
$this->process_termmeta( $tag, $id['term_id'] );
}
unset( $this->tags );
}
/**
* Create new terms based on import information
*
* Doesn't create a term its slug already exists
*/
function process_terms() {
try {
$this->terms = apply_filters( 'wp_import_terms', $this->terms );
// Post Types Unlimited Compaibility
foreach ( $this->posts as $post ) {
if ( 'ptu_tax' == $post['post_type'] ) {
$meta_tmp = $post['postmeta'];
$meta = array();
foreach ( $meta_tmp as $meta_item ) {
$meta[ $meta_item['key'] ] = array( $meta_item['value'] );
}
// Check custom post type name
$name = array_key_exists( '_ptu_name', $meta ) ? $meta['_ptu_name'][0] : '';
// Custom post type name is required
if ( ! $name ) {
continue;
}
// Get custom labels
$label = array_key_exists( '_ptu_label', $meta ) ? $meta['_ptu_label'][0] : $name;
$singular_name = array_key_exists( '_ptu_singular_name', $meta ) ? $meta['_ptu_singular_name'][0] : $label;
$search_items = array_key_exists( '_ptu_labels_search_items', $meta ) ? $meta['_ptu_labels_search_items'][0] : sprintf( _x( 'Search %s', 'taxonomy label', 'alpus' ), $label );
$all_items = array_key_exists( '_ptu_labels_all_items', $meta ) ? $meta['_ptu_labels_all_items'][0] : sprintf( _x( 'All %s', 'taxonomy label', 'alpus' ), $label );
$parent_item = array_key_exists( '_ptu_labels_parent_item', $meta ) ? $meta['_ptu_labels_parent_item'][0] : sprintf( _x( 'Parent %s', 'taxonomy label', 'alpus' ), $singular_name );
$parent_item_colon = array_key_exists( '_ptu_labels_parent_item_colon', $meta ) ? $meta['_ptu_labels_parent_item_colon'][0] : sprintf( _x( 'Parent %s:', 'taxonomy label', 'alpus' ), $singular_name );
$view_item = array_key_exists( '_ptu_labels_parent_view_item', $meta ) ? $meta['_ptu_labels_parent_view_item'][0] : sprintf( _x( 'View %s', 'taxonomy label', 'alpus' ), $singular_name );
$edit_item = array_key_exists( '_ptu_labels_parent_edit_item', $meta ) ? $meta['_ptu_labels_parent_edit_item'][0] : sprintf( _x( 'Edit %s', 'taxonomy label', 'alpus' ), $singular_name );
$update_item = array_key_exists( '_ptu_labels_update_item', $meta ) ? $meta['_ptu_labels_update_item'][0] : sprintf( _x( 'Update %s', 'taxonomy label', 'alpus' ), $singular_name );
$add_new_item = array_key_exists( '_ptu_labels_add_new_item', $meta ) ? $meta['_ptu_labels_add_new_item'][0] : sprintf( _x( 'Add New %s', 'taxonomy label', 'alpus' ), $singular_name );
$add_new_item_name = array_key_exists( '_ptu_labels_add_new_item_name', $meta ) ? $meta['_ptu_labels_add_new_item_name'][0] : sprintf( _x( 'New %s', 'taxonomy label', 'alpus' ), $singular_name );
$popular_items = array_key_exists( '_ptu_labels_popular_items', $meta ) ? $meta['_ptu_labels_popular_items'][0] : sprintf( _x( 'Popular %s', 'taxonomy label', 'alpus' ), $label );
$separate_items_with_commas = array_key_exists( '_ptu_labels_separate_items_with_commas', $meta ) ? $meta['_ptu_labels_separate_items_with_commas'][0] : sprintf( _x( 'Separate %s with commas', 'taxonomy label', 'alpus' ), $label );
$add_or_remove_items = array_key_exists( '_ptu_labels_add_or_remove_items', $meta ) ? $meta['_ptu_labels_add_or_remove_items'][0] : sprintf( _x( 'Add or remove %s', 'taxonomy label', 'alpus' ), $label );
$choose_from_most_used = array_key_exists( '_ptu_labels_choose_from_most_used', $meta ) ? $meta['_ptu_labels_choose_from_most_used'][0] : sprintf( _x( 'Choose from the most used %s', 'taxonomy label', 'alpus' ), $label );
$choose_not_found = array_key_exists( '_ptu_labels_choose_from_most_used', $meta ) ? $meta['_ptu_labels_choose_from_most_used'][0] : sprintf( _x( 'Choose from the most used %s', 'taxonomy label', 'alpus' ), $label );
$not_found = array_key_exists( '_ptu_labels_not_found', $meta ) ? $meta['_ptu_labels_not_found'][0] : sprintf( _x( 'Choose from the most used %s', 'taxonomy label', 'alpus' ), $label );
$back_to_items = array_key_exists( '_ptu_labels_back_to_items', $meta ) ? $meta['_ptu_labels_back_to_items'][0] : sprintf( _x( 'Back to %s', 'taxonomy label', 'alpus' ), $label );
// labels array
$labels = array(
'name' => $label,
'singular_name' => $singular_name,
'all_items' => $all_items,
'edit_item' => $edit_item,
'view_item' => $view_item,
'update_item' => $update_item,
'add_new_item' => $add_new_item,
'new_item_name' => $add_new_item_name,
'parent_item' => $parent_item,
'parent_item_colon' => $parent_item_colon,
'search_items' => $search_items,
'popular_items' => $popular_items,
'separate_items_with_commas' => $separate_items_with_commas,
'add_or_remove_items' => $add_or_remove_items,
'choose_from_most_used' => $choose_from_most_used,
'not_found' => $not_found,
'back_to_items' => $back_to_items,
);
if ( array_key_exists( '_ptu_menu_name', $meta ) ) {
$labels['menu_name'] = $meta['_ptu_menu_name'][0];
}
// Define taxonomy arguments
$description = array_key_exists( '_ptu_description', $meta ) ? $meta['_ptu_description'][0] : '';
$public = array_key_exists( '_ptu_public', $meta ) ? $meta['_ptu_public'][0] : true;
$publicly_queryable = array_key_exists( '_ptu_publicly_queryable', $meta ) ? $meta['_ptu_publicly_queryable'][0] : true;
$hierarchical = array_key_exists( '_ptu_hierarchical', $meta ) ? $meta['_ptu_hierarchical'][0] : false;
$show_ui = array_key_exists( '_ptu_show_ui', $meta ) ? $meta['_ptu_show_ui'][0] : true;
$show_in_nav_menus = array_key_exists( '_ptu_show_in_nav_menus', $meta ) ? $meta['_ptu_show_in_nav_menus'][0] : true;
$show_in_menu = array_key_exists( '_ptu_show_in_menu', $meta ) ? $meta['_ptu_show_in_menu'][0] : true;
$show_in_rest = array_key_exists( '_ptu_show_in_rest', $meta ) ? $meta['_ptu_show_in_rest'][0] : false;
$show_admin_column = array_key_exists( '_ptu_show_admin_column', $meta ) ? $meta['_ptu_show_admin_column'][0] : true;
$query_var = array_key_exists( '_ptu_query_var', $meta ) ? $meta['_ptu_query_var'][0] : true;
$rewrite = array_key_exists( '_ptu_rewrite', $meta ) ? $meta['_ptu_rewrite'][0] : true;
$slug = array_key_exists( '_ptu_slug', $meta ) ? $meta['_ptu_slug'][0] : '';
$with_front = array_key_exists( '_ptu_with_front', $meta ) ? $meta['_ptu_with_front'][0] : true;
$rewrite_hierarchical = array_key_exists( '_ptu_with_front', $meta ) ? $meta['_ptu_with_front'][0] : false;
$rest_controller_class = array_key_exists( '_ptu_rest_controller_class', $meta ) ? $meta['_ptu_rest_controller_class'][0] : null;
$args = array(
'labels' => $labels,
'description' => wp_strip_all_tags( $description ),
'public' => wp_validate_boolean( $public ),
'publicly_queryable' => wp_validate_boolean( $publicly_queryable ),
'hierarchical' => wp_validate_boolean( $hierarchical ),
'show_ui' => wp_validate_boolean( $show_ui ),
'show_in_nav_menus' => wp_validate_boolean( $show_in_nav_menus ),
'show_in_menu' => wp_validate_boolean( $show_in_menu ),
'show_admin_column' => wp_validate_boolean( $show_admin_column ),
'query_var' => wp_validate_boolean( $query_var ),
'show_in_rest' => wp_validate_boolean( $show_in_rest ),
);
if ( $rewrite ) {
$args['rewrite'] = array(
'slug' => $slug,
'with_front' => wp_validate_boolean( $with_front ),
'hierarchical' => wp_validate_boolean( $rewrite_hierarchical ),
);
} else {
$args['rewrite'] = false;
}
if ( array_key_exists( '_ptu_rest_base', $meta ) ) {
$labels['rest_base'] = $meta['_ptu_rest_base'][0];
}
if ( array_key_exists( '_ptu_rest_controller_class', $meta ) ) {
$labels['rest_controller_class'] = $meta['_ptu_rest_controller_class'][0];
}
$object_type = get_post_meta( $tax_id, '_ptu_object_type', true );
$object_type = is_array( $object_type ) ? $object_type : array( 'post' );
if ( function_exists( 'alpha_register_tax' ) ) {
alpha_register_tax( $name, $object_type, $args );
}
}
}
if ( empty( $this->terms ) ) {
return;
}
foreach ( $this->terms as $term ) {
// if the term already exists in the correct taxonomy leave it alone
$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;
}
update_option( 'alpha_import_processed_terms', $this->processed_terms );
$tax_meta_array = get_metadata( $term['term_taxonomy'], $term_id );
if ( $tax_meta_array && is_array( $tax_meta_array ) ) {
foreach ( $tax_meta_array as $old_meta_key => $old_meta_value ) {
delete_metadata( $term['term_taxonomy'], $term_id, $old_meta_key );
}
}
if ( ! empty( $term['tax_meta'] ) ) {
foreach ( $term['tax_meta'] as $meta ) {
$key = $meta['key'];
$value = $meta['value'];
update_metadata( $term['term_taxonomy'], $term_id, $key, $value );
}
}
/**
* Fires after update a term.
*
* @since 1.0
*/
do_action( 'alpha_importer_update_term', (int) $term_id, (int) $term['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'] : '';
$args = array(
'slug' => $term['slug'],
'description' => wp_slash( $description ),
'parent' => (int) $parent,
);
$id = wp_insert_term( wp_slash( $term['term_name'] ), $term['term_taxonomy'], $args );
if ( ! is_wp_error( $id ) ) {
if ( isset( $term['term_id'] ) ) {
/**
* Fires after add a term.
*
* @since 1.0
*/
do_action( 'alpha_importer_insert_term', $id['term_id'], (int) $term['term_id'] );
$this->processed_terms[ intval( $term['term_id'] ) ] = $id['term_id'];
}
update_option( 'alpha_import_processed_terms', $this->processed_terms );
if ( ! empty( $term['tax_meta'] ) ) {
foreach ( $term['tax_meta'] as $meta ) {
$key = $meta['key'];
$value = $meta['value'];
update_metadata( $term['term_taxonomy'], $id['term_id'], $key, $value );
}
}
} else {
printf( esc_html__( 'Failed to import %1$s %2$s', 'alpus' ), esc_html( $term['term_taxonomy'] ), esc_html( $term['term_name'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
echo ': ' . $id->get_error_message();
}
echo ' ';
continue;
}
$this->process_termmeta( $term, $id['term_id'] );
}
unset( $this->terms );
} catch ( Exception $e ) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
/**
* Add metadata to imported term.
*
* @since 0.6.2
*
* @param array $term Term data from WXR import.
* @param int $term_id ID of the newly created term.
*/
protected function process_termmeta( $term, $term_id ) {
if ( ! function_exists( 'add_term_meta' ) ) {
return;
}
if ( ! isset( $term['termmeta'] ) ) {
$term['termmeta'] = array();
}
/**
* Filters the metadata attached to an imported term.
*
* @since 0.6.2
*
* @param array $termmeta Array of term meta.
* @param int $term_id ID of the newly created term.
* @param array $term Term data from the WXR import.
*/
$term['termmeta'] = apply_filters( 'wp_import_term_meta', $term['termmeta'], $term_id, $term );
if ( empty( $term['termmeta'] ) ) {
return;
}
foreach ( $term['termmeta'] as $meta ) {
/**
* Filters the meta key for an imported piece of term meta.
*
* @since 0.6.2
*
* @param string $meta_key Meta key.
* @param int $term_id ID of the newly created term.
* @param array $term Term data from the WXR import.
*/
$key = apply_filters( 'import_term_meta_key', $meta['key'], $term_id, $term );
if ( ! $key ) {
continue;
}
// Export gets meta straight from the DB so could have a serialized string
$value = maybe_unserialize( $meta['value'] );
add_term_meta( $term_id, wp_slash( $key ), wp_slash_strings_only( $value ) );
/**
* Fires after term meta is imported.
*
* @since 0.6.2
*
* @param int $term_id ID of the newly created term.
* @param string $key Meta key.
* @param mixed $value Meta value.
*/
do_action( 'import_term_meta', $term_id, $key, $value );
}
}
/**
* Create new posts based on import information
*
* 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.
*/
function process_posts( $index = 0 ) {
$this->posts = apply_filters( 'wp_import_posts', $this->posts );
// Post Types Unlimted Compaibility
$whitelist = array();
foreach ( $this->posts as $post ) {
if ( 'ptu' == $post['post_type'] ) {
foreach ( $post['postmeta'] as $meta ) {
if ( '_ptu_name' == $meta['key'] ) {
$whitelist[] = $meta['value'];
}
}
}
}
// Post Types Unlimited Compaibility
foreach ( $this->posts as $post ) {
if ( 'ptu_tax' == $post['post_type'] ) {
$meta_tmp = $post['postmeta'];
$meta = array();
foreach ( $meta_tmp as $meta_item ) {
$meta[ $meta_item['key'] ] = array( $meta_item['value'] );
}
// Check custom post type name
$name = array_key_exists( '_ptu_name', $meta ) ? $meta['_ptu_name'][0] : '';
// Custom post type name is required
if ( ! $name ) {
continue;
}
// Get custom labels
$label = array_key_exists( '_ptu_label', $meta ) ? $meta['_ptu_label'][0] : $name;
$singular_name = array_key_exists( '_ptu_singular_name', $meta ) ? $meta['_ptu_singular_name'][0] : $label;
$search_items = array_key_exists( '_ptu_labels_search_items', $meta ) ? $meta['_ptu_labels_search_items'][0] : sprintf( _x( 'Search %s', 'taxonomy label', 'alpus' ), $label );
$all_items = array_key_exists( '_ptu_labels_all_items', $meta ) ? $meta['_ptu_labels_all_items'][0] : sprintf( _x( 'All %s', 'taxonomy label', 'alpus' ), $label );
$parent_item = array_key_exists( '_ptu_labels_parent_item', $meta ) ? $meta['_ptu_labels_parent_item'][0] : sprintf( _x( 'Parent %s', 'taxonomy label', 'alpus' ), $singular_name );
$parent_item_colon = array_key_exists( '_ptu_labels_parent_item_colon', $meta ) ? $meta['_ptu_labels_parent_item_colon'][0] : sprintf( _x( 'Parent %s:', 'taxonomy label', 'alpus' ), $singular_name );
$view_item = array_key_exists( '_ptu_labels_parent_view_item', $meta ) ? $meta['_ptu_labels_parent_view_item'][0] : sprintf( _x( 'View %s', 'taxonomy label', 'alpus' ), $singular_name );
$edit_item = array_key_exists( '_ptu_labels_parent_edit_item', $meta ) ? $meta['_ptu_labels_parent_edit_item'][0] : sprintf( _x( 'Edit %s', 'taxonomy label', 'alpus' ), $singular_name );
$update_item = array_key_exists( '_ptu_labels_update_item', $meta ) ? $meta['_ptu_labels_update_item'][0] : sprintf( _x( 'Update %s', 'taxonomy label', 'alpus' ), $singular_name );
$add_new_item = array_key_exists( '_ptu_labels_add_new_item', $meta ) ? $meta['_ptu_labels_add_new_item'][0] : sprintf( _x( 'Add New %s', 'taxonomy label', 'alpus' ), $singular_name );
$add_new_item_name = array_key_exists( '_ptu_labels_add_new_item_name', $meta ) ? $meta['_ptu_labels_add_new_item_name'][0] : sprintf( _x( 'New %s', 'taxonomy label', 'alpus' ), $singular_name );
$popular_items = array_key_exists( '_ptu_labels_popular_items', $meta ) ? $meta['_ptu_labels_popular_items'][0] : sprintf( _x( 'Popular %s', 'taxonomy label', 'alpus' ), $label );
$separate_items_with_commas = array_key_exists( '_ptu_labels_separate_items_with_commas', $meta ) ? $meta['_ptu_labels_separate_items_with_commas'][0] : sprintf( _x( 'Separate %s with commas', 'taxonomy label', 'alpus' ), $label );
$add_or_remove_items = array_key_exists( '_ptu_labels_add_or_remove_items', $meta ) ? $meta['_ptu_labels_add_or_remove_items'][0] : sprintf( _x( 'Add or remove %s', 'taxonomy label', 'alpus' ), $label );
$choose_from_most_used = array_key_exists( '_ptu_labels_choose_from_most_used', $meta ) ? $meta['_ptu_labels_choose_from_most_used'][0] : sprintf( _x( 'Choose from the most used %s', 'taxonomy label', 'alpus' ), $label );
$choose_not_found = array_key_exists( '_ptu_labels_choose_from_most_used', $meta ) ? $meta['_ptu_labels_choose_from_most_used'][0] : sprintf( _x( 'Choose from the most used %s', 'taxonomy label', 'alpus' ), $label );
$not_found = array_key_exists( '_ptu_labels_not_found', $meta ) ? $meta['_ptu_labels_not_found'][0] : sprintf( _x( 'Choose from the most used %s', 'taxonomy label', 'alpus' ), $label );
$back_to_items = array_key_exists( '_ptu_labels_back_to_items', $meta ) ? $meta['_ptu_labels_back_to_items'][0] : sprintf( _x( 'Back to %s', 'taxonomy label', 'alpus' ), $label );
// labels array
$labels = array(
'name' => $label,
'singular_name' => $singular_name,
'all_items' => $all_items,
'edit_item' => $edit_item,
'view_item' => $view_item,
'update_item' => $update_item,
'add_new_item' => $add_new_item,
'new_item_name' => $add_new_item_name,
'parent_item' => $parent_item,
'parent_item_colon' => $parent_item_colon,
'search_items' => $search_items,
'popular_items' => $popular_items,
'separate_items_with_commas' => $separate_items_with_commas,
'add_or_remove_items' => $add_or_remove_items,
'choose_from_most_used' => $choose_from_most_used,
'not_found' => $not_found,
'back_to_items' => $back_to_items,
);
if ( array_key_exists( '_ptu_menu_name', $meta ) ) {
$labels['menu_name'] = $meta['_ptu_menu_name'][0];
}
// Define taxonomy arguments
$description = array_key_exists( '_ptu_description', $meta ) ? $meta['_ptu_description'][0] : '';
$public = array_key_exists( '_ptu_public', $meta ) ? $meta['_ptu_public'][0] : true;
$publicly_queryable = array_key_exists( '_ptu_publicly_queryable', $meta ) ? $meta['_ptu_publicly_queryable'][0] : true;
$hierarchical = array_key_exists( '_ptu_hierarchical', $meta ) ? $meta['_ptu_hierarchical'][0] : false;
$show_ui = array_key_exists( '_ptu_show_ui', $meta ) ? $meta['_ptu_show_ui'][0] : true;
$show_in_nav_menus = array_key_exists( '_ptu_show_in_nav_menus', $meta ) ? $meta['_ptu_show_in_nav_menus'][0] : true;
$show_in_menu = array_key_exists( '_ptu_show_in_menu', $meta ) ? $meta['_ptu_show_in_menu'][0] : true;
$show_in_rest = array_key_exists( '_ptu_show_in_rest', $meta ) ? $meta['_ptu_show_in_rest'][0] : false;
$show_admin_column = array_key_exists( '_ptu_show_admin_column', $meta ) ? $meta['_ptu_show_admin_column'][0] : true;
$query_var = array_key_exists( '_ptu_query_var', $meta ) ? $meta['_ptu_query_var'][0] : true;
$rewrite = array_key_exists( '_ptu_rewrite', $meta ) ? $meta['_ptu_rewrite'][0] : true;
$slug = array_key_exists( '_ptu_slug', $meta ) ? $meta['_ptu_slug'][0] : '';
$with_front = array_key_exists( '_ptu_with_front', $meta ) ? $meta['_ptu_with_front'][0] : true;
$rewrite_hierarchical = array_key_exists( '_ptu_with_front', $meta ) ? $meta['_ptu_with_front'][0] : false;
$rest_controller_class = array_key_exists( '_ptu_rest_controller_class', $meta ) ? $meta['_ptu_rest_controller_class'][0] : null;
$args = array(
'labels' => $labels,
'description' => wp_strip_all_tags( $description ),
'public' => wp_validate_boolean( $public ),
'publicly_queryable' => wp_validate_boolean( $publicly_queryable ),
'hierarchical' => wp_validate_boolean( $hierarchical ),
'show_ui' => wp_validate_boolean( $show_ui ),
'show_in_nav_menus' => wp_validate_boolean( $show_in_nav_menus ),
'show_in_menu' => wp_validate_boolean( $show_in_menu ),
'show_admin_column' => wp_validate_boolean( $show_admin_column ),
'query_var' => wp_validate_boolean( $query_var ),
'show_in_rest' => wp_validate_boolean( $show_in_rest ),
);
if ( $rewrite ) {
$args['rewrite'] = array(
'slug' => $slug,
'with_front' => wp_validate_boolean( $with_front ),
'hierarchical' => wp_validate_boolean( $rewrite_hierarchical ),
);
} else {
$args['rewrite'] = false;
}
if ( array_key_exists( '_ptu_rest_base', $meta ) ) {
$labels['rest_base'] = $meta['_ptu_rest_base'][0];
}
if ( array_key_exists( '_ptu_rest_controller_class', $meta ) ) {
$labels['rest_controller_class'] = $meta['_ptu_rest_controller_class'][0];
}
$object_type = get_post_meta( $tax_id, '_ptu_object_type', true );
$object_type = is_array( $object_type ) ? $object_type : array( 'post' );
if ( function_exists( 'alpha_register_tax' ) ) {
alpha_register_tax( $name, $object_type, $args );
}
}
}
$post = $this->posts[ $index ];
if ( ! $post ) {
unset( $this->posts );
return;
}
$post = apply_filters( 'wp_import_post_data_raw', $post );
if ( ! post_type_exists( $post['post_type'] ) && ! in_array( $post['post_type'], $whitelist ) ) {
printf(
esc_html__( 'Failed to import “%1$s”: Invalid post type %2$s', 'alpus' ),
esc_html( $post['post_title'] ),
esc_html( $post['post_type'] )
);
echo ' ';
do_action( 'wp_import_post_exists', $post );
//continue;
unset( $this->posts );
return;
}
if ( isset( $this->processed_posts[ $post['post_id'] ] ) && ! empty( $post['post_id'] ) ) {
//continue;
unset( $this->posts );
return;
}
if ( 'auto-draft' == $post['status'] ) {
//continue;
unset( $this->posts );
return;
}
if ( 'nav_menu_item' == $post['post_type'] ) {
$this->process_menu_item( $post );
//continue;
unset( $this->posts );
return;
}
$post_type_object = get_post_type_object( $post['post_type'] );
$post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
$post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post );
if ( 'attachment' == $post['post_type'] && $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
$override_contents = ( isset( $_POST['override_contents'] ) && 'true' == $_POST['override_contents'] ) ? true : false;
if ( $override_contents ) {
wp_delete_attachment( $post_exists, true );
$post_exists = false;
}
}
if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
$comment_post_id = $post_id = $post_exists;
if ( in_array( $post['post_type'], Alpha_Setup_Wizard::get_instance()->demo_import_post_types ) ) {
$post_parent = (int) $post['post_parent'];
if ( $post_parent ) {
// if we already know the parent, map it to the new local ID
if ( isset( $this->processed_posts[ $post_parent ] ) ) {
$post_parent = $this->processed_posts[ $post_parent ];
// otherwise record the parent for later
} else {
$this->post_orphans[ intval( $post['post_id'] ) ] = $post_parent;
$post_parent = 0;
}
}
// map the post author
$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(
'ID' => $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'],
);
$postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
$comment_post_id = $post_id = wp_update_post( $postdata, true );
if ( is_wp_error( $post_id ) ) {
printf(
esc_html__( 'Failed to import %1$s “%2$s”', 'alpus' ),
$post_type_object->labels->singular_name,
esc_html( $post['post_title'] )
);
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
echo ': ' . $post_id->get_error_message();
}
echo ' ';
//continue;
unset( $this->posts );
return;
}
/**
* Fires after update a post.
*
* @since 1.0
*/
do_action( 'alpha_importer_update_post', $post_id, $post['post_id'], $postdata );
// meta fields to refresh
$meta_fields = array( '_wpb_post_custom_css', '_wpb_shortcodes_custom_css', '_elementor_data', '_elementor_css', '_elementor_edit_mode', '_elementor_page_settings', '_elementor_version', 'page_css', 'page_js' );
foreach ( $meta_fields as $key ) {
delete_post_meta( $post_id, $key );
}
if ( function_exists( 'alpha_ct_default_view_meta_fields' ) ) {
$meta_fields = alpha_ct_default_view_meta_fields();
foreach ( $meta_fields as $key => $value ) {
delete_post_meta( $post_id, $key );
}
}
if ( function_exists( 'alpha_ct_default_skin_meta_fields' ) ) {
$meta_fields = alpha_ct_default_skin_meta_fields();
foreach ( $meta_fields as $key => $value ) {
delete_post_meta( $post_id, $key );
}
}
}
} else {
$post_parent = (int) $post['post_parent'];
if ( $post_parent ) {
// if we already know the parent, map it to the new local ID
if ( isset( $this->processed_posts[ $post_parent ] ) ) {
$post_parent = $this->processed_posts[ $post_parent ];
// otherwise record the parent for later
} else {
$this->post_orphans[ intval( $post['post_id'] ) ] = $post_parent;
$post_parent = 0;
}
}
// map the post author
$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 );
$postdata = wp_slash( $postdata );
if ( 'attachment' == $postdata['post_type'] ) {
$remote_url = ! empty( $post['attachment_url'] ) ? $post['attachment_url'] : $post['guid'];
// try to use _wp_attached file for upload folder placement to ensure the same location as the export site
// e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
$postdata['upload_date'] = $post['post_date'];
if ( isset( $post['postmeta'] ) ) {
foreach ( $post['postmeta'] as $meta ) {
if ( '_wp_attached_file' == $meta['key'] ) {
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(
esc_html__( 'Failed to import %1$s “%2$s”', 'alpus' ),
$post_type_object->labels->singular_name,
esc_html( $post['post_title'] )
);
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
echo ': ' . $post_id->get_error_message();
}
echo ' ';
//continue;
unset( $this->posts );
return;
}
if ( 1 == $post['is_sticky'] ) {
stick_post( $post_id );
}
}
// map pre-import ID to local 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 );
// add categories, tags and other terms
if ( ! empty( $post['terms'] ) ) {
$terms_to_set = array();
foreach ( $post['terms'] as $term ) {
// back compat with WXR 1.0 map 'tag' to 'post_tag'
$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( esc_html__( 'Failed to import %1$s %2$s', 'alpus' ), esc_html( $taxonomy ), esc_html( $term['name'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
echo ': ' . $t->get_error_message();
}
echo ' ';
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 );
// add/update comments
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 this is a new post we can skip the comment_exists() check
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_data = wp_slash( $comment );
unset( $comment_data['commentmeta'] ); // Handled separately, wp_insert_comment() also expects `comment_meta`.
$comment_data = wp_filter_comment( $comment_data );
$inserted_comments[ $key ] = wp_insert_comment( $comment_data );
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 ], wp_slash( $meta['key'] ), wp_slash_strings_only( $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 );
// add/update post meta
if ( 'page' == $post['post_type'] ) {
if ( $post_exists ) {
delete_post_meta( $post_id, 'alpha_imported_date' );
}
add_post_meta( $post_id, 'alpha_imported_date', current_time( 'mysql' ) );
}
if ( ! empty( $post['postmeta'] ) ) {
$is_vc_post = false;
$has_vc_content = false;
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;
}
}
$demo = ( isset( $_POST['demo'] ) && $_POST['demo'] ) ? $_POST['demo'] : 'landing';
if ( false !== strpos( $demo, 'vc-' ) && false !== strpos( $key, '_elementor' ) ) {
continue;
}
if ( false !== strpos( $demo, 'wpb-' ) && false !== strpos( $key, '_elementor' ) ) {
continue;
}
if ( $key ) {
// export gets meta straight from the DB so could have a serialized string
if ( ! $value ) {
$value = maybe_unserialize( $meta['value'] );
}
if ( $post_exists ) {
delete_post_meta( $post_id, $key );
}
if ( alpha_get_feature( 'fs_pb_elementor' ) && '_elementor_data' == $key && defined( 'ELEMENTOR_VERSION' ) ) {
$imported = false;
$wp_importer = get_plugins( '/wordpress-importer' );
if ( ! empty( $wp_importer ) ) {
$wp_importer_version = $wp_importer['wordpress-importer.php']['Version'];
if ( version_compare( $wp_importer_version, '0.7', '<' ) ) {
add_post_meta( $post_id, wp_slash( $key ), $value );
$imported = true;
}
}
if ( ! $imported ) {
add_post_meta( $post_id, wp_slash( $key ), wp_slash_strings_only( $value ) );
}
} else {
add_post_meta( $post_id, wp_slash( $key ), wp_slash_strings_only( $value ) );
}
do_action( 'import_post_meta', $post_id, $key, $value );
// if the post has a featured image, take note of this in case of remap
if ( '_thumbnail_id' == $key ) {
$this->featured_images[ $post_id ] = (int) $value;
}
}
}
}
unset( $this->posts );
}
/**
* Attempt to create a new menu item from import data
*
* 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).
*
* @param array $item Menu item details from WXR file
*/
function process_menu_item( $item ) {
// skip draft, orphaned menu items
if ( 'draft' == $item['status'] ) {
return;
}
$menu_slug = false;
if ( isset( $item['terms'] ) ) {
// loop through terms, assume first nav_menu term is correct menu
foreach ( $item['terms'] as $term ) {
if ( 'nav_menu' == $term['domain'] ) {
$menu_slug = $term['slug'];
break;
}
}
}
// no nav_menu term associated with this menu item
if ( ! $menu_slug ) {
esc_html_e( 'Menu item skipped due to missing menu slug', 'alpus' );
echo ' ';
return;
}
$menu_id = term_exists( $menu_slug, 'nav_menu' );
if ( ! $menu_id ) {
printf( esc_html__( 'Menu item skipped due to invalid menu slug: %s', 'alpus' ), esc_html( $menu_slug ) );
echo ' ';
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 ) ];
} elseif ( '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 ) ];
} elseif ( 'custom' != $_menu_item_type ) {
// associated object is missing or not imported yet, we'll retry later
if ( ! in_array( $item, $this->missing_menu_items ) ) {
$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 ) ];
} elseif ( $_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;
}
// wp_update_nav_menu_item expects CSS classes as a space separated string
$_menu_item_classes = maybe_unserialize( $_menu_item_classes );
if ( is_array( $_menu_item_classes ) ) {
$_menu_item_classes = implode( ' ', $_menu_item_classes );
}
if ( isset( $_menu_item_url ) && strpos( $_menu_item_url, ALPHA_SERVER_URI . 'dummy' ) === 0 ) {
$_menu_item_url = str_replace( ALPHA_SERVER_URI . 'dummy', get_home_url(), $_menu_item_url );
}
$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'],
'menu-item-nolink' => isset( $_menu_item_nolink ) ? $_menu_item_nolink : '',
'menu-item-megamenu' => isset( $_menu_item_megamenu ) ? $_menu_item_megamenu : '',
'menu-item-megamenu_width' => isset( $_menu_item_megamenu_width ) ? $_menu_item_megamenu_width : '',
'menu-item-megamenu_pos' => isset( $_menu_item_megamenu_pos ) ? $_menu_item_megamenu_pos : '',
'menu-item-image' => isset( $_menu_item_image ) ? $_menu_item_image : '',
'menu-item-icon' => isset( $_menu_item_icon ) ? $_menu_item_icon : '',
'menu-item-label_name' => isset( $_menu_item_label_name ) ? $_menu_item_label_name : '',
'menu-item-block' => isset( $_menu_item_block ) ? $_menu_item_block : '',
);
$id = wp_update_nav_menu_item( $menu_id, 0, $args );
if ( $id && ! is_wp_error( $id ) ) {
/**
* Fires after insert nav menu item.
*
* @since 1.0
*/
do_action( 'alpha_importer_insert_nav_menu_item', (int) $id );
$this->processed_menu_items[ intval( $item['post_id'] ) ] = (int) $id;
}
}
/**
* If fetching attachments is enabled then attempt to create a new attachment
*
* @param array $post Attachment post details from WXR
* @param string $url URL to fetch attachment from
* @return int|WP_Error Post ID on success, WP_Error otherwise
*/
function process_attachment( $post, $url ) {
if ( ! $this->fetch_attachments ) {
return new WP_Error(
'attachment_processing_error',
esc_html__( 'Fetching attachments is not enabled', 'alpus' )
);
}
// if the URL is absolute, but does not contain address, then upload it assuming base_site_url
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', esc_html__( 'Invalid file type', 'alpus' ) );
}
$post['guid'] = $upload['url'];
$override_contents = ( isset( $_POST['override_contents'] ) && 'true' == $_POST['override_contents'] ) ? true : false;
if ( $override_contents && isset( $post['import_id'] ) && ! empty( $post['import_id'] ) ) {
$post_exists = get_post( $post['import_id'] );
if ( $post_exists ) {
if ( alpha_get_feature( 'fs_pb_elementor' ) && defined( 'ELEMENTOR_VERSION' ) && 'kit' == get_post_meta( $post['import_id'], '_elementor_template_type', true ) ) {
$_GET['force_delete_kit'] = true;
}
wp_delete_post( $post['import_id'], true );
unset( $_GET['force_delete_kit'] );
}
}
// as per wp-admin/includes/upload.php
$post_id = wp_insert_attachment( $post, $upload['file'] );
wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
/**
* Fires after insert an attachment.
*
* @since 1.0
*/
do_action( 'alpha_importer_insert_attachment', $post_id, isset( $post['import_id'] ) ? $post['import_id'] : false );
// remap resized image URLs, works by stripping the extension and remapping the URL stub.
if ( preg_match( '!^image/!', $info['type'] ) ) {
$parts = pathinfo( $url );
$name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
$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;
}
/**
* Attempt to download a remote file attachment
*
* @param string $url URL of item to fetch
* @param array $post Attachment details
* @return array|WP_Error Local file location details on success, WP_Error otherwise
*/
function fetch_remote_file( $url, $post ) {
// extract the file name and extension from the url
$file_name = basename( $url );
// get placeholder file in the upload dir with a unique, sanitized filename
$upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
if ( $upload['error'] ) {
return new WP_Error( 'upload_dir_error', $upload['error'] );
}
// fetch the remote url and write it to the placeholder file
$wp_http = new WP_Http_Streams();
$http_response = $wp_http->request(
$url,
array(
'filename' => $upload['file'],
'stream' => true,
'sslcertificates' => '',
'decompress' => false,
)
);
// request failed
if ( is_wp_error( $http_response ) ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', esc_html__( 'Remote server did not respond', 'alpus' ) );
}
// make sure the fetch was successful
$response = $http_response['response'];
if ( '200' != $response['code'] ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', sprintf( esc_html__( 'Remote server returned error response %1$d %2$s', 'alpus' ), esc_html( $response['code'] ), get_status_header_desc( $response['code'] ) ) );
}
$headers = $http_response['headers'];
$filesize = filesize( $upload['file'] );
$max_size = (int) $this->max_attachment_size();
if ( ( ! empty( $max_size ) && $filesize > $max_size ) || ( ! empty( $max_size ) && isset( $headers['content-length'] ) && $headers['content-length'] > $max_size ) ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', sprintf( esc_html__( 'Remote file is too large, limit is %s', 'alpus' ), size_format( $max_size ) ) );
}
// keep track of the old and new urls so we can substitute them later
$this->url_remap[ $url ] = $upload['url'];
$this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
// keep track of the destination if the remote url is redirected somewhere else
if ( isset( $headers['x-final-location'] ) && $headers['x-final-location'] != $url ) {
$this->url_remap[ $headers['x-final-location'] ] = $upload['url'];
}
return $upload;
}
/**
* Attempt to associate posts and menu items with previously missing parents
*
* An imported post's parent may not have been imported when it was first created
* so try again. Similarly for child menu items and menu items which were missing
* the object (e.g. post) they represent in the menu
*/
function backfill_parents() {
global $wpdb;
// find parents for post orphans
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' );
}
}
}
function process_menus( $index = 0 ) {
// all other posts/terms are imported, retry menu items with missing associated object
$missing_menu_items = $this->missing_menu_items;
$item = $missing_menu_items[ $index ];
$this->process_menu_item( $item );
}
function process_menus_orphans( $index = 0 ) {
// find parents for menu item orphans
$menu_item_orphans_values = array_values( $this->menu_item_orphans );
$parent_id = $menu_item_orphans_values[ $index ];
$menu_item_orphans_keys = array_keys( $this->menu_item_orphans );
$child_id = $menu_item_orphans_keys[ $index ];
$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 );
}
}
/**
* Use stored mapping information to update old attachment URLs
*/
function backfill_attachment_urls() {
global $wpdb;
// make sure we do the longest urls first, in case one is a substring of another
uksort( $this->url_remap, array( $this, 'cmpr_strlen' ) );
foreach ( $this->url_remap as $from_url => $to_url ) {
// remap urls in post_content
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url ) );
// remap enclosure urls
$result = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url ) );
}
}
/**
* Update _thumbnail_id meta to new, imported attachment IDs
*/
function remap_featured_images() {
// cycle through posts that have a featured image
foreach ( $this->featured_images as $post_id => $value ) {
if ( isset( $this->processed_posts[ $value ] ) ) {
$new_id = $this->processed_posts[ $value ];
// only update if there's a difference
if ( $new_id != $value ) {
update_post_meta( $post_id, '_thumbnail_id', $new_id );
}
}
}
}
/**
* Parse a WXR file
*
* @param string $file Path to WXR file for parsing
* @return array Information gathered from the WXR file
*/
function parse( $file ) {
$parser = new ALPHA_WXR_Parser();
return $parser->parse( $file );
}
// Display import page title
function header() {
echo '
';
/* translators: %s: importer version */
printf( esc_html__( 'A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'alpus' ), $update->update->new_version );
echo '
';
}
}
// Close div.wrap
function footer() {
echo '
';
}
/**
* Display introductory text and file upload form
*/
function greet() {
echo '
';
echo '
' . esc_html__( 'Howdy! Upload your WordPress eXtended RSS (WXR) file and we’ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'alpus' ) . '
';
echo '
' . esc_html__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'alpus' ) . '
';
}
/**
* Decide if the given meta key maps to information we will want to import
*
* @param string $key The meta key to check
* @return string|bool The key if we do want to import, false if not
*/
function is_valid_meta_key( $key ) {
// skip attachment metadata since we'll regenerate it from scratch
// skip _edit_lock as not relevant for import
if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) ) {
return false;
}
return $key;
}
/**
* Decide whether or not the importer is allowed to create users.
* Default is true, can be filtered via import_allow_create_users
*
* @return bool True if creating users is allowed
*/
function allow_create_users() {
return apply_filters( 'import_allow_create_users', true );
}
/**
* Decide whether or not the importer should attempt to download attachment files.
* Default is true, can be filtered via import_allow_fetch_attachments. The choice
* made at the import options screen must also be true, false here hides that checkbox.
*
* @return bool True if downloading attachments is allowed
*/
function allow_fetch_attachments() {
return apply_filters( 'import_allow_fetch_attachments', true );
}
/**
* Decide what the maximum file size for downloaded attachments is.
* Default is 0 (unlimited), can be filtered via import_attachment_size_limit
*
* @return int Maximum attachment file size to import
*/
function max_attachment_size() {
return apply_filters( 'import_attachment_size_limit', 0 );
}
/**
* Added to http_request_timeout filter to force timeout at 60 seconds during import
* @return int 60
*/
function bump_request_timeout( $val ) {
return 60;
}
// return the difference in length between two strings
function cmpr_strlen( $a, $b ) {
return strlen( $b ) - strlen( $a );
}
}
} // class_exists( 'ALPHA_WP_Importer' )