WordPress.org

Plugin Directory

Changeset 309430


Ignore:
Timestamp:
11/08/10 23:24:10 (3 years ago)
Author:
duck_
Message:

Fix 15272, 15283 and half of 15275. Documentation first pass.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wordpress-importer/trunk/wordpress-importer.php

    r306039 r309430  
    66Author: wordpressdotorg 
    77Author URI: http://wordpress.org/ 
    8 Version: 0.3-beta1 
     8Version: 0.3-beta2 
    99License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
    1010*/ 
     
    5353    var $missing_menu_items = array(); 
    5454 
    55     var $authors_from_posts = false; 
    5655    var $fetch_attachments = false; 
    5756    var $url_remap = array(); 
     
    5958    function WP_Import() { /* nothing */ } 
    6059 
     60    /** 
     61     * Registered callback function for the WordPress Importer 
     62     * 
     63     * Manages the three separate stages of the WXR import process 
     64     */ 
    6165    function dispatch() { 
    6266        $this->header(); 
     
    8488    } 
    8589 
     90    /** 
     91     * The main controller for the actual import stage. 
     92     * 
     93     * @param string $file Path to the WXR file for importing 
     94     */ 
    8695    function import( $file ) { 
    8796        add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) ); 
     
    106115    } 
    107116 
     117    /** 
     118     * Parses the WXR file and prepares us for the task of processing parsed data 
     119     * 
     120     * @param string $file Path to the WXR file for importing 
     121     */ 
    108122    function import_start( $file ) { 
     123        if ( ! is_file($file) ) { 
     124            echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; 
     125            echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>'; 
     126            $this->footer(); 
     127            die(); 
     128        } 
     129 
    109130        $import_data = $this->parse( $file ); 
    110131 
    111132        if ( is_wp_error( $import_data ) ) { 
    112             echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong></p>'; 
    113             echo '<p>' . esc_html( $import_data->get_error_message() ) . '</p>'; 
     133            echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; 
     134            echo esc_html( $import_data->get_error_message() ) . '</p>'; 
    114135            $this->footer(); 
    115136            die(); 
     
    129150    } 
    130151 
     152    /** 
     153     * Performs post-import cleanup of files and the cache 
     154     */ 
    131155    function import_end() { 
    132156        wp_import_cleanup( $this->id ); 
     
    146170    } 
    147171 
     172    /** 
     173     * Handles the WXR upload and initial parsing of the file to prepare for 
     174     * displaying author import options 
     175     * 
     176     * @return bool False if error uploading or invalid file, true otherwise 
     177     */ 
    148178    function handle_upload() { 
    149179        $file = wp_import_handle_upload(); 
    150180 
    151181        if ( isset( $file['error'] ) ) { 
    152             echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong></p>'; 
    153             echo '<p>' . esc_html( $file['error'] ) . '</p>'; 
     182            echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; 
     183            echo esc_html( $file['error'] ) . '</p>'; 
    154184            return false; 
    155185        } 
     
    158188        $import_data = $this->parse( $file['file'] ); 
    159189        if ( is_wp_error( $import_data ) ) { 
    160             echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong></p>'; 
    161             echo '<p>' . esc_html( $import_data->get_error_message() ) . '</p>'; 
     190            echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; 
     191            echo esc_html( $import_data->get_error_message() ) . '</p>'; 
    162192            return false; 
    163193        } 
     
    168198    } 
    169199 
     200    /** 
     201     * Retrieve authors from parsed WXR data 
     202     * 
     203     * Uses the provided author information from WXR 1.1 files 
     204     * or extracts info from each post for WXR 1.0 files 
     205     * 
     206     * @param array $import_data Data returned by a WXR parser 
     207     */ 
    170208    function get_authors_from_import( $import_data ) { 
    171209        if ( ! empty( $import_data['authors'] ) ) { 
     
    187225                    ); 
    188226            } 
    189             $this->authors_from_posts = true; 
    190         } 
    191     } 
    192  
     227        } 
     228    } 
     229 
     230    /** 
     231     * Display pre-import options, author importing/mapping and option to 
     232     * fetch attachments 
     233     */ 
    193234    function import_options() { 
    194235        $j = 0; 
     
    201242    <h3><?php _e('Assign Authors', 'wordpress-importer'); ?></h3> 
    202243    <p><?php _e( 'To make it easier for you to edit and save the imported content, you may want to reassign the author of the imported item to an existing user of this site. For example, you may want to import all the entries as <code>admin</code>s entries.', 'wordpress-importer' ); ?></p> 
    203 <?php if ( ! $this->authors_from_posts && $this->allow_create_users() ) : ?> 
     244<?php if ( $this->allow_create_users() ) : ?> 
    204245    <p><?php printf( __( 'If a new user is created by WordPress, a new password will be randomly generated and the new user&#8217;s role will be set as %s. Manually changing the new user&#8217;s details will be necessary.', 'wordpress-importer' ), esc_html( get_option('default_role') ) ); ?></p> 
    205246<?php endif; ?> 
     
    224265    } 
    225266 
     267    /** 
     268     * Display import options for an individual author. That is, either create 
     269     * a new user based on import info or map to an existing user 
     270     * 
     271     * @param int $n Index for each author in the form 
     272     * @param array $author Author information, e.g. login, display name, email 
     273     */ 
    226274    function author_select( $n, $author ) { 
    227         if ( ! $this->authors_from_posts && $this->allow_create_users() ) 
    228             printf( __( 'Import author %s or map to existing user', 'wordpress-importer' ), '<strong>' . esc_html( $author['author_display_name'] ) . '</strong>' ); 
    229         else 
    230             printf( __( 'Map author %s to existing user', 'wordpress-importer' ), '<strong>' . esc_html( $author['author_display_name'] ) . '</strong>' ); 
    231 ?> 
    232         <input type="hidden" name="imported_authors[<?php echo $n; ?>]" value="<?php esc_attr_e( $author['author_login'] ); ?>" /> 
    233         <?php wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) ); ?> 
    234 <?php 
    235     } 
    236  
     275        _e( 'Import author:', 'wordpress-importer' ); 
     276        echo ' <strong>' . esc_html( $author['author_display_name'] . ' (' . esc_html( $author['author_login'] ) . ')' ) . '</strong><br />'; 
     277 
     278        if ( $this->allow_create_users() ) { 
     279            _e( 'Create new user with login name', 'wordpress-importer' ); 
     280            echo ' <input type="text" name="user_new['.$n.']" value="'. esc_attr( sanitize_user( $author['author_login'], true ) ) .'" /><br />'; 
     281        } 
     282 
     283        _e( 'Map to existing user', 'wordpress-importer' ); 
     284        wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) ); 
     285        echo '<input type="hidden" name="imported_authors['.$n.']" value="' . esc_attr( $author['author_login'] ) . '" />'; 
     286    } 
     287 
     288    /** 
     289     * Map old author logins to local user IDs based on decisions made 
     290     * in import options form. Can map to an existing user, create a new user 
     291     * or falls back to the current user in case of error with either of the previous 
     292     */ 
    237293    function get_author_mapping() { 
    238294        if ( ! isset( $_POST['imported_authors'] ) ) 
    239295            return; 
    240296 
    241         foreach ( (array) $_POST['imported_authors'] as $i => $login ) { 
    242             $bad_login = $login; 
    243             $login = sanitize_user( $login, true ); 
    244  
     297        foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) { 
    245298            if ( ! empty( $_POST['user_map'][$i] ) ) { 
    246299                $user = get_userdata( intval($_POST['user_map'][$i]) ); 
    247300                if ( isset( $user->ID ) ) 
    248                     $this->processed_authors[$login] = $user->ID; 
    249             } else if ( ! $this->authors_from_posts && $this->allow_create_users() ) { 
     301                    $this->processed_authors[$old_login] = $user->ID; 
     302            } else if ( $this->allow_create_users() && ! empty($_POST['user_new'][$i]) ) { 
     303                $login = sanitize_user( $_POST['user_new'][$i], true ); 
    250304                $user_id = username_exists( $login ); 
    251305                if ( ! $user_id ) { 
     
    253307                        'user_login' => $login, 
    254308                        'user_pass' => wp_generate_password(), 
    255                         'user_email' => $this->authors[$login]['author_email'], 
    256                         'display_name' => $this->authors[$login]['author_display_name'], 
    257                         'first_name' => $this->authors[$login]['author_first_name'], 
    258                         'last_name' => $this->authors[$login]['author_last_name'], 
     309                        'user_email' => isset( $this->authors[$old_login]['author_email'] ) ? $this->authors[$old_login]['author_email'] : '', 
     310                        'display_name' => $this->authors[$old_login]['author_display_name'], 
     311                        'first_name' => isset( $this->authors[$old_login]['author_first_name'] ) ? $this->authors[$old_login]['author_first_name'] : '', 
     312                        'last_name' => isset( $this->authors[$old_login]['author_last_name'] ) ? $this->authors[$old_login]['author_last_name'] : '', 
    259313                    ); 
    260314                    $user_id = wp_insert_user( $user_data ); 
     
    262316 
    263317                if ( ! is_wp_error( $user_id ) ) { 
    264                     $this->processed_authors[$login] = $user_id; 
     318                    $this->processed_authors[$old_login] = $user_id; 
    265319                } else { 
    266                     _e( sprintf( 'Failed to import author %s. Their posts will be attributed to the current user.', esc_html( $this->authors[$login]['author_display_name'] ) ) ); 
     320                    _e( sprintf( 'Failed to create new user for %s. Their posts will be attributed to the current user.', esc_html( $this->authors[$old_login]['author_display_name'] ) ) ); 
    267321                    if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) 
    268322                        echo ' ' . $user_id->get_error_message(); 
     
    272326 
    273327            // failsafe: if the user_id was invalid, default to the current user 
    274             if ( empty( $this->processed_authors[$login] ) ) 
    275                 $this->processed_authors[$login] = (int) get_current_user_id(); 
    276         } 
    277     } 
    278  
     328            if ( empty( $this->processed_authors[$old_login] ) ) 
     329                $this->processed_authors[$old_login] = (int) get_current_user_id(); 
     330        } 
     331    } 
     332 
     333    /** 
     334     * Create new categories based on import information 
     335     * 
     336     * Doesn't create a new category if its slug already exists 
     337     */ 
    279338    function process_categories() { 
    280339        if ( empty( $this->categories ) ) 
     
    312371    } 
    313372 
     373    /** 
     374     * Create new post tags based on import information 
     375     * 
     376     * Doesn't create a tag if its slug already exists 
     377     */ 
    314378    function process_tags() { 
    315379        if ( empty( $this->tags ) ) 
     
    341405    } 
    342406 
     407    /** 
     408     * Create new terms based on import information 
     409     * 
     410     * Doesn't create a term its slug already exists 
     411     */ 
    343412    function process_terms() { 
    344413        if ( empty( $this->terms ) ) 
     
    376445    } 
    377446 
     447    /** 
     448     * Create new posts based on import information 
     449     * 
     450     * Posts marked as having a parent which doesn't exist will become top level items. 
     451     * Doesn't create a new post if: the post type doesn't exist, the given post ID 
     452     * is already noted as imported or a post with the same title and date already exists. 
     453     * Note that new/updated terms, comments and meta are imported for the last of the above. 
     454     */ 
    378455    function process_posts() { 
    379456        foreach ( $this->posts as $post ) { 
     
    530607    } 
    531608 
     609    /** 
     610     * Attempt to create a new menu item from import data 
     611     * 
     612     * Fails for draft, orphaned menu items and those without an associated nav_menu 
     613     * or an invalid nav_menu term. If the post type or term object which the menu item 
     614     * represents doesn't exist then the menu item will not be imported (waits until the 
     615     * end of the import to retry again before discarding). 
     616     * 
     617     * @param array $item Menu item details from WXR file 
     618     */ 
    532619    function process_menu_item( $item ) { 
    533620        // skip draft, orphaned menu items 
     
    608695    } 
    609696 
     697    /** 
     698     * If fetching attachments is enabled then attempt to create a new attachment 
     699     * 
     700     * @param array $post Attachment post details from WXR 
     701     * @param string $url URL to fetch attachment from 
     702     * @return int|WP_Error Post ID on success, WP_Error otherwise 
     703     */ 
    610704    function process_attachment( $post, $url ) { 
    611705        if ( ! $this->fetch_attachments ) 
     
    643737    } 
    644738 
     739    /** 
     740     * Attempt to download a remote file attachment 
     741     * 
     742     * @param string $url URL of item to fetch 
     743     * @param array $post Attachment details 
     744     * @return array|WP_Error Local file location details on success, WP_Error otherwise 
     745     */ 
    645746    function fetch_remote_file( $url, $post ) { 
    646747        add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) ); 
     
    697798    } 
    698799 
     800    /** 
     801     * Attempt to associate posts and menu items with previously missing parents 
     802     * 
     803     * An imported post's parent may not have been imported when it was first created 
     804     * so try again. Similarly for child menu items and menu items which were missing 
     805     * the object (e.g. post) they represent in the menu 
     806     */ 
    699807    function backfill_parents() { 
    700808        global $wpdb; 
     
    730838    } 
    731839 
     840    /** 
     841     * Use stored mapping information to update old attachment URLs 
     842     */ 
    732843    function backfill_attachment_urls() { 
    733844        global $wpdb; 
     
    744855    } 
    745856 
     857    /** 
     858     * Parse a WXR file 
     859     * 
     860     * @param string $file Path to WXR file for parsing 
     861     * @return array Information gathered from the WXR file 
     862     */ 
    746863    function parse( $file ) { 
    747864        $parser = new WXR_Parser(); 
     
    749866    } 
    750867 
     868    // Display import page title 
    751869    function header() { 
    752870        echo '<div class="wrap">'; 
     
    755873    } 
    756874 
     875    // Close div.wrap 
    757876    function footer() { 
    758877        echo '</div>'; 
    759878    } 
    760879 
     880    /** 
     881     * Display introductory text and file upload form 
     882     */ 
    761883    function greet() { 
    762884        echo '<div class="narrow">'; 
     
    767889    } 
    768890 
     891    /** 
     892     * Decide if the given meta key maps to information we will want to import 
     893     * 
     894     * @param string $key The meta key to check 
     895     * @return bool True for keys we do want to save, false if not 
     896     */ 
    769897    function is_valid_meta_key( $key ) { 
    770898        // skip attachment metadata since we'll regenerate it from scratch 
     
    775903    } 
    776904 
    777     // give the user the option of creating new users to represent authors in the import file? 
     905    /** 
     906     * Decide whether or not the importer is allowed to create users. 
     907     * Default is true, can be filtered via import_allow_create_users 
     908     * 
     909     * @return bool True if creating users is allowed 
     910     */ 
    778911    function allow_create_users() { 
    779912        return apply_filters( 'import_allow_create_users', true ); 
    780913    } 
    781914 
    782     // give the user the option of downloading and importing attached files 
     915    /** 
     916     * Decide whether or not the importer should attempt to download attachment files. 
     917     * Default is true, can be filtered via import_allow_fetch_attachments. The choice 
     918     * made at the import options screen must also be true, false here hides that checkbox. 
     919     * 
     920     * @return bool True if downloading attachments is allowed 
     921     */ 
    783922    function allow_fetch_attachments() { 
    784923        return apply_filters( 'import_allow_fetch_attachments', true ); 
    785924    } 
    786925 
     926    /** 
     927     * Decide what the maximum file size for downloaded attachments is. 
     928     * Default is 0 (unlimited), can be filtered via import_attachment_size_limit 
     929     * 
     930     * @return int Maximum attachment file size to import 
     931     */ 
     932    function max_attachment_size() { 
     933        return apply_filters( 'import_attachment_size_limit', 0 ); 
     934    } 
     935 
     936    /** 
     937     * Added to http_request_timeout filter to force timeout at 60 seconds during import 
     938     * @return int 60 
     939     */ 
    787940    function bump_request_timeout() { 
    788941        return 60; 
    789942    } 
    790943 
    791     function max_attachment_size() { 
    792         return apply_filters( 'import_attachment_size_limit', 0 ); 
    793     } 
    794  
     944    // return the difference in length between two strings 
    795945    function cmpr_strlen( $a, $b ) { 
    796946        return strlen($b) - strlen($a); 
     
    799949 
    800950/** 
    801  * Register WordPress Importer 
    802  * 
    803  * @since unknown 
    804  * @var WP_Import 
    805  * @name $wp_import 
     951 * WordPress Importer object for registering the import callback 
     952 * @global WP_Import $wp_import 
    806953 */ 
    807954$wp_import = new WP_Import(); 
    808  
    809 register_importer('wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'wordpress-importer'), array ($wp_import, 'dispatch')); 
     955register_importer( 'wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'wordpress-importer'), array( $wp_import, 'dispatch' ) ); 
    810956 
    811957} // class_exists( 'WP_Importer' ) 
Note: See TracChangeset for help on using the changeset viewer.