OwlCyberSecurity - MANAGER
Edit File: importer.php
<?php /** * VamTam Content Importer */ if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) return; /** Display verbose errors */ if ( ! defined( 'IMPORT_DEBUG' ) ) { define( 'IMPORT_DEBUG', false ); } // Load Importer API require_once ABSPATH . 'wp-admin/includes/import.php'; if ( ! class_exists( 'WP_Importer' ) ) { $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php'; if ( file_exists( $class_wp_importer ) ) require $class_wp_importer; } // include WXR file parsers require dirname( __FILE__ ) . '/parsers.php'; /** * WordPress Importer class for managing the import process of a WXR file * * @package Importer */ if ( class_exists( 'WP_Importer' ) ) { class Vamtam_Import extends WP_Importer { var $max_wxr_version = 1.1; // max. supported WXR version var $id; // WXR attachment ID // information to import from WXR file var $version; var $authors = array(); var $posts = array(); var $terms = array(); var $categories = array(); var $tags = array(); var $base_url = ''; var $front_page = 0; var $woocommerce_pages = 0; private $attachments = []; // mappings from old information to new var $processed_authors = array(); var $author_mapping = array(); var $processed_terms = array(); var $processed_posts = array(); var $processed_attachments = array(); var $post_orphans = array(); var $processed_menu_items = array(); var $menu_item_orphans = array(); var $missing_menu_items = array(); var $fetch_attachments = false; var $url_remap = array(); var $featured_images = array(); function __construct() { /* nothing */ } /** * Registered callback function for the WordPress Importer * * Manages the three separate stages of the WXR import process */ function dispatch() { $this->header(); $step = empty( $_GET['step'] ) ? 0 : $_GET['step']; check_admin_referer( 'vamtam-import' ); $file = VAMTAM_SAMPLES_DIR . 'content.xml'; $this->fetch_attachments = ( $this->allow_fetch_attachments() ); $this->one_click = true; update_option( 'vamtam-used-one-click-import', true ); set_time_limit( 0 ); $this->import( $file ); $this->footer(); } /** * The main controller for the actual import stage. * * @param string $file Path to the WXR file for importing */ function import( $file ) { global $vamtam_defaults; add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) ); add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) ); do_action( 'vamtam_before_content_import' ); $start = microtime( true ); $this->import_start( $file ); $this->get_author_mapping(); wp_suspend_cache_invalidation( true ); $this->process_categories(); $this->process_tags(); $this->process_terms(); $this->process_posts(); wp_suspend_cache_invalidation( false ); if ( get_option( 'vamtam_theme', false ) === false && is_array( $vamtam_defaults ) ) { update_option( 'vamtam_theme', $vamtam_defaults ); } // update incorrect/missing information in the DB $this->backfill_parents(); $this->import_end(); $end = microtime( true ); echo 'Parse and Import: ' . ( $end - $start ) . "s\n"; do_action( 'vamtam_after_content_import' ); } /** * Parses the WXR file and prepares us for the task of processing parsed data * * @param string $file Path to the WXR file for importing */ function import_start( $file ) { if ( ! is_file( $file ) ) { echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; echo esc_html__( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>'; $this->footer(); die(); } $import_data = $this->parse( $file ); if ( is_wp_error( $import_data ) ) { echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; echo esc_html( $import_data->get_error_message() ) . '</p>'; $this->footer(); die(); } $this->version = $import_data['version']; $this->get_authors_from_import( $import_data ); $this->posts = $import_data['posts']; $this->terms = $import_data['terms']; $this->categories = $import_data['categories']; $this->tags = $import_data['tags']; $this->base_url = esc_url( $import_data['base_url'] ); if ( isset( $import_data['frontpage'] ) ) { $this->front_page = $import_data['frontpage']; } if ( isset( $import_data['woocommerce_pages'] ) ) { $this->woocommerce_pages = $import_data['woocommerce_pages']; } wp_defer_term_counting( true ); wp_defer_comment_counting( true ); do_action( 'import_start' ); } /** * Performs post-import cleanup of files and the cache */ function import_end() { wp_import_cleanup( $this->id ); wp_cache_flush(); foreach ( get_taxonomies() as $tax ) { delete_option( "{$tax}_children" ); _get_term_hierarchy( $tax ); } wp_defer_term_counting( false ); wp_defer_comment_counting( false ); echo '<p>' . esc_html__( 'All done.', 'wordpress-importer' ) . ' <a href="' . esc_url( admin_url() ) . '">' . esc_html__( 'Have fun!', 'wordpress-importer' ) . '</a></p>'; echo '<p>' . esc_html__( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>'; if ( $this->one_click ) { if ( $this->front_page ) { update_option( 'page_on_front', $this->processed_posts[ intval( $this->front_page ) ] ); update_option( 'show_on_front', 'page' ); } if ( $this->woocommerce_pages ) { $wc_pages = json_decode( $this->woocommerce_pages ); foreach ( $wc_pages as $option => $pageid ) { if ( isset( $this->processed_posts[ intval( $pageid ) ] ) ) { update_option( $option, $this->processed_posts[ intval( $pageid ) ] ); } } } update_option( 'vamtam_last_import_map', array( 'posts' => $this->processed_posts, 'terms' => $this->processed_terms, 'authors' => $this->processed_authors, 'featured_images' => $this->featured_images, 'menus' => $this->processed_menu_items, ) ); update_option( 'vamtam_import_attachments_todo', [ 'base_url' => $this->base_url, 'attachments' => $this->attachments, ] ); } if ( vamtam_has_woocommerce() ) { self::wc_import_status_actions(); } do_action( 'import_end' ); } /** * Handles the WXR upload and initial parsing of the file to prepare for * displaying author import options * * @return bool False if error uploading or invalid file, true otherwise */ function handle_upload() { $file = wp_import_handle_upload(); if ( isset( $file['error'] ) ) { echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; echo esc_html( $file['error'] ) . '</p>'; return false; } $this->id = (int) $file['id']; $import_data = $this->parse( $file['file'] ); if ( is_wp_error( $import_data ) ) { echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; echo esc_html( $import_data->get_error_message() ) . '</p>'; return false; } $this->version = $import_data['version']; if ( $this->version > $this->max_wxr_version ) { echo '<div class="error"><p><strong>'; printf( esc_html__( 'This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'wordpress-importer' ), esc_html( $import_data['version'] ) ); echo '</strong></p></div>'; } $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 ) ) { printf( esc_html__( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) ); echo '<br />'; continue; } if ( ! isset( $this->authors[ $login ] ) ) $this->authors[ $login ] = array( 'author_login' => $login, 'author_display_name' => $post['post_author'], ); } } } /** * 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 ) { $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[ $old_login ] = $user->ID; } } else if ( $create_users ) { if ( ! empty( $_POST['user_new'][ $i ] ) ) { $user_id = wp_create_user( $_POST['user_new'][ $i ], wp_generate_password() ); } else if ( $this->version != '1.0' ) { $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[ $old_login ] = $user_id; } else { printf( esc_html__( 'Failed to create new user for %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $this->authors[ $old_login ]['author_display_name'] ) ); if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) { echo ' ' . $user_id->get_error_message(); // xss ok } echo '<br />'; } } // failsafe: if the user_id was invalid, default to the current user if ( ! isset( $this->author_mapping[ $old_login ] ) ) { if ( $old_id ) $this->processed_authors[ $old_id ] = (int) get_current_user_id(); $this->author_mapping[ $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; continue; } $category_parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] ); $category_description = isset( $cat['category_description'] ) ? $cat['category_description'] : ''; $catarr = array( 'category_nicename' => $cat['category_nicename'], 'category_parent' => $category_parent, 'cat_name' => $cat['cat_name'], 'category_description' => $category_description ); $catarr = wp_slash( $catarr ); $id = wp_insert_category( $catarr ); if ( ! is_wp_error( $id ) ) { if ( isset($cat['term_id']) ) $this->processed_terms[intval($cat['term_id'])] = $id; } else { printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) ); if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) echo ': ' . $id->get_error_message(); echo '<br />'; 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; continue; } $tag = wp_slash( $tag ); $tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : ''; $tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc ); $id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr ); if ( ! is_wp_error( $id ) ) { if ( isset($tag['term_id']) ) $this->processed_terms[intval($tag['term_id'])] = $id['term_id']; } else { printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) ); if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) echo ': ' . $id->get_error_message(); echo '<br />'; 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() { $this->terms = apply_filters( 'wp_import_terms', $this->terms ); 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; 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']; } $term = wp_slash( $term ); $description = isset( $term['term_description'] ) ? $term['term_description'] : ''; $termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) ); $id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr ); if ( ! is_wp_error( $id ) ) { if ( isset($term['term_id']) ) $this->processed_terms[intval($term['term_id'])] = $id['term_id']; } else { printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) ); if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) echo ': ' . $id->get_error_message(); echo '<br />'; continue; } $this->process_termmeta( $term, $id['term_id'] ); } unset( $this->terms ); } /** * 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 ( ! 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, $key, $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() { $this->posts = apply_filters( 'wp_import_posts', $this->posts ); foreach ( $this->posts as $post ) { $post = apply_filters( 'wp_import_post_data_raw', $post ); if ( ! post_type_exists( $post['post_type'] ) ) { printf( __( 'Failed to import “%s”: Invalid post type %s', 'wordpress-importer' ), esc_html($post['post_title']), esc_html($post['post_type']) ); echo '<br />'; do_action( 'wp_import_post_exists', $post ); continue; } if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) ) continue; if ( $post['status'] == 'auto-draft' ) continue; if ( 'nav_menu_item' == $post['post_type'] ) { $this->process_menu_item( $post ); continue; } $post_type_object = get_post_type_object( $post['post_type'] ); $post_exists = post_exists( $post['post_title'], '', $post['post_date'] ); /** * Filter ID of the existing post corresponding to post currently importing. * * Return 0 to force the post to be imported. Filter the ID to be something else * to override which existing post is mapped to the imported post. * * @see post_exists() * @since 0.6.2 * * @param int $post_exists Post ID, or 0 if post did not exist. * @param array $post The post array to be inserted. */ $post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post ); if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) { printf( __('%s “%s” already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) ); echo '<br />'; $comment_post_ID = $post_id = $post_exists; $this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists ); } 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 ( $meta['key'] == '_wp_attached_file' ) { 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 ); $this->attachments[] = [ $postdata, $remote_url ]; $this->processed_posts[ intval( $post['post_id'] ) ] = '__TO_BE_IMPORTED__'; printf( __( 'Attachment skipped: %s', 'wpv' ), $remote_url ); echo '<br>'; continue; } 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( __( 'Failed to import %s “%s”', 'wordpress-importer' ), $post_type_object->labels->singular_name, esc_html($post['post_title']) ); if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) echo ': ' . $post_id->get_error_message(); echo '<br />'; continue; } if ( $post['is_sticky'] == 1 ) { stick_post( $post_id ); } } // map pre-import ID to local ID $this->processed_posts[intval($post['post_id'])] = (int) $post_id; Vamtam_Importers_E::process_post_additional_data( $post, $post_id, $post_exists, $this->processed_authors, $this->featured_images ); } 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 ) { _e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' ); echo '<br />'; return; } $menu_id = term_exists( $menu_slug, 'nav_menu' ); if ( ! $menu_id ) { printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) ); echo '<br />'; 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)]; } else if ( '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)]; } else if ( 'custom' != $_menu_item_type ) { // associated object is missing or not imported yet, we'll retry later $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)]; } else if ( $_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 ); $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'] ); $id = $id = wp_update_nav_menu_item( $menu_id, 0, $args ); if ( $id && ! is_wp_error( $id ) ) $this->processed_menu_items[intval($item['post_id'])] = (int) $id; foreach ( $item['postmeta'] as $meta ) { if ( strpos( '_menu', $meta['key'] ) === false ) { $key = apply_filters( 'import_post_meta_key', $meta['key'], $id, $item ); $value = false; if ( $key ) { // export gets meta straight from the DB so could have a serialized string if ( ! $value ) $value = maybe_unserialize( $meta['value'] ); add_post_meta( $id, $key, $value ); do_action( 'import_post_meta', $id, $key, $value ); } } } } /** * 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' ); } // all other posts/terms are imported, retry menu items with missing associated object $missing_menu_items = $this->missing_menu_items; foreach ( $missing_menu_items as $item ) $this->process_menu_item( $item ); // find parents for menu item orphans foreach ( $this->menu_item_orphans as $child_id => $parent_id ) { $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 ); } } /** * 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 VAMTAM_WXR_Parser(); return $parser->parse( $file ); } // Display import page title function header() { echo '<div class="wrap">'; echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>'; $updates = get_plugin_updates(); $basename = plugin_basename(__FILE__); if ( isset( $updates[$basename] ) ) { $update = $updates[$basename]; echo '<div class="error"><p><strong>'; printf( __( 'A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'wordpress-importer' ), $update->update->new_version ); echo '</strong></p></div>'; } } // Close div.wrap function footer() { echo '</div>'; } /** * Display introductory text and file upload form */ function greet() { echo '<div class="narrow">'; echo '<p>'.__( 'Howdy! Upload your WordPress eXtended RSS (WXR) file and we’ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer' ).'</p>'; echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>'; wp_import_upload_form( 'admin.php?import=wordpress&step=1' ); echo '</div>'; } /** * 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; } function wc_import_status_actions() { if ( ! class_exists( 'WC_REST_System_Status_Tools_Controller' ) ) { // TODO: Add some messaging here to tell the user to run the required actions from WC->Status->Tools menu manually. return false; } $tools_controller = new WC_REST_System_Status_Tools_Controller(); $failed_actions = []; $actions_to_run = [ 'clear_expired_transients', 'clear_transients', 'regenerate_product_lookup_tables', 'verify_db_tables', ]; foreach ( $actions_to_run as $action ) { $response = $tools_controller->execute_tool( $action ); if ( $response['success'] === false ) { $failed_actions[] = $action; } } if ( ! empty( $failed_actions ) ) { // TODO: Add some messaging here to tell the user to run the failed actions from WC->Status->Tools menu manually. return false; } } } } // class_exists( 'WP_Importer' ) function vamtam_importer_init() { if ( defined( 'VAMTAM_SAMPLES_DIR' ) ) { $GLOBALS['vamtam_import'] = new VAMTAM_Import(); register_importer( 'wpv', 'Vamtam Demo Content', sprintf( esc_html__( 'Import posts, pages, comments, custom fields, categories, tags and sidebars from a %s dummy content file.', 'wordpress-importer' ), VAMTAM_THEME_NAME ), array( $GLOBALS['vamtam_import'], 'dispatch' ) ); } } add_action( 'admin_init', 'vamtam_importer_init' );