' . __( 'Sorry, there has been an error.', 'gpls-cpt-exporter-importer' ) . ' ';
printf( __( 'The export file could not be found at %s. It is likely that this was caused by a permissions problem.', 'gpls-cpt-exporter-importer' ), esc_html( $file['file'] ) );
echo '
' . __( 'Sorry, there has been an error.', 'gpls-cpt-exporter-importer' ) . ' ';
echo __( 'Invalid File Type, Upload the exported CPTs XML File!', 'gpls-cpt-exporter-importer' ) . '
' . __( 'Sorry, there has been an error.', 'gpls-cpt-exporter-importer' ) . ' ';
printf( __( 'The export file could not be found at %s. It is likely that this was caused by a permissions problem.', 'gpls-cpt-exporter-importer' ), esc_html( $file['file'] ) );
echo '
';
}
/**
* Import Start Process
*
* @param string $file File Path.
* @return void
*/
public function import_start( $file, $step ) {
if ( ! is_file( $file ) ) {
echo '
' . __( 'Sorry, there has been an error.', 'gpls-cpt-exporter-importer' ) . ' ';
echo __( 'The file does not exist, please try again.', 'gpls-cpt-exporter-importer' ) . '
';
die();
}
$this->parser = new GPLS_AEI_XML_Parser( $this );
if ( $step > 0 ) {
$this->processed_posts = get_site_option( $this->plugin_info['name'] . '-processed-posts', array() );
$this->posts_exists = get_site_option( $this->plugin_info['name'] . '-processed-posts-exists', array() );
$this->post_orphans = get_site_option( $this->plugin_info['name'] . '-processed-posts-orphans', array() );
$this->processed_terms = (array) json_decode( get_site_option( $this->plugin_info['name'] . '-processed-terms', '' ), true );
$this->processed_authors = get_site_option( $this->plugin_info['name'] . '-processed-authors', array() );
$this->processed_attachments = get_site_option( $this->plugin_info['name'] . '-processed-attachments', array() );
$this->processed_missing_taxonomies_names = get_site_option( $this->plugin_info['name'] . '-processed-missing-taxonomies-names', array() );
$this->processed_terms_have_thumbnail = get_site_option( $this->plugin_info['name'] . '-processed-terms-have-thumbnail', array() );
$this->featured_images = get_site_option( $this->plugin_info['name'] . '-processed-featured-images', array() );
$this->author_mapping_type = get_site_option( $this->plugin_info['name'] . '-author-mapping-type', array() );
$this->all_posts_single_author_mapping_id = get_site_option( $this->plugin_info['name'] . '-single-author-mapping', '' );
$this->author_mapping = get_site_option( $this->plugin_info['name'] . '-author-mapping', array() );
$this->attachments_needs_custom_sizes = (array) json_decode( get_site_option( $this->plugin_info['name'] . '-attachments-needs-custom-sizes', '' ), true );
$this->postmeta_remap = (array) json_decode( get_site_option( $this->plugin_info['name'] . '-postmeta-remap', '' ), true );
$this->errors = (array) json_decode( get_site_option( $this->plugin_info['name'] . '-processed-errors', '' ), true );
}
// Pypass the empty content import check.
add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 );
$this->process_tables_exists();
wp_defer_term_counting( true );
wp_defer_comment_counting( true );
}
/**
* Process Import
*
* @return void
*/
public function process_import( $file, $type, $step ) {
set_time_limit( 0 );
$this->import_start( $file, $step );
wp_suspend_cache_invalidation( true );
if ( $step > 0 ) {
$this->set_missing_taxonomies();
}
$this->parser->parse( $file, $step );
$this->backfill_parents();
$this->remap_featured_images();
$this->remap_terms_thumbnail();
$this->backfill_postmeta();
$this->remap_shortcodes( $step );
wp_suspend_cache_invalidation( false );
$this->import_end( $type, $step );
}
/**
* Import End Function
*
* @param string $type
* @return void
*/
public function import_end( $type, $step ) {
wp_cache_flush();
foreach ( get_taxonomies() as $tax ) {
delete_option( "{$tax}_children" );
_get_term_hierarchy( $tax );
}
$this->delete_transients();
wp_defer_comment_counting( false );
$this->update_terms_count();
$this->clean_registered_taxonomies();
if ( $step > 0 ) {
$this->errors = (array) json_decode( get_site_option( $this->plugin_info['name'] . '-processed-errors' ), true );
}
$this->clean_stored_processed();
wp_delete_attachment( $this->id, true );
}
/**
* Remove Stored Processed Data.
*
* @return void
*/
public function clean_stored_processed() {
delete_site_option( $this->plugin_info['name'] . '-processed-posts' );
delete_site_option( $this->plugin_info['name'] . '-processed-posts-exists' );
delete_site_option( $this->plugin_info['name'] . '-processed-posts-orphans' );
delete_site_option( $this->plugin_info['name'] . '-processed-authors' );
delete_site_option( $this->plugin_info['name'] . '-single-author-mapping' );
delete_site_option( $this->plugin_info['name'] . '-author-mapping-type' );
delete_site_option( $this->plugin_info['name'] . '-author-mapping' );
delete_site_option( $this->plugin_info['name'] . '-processed-terms' );
delete_site_option( $this->plugin_info['name'] . '-processed-authors' );
delete_site_option( $this->plugin_info['name'] . '-processed-attachments' );
delete_site_option( $this->plugin_info['name'] . '-processed-missing-taxonomies-names' );
delete_site_option( $this->plugin_info['name'] . '-processed-terms-have-thumbnail' );
delete_site_option( $this->plugin_info['name'] . '-processed-initials' );
delete_site_option( $this->plugin_info['name'] . '-processed-featured-images' );
delete_site_option( $this->plugin_info['name'] . '-shortcodes-posts-remap' );
delete_site_option( $this->plugin_info['name'] . '-processed-errors' );
delete_site_option( $this->plugin_info['name'] . '-processed-posts-names-types-combination' );
delete_site_option( $this->plugin_info['name'] . '-processed-empty-names-types-combination' );
delete_site_option( $this->plugin_info['name'] . '-attachments-needs-custom-sizes' );
delete_site_option( $this->plugin_info['name'] . '-menu-items-new-remapping' );
delete_site_option( $this->plugin_info['name'] . '-postmeta-remap' );
}
/**
* Remap Posts Shortcodes Targets remap.
*
* @return void
*/
public function remap_shortcodes( $step ) {
if ( $step > 0 ) {
$this->shortcodes_posts_to_remap = explode( ',', get_site_option( $this->plugin_info['name'] . '-shortcodes-posts-remap', array() ) );
}
foreach ( $this->shortcodes_posts_to_remap as $post_id ) {
if ( isset( $this->processed_posts[ intval( $post_id ) ] ) ) {
$this->update_post_shortcodes_targets( $this->processed_posts[ intval( $post_id ) ] );
}
}
}
/**
* Update Post Content Shortcode Targets.
*
* @param array $post_id
* @return void
*/
public function update_post_shortcodes_targets( $post_id ) {
global $wpdb;
$is_post_cached = wp_cache_get( $post_id, 'posts' );
if ( ! $is_post_cached ) {
$post_content = $wpdb->get_var( "SELECT post_content FROM {$wpdb->posts} WHERE ID = $post_id" );
} else {
$post_content = $is_post_cached->post_content;
}
if ( empty( $post_content ) ) {
return;
}
$shortcodes = $this->has_target_shortcodes( $post_content );
$this->update_ids_from_shortcodes( $shortcodes, $post_content, $post_id );
}
/**
* Display Import Error.
*
* @return void
*/
public function display_errors() {
foreach ( $this->errors as $error ) {
echo $error . ' ';
}
}
/**
* Process XML WooCommerce Attribute Taxonomy.
*
* @param array $wc_attribute_item
* @return void
*/
public function process_xml_wc_attribute_taxonomy( $wc_attribute_item ) {
global $wpdb;
$table_name = $wpdb->prefix . 'woocommerce_attribute_taxonomies';
if ( in_array( $table_name, array_keys( $this->tables_exists ) ) ) {
$attributes_names = $wpdb->get_col(
"SELECT
attribute_name
FROM
{$table_name}"
);
if ( ! in_array( $wc_attribute_item['attribute_name'], $attributes_names ) ) {
$result = $wpdb->query(
$wpdb->prepare(
"INSERT INTO {$table_name}
( attribute_name, attribute_label, attribute_type, attribute_orderby, attribute_public )
VALUES
( %s, %s, %s, %s, %d )
",
array(
$wc_attribute_item['attribute_name'],
$wc_attribute_item['attribute_label'],
$wc_attribute_item['attribute_type'],
$wc_attribute_item['attribute_orderby'],
$wc_attribute_item['attribute_public'],
)
)
);
}
}
}
/**
* Delete Needed transients after importing.
*
* @return void
*/
public function delete_transients() {
// Woo Attributes.
delete_transient( 'wc_attribute_taxonomies' );
// Woo Featured Attributes.
delete_transient( 'wc_featured_products' );
// Woo Terms Counts.
delete_transient( 'wc_term_counts' );
}
/**
* Update Terms Count in DB.
*
* @return void
*/
public function update_terms_count() {
global $wpdb, $wp_taxonomies;
foreach ( $this->processed_terms as $old_term_id => $term_new_id ) {
$term_taxonomy_id = intval( $term_new_id['term_taxonomy_id'] );
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = $term_taxonomy_id" );
$wpdb->update(
$wpdb->term_taxonomy,
array(
'count' => $count,
),
array(
'term_taxonomy_id' => intval( $term_new_id['term_taxonomy_id'] ),
)
);
}
}
/**
* Set Missing Taxonomies.
*
* @return void
*/
public function set_missing_taxonomies() {
global $wp_taxonomies;
if ( ! empty( $this->processed_missing_taxonomies_names ) ) {
foreach ( $this->processed_missing_taxonomies_names as $term_taxonomy ) {
$wp_taxonomies[ $term_taxonomy ] = new WP_Taxonomy( $term_taxonomy, array() );
}
}
}
/**
* Process XML Term.
*
* @param array $term
* @return void
*/
public function process_xml_term( $term ) {
global $wp_taxonomies;
// pypass Registering The Txonomy to avoid failing importing the terms.
if ( ! isset( $wp_taxonomies[ $term['taxonomy'] ] ) ) {
$this->processed_missing_taxonomies_names[] = $term['taxonomy'];
$wp_taxonomies[ $term['taxonomy'] ] = new WP_Taxonomy( $term['taxonomy'], array() );
}
if ( empty( $term['parent'] ) || 0 == $term['parent'] ) {
$parent = 0;
$term['parent'] = 0;
} else {
if ( isset( $this->processed_terms[ intval( $term['parent'] ) ] ) ) {
$parent = $this->processed_terms[ intval( $term['parent'] ) ]['term_id'];
}
if ( ! $parent ) {
$parent = term_exists( $term['parent'], $term['taxonomy'] );
if ( is_array( $parent ) ) {
$parent = $parent['term_id'];
}
}
$term['parent'] = $parent;
}
// if the term already exists in the correct taxonomy leave it alone.
$term_id = term_exists( $term['slug'], $term['taxonomy'], $parent );
if ( $term_id ) {
if ( isset( $term_id['term_id'] ) ) {
$this->processed_terms[ intval( $term['term_id'] ) ] = array(
'term_id' => (int) $term_id['term_id'],
'term_taxonomy_id' => (int) $term_id['term_taxonomy_id'],
'term_taxonomy' => $term['taxonomy'],
);
}
return;
}
$description = isset( $term['description'] ) ? $term['description'] : '';
$args = array(
'slug' => $term['slug'],
'description' => $description,
'parent' => (int) $parent,
);
$term_id = $this->xml_insert_term( $term );
if ( ! is_wp_error( $term_id ) ) {
$this->processed_terms[ intval( $term['term_id'] ) ] = array(
'term_id' => $term_id['term_id'],
'term_taxonomy_id' => $term_id['term_taxonomy_id'],
'term_taxonomy' => $term['taxonomy'],
);
} else {
$this->errors[] = sprintf( __( 'Failed to import %1$s %2$s', 'gpls-cpt-exporter-importer' ), esc_html( $term['taxonomy'] ), esc_html( $term['name'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
error_log( print_r( $term_id->get_error_message(), true ) );
}
return;
}
if ( ! empty( $term['termmeta'] ) ) {
$this->process_termmeta( $term['termmeta'], $term_id['term_id'] );
}
}
/**
* 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_meta, $term_id ) {
global $wpdb;
$table = $wpdb->termmeta;
foreach ( $term_meta as $meta ) {
$wpdb->insert(
$table,
array(
'term_id' => $term_id,
'meta_key' => $meta->key,
'meta_value' => $meta->value,
)
);
// Check if the term has thumbnail_id termmeta and store it in the processed_meta var as [ $new_term_id ] => old_attachment_id.
if ( ( 'thumbnail_id' === $meta->key ) && ( 0 != $meta->value ) ) {
$this->processed_terms_have_thumbnail[ intval( $term_id ) ] = $meta->value;
}
}
}
/**
* process XML Author Import.
*
* @param array $authordata
* @return int
*/
public function process_xml_author( $author_data ) {
global $wpdb;
if ( empty( $author_data ) ) {
return false;
}
$user_email = $author_data['user_email'];
if ( email_exists( $user_email ) ) {
$already_user = get_user_by( 'email', $user_email );
if ( $already_user ) {
return $already_user->ID;
}
}
$user_login = $author_data['user_login'];
if ( empty( $author_data['user_nicename'] ) ) {
$user_nicename = mb_substr( $user_login, 0, 50 );
} else {
$user_nicename = $author_data['user_nicename'];
}
$user_nicename = apply_filters( 'pre_user_nicename', $user_nicename );
$user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1", $user_nicename, $user_login ) );
if ( $user_nicename_check ) {
$suffix = 2;
while ( $user_nicename_check ) {
// user_nicename allows 50 chars. Subtract one for a hyphen, plus the length of the suffix.
$base_length = 49 - mb_strlen( $suffix );
$alt_user_nicename = mb_substr( $user_nicename, 0, $base_length ) . "-$suffix";
$user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1", $alt_user_nicename, $user_login ) );
$suffix++;
}
$user_nicename = $alt_user_nicename;
}
$data = array(
'user_login' => $author_data['user_login'],
'user_pass' => $author_data['user_pass'],
'user_nicename' => $author_data['user_nicename'],
'user_email' => $author_data['user_email'],
'user_url' => $author_data['user_url'],
'user_registered' => $author_data['user_registered'],
'user_activation_key' => $author_data['user_activation_key'],
'user_status' => $author_data['user_status'],
'display_name' => $author_data['display_name'],
);
$wpdb->insert( $wpdb->users, $data );
$user_id = (int) $wpdb->insert_id;
foreach ( $author_data['usermeta'] as $meta ) {
$result = $wpdb->insert(
$wpdb->usermeta,
array(
'user_id' => $user_id,
'meta_key' => $meta->key,
'meta_value' => $meta->value,
)
);
}
return $user_id;
}
/**
* Process XML Post
*
* @param array $post
* @return void
*/
public function process_xml_post( $postdata, $step ) {
global $wpdb;
if ( 'auto-draft' === $postdata['post_status'] ) {
return;
}
if ( 'nav_menu_item' === $postdata['post_type'] ) {
return;
}
$post_exists = $this->is_post_exists( $postdata['post_name'], $postdata['post_title'], $postdata['post_type'], $step );
if ( $post_exists ) {
$this->errors[] = sprintf( __( '%1$s “%2$s” already exists.', 'gpls-cpt-exporter-importer' ), $postdata['post_type'], esc_html( $postdata['post_title'] ) );
$this->posts_exists[ intval( $postdata['import_id'] ) ] = $post_exists;
$this->processed_posts[ intval( $postdata['import_id'] ) ] = $post_exists;
if ( 'attachment' === $postdata['post_type'] ) {
$this->processed_attachments[ intval( $postdata['import_id'] ) ] = $post_exists;
}
return;
} else {
$post_parent = (int) $postdata['post_parent'];
if ( $post_parent ) {
// if we already know the parent, map it to the new local ID
if ( isset( $this->processed_posts[ intval( $post_parent ) ] ) ) {
$postdata['post_parent'] = $this->processed_posts[ intval( $post_parent ) ];
// otherwise record the parent for later
} else {
$this->post_orphans[ intval( $postdata['import_id'] ) ] = intval( $post_parent );
$postdata['post_parent'] = 0;
}
}
// map the post author
$author = sanitize_user( $postdata['post_author'], true );
// Map All posts to single Author.
if ( 1 == $this->author_mapping_type && 0 !== $this->all_posts_single_author_mapping_id ) {
$author = $this->all_posts_single_author_mapping_id;
} elseif ( isset( $this->author_mapping[ $author ] ) ) {
// Map All Author.
$author = $this->author_mapping[ $author ];
} else {
// Fallback to current user.
$author = (int) get_current_user_id();
}
$postdata['post_author'] = $author;
$original_post_ID = intval( $postdata['import_id'] );
if ( 'attachment' == $postdata['post_type'] ) {
$remote_url = ! empty( $postdata['attachment_url'] ) ? $postdata['attachment_url'] : $postdata['guid'];
$comment_post_ID = $post_id = $this->process_attachment_v2( $postdata, $remote_url );
if ( false === $post_id ) {
return;
}
} else {
$postdata = $this->update_post_content_attachments_and_urls( $postdata );
$comment_post_ID = $post_id = $this->xml_insert_post( $postdata, true );
}
if ( is_wp_error( $post_id ) ) {
$this->errors[] = sprintf( __( 'Failed to import %1$s “%2$s”', 'gpls-cpt-exporter-importer' ), $postdata['post_type'], esc_html( $postdata['post_title'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
error_log( print_r( $post_id->get_error_message(), true ) );
}
return;
}
if ( ! empty( $postdata['is_sticky'] ) && $postdata['is_sticky'] == 1 ) {
stick_post( $post_id );
}
}
// map pre-import ID to local ID
$this->processed_posts[ intval( $original_post_ID ) ] = (int) $post_id;
// add categories, tags and other terms.
if ( ! empty( $postdata['terms'] ) ) {
$terms_to_set = array();
$terms_placeholders = array();
$term_order_counter = array();
foreach ( $postdata['terms'] as $term_id ) {
if ( isset( $this->processed_terms[ intval( $term_id ) ] ) ) {
$term_taxonomy_id = $this->processed_terms[ intval( $term_id ) ]['term_taxonomy_id'];
$term_taxonomy = $this->processed_terms[ intval( $term_id ) ]['term_taxonomy'];
$term_order = 0;
$terms_placeholders[] = '( %d, %d, %d )';
$term_taxonomy_obj = get_taxonomy( $term_taxonomy );
if ( $term_taxonomy_obj && isset( $term_taxonomy_obj->sort ) && $term_taxonomy_obj->sort ) {
if ( isset( $term_order_counter[ $term_taxonomy ] ) ) {
$term_order = $term_order_counter[ $term_taxonomy ];
$term_order_counter[ $term_taxonomy ] = $term_order + 1;
$term_order++;
} else {
$term_order_counter[ $term_taxonomy ] = 0;
}
}
array_push( $terms_to_set, $post_id, $term_taxonomy_id, $term_order );
}
}
if ( ! empty( $terms_to_set ) ) {
$relation_query =
"INSERT INTO {$wpdb->term_relationships}
( `object_id`, `term_taxonomy_id`, `term_order` )
VALUES
";
$relation_query .= implode( ', ', $terms_placeholders );
$wpdb->query( $wpdb->prepare( $relation_query, $terms_to_set ) );
}
unset( $postdata['terms'], $terms_to_set );
}
// add/update comments.
if ( ! empty( $postdata['comments'] ) ) {
$inserted_comments = array();
ksort( $postdata['comments'] );
foreach ( $postdata['comments'] as $comment_id => $comment ) {
if ( isset( $inserted_comments[ $comment['comment_parent'] ] ) ) {
$comment['comment_parent'] = $inserted_comments[ $comment['comment_parent'] ];
}
$comment_meta = ! empty( $comment['comment_meta'] ) ? $comment['comment_meta'] : array();
$comment['comment_post_ID'] = $comment_post_ID;
unset( $comment['comment_meta'] );
unset( $comment['comment_ID'] );
$wpdb->insert(
$wpdb->comments,
$comment
);
$new_comment_id = (int) $wpdb->insert_id;
$inserted_comments[ $comment_id ] = $new_comment_id;
foreach ( $comment_meta as $meta ) {
$wpdb->insert(
$wpdb->commentmeta,
array(
'comment_id' => $new_comment_id,
'meta_key' => $meta['key'],
'meta_value' => $meta['value'],
)
);
}
}
unset( $comment_meta, $inserted_comments, $postdata['comments'] );
}
// add/update post meta.
if ( ! empty( $postdata['postmeta'] ) ) {
$postmeta_counter = 1;
$postmetas_to_set = array();
$postmetas_placeholders = array();
foreach ( $postdata['postmeta'] as $meta_arr ) {
$key = $meta_arr['key'];
$value = $meta_arr['value'];
if ( '_edit_last' == $key ) {
if ( isset( $this->processed_authors[ intval( $value ) ] ) ) {
$value = $this->processed_authors[ intval( $value ) ];
} else {
$key = false;
}
} elseif ( '_product_image_gallery' == $key ) {
if ( isset( $this->postmeta_remap[ $post_id ] ) ) {
$this->postmeta_remap[ $post_id ][] = '_product_image_gallery';
} else {
$this->postmeta_remap[ $post_id ] = array();
$this->postmeta_remap[ $post_id ][] = '_product_image_gallery';
}
} elseif ( '_children' == $key ) {
if ( isset( $this->postmeta_remap[ $post_id ] ) ) {
$this->postmeta_remap[ $post_id ][] = '_children';
} else {
$this->postmeta_remap[ $post_id ] = array();
$this->postmeta_remap[ $post_id ][] = '_children';
}
} elseif ( '_thumbnail_id' == $key ) {
$this->featured_images[ $post_id ] = (int) $value;
} elseif ( ! empty( $postdata['postmeta_urls_remap'] ) && array_key_exists( $key, $postdata['postmeta_urls_remap'] ) ) {
foreach ( $postdata['postmeta_urls_remap'][ $key ] as $_postmeta_link_to_remap ) {
$url_to_remap = str_replace( $this->base_url, $this->home_url, $_postmeta_link_to_remap );
$value = str_replace( $_postmeta_link_to_remap, $url_to_remap, $value );
// Check if URL is an image and create missing subsize.
if ( $this->is_image_url( $url_to_remap ) && $this->is_local_image( $url_to_remap, $this->base_url ) ) {
$current_upload_dir = wp_get_upload_dir();
$current_domain_details = wp_parse_url( $this->home_url );
$image_url_details = wp_parse_url( $url_to_remap );
$image_info = pathinfo( $url_to_remap );
$image_dirname = substr( $image_url_details['path'], strpos( $image_url_details['path'], '/wp-content/uploads/' ) + 20 );
// Check if the image URL targets subsize.
$is_image_subsize = $this->is_image_subsize( $image_info, $image_url_details );
if ( $is_image_subsize && is_array( $is_image_subsize ) && ! empty( $is_image_subsize[1] ) ) {
// Image Path including the subsize extension name.
$image_url_to_path = trailingslashit( $current_upload_dir['basedir'] ) . $image_dirname;
$image_size = explode( 'x', $is_image_subsize[1] );
// Check if the subsize is registered.
if ( ! file_exists( $image_url_to_path ) && ! $this->is_image_subsize_exists( $image_size[0], $image_size[1] ) ) {
// Image path without the subsize extension name.
$image_url_to_path = str_replace( '-' . $is_image_subsize[1], '', $image_url_to_path );
// Check if the file exists.
if ( file_exists( $image_url_to_path ) ) {
$image_name = strstr( basename( $image_url_details['path'] ), $is_image_subsize[0], true );
$image_details = array(
'src' => $url_to_remap,
'width' => $image_size[0],
'height' => $image_size[1],
'image_name' => $image_name,
);
$this->attachments_needs_custom_sizes[ $image_name ] = $image_details;
}
}
}
}
}
}
$value_format = ( is_string( $value ) ? '%s' : ( is_int( $value ) ? '%d' : ( is_float( $value ) ? '%f' : '%s' ) ) );
$postmetas_placeholders[] = "( '%d', '%s', '" . $value_format . "' )";
array_push( $postmetas_to_set, $post_id, $key, $value );
if ( $postmeta_counter >= 20 ) {
$postmeta_query =
"INSERT INTO {$wpdb->postmeta}
( `post_id`, `meta_key`, `meta_value` )
VALUES
";
$postmeta_query .= implode( ', ', $postmetas_placeholders );
$wpdb->query( $wpdb->prepare( $postmeta_query, $postmetas_to_set ) );
$postmetas_placeholders = array();
$postmetas_to_set = array();
$postmeta_counter = 0;
}
$postmeta_counter += 1;
}
if ( ! empty( $postmetas_placeholders ) ) {
$postmeta_query =
"INSERT INTO {$wpdb->postmeta}
( `post_id`, `meta_key`, `meta_value` )
VALUES
";
$postmeta_query .= implode( ', ', $postmetas_placeholders );
$wpdb->query( $wpdb->prepare( $postmeta_query, $postmetas_to_set ) );
$postmetas_placeholders = array();
$postmetas_to_set = array();
}
}
}
/**
* Check if URL is for an image.
*
* @param string $url
* @return boolean
*/
public function is_image_url( $url ) {
$wp_filetype = wp_check_filetype( $url );
if( $wp_filetype['type'] && 0 === strpos( $wp_filetype['type'], 'image/' ) ) {
return true;
}
return false;
}
/**
* 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
*/
public function process_attachment_v2( $post, $url ) {
$import_id = intval( $post['import_id'] );
$attachment_upload_pos = strpos( $url, '/wp-content/uploads/' );
if ( false !== $attachment_upload_pos ) {
$attachment_full_name = substr( $url, $attachment_upload_pos + 20 ); // Extract the path after wp-content/uploads/
$attachment_path = $this->uploads_dir['basedir'] . '/' . $attachment_full_name;
// 1) Check if the Attachment exists.
if ( file_exists( $attachment_path ) ) {
$image_pathinfo = pathinfo( $url );
$image_url_details = wp_parse_url( $url );
$attachment_name = basename( parse_url( $url, PHP_URL_PATH ) );
$wp_filetype = wp_check_filetype_and_ext( $attachment_path, $attachment_name );
$upload = array(
'file' => $attachment_path,
'url' => $this->uploads_dir['baseurl'] . '/' . $attachment_full_name,
'type' => $wp_filetype['type'],
'error' => false,
);
$post['guid'] = $upload['url'];
$post['file'] = $upload['file'];
$post['post_mime_type'] = ( ! empty( $wp_filetype['type'] ) && false !== $wp_filetype['type'] ) ? $wp_filetype['type'] : $post['post_mime_type'];
$post_id = $this->xml_insert_post( $post );
if ( is_wp_error( $post_id ) ) {
return false;
}
$image_meta = wp_generate_attachment_metadata( $post_id, $upload['file'] );
if ( preg_match( '!^image/!', $wp_filetype['type'] ) ) {
// check if needs create a missing subsize image.
$image_exact_name = $image_pathinfo['filename'];
if ( ! empty( $this->attachments_needs_custom_sizes[ $image_exact_name ] ) ) {
$new_size = array(
'file' => $image_pathinfo['basename'],
'width' => $this->attachments_needs_custom_sizes[ $image_exact_name ]['width'],
'height' => $this->attachments_needs_custom_sizes[ $image_exact_name ]['height'],
'mime-type' => $wp_filetype['type'],
);
// Create the missing subsize.
$this->make_image_subsize( $upload['file'], $post_id, $image_meta, $new_size );
if ( empty( $image_meta['sizes'] ) ) {
$image_meta['sizes'] = array();
}
$image_meta['sizes'][ $new_size['width'] . 'x' . $new_size['height'] ] = $new_size;
}
}
wp_update_attachment_metadata( $post_id, $image_meta );
$this->processed_attachments[ intval( $import_id ) ] = $post_id;
return $post_id;
} else {
$this->errors[] = sprintf( __('Attachment “%s” is missing', 'gpls-cpt-exporter-importer'), esc_html( $post['post_title'] ) );
}
}
// Missing Local Attachment. //
if ( 0 === strpos( $this->base_url, $url ) ) {
$url = str_replace( $this->base_url, $this->home_url, $url );
}
// 2) Remote Attachment.
$post['guid'] = $url;
$post_id = $this->xml_insert_post( $post );
$this->processed_attachments[ intval( $import_id ) ] = $post_id;
return $post_id;
}
/**
* Insert POST From XML into DB.
*
* @param array $postarr
* @return integer
*/
public function xml_insert_post( $postarr ) {
global $wpdb;
$user_id = get_current_user_id();
$postarr = sanitize_post( $postarr, 'db' );
$post_ID = 0;
$post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id;
$post_date = $postarr['post_date'];
$post_date_gmt = $postarr['post_date_gmt'];
$post_content = $postarr['post_content'];
$post_title = ! empty( $postarr['post_title'] ) ? $postarr['post_title'] : '';
$post_excerpt = $postarr['post_excerpt'];
$post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
$comment_status = ! empty( $postarr['comment_status'] ) ? $postarr['comment_status'] : 'closed';
$ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status'];
$post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : '';
$post_name = ! empty( $postarr['post_name'] ) ? $postarr['post_name'] : '';
$to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : '';
$pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : '';
$post_modified = $postarr['post_modified'];
$post_modified_gmt = $postarr['post_modified_gmt'];
$post_content_filtered = ! empty( $postarr['post_content_filtered'] ) ? $postarr['post_content_filtered'] : '';
$post_parent = $postarr['post_parent'];
$guid = '';
$menu_order = $postarr['menu_order'];
$post_type = $postarr['post_type'];
$post_mime_type = $postarr['post_mime_type'];
$comment_count = $postarr['comment_count'];
$import_id = isset( $postarr['import_id'] ) ? intval( $postarr['import_id'] ) : 0;
$data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' );
$data = wp_unslash( $data );
if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
if ( $wp_error ) {
return new WP_Error( 'db_insert_error', __( 'Could not insert post into the database' ), $wpdb->last_error );
} else {
return 0;
}
}
$post_ID = (int) $wpdb->insert_id;
$where = array( 'ID' => $post_ID );
$this->posts_to_update_guid[ $post_ID ] = $this->home_url . '/?p=' . $post_ID;
// Attachment attached file field.
if ( 'attachment' === $postarr['post_type'] ) {
if ( ! empty( $postarr['file'] ) ) {
update_attached_file( $post_ID, $postarr['file'] );
}
if ( ! empty( $postarr['context'] ) ) {
add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true );
}
}
return $post_ID;
}
/**
* Update Posts GUID.
*
* @return void
*/
public function update_posts_guid() {
global $wpdb;
if ( empty( $this->posts_to_update_guid ) ) {
return;
}
$posts_ids = array_keys( $this->posts_to_update_guid );
$query =
"UPDATE
$wpdb->posts
SET
`guid` =
CASE ";
foreach ( $this->posts_to_update_guid as $post_id => $post_guid ) {
$query .= ' WHEN ID = ' . $post_id . ' THEN ' . "'" . $post_guid . "'";
}
$query .= ' END';
$query .= " WHERE ID IN ('" . implode( "','", $posts_ids ) . "')";
$wpdb->query( $query );
$this->posts_to_update_guid = array();
}
/**
* Insert Term into DB.
*
* @param array $term
* @return void
*/
public function xml_insert_term( $term ) {
global $wpdb;
$data = array(
'name' => $term['name'],
'slug' => $term['slug'],
'term_group' => $term['term_group'],
);
$wpdb->insert(
$wpdb->terms,
$data
);
$term_id = (int) $wpdb->insert_id;
if ( false === $term_id ) {
return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database.', 'gpls-cpt-exporter-importer' ), $wpdb->last_error );
}
$wpdb->insert(
$wpdb->term_taxonomy,
array(
'term_id' => $term_id,
'taxonomy' => $term['taxonomy'],
'description' => $term['description'],
'parent' => $term['parent'],
'count' => 0,
)
);
$term_taxonomy_id = (int) $wpdb->insert_id;
if ( false === $term_taxonomy_id ) {
return new WP_Error( 'db_insert_error', __( 'Could not insert term taxonomy into the database.' ), $wpdb->last_error );
}
return array( 'term_id' => $term_id, 'term_taxonomy_id' => $term_taxonomy_id );
}
/**
* Get Site URL.
*
* @return void
*/
public function get_site_url() {
if ( is_multisite() ) {
// Multisite: the base URL.
return network_home_url();
} else {
// WordPress (single site): the blog URL.
return get_bloginfo_rss( 'url' );
}
}
/**
* Backfill Postmeta targetting other IDs.
*
* @return void
*/
public function backfill_postmeta() {
global $wpdb;
foreach ( $this->postmeta_remap as $post_id => $post_keys ) {
foreach ( $post_keys as $post_key ) {
if ( '_product_image_gallery' == $post_key ) {
$value = get_post_meta( $post_id, $post_key, true );
$value = explode( ',', $value );
$changed = false;
foreach ( $value as &$id ) {
if ( ! empty( $this->processed_posts[ $id ] ) && $id != $this->processed_posts[ $id ] ) {
$id = $this->processed_posts[ $id ];
$changed = true;
}
}
if ( $changed ) {
$wpdb->update(
$wpdb->postmeta,
array(
'meta_value' => implode( ',', $value ),
),
array(
'post_id' => $post_id,
'meta_key' => $post_key,
),
array( '%s' ),
array( '%d', '%s' )
);
}
} elseif ( '_children' == $post_key ) {
$value = get_post_meta( $post_id, $post_key, true );
$changed = false;
foreach ( $value as &$id ) {
if ( ! empty( $this->processed_posts[ $id ] ) && $id != $this->processed_posts[ $id ] ) {
$id = $this->processed_posts[ $id ];
$changed = true;
}
}
if ( $changed ) {
$wpdb->update(
$wpdb->postmeta,
array(
'meta_value' => serialize( $value ),
),
array(
'post_id' => $post_id,
'meta_key' => $post_key,
),
array( '%s' ),
array( '%d', '%s' )
);
}
}
}
}
}
/**
* Update Menu Items Parents After Finish importing menu items.
*
* @return void
*/
public 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[ intval( $child_id ) ] ) ) {
$local_child_id = $this->processed_posts[ intval( $child_id ) ];
}
if ( isset( $this->processed_posts[ $parent_id ] ) ) {
$local_parent_id = $this->processed_posts[ intval( $parent_id ) ];
}
if ( $local_child_id && $local_parent_id ) {
$wpdb->update(
$wpdb->posts,
array(
'post_parent' => $local_parent_id
),
array( 'ID' => $local_child_id ),
array( '%d' ),
array( '%d' )
);
}
}
// find parents for menu item orphans
foreach ( $this->missing_menu_items_parent as $parent_old_id => $child_new_ids_arr ) :
$new_parent_id = 0;
if ( array_key_exists( $parent_old_id, $this->processed_menu_items ) ) :
foreach ( $child_new_ids_arr as $child_new_id ) :
$new_parent_id = $this->processed_menu_items[ $parent_old_id ];
$wpdb->update(
$wpdb->postmeta,
array(
'meta_value' => (int) $new_parent_id,
),
array(
'post_id' => $child_new_id,
'meta_key' => '_menu_item_menu_item_parent',
),
array( '%d' ),
array( '%d', '%s' )
);
endforeach;
else :
// Find nearset Parent.
if ( array_key_exists( $parent_old_id, $this->menu_items_new_remapping ) ) {
while ( ! array_key_exists( $parent_old_id, $this->processed_menu_items ) && ( 0 != $this->menu_items_new_remapping[ $parent_old_id ]['parent'] ) ) {
$parent_old_id = (int) $this->menu_items_new_remapping[ $parent_old_id ]['parent'];
}
if ( array_key_exists( $parent_old_id, $this->processed_menu_items ) ) {
$parent_old_id = (int) $this->processed_menu_items[ $parent_old_id ];
} else {
$parent_old_id = 0;
}
}
foreach ( $child_new_ids_arr as $child_new_id ) :
$wpdb->update(
$wpdb->postmeta,
array(
'meta_value' => $parent_old_id,
),
array(
'post_id' => $child_new_id,
'meta_key' => '_menu_item_menu_item_parent',
),
array( '%d' ),
array( '%d', '%s' )
);
endforeach;
endif;
endforeach;
}
/**
* 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.
*
* @return bool True if creating users is allowed
*/
function allow_create_users() {
return true;
}
/**
* Decide what the maximum file size for downloaded attachments is.
*
* @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 );
}
/**
* Author Mapping.
*
*/
function get_author_mapping() {
$this->author_mapping_type = ( ! empty( $_POST['user_mapping_type'] ) ? intval( $_POST['user_mapping_type'] ) : 2 );
// Assign All posts to single Author. (1)
if ( 1 == $this->author_mapping_type ) {
if ( ! empty( $_POST['user_mapping_single_author_select'] ) && $_POST['user_mapping_single_author_select'] > 0 ) {
$single_author = (int) sanitize_text_field( $_POST['user_mapping_single_author_select'] );
} else {
$single_author = get_current_user_id();
}
$this->all_posts_single_author_mapping_id = $single_author;
} elseif ( 2 == $this->author_mapping_type ) {
// Mapping Every Author. (2)
if ( ! isset( $_POST['imported_authors'] ) ) {
return;
}
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_text_field( $old_login );
$old_id = isset( $this->authors[ $santized_old_login ]['ID'] ) ? intval( $this->authors[ $santized_old_login ]['ID'] ) : false;
if ( ! empty( $_POST['user_map'] ) && ! empty( $_POST['user_map'][ $i ] ) && ( 0 < $_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 {
$user_id = username_exists( $santized_old_login );
if ( ! $user_id ) {
$user_id = $this->process_xml_author( $this->authors[ $santized_old_login ] );
}
if ( ( false !== $user_id ) && ( ! is_wp_error( $user_id ) ) ) {
if ( $old_id ) {
$this->processed_authors[ $old_id ] = $user_id;
}
$this->author_mapping[ $santized_old_login ] = $user_id;
} else {
$this->errors[] = sprintf( __( 'Failed to create new user for %s Their posts will be attributed to the current user.', 'gpls-cpt-exporter-importer' ), esc_html($this->authors[$old_login]['author_display_name'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
error_log( print_r( $user_id->get_error_message(), true ) );
}
}
}
// 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();
}
}
}
update_site_option( $this->plugin_info['name'] . '-single-author-mapping', $this->all_posts_single_author_mapping_id, '', false );
update_site_option( $this->plugin_info['name'] . '-processed-authors', $this->processed_authors, '', false );
update_site_option( $this->plugin_info['name'] . '-author-mapping-type', $this->author_mapping_type, '', false );
update_site_option( $this->plugin_info['name'] . '-author-mapping', $this->author_mapping, '', false );
}
/**
* Update _thumbnail_id meta to new, imported attachment IDs
*/
function remap_featured_images() {
global $wpdb;
// cycle through posts that have a featured image
foreach ( $this->featured_images as $post_id => $value ) {
if ( isset( $this->processed_attachments[ intval( $value ) ] ) ) {
$new_id = $this->processed_attachments[ intval( $value ) ];
// only update if there's a difference
if ( $new_id != $value ) {
$wpdb->update(
$wpdb->postmeta,
array(
'meta_value' => $new_id,
),
array(
'post_id' => $post_id,
'meta_key' => '_thumbnail_id',
),
array( '%d' ),
array( '%d', '%s' )
);
}
}
}
}
/**
* Update _thumbnail_id meta to new, imported attachment IDs
*/
function remap_terms_thumbnail() {
global $wpdb;
// Loop over terms that have thumbnails and update the termmeta with the new thumbnail id.
foreach ( $this->processed_terms_have_thumbnail as $new_term_id => $old_thumbnail_id ) {
if ( isset( $this->processed_attachments[ intval( $old_thumbnail_id ) ] ) ) {
$new_thumbnail_id = $this->processed_attachments[ intval( $old_thumbnail_id ) ];
$wpdb->update(
$wpdb->termmeta,
array(
'meta_value' => (int) $new_thumbnail_id,
),
array(
'term_id' => $new_term_id,
'meta_key' => 'thumbnail_id',
),
array( '%d' ),
array( '%d' )
);
}
}
}
/**
* Clear the Missing Taxonomies from global taxonomies at the end
*
* @return void
*/
function clean_registered_taxonomies() {
global $wp_taxonomies;
foreach ( $this->processed_missing_taxonomies_names as $tax_name => $tax_obj ) {
unset( $wp_taxonomies[ $tax_name ] );
}
}
/**
* Check if tables exist before import.
*
* @return void
*/
public function process_tables_exists() {
global $wpdb;
$tables = array(
$table_name = $wpdb->prefix . 'woocommerce_attribute_taxonomies',
);
foreach ( $tables as $table ) {
if ( $this->check_table_exists( $table ) ) {
$this->tables_exists[ $table ] = true;
} else {
// $this->errors[] = sprintf( __( 'Failed to import WooCommerce Attributes, %s table does\'nt exist', 'gpls-cpt-exporter-importer' ), $table_name );
}
}
}
/**
* Check if table exists.
*
* @param string $table_name
* @return boolean
*/
public function check_table_exists( $table_name ) {
global $wpdb;
$query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $table_name ) );
if ( $wpdb->get_var( $query ) == $table_name ) {
return true;
}
return false;
}
/**
* Handle importer uploading and add attachment.
*
* @since 2.0.0
*
* @return array Uploaded file's details on success, error message on failure
*/
function zip_import_handle_upload() {
if ( ! isset( $_FILES['import'] ) ) {
wp_safe_redirect( add_query_arg( array( 'tab' => 'import', 'error' => 1 ), admin_url( 'tools.php' ) . '?page=' . $this->plugin_info['name'] ) );
die();
}
if ( 'zip' !== pathinfo( $_FILES['import']['name'], PATHINFO_EXTENSION ) ) {
wp_safe_redirect( add_query_arg( array( 'tab' => 'import', 'error' => 2 ), admin_url( 'tools.php' ) . '?page=' . $this->plugin_info['name'] ) );
die();
}
$overrides = array(
'test_form' => false,
'test_type' => false,
);
$upload = wp_handle_upload( $_FILES['import'], $overrides );
if ( isset( $upload['error'] ) ) {
return $upload;
}
// Construct the object array.
$object = array(
'post_title' => wp_basename( $upload['file'] ),
'post_content' => $upload['url'],
'post_mime_type' => $upload['type'],
'guid' => $upload['url'],
'context' => 'import',
'post_status' => 'private',
);
// Save the data.
$id = wp_insert_attachment( $object, $upload['file'] );
return array(
'file' => $upload['file'],
'id' => $id,
);
}
/**
* Outputs the form used by the importers to accept the data to be imported.
*
* @param string $file_type File Type to upload.
* @param string $action The action attribute for the form.
*/
public function import_upload_form( $file_type, $action ) {
$bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
$size = size_format( $bytes );
$upload_dir = wp_upload_dir();
if ( ! empty( $upload_dir['error'] ) ) :
?>