WordPress.org

Make WordPress Core

Ticket #19225: better-attachment-url-backfilling.diff

File better-attachment-url-backfilling.diff, 1.5 KB (added by tott, 7 years ago)
  • wordpress-importer.php

     
    952952                global $wpdb;
    953953                // make sure we do the longest urls first, in case one is a substring of another
    954954                uksort( $this->url_remap, array(&$this, 'cmpr_strlen') );
    955 
     955                $replacements = array();
    956956                foreach ( $this->url_remap as $from_url => $to_url ) {
    957                         // remap urls in post_content
    958                         $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) );
    959                         // remap enclosure urls
    960                         $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) );
     957                        $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE %s", "%$from_url%" ) );
     958                        if ( count( $post_ids ) >  0 ) {
     959                                foreach ( $post_ids as $id ) {
     960                                        $replacements[$id][$from_url] = $to_url;
     961                                }
     962                        }
     963                }
     964                foreach( $replacements as $post_id => $post_replacements ) {
     965                        $post = get_post( $post_id );
     966                        uksort( $post_replacements, array(&$this, 'cmpr_strlen') );
     967                        $new_post_content = str_replace( array_keys( $post_replacements ), array_values( $post_replacements ), $post->post_content );
     968                        if ( $new_post_content <> $post->post_content )
     969                                $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET post_content = %s WHERE ID = %d", $new_post_content, $post_id ) );
    961970                }
    962971        }
    963972