Make WordPress Core


Ignore:
Timestamp:
12/20/2007 05:31:39 AM (17 years ago)
Author:
ryan
Message:

Attachment and post meta import improvements from tellyworth. fixes #5497

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/import/wordpress.php

    r6390 r6428  
    367367                $this->post_ids_processed[intval($post_ID)] = intval($post_id);
    368368            }
    369 
     369           
    370370            // Add categories.
    371371            if (count($categories) > 0) {
     
    439439            $value = $this->get_tag( $p, 'wp:meta_value' );
    440440            $value = stripslashes($value); // add_post_meta() will escape.
    441             add_post_meta( $post_id, $key, $value );
     441           
     442            $this->process_post_meta($post_id, $key, $value);
     443
    442444        } }
    443445       
     446        do_action('import_post_added', $post_id);
    444447        print "</li>\n";
     448    }
     449   
     450    function process_post_meta($post_id, $key, $value) {
     451        // the filter can return false to skip a particular metadata key
     452        $_key = apply_filters('import_post_meta_key', $key);
     453        if ( $_key ) {
     454            add_post_meta( $post_id, $_key, $value );
     455            do_action('import_post_meta', $post_id, $_key, $value);
     456        }
    445457    }
    446458   
     
    456468                print '('.size_format(filesize($upload['file'])).')';
    457469            }
     470           
     471            if ( $info = wp_check_filetype($upload['file']) ) {
     472                $postdata['post_mime_type'] = $info['type'];
     473            }
     474            else {
     475                print __('Invalid file type');
     476                return;
     477            }
    458478               
    459479            $postdata['guid'] = $upload['url'];
     
    462482            $post_id = wp_insert_attachment($postdata, $upload['file']);
    463483            wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
     484           
     485            // remap the thumbnail url.  this isn't perfect because we're just guessing the original url.
     486            if ( preg_match('@^image/@', $info['type']) && $thumb_url = wp_get_attachment_thumb_url($post_id) ) {
     487                $parts = pathinfo($remote_url);
     488                $ext = $parts['extension'];
     489                $name = basename($parts['basename'], ".{$ext}");
     490                $this->url_remap[$parts['dirname'] . '/' . $name . '.thumbnail.' . $ext] = $thumb_url;
     491            }
     492           
    464493            return $post_id;
    465494        }
     
    500529       
    501530    }
     531
     532    // sort by strlen, longest string first
     533    function cmpr_strlen($a, $b) {
     534        return strlen($b) - strlen($a);
     535    }
    502536   
    503537    // update url references in post bodies to point to the new local files
     
    505539       
    506540        // make sure we do the longest urls first, in case one is a substring of another
    507         function cmpr_strlen($a, $b) {
    508             return strlen($b) - strlen($a);
    509         }
    510         uksort($this->url_remap, 'cmpr_strlen');
     541        uksort($this->url_remap, array(&$this, 'cmpr_strlen'));
    511542               
    512543        global $wpdb;
    513544        foreach ($this->url_remap as $from_url => $to_url) {
     545            // remap urls in post_content
    514546            $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, '%s', '%s')", $from_url, $to_url) );
     547            // remap enclosure urls
     548            $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, '%s', '%s') WHERE meta_key='enclosure'", $from_url, $to_url) );
    515549        }
    516550    }
     
    528562        }
    529563    }
     564   
     565    function is_valid_meta_key($key) {
     566        // skip _wp_attached_file metadata since we'll regenerate it from scratch
     567        if ( $key == '_wp_attached_file' )
     568            return false;
     569        return $key;
     570    }
    530571
    531572    function import($id, $fetch_attachments = false) {
     
    533574        $this->fetch_attachments = (bool) $fetch_attachments;
    534575
     576        add_filter('import_post_meta_key', array($this, 'is_valid_meta_key'));
    535577        $file = get_attached_file($this->id);
    536578        $this->import_file($file);
     
    548590        $this->backfill_parents();
    549591        $this->backfill_attachment_urls();
     592       
     593        // clear the caches after backfilling
     594        foreach ($this->post_ids_processed as $post_id)
     595            clean_post_cache($post_id);
     596       
    550597        wp_defer_term_counting(false);
    551598        if ( is_wp_error( $result ) )
Note: See TracChangeset for help on using the changeset viewer.